File: bam2roi.cpp

package info (click to toggle)
seqan2 2.4.0+dfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 223,504 kB
  • sloc: cpp: 256,886; ansic: 91,672; python: 8,339; sh: 995; xml: 570; makefile: 251; awk: 51
file content (407 lines) | stat: -rw-r--r-- 17,259 bytes parent folder | download | duplicates (4)
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// ==========================================================================
//                   NGS: Regions of Interest Analysis
// ==========================================================================
// Copyright (c) 2012-2018, Bernd Jagla, Institut Pasteur
// 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: Bernd Jagla <bernd.jagla@pasteur.fr>
// Author: Manuel Holtgrewe <manuel.holtgrewe@fu-berlin.de>
// ==========================================================================


#include <fstream>
#include <iostream>

#include <seqan/arg_parse.h>
#include <seqan/basic.h>
#include <seqan/sequence.h>
#include <seqan/file.h>
#include <seqan/stream.h>
#include <seqan/bam_io.h>

#include "roi_builder.h"

// ==========================================================================
// Classes
// ==========================================================================

// ----------------------------------------------------------------------------
// Class Options
// ----------------------------------------------------------------------------

// Options for the bam2roi Application.

struct Options
{
    // Verbosity of output: 0 -- quiet, 1 -- normal, 2 -- verbose, 3 -- very verbose.
    int verbosity;

    // -----------------------------------------------------------------------
    // Input / Output Options
    // -----------------------------------------------------------------------

    // Paths to input read files.
    seqan::CharString inputFileName;

    // Paths to output read files of accepted reads.
    seqan::CharString outputFileName;

    // -----------------------------------------------------------------------
    // ROI Creation Options
    // -----------------------------------------------------------------------

	// Whether or not the experiment is strand-specific.
	bool strandSpecific;

    // Whether or not to use paired information.
    bool usePairing;

    // Whether or not to link over skipped bases.
    bool linkOverSkipped;

    Options() : verbosity(0), strandSpecific(false), usePairing(false), linkOverSkipped(false)
    {}
};

// ==========================================================================
// Functions
// ==========================================================================

// --------------------------------------------------------------------------
// Function yesNo()                                                 [Options]
// --------------------------------------------------------------------------

char const * yesNo(bool b)
{
    return b ? "YES" : "NO";
}

// --------------------------------------------------------------------------
// Function print()                                                 [Options]
// --------------------------------------------------------------------------

void print(std::ostream & out, Options const & options)
{
    out << "__OPTIONS_____________________________________________________________________\n"
        << "\n"
        << "INPUT FILE       \t" << options.inputFileName << "\n"
        << "OUTPUT FILE      \t" << options.outputFileName << "\n"
        << "\n"
        << "STRAND SPECIFIC  \t" << yesNo(options.strandSpecific) << "\n"
        << "USE PAIRED INFO  \t" << yesNo(options.usePairing) << "\n"
        << "LINK OVER SKIPPED\t" << yesNo(options.linkOverSkipped) << "\n"
        << "\n";
}

// --------------------------------------------------------------------------
// Function parseCommandLine()
// --------------------------------------------------------------------------

// Parse command line with ArgumentParser class.

seqan::ArgumentParser::ParseResult
parseCommandLine(Options & options, int argc, char const ** argv)
{
    // Setup ArgumentParser.
    seqan::ArgumentParser parser("bam2roi");
    setCategory(parser, "NGS ROI Analysis");

    // Set short description, version, and date.
    setShortDescription(parser, "Create ROI from BAM file.");
    setVersion(parser, SEQAN_APP_VERSION " [" SEQAN_REVISION "]");
    setDate(parser, SEQAN_DATE);

    // Define usage line and long description.
    addUsageLine(parser, "\\fB-if\\fP \\fIIN.bam\\fP \\fB-of\\fP \\fIOUT.roi\\fP");

    addDescription(parser,
                   "Calculated consecutive regions of coverage from alignment file \\fIIN.bam\\fP "
                   "and write regions of interest to file \\fIOUT.roi\\fP. "
				   "Counting is performed over the entire region (including intron and N-regions) "
				   "based on the CIGAR string of the alignment record.");
	// it is in the scope of this application to make any assumtions over splice junctions
	// since there are many boundary issues concerning splice junctions of many reads we
	// consider this an alignment artefact. Splice junctions can be analyzed separately by
	// using special GFF annotations in conjunction with these roi files.

    // -----------------------------------------------------------------------
    // General Options
    // -----------------------------------------------------------------------

    addSection(parser, "General Options");

    addOption(parser, seqan::ArgParseOption("v", "verbose", "Verbose mode."));
    addOption(parser, seqan::ArgParseOption("vv", "very-verbose", "Very verbose mode."));

    // -----------------------------------------------------------------------
    // Input / Output Options
    // -----------------------------------------------------------------------

    addSection(parser, "Input / Output Parameters");

    addOption(parser, seqan::ArgParseOption("if", "input-file", "SAM/BAM formatted file.  Must be sorted by coordinate.",
                                            seqan::ArgParseOption::INPUT_FILE));
    setValidValues(parser, "input-file", seqan::BamFileIn::getFileExtensions());
    setRequired(parser, "input-file");

    addOption(parser, seqan::ArgParseOption("of", "output-file", "Output file with regions of interest.",
                                            seqan::ArgParseOption::OUTPUT_FILE));
    setValidValues(parser, "output-file", seqan::RoiFileIn::getFileExtensions());
    setRequired(parser, "output-file");

    // -----------------------------------------------------------------------
    // Optional Parameters
    // -----------------------------------------------------------------------

    addSection(parser, "ROI Construction Options");

	addOption(parser, seqan::ArgParseOption("ss", "strand-specific",
                                            "Calculate strand-specific ROIs (see section Strand Specificness below."));

	addOption(parser, seqan::ArgParseOption("ip", "ignore-pairing",
                                            "Ignore paired information.  Also see Section ROI Creation Details."));

	addOption(parser, seqan::ArgParseOption("ls", "link-over-skipped", "Link over skipped bases in the read alignment."));

    // -----------------------------------------------------------------------
    // Documentation of ROI Creation Details.
    // -----------------------------------------------------------------------

    addTextSection(parser, "ROI Creation Details");

    addText(parser,
            "By default, ROIs are created from single-end data by looking at the alignment only.  Matches, mismatches, "
            "and deletions from the CIGAR string are counted as consecutive bases with aligned read bases.  Skipped bases "
            "(CIGAR operation \"N\") are counted as uncovered and do not link two regions.");

    addText(parser,
            "This behaviour can be changed by specifying \\fB--link-over-skipped\\fP where these skipped bases are not "
            "counted as being covered but they link two otherwise not connected ROIs into one with possibly 0 counts in "
            "between.");

    addText(parser,
            "For paired data, we by default link two ROIs that have no interconnecting bases if there is a pair of reads "
            "where one mate maps to either ROI.  You can switch off this behaviour using \\fP--ignore-pairing\\fP.  The "
            "forward and reverse strand of the paired read will still be used when using strand specific information.");

    // -----------------------------------------------------------------------
    // Documentation of ROI file format.
    // -----------------------------------------------------------------------

    addTextSection(parser, "ROI File Format");

    addText(parser,
            "The file begins with a sequence of comments, followed by one column header, followed by the records.");

    addText(parser, "\\fIComments\\fP start with a single hash (#) and are ignored in analysis.");

    addText(parser,
            "The \\fIcolumn header\\fP starts with two hashes (##) and is followed by the tab separated column names. "
            "The column names must not contain spaces.");

    addText(parser,
            "The columns \\fI1-7\\fP are fixed.  The last column is always \"counts\".  The columns are as follows");

    addListItem(parser, "1. ref", "The reference name.");
    addListItem(parser, "2. begin_pos", "The begin position.");
    addListItem(parser, "3. end_pos", "The end position.");
    addListItem(parser, "4. region_name", "The name of the region.");
    addListItem(parser, "5. length", "The length of the region.");
    addListItem(parser, "6. strand", "The strand, one of \"+\" and \"-\". \"+\" in case of being not strand-specific.");
    addListItem(parser, "7. max_count", "The largest value of the \\fIcounts\\fP columns.");
    addListItem(parser, "additional columns", "Additional annotation data for the ROI.");
    addListItem(parser, "N. counts", "Comma-separated list of \\fIlength\\fP unsignd integers.");

    // TODO(holtgrew): Write me!

    // -----------------------------------------------------------------------
    // Parsing and Value Extraction
    // -----------------------------------------------------------------------

    // Parse command line.
    seqan::ArgumentParser::ParseResult res = seqan::parse(parser, argc, argv);

    // Only extract  options if the program will continue after parseCommandLine()
    if (res != seqan::ArgumentParser::PARSE_OK)
        return res;

    // Extract option values.

    options.verbosity = 1;
    if (isSet(parser, "verbose"))
        options.verbosity = 2;
    if (isSet(parser, "very-verbose"))
        options.verbosity = 3;

    getOptionValue(options.inputFileName, parser, "input-file");
    getOptionValue(options.outputFileName, parser, "output-file");

    options.strandSpecific = isSet(parser, "strand-specific");
    options.usePairing = !isSet(parser, "ignore-pairing");
    options.linkOverSkipped = isSet(parser, "link-over-skipped");

	return seqan::ArgumentParser::PARSE_OK;
}

// --------------------------------------------------------------------------
// Function main()
// --------------------------------------------------------------------------

int main(int argc, char const ** argv)
{
	// Parse the command line.
    Options options;
    seqan::ArgumentParser::ParseResult res = parseCommandLine(options, argc, argv);

    // If parsing was not successful then exit with code 1 if there were errors.  Otherwise, exit with code 0 (e.g. help
    // was printed).
    if (res != seqan::ArgumentParser::PARSE_OK)
        return res == seqan::ArgumentParser::PARSE_ERROR;

    // Print program name and options.

    if (options.verbosity >= 1)
    {
        std::cerr << "BAM TO ROI\n"
                  << "==========\n"
                  << "\n";
        print(std::cerr, options);
    }

    // -----------------------------------------------------------------------
    // Open input and output file
    // -----------------------------------------------------------------------

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

    if (options.verbosity >= 1)
         std::cerr << "Opening " << options.inputFileName << " ...";
	seqan::BamFileIn bamFileIn;
    if (!open(bamFileIn, toCString(options.inputFileName)))
	{
		std::cerr << "ERROR: Could not open " << options.inputFileName << "\n";
        return 1;
	}
    if (options.verbosity >= 1)
        std::cerr << "OK\n";

    if (options.verbosity >= 1)
         std::cerr << "Opening " << options.outputFileName << " ...";
    seqan::RoiFileOut roiFileOut;
    if (!open(roiFileOut, toCString(options.outputFileName)))
	{
		std::cerr << "ERROR: Could not open " << options.outputFileName << "\n";
        return 1;
	}
    if (options.verbosity >= 1)
        std::cerr << "OK\n";

    // -----------------------------------------------------------------------
    // Build ROIs
    // -----------------------------------------------------------------------

    if (options.verbosity >= 1)
        std::cerr << "__BUILD ROI___________________________________________________________________\n"
                  << "\n"
                  << "Working ...";

    // We have two RoiBuilder objects, one for ROIs on the forward strand and one for ROIs on the reverse strand.  In
    // the case of not being strand specific, we only use the forward builder.

    RoiBuilderOptions roiBuilderOptions(options.verbosity, options.strandSpecific,
                                        options.usePairing, options.linkOverSkipped);
    RoiBuilder roiBuilderF(roiFileOut, roiBuilderOptions);
    roiBuilderF.writeHeader();  // only once
    RoiBuilder roiBuilderR(roiFileOut, roiBuilderOptions);
    // Set the reference sequence names.
    seqan::BamHeader header;
    readHeader(header, bamFileIn);
    for (unsigned i = 0; i < length(contigNames(context(bamFileIn))); ++i)
    {
        appendValue(roiBuilderF.refNames, contigNames(context(bamFileIn))[i]);
        appendValue(roiBuilderR.refNames, contigNames(context(bamFileIn))[i]);
    }

    // TODO(holtgrew): This is only suited for the Illumina mate pair protocol at the moment (--> <--).
    int oldRId = 0;
    int oldPos = 0;
    seqan::BamAlignmentRecord record;
    while (!atEnd(bamFileIn))
    {
        readRecord(record, bamFileIn);

        // Break if record is unmapped.
        if (hasFlagUnmapped(record))
            break;  // Found first unmapped record!

        // Check sorting.
        if (record.rID < oldRId || (record.rID == oldRId && record.beginPos < oldPos))
        {
            std::cerr << "\nERROR: The BAM file is not sorted properly!\n";
            return 1;
        }
        oldRId = record.rID;
        oldPos = record.beginPos;

        // Process record, different cases, depending on strandedness and pairedness.
        if (hasFlagMultiple(record))
        {
            if (hasFlagFirst(record) && !hasFlagRC(record))
                roiBuilderF.pushRecord(record);
            else if (hasFlagFirst(record) && hasFlagRC(record))
                roiBuilderR.pushRecord(record);
            else if (hasFlagLast(record) && !hasFlagRC(record))
                roiBuilderF.pushRecord(record);
            else  // (hasFlagLast(record) && hasFlagRC(record))
                roiBuilderR.pushRecord(record);
        }
        else
        {
            if (options.strandSpecific)
            {
                if (hasFlagRC(record))
                    roiBuilderR.pushRecord(record);
                else
                    roiBuilderF.pushRecord(record);
            }
            else
            {
                roiBuilderF.pushRecord(record);
            }
        }
    }

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

    return 0;
}