Debian Codegraph @

src/seqtools/seqtools.cpp

raw symbols SCIP
1 #include <getopt.h>
2 #include <iostream>
3 #include <string>
4 #include <sstream>
5 #include <cassert>
6
7 #include "SeqLib/BFC.h"
8 #include "SeqLib/FastqReader.h"
9 #include "SeqLib/BamReader.h"
10 #include "SeqLib/BamWriter.h"
11 #include "SeqLib/BWAWrapper.h"
12 #include "SeqLib/FermiAssembler.h"
13 #include "SeqLib/ReadFilter.h"
14
15 void kt_pipeline(int n_threads, void *(*func)(void*, int, void*), void *shared_data, int n_steps);
16
17 #define AUTHOR "Jeremiah Wala <[email protected]>"
18
19 static const char *SEQTOOLS_USAGE_MESSAGE =
20 "Program: seqtools \n"
21 "Contact: Jeremiah Wala [ [email protected] ]\n"
22 "Usage: seqtools <command> [options]\n\n"
23 "Commands:\n"
24 " bfc Error correction from a BAM or fasta, direct to re-aligned BAM\n"
25 " fml FermiKit assembly (with error correction), direct to re-aligned BAM\n"
26 "\nReport bugs to [email protected] \n\n";
27
28 static const char *BFC_USAGE_MESSAGE =
29 "Program: seqtools bfc \n"
30 "Contact: Jeremiah Wala [ [email protected] ]\n"
31 "Usage: seqtools bfc [options]\n\n"
32 "Commands:\n"
33 " --verbose, -v Set verbose output\n"
34 " --fasta, -f Output stream should be a FASTA (no realignment)\n"
35 " --bam, -b Output stream should be a BAM (not SAM)\n"
36 " --cram, -C Output stream should be a CRAM (not SAM)\n"
37 " --infasta, -F <file> Input a FASTA insted of BAM/SAM/CRAM stream\n"
38 " --reference, -G <file> Reference genome if using BWA-MEM realignment\n"
39 "\nReport bugs to [email protected] \n\n";
40
41 static const char *FML_USAGE_MESSAGE =
42 "Program: seqtools fml \n"
43 "Contact: Jeremiah Wala [ [email protected] ]\n"
44 "Usage: seqtools fml [options]\n\n"
45 "Description: Extract sequences and assemble and realign contigs\n"
46 "Commands:\n"
47 " --verbose, -v Set verbose output\n"
48 " --fasta, -f Output stream should be a FASTA (no realignment)\n"
49 " --bam, -b Output stream should be a BAM (not SAM)\n"
50 " --cram, -C Output stream should be a CRAM (not SAM)\n"
51 " --infasta, -F <file> Input a FASTA insted of BAM/SAM/CRAM stream\n"
52 " --reference, -G <file> Reference genome if using BWA-MEM realignment\n"
53 "\nReport bugs to [email protected] \n\n";
54
55 void runbfc(int argc, char** argv);
56 void runfml(int argc, char** argv);
57 void parseOptions(int argc, char** argv, const char* msg);
58
59 namespace opt {
60
61 static bool verbose = false;
62 static char mode = 's';
63 static std::string input;
64 static std::string reference = "/seq/references/Homo_sapiens_assembly19/v1/Homo_sapiens_assembly19.fasta";
65 static std::string fasta; // input is a fasta
66 static std::string target; // input target sequence
67 }
68
69 static const char* shortopts = "hbfvCG:F:T:";
70 static const struct option longopts[] = {
71 { "help", no_argument, NULL, 'h' },
72 { "verbose", no_argument, NULL, 'v' },
73 { "bam", no_argument, NULL, 'b' },
74 { "cram", no_argument, NULL, 'C' },
75 { "fasta", no_argument, NULL, 'f' },
76 { "infasta", required_argument, NULL, 'F' },
77 { "reference", required_argument, NULL, 'G' },
78 { "target", required_argument, NULL, 'T' },
79 { NULL, 0, NULL, 0 }
80 };
81
82 int main(int argc, char** argv) {
83
84 if (argc <= 1) {
85 std::cerr << SEQTOOLS_USAGE_MESSAGE;
86 return 0;
87 } else {
88 std::string command(argv[1]);
89 if (command == "help" || command == "--help") {
90 std::cerr << SEQTOOLS_USAGE_MESSAGE;
91 return 0;
92 } else if (command == "bfc") {
93 runbfc(argc -1, argv + 1);
94 } else if (command == "fml") {
95 runfml(argc -1, argv + 1);
96 } else {
97 std::cerr << SEQTOOLS_USAGE_MESSAGE;
98 return 0;
99 }
100 }
101
102 return 0;
103
104 }
105
106 void runfml(int argc, char** argv) {
107
108 parseOptions(argc, argv, FML_USAGE_MESSAGE);
109
110 SeqLib::FermiAssembler fml;
111
112 if (!opt::fasta.empty()) {
113 SeqLib::FastqReader f(opt::fasta);
114
115 SeqLib::UnalignedSequenceVector usv;
116 std::string qn, seq;
117 SeqLib::UnalignedSequence u;
118 if (opt::verbose)
119 std::cerr << "...reading fasta/fastq file " << opt::fasta << std::endl;
120 while (f.GetNextSequence(u))
121 fml.AddRead(u);
122
123 if (opt::verbose)
124 std::cerr << "...read in " << SeqLib::AddCommas(fml.NumSequences()) << " sequences" << std::endl;
125
126 } else {
127
128 SeqLib::BamReader br;
129 if (!br.Open(opt::input == "-" ? "-" : opt::input))
130 exit(EXIT_FAILURE);
131
132 SeqLib::Filter::ReadFilter rf;
133
134
135 if (opt::verbose)
136 std::cerr << "...opened " << opt::input << std::endl;
137 SeqLib::BamRecord rec;
138 SeqLib::BamRecordVector brv;
139 size_t count = 0;
140 while(br.GetNextRecord(rec)) {
141 if (++count % 1000000 == 0 && opt::verbose)
142 std::cerr << "...at read " << SeqLib::AddCommas(count) << " " << rec.Brief() << std::endl;
143 brv.push_back(rec); //rec.Sequence().c_str(), rec.Qualities().c_str(), rec.Qname().c_str());
144 }
145 fml.AddReads(brv);
146
147 }
148
149 if (opt::verbose)
150 std::cerr << "...error correcting " << std::endl;
151 fml.CorrectReads();
152
153 if (opt::verbose)
154 std::cerr << "...assembling " << std::endl;
155 fml.PerformAssembly();
156
157 // retrieve the reads
158 std::vector<std::string> contigs = fml.GetContigs();
159
160 size_t count = 0;
161 if (opt::mode == 'f') {
162 for (std::vector<std::string>::const_iterator i = contigs.begin(); i != contigs.end(); ++i)
163 std::cout << ">contig" << ++count << std::endl << *i << std::endl;
164 return;
165 }
166
167 SeqLib::BamWriter bw;
168 if (opt::mode == 'b')
169 bw = SeqLib::BamWriter(SeqLib::BAM);
170 else if (opt::mode == 's')
171 bw = SeqLib::BamWriter(SeqLib::SAM);
172 else if (opt::mode == 'C') {
173 bw = SeqLib::BamWriter(SeqLib::CRAM);
174 bw.SetCramReference(opt::reference);
175 }
176 else {
177 std::cerr << "Unrecognized output stream mode " << opt::mode << std::endl;
178 exit(EXIT_FAILURE);
179 }
180
181 if (!bw.Open("-"))
182 exit(EXIT_FAILURE);
183
184 SeqLib::BWAWrapper bwa;
185 if (!bwa.LoadIndex(opt::reference)) {
186 std::cerr << "Failed to load index for BWA-MEM from: " << opt::reference << std::endl;
187 exit(EXIT_FAILURE);
188 }
189
190 bw.SetHeader(bwa.HeaderFromIndex());
191 bw.WriteHeader();
192
193 if (opt::verbose)
194 std::cerr << "...aligning contig with BWA-MEM" << std::endl;
195
196 // run through and read
197 std::stringstream ss;
198 for (std::vector<std::string>::const_iterator i = contigs.begin(); i != contigs.end(); ++i) {
199 SeqLib::BamRecordVector brv;
200 const bool hardclip = false;
201 const double frac = 0.9;
202 ss << "contig" << ++count;
203 const int max_secondary = 10;
204 bwa.AlignSequence(*i, ss.str(), brv, hardclip, frac, max_secondary);
205 ss.str(std::string());
206 for (SeqLib::BamRecordVector::iterator r = brv.begin();
207 r != brv.end(); ++r) {
208 bw.WriteRecord(*r);
209 }
210 }
211
212 }
213
214 void runbfc(int argc, char** argv) {
215
216 parseOptions(argc, argv, BFC_USAGE_MESSAGE);
217
218 SeqLib::BFC b;
219
220 if (!opt::fasta.empty()) {
221 // read in a fasta file
222 SeqLib::FastqReader f(opt::fasta);
223
224 SeqLib::UnalignedSequence u;
225 while (f.GetNextSequence(u)) {
226 if (!b.AddSequence(u.Seq.c_str(), u.Qual.c_str(), u.Name.c_str())) {
227 std::cerr << "Error adding sequence from fasta: " << u.Seq << std::endl;
228 exit(EXIT_FAILURE);
229 }
230 }
231 } else { //if (opt::mode == 'b' || opt::mode == 's' || opt::mode == 'C') {
232 SeqLib::BamReader br;
233 if (!br.Open(opt::input == "-" ? "-" : opt::input))
234 exit(EXIT_FAILURE);
235 if (opt::verbose)
236 std::cerr << "...opened " << opt::input << std::endl;
237 SeqLib::BamRecord rec;
238 size_t count = 0;
239 while(br.GetNextRecord(rec)) {
240 if (++count % 1000000 == 0 && opt::verbose)
241 std::cerr << "...at read " << SeqLib::AddCommas(count) << " " << rec.Brief() << std::endl;
242 //b.AddSequence(rec); //rec.Sequence().c_str(), rec.Qualities().c_str(), rec.Qname().c_str());
243 b.AddSequence(rec.Sequence().c_str(), rec.Qualities().c_str(), rec.Qname().c_str());
244 }
245 }
246
247 if (opt::verbose)
248 std::cerr << "...read in " << SeqLib::AddCommas(b.NumSequences()) << " sequences" << std::endl;
249
250 if (!b.Train()) {
251 std::cerr << "Training failed on " << b.NumSequences() << std::endl;
252 exit(EXIT_FAILURE);
253 }
254 if (opt::verbose)
255 std::cerr << "...finished training " << SeqLib::AddCommas(b.NumSequences()) << " sequences" << std::endl;
256 if (!b.ErrorCorrect()) {
257 std::cerr << "Correction failed on " << b.NumSequences() << std::endl;
258 exit(EXIT_FAILURE);
259 }
260 if (opt::verbose)
261 std::cerr << "...finished correcting " << SeqLib::AddCommas(b.NumSequences()) << " sequences" << std::endl;
262
263 SeqLib::UnalignedSequenceVector u;
264 std::string seqr, namr;
265 while (b.GetSequence(seqr, namr))
266 u.push_back(SeqLib::UnalignedSequence(namr, seqr));
267
268 if (opt::verbose)
269 std::cerr << "nseqs: " << u.size()
270 << " kcov: " << b.GetKCov()
271 << " kmer: " << b.GetKMer() << std::endl;
272
273 if (opt::mode == 'f') {
274 for (SeqLib::UnalignedSequenceVector::const_iterator i = u.begin();
275 i != u.end(); ++i) {
276 std::cout << ">" << i->Name << std::endl << i->Seq << std::endl;
277 }
278 return;
279 }
280
281 SeqLib::BamWriter bw;
282 if (opt::mode == 'b')
283 bw = SeqLib::BamWriter(SeqLib::BAM);
284 else if (opt::mode == 's')
285 bw = SeqLib::BamWriter(SeqLib::SAM);
286 else if (opt::mode == 'C') {
287 bw = SeqLib::BamWriter(SeqLib::CRAM);
288 bw.SetCramReference(opt::reference);
289 }
290 else {
291 std::cerr << "Unrecognized output stream mode " << opt::mode << std::endl;
292 exit(EXIT_FAILURE);
293 }
294
295 if (!bw.Open("-"))
296 exit(EXIT_FAILURE);
297
298 SeqLib::BWAWrapper bwa;
299 if (opt::verbose)
300 std::cerr << "...loading reference genome" << std::endl;
301 if (!bwa.LoadIndex(opt::reference)) {
302 std::cerr << "Failed to load index for BWA-MEM from: " << opt::reference << std::endl;
303 exit(EXIT_FAILURE);
304 }
305
306 bw.SetHeader(bwa.HeaderFromIndex());
307 bw.WriteHeader();
308
309 if (opt::verbose)
310 std::cerr << "...realigning corrected sequences with BWA-MEM" << std::endl;
311 // run through and read
312 for (SeqLib::UnalignedSequenceVector::const_iterator i = u.begin(); i != u.end(); ++i) {
313 SeqLib::BamRecordVector brv;
314 const bool hardclip = false;
315 const double frac = 0.9;
316 const int max_secondary = 10;
317 bwa.AlignSequence(i->Seq, i->Name, brv, hardclip, frac, max_secondary);
318 for (SeqLib::BamRecordVector::iterator r = brv.begin();
319 r != brv.end(); ++r) {
320 if (!i->Qual.empty())
321 r->SetQualities(i->Qual, 33);
322 bw.WriteRecord(*r);
323 }
324 }
325
326 }
327 // parse the command line options
328 void parseOptions(int argc, char** argv, const char* msg) {
329
330 bool die = false;
331 bool help = false;
332
333 // get the first argument as input
334 if (argc > 1)
335 opt::input = std::string(argv[1]);
336
337 for (char c; (c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1;) {
338 std::istringstream arg(optarg != NULL ? optarg : "");
339 switch (c) {
340 case 'v': opt::verbose = true; break;
341 case 'f': opt::mode = 'f'; break;
342 case 'F': arg >> opt::fasta; break;
343 case 'b': opt::mode = 'b'; break;
344 case 'C': opt::mode = 'C'; break;
345 case 'T': arg >> opt::target; break;
346 case 'G': arg >> opt::reference; break;
347 default: die= true;
348 }
349 }
350
351 if (die || help || (opt::input.empty() && opt::fasta.empty())) {
352 std::cerr << "\n" << msg;
353 if (die)
354 exit(EXIT_FAILURE);
355 else
356 exit(EXIT_SUCCESS);
357 }
358 }