Debian Codegraph @

src/bamleftalign.cpp

raw
1 #include <iostream>
2 #include <getopt.h>
3 #include <fstream>
4 #include <iostream>
5 #include <sstream>
6 #include <signal.h>
7 #include <stdlib.h>
8 #include <cmath>
9 #include <algorithm>
10 #include <map>
11 #include <vector>
12
13 #include "FBFasta.h"
14
15 #include "LeftAlign.h"
16
17 #ifdef VERBOSE_DEBUG
18 #define DEBUG(msg) \
19 if (debug) { cerr << msg; }
20 #else
21 #define DEBUG(msg)
22 #endif
23
24 using namespace std;
25
26 void printUsage(char** argv) {
27 cerr << "usage: [BAM data stream] | " << argv[0] << " [options]" << endl
28 << endl
29 << "Left-aligns and merges the insertions and deletions in all alignments in stdin." << endl
30 << "Iterates until each alignment is stable through a left-realignment step." << endl
31 << endl
32 << "arguments:" << endl
33 << " -f --fasta-reference FILE FASTA reference file to use for realignment (required)" << endl
34 << " -d --debug Print debugging information about realignment process" << endl
35 << " -s --suppress-output Don't write BAM output stream (for debugging)" << endl
36 << " -m --max-iterations N Iterate the left-realignment no more than this many times" << endl
37 << " -c --compressed Write compressed BAM on stdout, default is uncompressed" << endl;
38 }
39
40 int main(int argc, char** argv) {
41
42 int c;
43
44 FB::FastaReference reference;
45 bool has_ref = false;
46 bool suppress_output = false;
47 bool debug = false;
48 bool isuncompressed = true;
49
50 int maxiterations = 50;
51
52 if (argc < 2) {
53 printUsage(argv);
54 exit(1);
55 }
56
57 while (true) {
58 static struct option long_options[] =
59 {
60 {"help", no_argument, 0, 'h'},
61 {"debug", no_argument, 0, 'd'},
62 {"fasta-reference", required_argument, 0, 'f'},
63 {"max-iterations", required_argument, 0, 'm'},
64 {"suppress-output", no_argument, 0, 's'},
65 {"compressed", no_argument, 0, 'c'},
66 {0, 0, 0, 0}
67 };
68
69 int option_index = 0;
70
71 c = getopt_long (argc, argv, "hdcsf:m:",
72 long_options, &option_index);
73
74 /* Detect the end of the options. */
75 if (c == -1)
76 break;
77
78 switch (c) {
79
80 case 'f':
81 reference.open(optarg); // will exit on open failure
82 has_ref = true;
83 break;
84
85 case 'm':
86 maxiterations = atoi(optarg);
87 break;
88
89 case 'd':
90 debug = true;
91 break;
92
93 case 's':
94 suppress_output = true;
95 break;
96
97 case 'c':
98 isuncompressed = false;
99 break;
100
101 case 'h':
102 printUsage(argv);
103 exit(0);
104 break;
105
106 case '?':
107 printUsage(argv);
108 exit(1);
109 break;
110
111 default:
112 abort();
113 break;
114 }
115 }
116
117 if (!has_ref) {
118 cerr << "no FASTA reference provided, cannot realign" << endl;
119 exit(1);
120 }
121
122
123 BAMSINGLEREADER reader;
124 if (!reader.Open(STDIN)) {
125 cerr << "could not open stdin for reading" << endl;
126 exit(1);
127 }
128
129 #ifdef HAVE_BAMTOOLS
130
131 BamWriter writer;
132
133 if (isuncompressed) {
134 writer.SetCompressionMode(BamWriter::Uncompressed);
135 }
136
137 if (!suppress_output && !writer.Open("stdout", reader.GetHeaderText(), reader.GetReferenceData())) {
138 cerr << "could not open stdout for writing" << endl;
139 exit(1);
140 }
141 #else
142
143 SeqLib::BamWriter writer(SeqLib::BAM);
144 SeqLib::BamHeader hdr = reader.Header();
145 if (hdr.isEmpty()) {
146 cerr << "could not open header for input" << endl;
147 exit(1);
148 }
149 writer.SetHeader(hdr);
150
151 if (!suppress_output && !writer.Open("-")) {
152 cerr << "could not open stdout for writing" << endl;
153 exit(1);
154 }
155 writer.WriteHeader();
156 #endif
157
158 // store the names of all the reference sequences in the BAM file
159 map<int, string> referenceIDToName;
160 REFVEC referenceSequences = reader.GETREFDATA;
161 int i = 0;
162 for (REFVEC::iterator r = referenceSequences.begin(); r != referenceSequences.end(); ++r) {
163 referenceIDToName[i] = r->REFNAME;
164 ++i;
165 }
166
167 BAMALIGN alignment;
168
169 while (GETNEXT(reader, alignment)) {
170
171 DEBUG("--------------------------- read --------------------------" << endl);
172 DEBUG("| " << referenceIDToName[alignment.REFID] << ":" << alignment.POSITION << endl);
173 DEBUG("| " << alignment.QNAME << ":" << alignment.ENDPOSITION << endl);
174 DEBUG("| " << alignment.QNAME << ":" << (alignment.ISMAPPED ? " mapped" : " unmapped") << endl);
175 DEBUG("| " << alignment.QNAME << ":" << " cigar data size: " << alignment.GETCIGAR.size() << endl);
176 DEBUG("--------------------------- realigned --------------------------" << endl);
177
178 // skip unmapped alignments, as they cannot be left-realigned without CIGAR data
179 if (alignment.ISMAPPED) {
180
181 int endpos = alignment.ENDPOSITION;
182 int length = endpos - alignment.POSITION + 1;
183 if (alignment.POSITION >= 0 && length > 0) {
184 if (!stablyLeftAlign(alignment,
185 reference.getSubSequence(
186 referenceIDToName[alignment.REFID],
187 alignment.POSITION,
188 length),
189 maxiterations, debug)) {
190 cerr << "unstable realignment of " << alignment.QNAME
191 << " at " << referenceIDToName[alignment.REFID] << ":" << alignment.POSITION << endl
192 << alignment.QUERYBASES << endl;
193 }
194 }
195
196 }
197
198 DEBUG("----------------------------------------------------------------" << endl);
199 DEBUG(endl);
200
201 if (!suppress_output)
202 WRITEALIGNMENT(writer, alignment);
203
204 }
205
206 reader.Close();
207 if (!suppress_output)
208 writer.Close();
209
210 return 0;
211 }