File: seqcons_app.cpp

package info (click to toggle)
seqan2 2.4.0%2Bdfsg-16
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 224,180 kB
  • sloc: cpp: 256,886; ansic: 91,672; python: 8,330; sh: 995; xml: 570; makefile: 252; awk: 51; javascript: 21
file content (334 lines) | stat: -rw-r--r-- 12,001 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// ==========================================================================
//                                 SeqCons
// ==========================================================================
// Copyright (c) 2006-2018, Knut Reinert, FU Berlin
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of Knut Reinert or the FU Berlin nor the names of
//       its contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
//
// ==========================================================================
// Author: Manuel Holtgrewe <manuel.holtgrewe@fu-berlin.de>
// ==========================================================================

#include "seqcons_app.h"

#include <cctype>
#include <iostream>

#include <seqan/realign.h>
#include <seqan/sequence.h>
#include <seqan/store.h>
#include <seqan/seq_io.h>

#include "seqcons_options.h"

namespace {

// ---------------------------------------------------------------------------
// Function endsWithIgnoreCase()
// ---------------------------------------------------------------------------

bool endsWithIgnoreCase(std::string str, std::string suffix)
{
    std::transform(str.begin(), str.end(), str.begin(), ::tolower);
    std::transform(suffix.begin(), suffix.end(), suffix.begin(), ::tolower);
    return seqan::endsWith(str, suffix);
}

// ---------------------------------------------------------------------------
// Function trimAfterSpace()
// ---------------------------------------------------------------------------

void trimAfterSpace(seqan::CharString & s)
{
    unsigned i = 0;
    for (; i < length(s); ++i)
        if (isspace(s[i]))
            break;
    resize(s, i);
}

}

// ----------------------------------------------------------------------------
// Class SeqConsAppImpl
// ----------------------------------------------------------------------------

class SeqConsAppImpl
{
public:
    SeqConsAppImpl(SeqConsOptions const & options) : options(options)
    {}

    void run();

private:
    // Load reads without alignment information from FASTA file.  Will create one pseudo-contig and put all reads at the
    // first position.
    void loadReads(char const * fileName);
    // Load alignments from SAM file, will set reference to string of N of sufficient length.
    void loadAlignments(char const * fileName);

    // Perform the consensus alignment, optionally interpreting coordinates.
    void performConsensusAlignment(bool useContigID, bool usePositions, bool useGlobalAlignment);
    // Perform realignment on store only.
    void performRealignment();

    // Write out consensus sequence to file.
    void writeConsensus();
    // Write out alignments to file.
    void writeAlignments();

    // The fragment store for the data.
    seqan::FragmentStore<> store;

    // Configuration.
    SeqConsOptions options;
};

void SeqConsAppImpl::writeConsensus()
{
    if (options.verbosity >= 1)
        std::cerr << "Writing consensus to " << options.outputFileConsensus << " ...";
    seqan::SeqFileOut seqFileOut;
    if (!open(seqFileOut, options.outputFileConsensus.c_str()))
        throw std::runtime_error("Could not open consensus output file for writing.");
    for (unsigned contigID = 0; contigID < length(store.contigStore); ++contigID)
    {
        std::stringstream ss;
        ss << "consensus_" << contigID;
        writeRecord(seqFileOut, ss.str(), store.contigStore[contigID].seq);
    }
    if (options.verbosity >= 1)
        std::cerr << " OK\n";
}

void SeqConsAppImpl::writeAlignments()
{
    if (options.verbosity >= 1)
        std::cerr << "Writing alignments to " << options.outputFileAlignment << " ...";

    if (endsWithIgnoreCase(options.outputFileAlignment, ".txt"))
    {
        std::fstream out(options.outputFileAlignment.c_str(), std::ios::binary | std::ios::out);
        seqan::AlignedReadLayout layout;
        layoutAlignment(layout, store);
        for (unsigned contigID = 0; contigID < length(store.contigStore); ++contigID)
        {
            int endPos = 0;
            for (unsigned i = 0; i < length(store.alignedReadStore); ++i)
                if (store.alignedReadStore[i].contigId == contigID)
                    endPos = std::max(endPos, (int)store.alignedReadStore[i].endPos);
            out << ">consensus_" << contigID << "\n";
            printAlignment(out, layout, store, /*contigID=*/contigID, /*beginPos=*/0, /*endPos=*/endPos, 0, 100);
        }
    }
    else  // ends in .sam
    {
        seqan::BamFileOut out;
        if (!open(out, options.outputFileAlignment.c_str()))
            throw std::runtime_error("Could not open output file.");
        writeRecords(out, store);
    }
    if (options.verbosity >= 1)
        std::cerr << " OK\n";
}

void SeqConsAppImpl::run()
{
    // Load read or alignment data.
    if (options.verbosity >= 1)
        std::cerr << "\n__LOADING DATA_______________________________________________________________\n"
                  << '\n';
    if (endsWithIgnoreCase(options.inputFile, ".sam"))
        loadAlignments(options.inputFile.c_str());
    else  // sequence file
        loadReads(options.inputFile.c_str());

    if (options.verbosity >= 1)
        std::cerr << "\n__COMPUTATION________________________________________________________________\n"
                  << '\n';
    // Perform the consensus or realignment computation.
    switch (options.operation)
    {
        case SeqConsOptions::ALN_CONSENSUS:
            performConsensusAlignment(false, false, true);
            break;

        case SeqConsOptions::OVL_CONSENSUS:
            performConsensusAlignment(false, false, false);
            break;

        case SeqConsOptions::CTG_CONSENSUS:
            performConsensusAlignment(true, false, false);
            break;

        case SeqConsOptions::POS_CONSENSUS:
            performConsensusAlignment(true, true, false);
            break;

        case SeqConsOptions::REALIGN:
            performRealignment();
            break;

        case SeqConsOptions::NOP:
        default:
            // do nothing, will just write out store
            break;
    }

    // Write the consensus and/or the alignments.
    if (options.verbosity >= 1)
        std::cerr << "\n__WRITING RESULT_____________________________________________________________\n"
                  << '\n';
    if (!options.outputFileConsensus.empty())
        writeConsensus();
    if (!options.outputFileAlignment.empty())
        writeAlignments();
}

void SeqConsAppImpl::loadReads(char const * fileName)
{
    // Allocate space for one contig in the store.
    resize(store.contigStore, 1);
    std::stringstream ns;
    ns << "consensus_1";
    appendValue(store.contigNameStore, ns.str());
    // Load reads from sequence file.
    seqan::SeqFileIn seqFileIn;
    if (options.verbosity >= 1)
        std::cerr << "Loading reads from " << fileName << "...";
    if (!open(seqFileIn, fileName))
        throw std::runtime_error("Problem opening input sequence file for reading.");

    int maxPos = 0;
    seqan::CharString id, seq;
    while (!atEnd(seqFileIn))
    {
        try
        {
            readRecord(id, seq, seqFileIn);
        }
        catch (seqan::ParseError const & e)
        {
            throw std::runtime_error(std::string("Problem reading from input sequence file: ") +
                                     e.what());
        }
        trimAfterSpace(id);
        int64_t readID = appendRead(store, seq, id);
        appendAlignedRead(store, readID, /*contigID=*/0, /*beginPos=*/0, (int)length(seq));
        maxPos = std::max(maxPos, (int)length(seq));
    }

    // Create sequence of Ns with sufficient size.
    resize(store.contigStore[0].seq, 'N');

    if (options.verbosity >= 1)
        std::cerr << "OK\n";
}

void SeqConsAppImpl::loadAlignments(char const * fileName)
{
    if (options.verbosity >= 1)
        std::cerr << "  Loading alignments from " << fileName << "...";
    seqan::BamFileIn bamFileIn;
    if (!open(bamFileIn, fileName))
        throw std::runtime_error("Problem opening input alignment file for reading.");
    try
    {
        readRecords(store, bamFileIn);
    }
    catch (seqan::ParseError const & e)
    {
        throw std::runtime_error(e.what());
    }
    if (options.verbosity >= 1)
        std::cerr << "OK\n";
}

void SeqConsAppImpl::performConsensusAlignment(bool useContigID, bool usePositions, bool useGlobalAlignment)
{
    // Setup the consensus alignment options.
    seqan::ConsensusAlignmentOptions caOptions;
    caOptions.useContigID = useContigID;
    caOptions.usePositions = usePositions;
    caOptions.useGlobalAlignment = useGlobalAlignment;
    caOptions.runRealignment = false;  // will run manually.
    if (options.verbosity >= 3)
        caOptions.verbosity = 3;

    caOptions.overlapMaxErrorRate = (int)options.overlapMaxErrorRate;
    caOptions.overlapMinLength = options.overlapMinLength;
    caOptions.overlapMinCount = options.overlapMinCount;
    caOptions.posDelta = options.overlapWindowSize;

    caOptions.kMerSize = options.kMerSize;
    caOptions.kMerMaxOcc = options.kMerMaxOcc;

    // Perform the consensus alignment.
    double startTime = seqan::sysTime();
    if (options.verbosity >= 1)
        std::cerr << "Performing consensus computation...";
    if (options.verbosity >= 3)
        std::cerr << "\n";
    consensusAlignment(store, caOptions);
    if (options.verbosity >= 1)
        std::cerr << " OK\n";
    if (options.verbosity >= 2)
        std::cerr << "\t=> consensus step took " << seqan::sysTime() - startTime << "s\n";

    // Finally, compute realignment of the resulting multi-read alignment.
    performRealignment();
}

void SeqConsAppImpl::performRealignment()
{
    double startTime = seqan::sysTime();
    if (options.verbosity >= 1)
        std::cerr << "Performing realignment...";
    if (options.verbosity >= 3)
        std::cerr << "\n";
    for (unsigned contigID = 0; contigID < length(store.contigStore); ++contigID)
        reAlignment(store, contigID, /*method=*/2, options.reAlignmentBandwidth, /*includeReference=*/false);
    if (options.verbosity >= 1)
        std::cerr << " OK\n";
    if (options.verbosity >= 2)
        std::cerr << "\t=> realignment took " << seqan::sysTime() - startTime << "s\n";
}

// ----------------------------------------------------------------------------
// Class SeqConsApp
// ----------------------------------------------------------------------------

SeqConsApp::SeqConsApp(SeqConsOptions const & options) : impl(new SeqConsAppImpl(options))
{}

SeqConsApp::~SeqConsApp()
{}

void SeqConsApp::run()
{
    impl->run();
}