Debian Codegraph @

seq_test/seq_test.cpp

raw symbols SCIP
1 #define BOOST_TEST_DYN_LINK
2 #define BOOST_TEST_MODULE MyTest
3 #include<boost/test/unit_test.hpp>
4
5 #include <climits>
6 #include <boost/test/included/unit_test.hpp>
7
8 #include "SeqLib/BWAWrapper.h"
9 #include "SeqLib/BamReader.h"
10 #include "SeqLib/BamWriter.h"
11 #include "SeqLib/ReadFilter.h"
12 #include "SeqLib/FermiAssembler.h"
13 #include "SeqLib/SeqPlot.h"
14 #include "SeqLib/RefGenome.h"
15
16 #define GZBED "test_data/test.bed.gz"
17 #define GZVCF "test_data/test.vcf.gz"
18 #define SBAM "test_data/small.bam"
19 #define OBAM "test_data/small_out.bam"
20 #define OCRAM "test_data/small_out.cram"
21 #define HGREF "/seq/references/Homo_sapiens_assembly19/v1/Homo_sapiens_assembly19.fasta"
22 #define TREF "test_data/test_ref.fa"
23 #define OREF "tmp_output.fa"
24 #define BEDFILE "test_data/test.bed"
25 #define VCFFILE "test_data/test.vcf"
26 #define JSON1 "test_data/example4.json"
27
28 using namespace SeqLib::Filter;
29 using namespace SeqLib;
30
31 #include <fstream>
32 #include "SeqLib/BFC.h"
33
34 BOOST_AUTO_TEST_CASE( read_gzbed ) {
35
36 SeqLib::BamReader br;
37 br.Open("test_data/small.bam");
38
39 SeqLib::GRC g(GZBED, br.Header());
40
41 BOOST_CHECK_EQUAL(g.size(), 3);
42
43 BOOST_CHECK_EQUAL(g[2].chr, 21);
44
45 SeqLib::GRC v(GZVCF, br.Header());
46 BOOST_CHECK_EQUAL(v.size(), 57);
47
48 BOOST_CHECK_EQUAL(v[29].chr, 0);
49 }
50
51 BOOST_AUTO_TEST_CASE ( bfc ) {
52
53 BFC b;
54
55 SeqLib::BamReader br;
56 br.Open("test_data/small.bam");
57
58 SeqLib::BamRecord rec;
59 BamRecordVector brv, brv2;
60 size_t count = 0;
61 while(br.GetNextRecord(rec) && count++ < 10000)
62 brv.push_back(rec);
63
64 for (int i = 5000; i < brv.size(); ++i)
65 brv2.push_back(brv[i]);
66
67 count = 0;
68 while(br.GetNextRecord(rec) && count++ < 10000)
69 brv2.push_back(rec);
70
71 std::ofstream orig("orig.fa");
72 std::ofstream corr("corr.fa");
73
74 for (auto& i : brv)
75 orig << ">" << i.Qname() << std::endl << i.Sequence() << std::endl;
76
77 // add the seqs
78 for (auto& r : brv)
79 b.AddSequence(r.Sequence().c_str(), r.Qualities().c_str(), r.Qname().c_str());
80
81 b.Train();
82 b.clear();
83
84 //b.ErrorCorrectToTag(brv2, "KC");
85
86 //UnalignedSequenceVector v;
87 //b.GetSequences(v);
88
89 // write to corrected
90 //for (auto& i : v) {
91 // corr << ">" << i.Name << std::endl << i.Seq << std::endl;
92 //}
93 //orig.close();
94 //corr.close();
95
96 //
97 //v.clear();
98 //b.FilterUnique();
99 //b.GetSequences(v);
100
101 // do everything at once
102 //b.TrainAndCorrect(brv2);
103
104 // do everything in place
105 //b.TrainCorrection(brv2);
106 //b.ErrorCorrectInPlace(brv2);
107 }
108
109 BOOST_AUTO_TEST_CASE( correct_and_assemble ) {
110
111 BFC b;
112
113 SeqLib::BamReader br;
114 br.Open("test_data/small.bam");
115
116 SeqLib::BamRecord rec;
117 BamRecordVector brv, brv2;
118 size_t count = 0;
119 while(br.GetNextRecord(rec) && count++ < 10000)
120 b.AddSequence(rec.Sequence().c_str(), rec.Qualities().c_str(), rec.Qname().c_str());
121
122 b.Train();
123 b.ErrorCorrect();
124
125 float kcov = b.GetKCov();
126 int kmer = b.GetKMer();
127
128 UnalignedSequenceVector v;
129
130 v.clear();
131 std::string seq, name;
132 while (b.GetSequence(seq, name))
133 v.push_back({name, seq});
134
135 //std::ofstream filt("filt.fa");
136 //for (auto& i : v) {
137 // filt << ">" << i.Name << std::endl << i.Seq << std::endl;
138 //}
139 //filt.close();
140
141 FermiAssembler f;
142 f.AddReads(v);
143 f.DirectAssemble(kcov);
144
145 // retrieve the contigs
146 std::vector<std::string> contigs = f.GetContigs();
147
148 std::ofstream cont("contigs.fa");
149 size_t cc = 0;
150 for (auto& i : f.GetContigs()) {
151 ++cc;
152 cont << ">" << cc << std::endl << i << std::endl;
153 }
154 cont.close();
155
156 }
157
158 BOOST_AUTO_TEST_CASE( header_check ) {
159
160 SeqLib::BamReader br;
161 br.Open("test_data/small.bam");
162 SeqLib::BamHeader h = br.Header();
163
164 // a BAM header check
165 BOOST_CHECK_EQUAL(h.GetSequenceLength(0), 249250621);
166 BOOST_CHECK_EQUAL(h.GetSequenceLength(3), 191154276);
167 BOOST_CHECK_EQUAL(h.GetSequenceLength("1"), 249250621);
168 BOOST_CHECK_EQUAL(h.GetSequenceLength("4"), 191154276);
169 BOOST_CHECK_EQUAL(h.GetSequenceLength("d4"), -1);
170 BOOST_CHECK_EQUAL(h.GetSequenceLength(10000), -1);
171
172 BOOST_CHECK_EQUAL(h.GetHeaderSequenceVector().size(), h.NumSequences());
173 BOOST_CHECK_EQUAL(h.GetHeaderSequenceVector().begin()->Length, 249250621);
174
175 SeqLib::BamRecord rec;
176 size_t count = 0;
177
178 while(br.GetNextRecord(rec) && count++ < 10000) {
179
180 }
181
182 }
183
184 BOOST_AUTO_TEST_CASE( merge ) {
185
186 SeqLib::GRC grc;
187 // add two more that we know of
188 grc.add(SeqLib::GenomicRegion(23, 10,100));
189 grc.add(SeqLib::GenomicRegion(23, 20,110));
190
191 grc.add(SeqLib::GenomicRegion(2, 10,100));
192 grc.add(SeqLib::GenomicRegion(2, 20,110));
193 grc.add(SeqLib::GenomicRegion(2, 200,310));
194
195 grc.MergeOverlappingIntervals();
196 BOOST_CHECK_EQUAL(grc.size(), 3);
197 BOOST_CHECK_EQUAL(grc[0].chr, 2);
198 BOOST_CHECK_EQUAL(grc[1].chr, 2);
199 BOOST_CHECK_EQUAL(grc[2].chr, 23);
200 BOOST_CHECK_EQUAL(grc[2].pos2, 110);
201 BOOST_CHECK_EQUAL(grc[2].pos1, 10);
202 }
203
204 BOOST_AUTO_TEST_CASE ( interval_queries ) {
205
206 SeqLib::GRC grc;
207
208 // create a large GRC
209 for (int i = 0; i < 10; ++i) {
210 int chr = rand() % 23;
211 int pos = rand() % 10000;
212 grc.add(SeqLib::GenomicRegion(chr, pos, pos + 100));
213 }
214 grc.MergeOverlappingIntervals();
215
216 // add two more that we know of
217 grc.add(SeqLib::GenomicRegion(23, 10,100));
218 grc.add(SeqLib::GenomicRegion(23, 20,110));
219
220 // create the interval tree
221 grc.CreateTreeMap();
222
223 SeqLib::GRC results = grc.FindOverlaps(SeqLib::GenomicRegion(23, 10, 100), true);
224
225 for (auto& i : results)
226 std::cerr << " GRC overlaps results " << i << std::endl;
227
228 BOOST_CHECK_EQUAL(results.size(), 2);
229 BOOST_CHECK_EQUAL(results[1].pos2, 100);
230
231 grc.MergeOverlappingIntervals();
232 grc.CreateTreeMap();
233
234 for(auto& r : grc)
235 std::cerr << r << std::endl;
236
237 std::vector<int32_t> q, s;
238 results = grc.FindOverlaps(grc, q, s, true);
239
240 std::cerr << " results.size " << results.size() << " Input size " << grc.size() << std::endl;
241 BOOST_CHECK_EQUAL(results.size(), grc.size());
242 BOOST_CHECK_EQUAL(results.TotalWidth(), grc.TotalWidth());
243
244 }
245
246 BOOST_AUTO_TEST_CASE( json_parse_from_file ) {
247
248 SeqLib::BamReader br;
249 br.Open("test_data/small.bam");
250
251 std::string rules = "{\"global\" : {\"!anyflag\" : 1536}, \"\" : { \"rules\" : [{\"ic\" : true}, {\"clip\" : 5}, {\"ins\" : true}, {\"del\" : true}, {\"mapped\": true , \"mate_mapped\" : false}, {\"mate_mapped\" : true, \"mapped\" : false}]}}";
252 ReadFilterCollection rfc(rules, br.Header());
253
254 std::cerr << rfc << std::endl;
255
256 SeqLib::BamRecord rec;
257 size_t count = 0;
258
259 while(br.GetNextRecord(rec) && count++ < 10000) {
260 if (!rfc.isValid(rec))
261 continue;
262 // test global flag rule
263 if ( (rec.QCFailFlag() || rec.DuplicateFlag())) {
264 std::cerr << rec << std::endl;
265 assert(false);
266 }
267 }
268
269 /// direct from string
270 ReadFilterCollection rfc2(rules, br.Header());
271
272 while(br.GetNextRecord(rec) && count++ < 10000) {
273 if (!rfc.isValid(rec))
274 continue;
275 // test global flag rule
276 if ( (rec.QCFailFlag() || rec.DuplicateFlag())) {
277 std::cerr << rec << std::endl;
278 assert(false);
279 }
280 }
281
282 // check that a bad key throws error
283 //rules = "{\"global\" : {\"!anyflagf\" : 1536}, \"\" : { \"rules\" : [{\"ic\" : true}, {\"clip\" : 5}, {\"ins\" : true}, {\"del\" : true}, {\"mapped\": true , \"mate_mapped\" : false}, {\"mate_mapped\" : true, \"mapped\" : false}]}}";
284 //BOOST_CHECK_THROW(ReadFilterCollection rfc2(rules, br.Header()), std::invalid_argument);
285 // bad value, expected int
286 //rules = "{\"global\" : {\"!anyflag\" : \"BAD\"}, \"\" : { \"rules\" : [{\"ic\" : true}, {\"clip\" : 5}, {\"ins\" : true}, {\"del\" : true}, {\"mapped\": true , \"mate_mapped\" : false}, {\"mate_mapped\" : true, \"mapped\" : false}]}}";
287 //BOOST_CHECK_THROW(ReadFilterCollection rfc3(rules, br.Header()), std::invalid_argument);
288 // bad JSON itself
289 rules = "{\"global\" : \"!anyflag\" : 1536}, \"\" : { \"rules\" : [{\"ic\" : true}, {\"clip\" : 5}, {\"ins\" : true}, {\"del\" : true}, {\"mapped\": true , \"mate_mapped\" : false}, {\"mate_mapped\" : true, \"mapped\" : false}]}}";
290 BOOST_CHECK_THROW(ReadFilterCollection rfc4(rules, br.Header()), std::invalid_argument);
291 // bad value, expected range
292 rules = "{\"global\" : {\"!anyflag\" : 1536}, \"\" : { \"rules\" : [{\"isize\" : \"BAD\"}]}}";
293 BOOST_CHECK_THROW(ReadFilterCollection rfc4(rules, br.Header()), std::invalid_argument);
294
295
296 }
297
298 BOOST_AUTO_TEST_CASE( sw_alignment ) {
299
300 const std::string ref = "ACTGCGAGCGACTAGCTCGTAGCTAGCTAGCTAGCTAGTGACTGCGGGCGATCATCGATCTTTTATTATCGCGATCGCTACGAC";
301 const std::string seq = "ACTGCGAGCGACTAGCTCGTAGCTAGCTAGCTAGCTAGTGACTGCGGGCGATCATCGATCTTTTATTATCGCGATCGCTACGAC";
302 //const std::string seq = "CTCGTAGCTAGCTGCTAGCTAGTGACTGCGGGCGATCATCGATCTTTTATTATCGCG";
303 const SeqLib::GenomicRegion gr(0,0,0);
304 SeqLib::BamRecord b("test_name", seq, ref, &gr);
305
306 std::cerr << " SMITH WATERMAN " << std::endl;
307 std::cerr << b << std::endl;
308 }
309
310 BOOST_AUTO_TEST_CASE( read_filter_1 ) {
311
312 SeqLib::BamReader br;
313 br.Open("test_data/small.bam");
314 SeqLib::BamHeader h = br.Header();
315
316 SeqLib::GRC g;
317 g.add(SeqLib::GenomicRegion(h.Name2ID("X"), 1100000, 1800000));
318 g.CreateTreeMap();
319
320 // make a new rule set
321 ReadFilterCollection rfc;
322
323 // make a new filter region
324 ReadFilter rf;
325
326 // add an isize rule on whole-genome
327 AbstractRule ar;
328 ar.isize = Range(200, 600, false); // 200 to 600, not inverted
329 ar.mapq = Range(10, 50, false); // 200 to 600, not inverted
330 ar.nm = Range(1, 1, false); // 200 to 600, not inverted
331 rf.AddRule(ar);
332
333 rf.setRegions(g);
334
335 // add to the filter collection
336 rfc.AddReadFilter(rf);
337
338 SeqLib::GRC gback = rfc.getAllRegions();
339 BOOST_CHECK_EQUAL(gback.size(), g.size());
340 for (size_t i = 0; i < gback.size(); ++i)
341 assert(g[i] == gback[i]);
342
343
344 // display
345 std::cerr << br.PrintRegions() << std::endl;
346
347 // read / filter the reads
348 SeqLib::BamRecord rec;
349 size_t count = 0;
350
351 while(br.GetNextRecord(rec) && count++ < 10000) {
352
353 if (!rfc.isValid(rec))
354 continue;
355
356 // test isize rule
357 if (!(rec.FullInsertSize() >= 200 || rec.FullInsertSize() <= 600)) {
358 std::cerr << rec.FullInsertSize() << std::endl;
359 assert(false);
360 }
361 // test mapq rule
362 if (!(rec.MapQuality() >= 10 || rec.MapQuality() <= 50)) {
363 std::cerr << rec.MapQuality() << std::endl;
364 assert(false);
365 }
366 // test nm rule
367 int32_t nm;
368 rec.GetIntTag("NM", nm);
369 if (nm == 1)
370 assert(false);
371 }
372 }
373
374 BOOST_AUTO_TEST_CASE ( fermi_add_reads ) {
375
376 SeqLib::FermiAssembler f;
377
378 SeqLib::BamReader br;
379 br.Open("test_data/small.bam");
380 SeqLib::BamRecord r;
381 size_t count = 0;
382 while (br.GetNextRecord(r) && count++ < 1000)
383 f.AddRead(r);
384 while (br.GetNextRecord(r) && count++ < 2000)
385 f.AddRead(SeqLib::UnalignedSequence(r.Qname(), r.Sequence(), r.Qualities()));
386
387 f.CorrectReads();
388 f.PerformAssembly();
389 std::vector<std::string> out = f.GetContigs();
390
391 }
392
393 BOOST_AUTO_TEST_CASE ( seq_utils ) {
394
395 // add commas
396 BOOST_CHECK_EQUAL(SeqLib::AddCommas(1),"1");
397 BOOST_CHECK_EQUAL(SeqLib::AddCommas(1000000),"1,000,000");
398
399 // percent calc
400 BOOST_CHECK_EQUAL(SeqLib::percentCalc(10,100), 10);
401 BOOST_CHECK_EQUAL(SeqLib::percentCalc(7,8), 87);
402 BOOST_CHECK_EQUAL(SeqLib::percentCalc(9,10), 90);
403 BOOST_CHECK_EQUAL(SeqLib::percentCalc(2,3), 66);
404
405 // scrub string
406 BOOST_CHECK_EQUAL(SeqLib::scrubString("chr1", "chr"), "1");
407 BOOST_CHECK_EQUAL(SeqLib::scrubString("chr1", ""), "chr1");
408 BOOST_CHECK_EQUAL(SeqLib::scrubString("chr1", "dd"), "chr1");
409 BOOST_CHECK_EQUAL(SeqLib::scrubString("chr1", "1"), "chr");
410
411 }
412
413 BOOST_AUTO_TEST_CASE( bam_record ) {
414
415 // get a record
416 SeqLib::BamReader br, br2;
417
418 // try multiple open
419 std::vector<std::string> bs = {"test_data/small.bam", "test_data/small.bam"};
420 BOOST_CHECK(!br2.Open(bs)); // should be false, no dups
421 bs = {"test_data/small.bam", "test_data/small.cram"};
422 BOOST_CHECK(br.Open(bs)); // should be true
423 SeqLib::BamRecord r;
424
425 SeqLib::BamRecordVector brv;
426
427 size_t count = 0;
428 br.GetNextRecord(r);
429
430 BOOST_CHECK_EQUAL(r.AsGenomicRegion().chr, 0);
431 BOOST_CHECK_EQUAL(r.AsGenomicRegion().pos1,9995);
432 BOOST_CHECK_EQUAL(r.AsGenomicRegion().pos2,10075);
433 BOOST_CHECK_EQUAL(r.AsGenomicRegion().strand,'-');
434
435 BOOST_CHECK_EQUAL(r.AsGenomicRegionMate().chr, 15);
436 BOOST_CHECK_EQUAL(r.AsGenomicRegionMate().pos1,67300983);
437 BOOST_CHECK_EQUAL(r.AsGenomicRegionMate().pos2,67301134);
438 BOOST_CHECK_EQUAL(r.AsGenomicRegionMate().strand,'-');
439
440 BOOST_CHECK_EQUAL(std::floor(r.MeanPhred()), 15);
441
442 BOOST_CHECK_EQUAL(r.CountNBases(), 0);
443
444 r.SetQname("testq");
445 BOOST_CHECK_EQUAL(r.Qname(), "testq");
446
447 const std::string s = "ACTGCTAGCTAGCTACTCTGCTACTATATTAGCGCGCATTCGC";
448 r.SetSequence(s);
449 BOOST_CHECK_EQUAL(r.Sequence(), s);
450
451 r.SmartAddTag("ST", "1");
452 r.SmartAddTag("ST", "3");
453 r.SmartAddTag("ST", "5");
454 r.SmartAddTag("S2", "5");
455
456 BOOST_CHECK_EQUAL(r.GetSmartIntTag("ST").size(), 3);
457 BOOST_CHECK_EQUAL(r.GetSmartIntTag("ST").at(2), 5);
458 BOOST_CHECK_EQUAL(r.GetSmartDoubleTag("ST").size(), 3);
459 BOOST_CHECK_EQUAL(r.GetSmartDoubleTag("ST").at(2), 5.0);
460
461 BOOST_CHECK_EQUAL(r.GetSmartStringTag("ST").size(), 3);
462 BOOST_CHECK_EQUAL(r.GetSmartStringTag("ST")[1], "3");
463
464 BOOST_CHECK_EQUAL(r.GetSmartDoubleTag("S2").at(0), 5.0);
465 BOOST_CHECK_EQUAL(r.GetSmartIntTag("S2").at(0), 5);
466 }
467
468 BOOST_AUTO_TEST_CASE( fermi_assemble ) {
469
470 SeqLib::FermiAssembler f;
471
472 SeqLib::BamReader br;
473 br.Open("test_data/small.bam");
474 SeqLib::BamRecord r;
475
476 SeqLib::BamRecordVector brv;
477
478 size_t count = 0;
479 while(br.GetNextRecord(r) && count++ < 1000) {
480 brv.push_back(r);
481 }
482
483 f.AddReads(brv);
484
485 f.CorrectReads();
486
487 SeqLib::UnalignedSequenceVector reads = f.GetSequences();
488 BOOST_CHECK_EQUAL(reads.size(), brv.size());
489
490 for (int i = 0; i < reads.size(); ++i) {
491 if (brv[i].Sequence() != reads[i].Seq) {
492 std::cerr << "************" << std::endl;
493 std::cerr << brv[i].Sequence() << std::endl;
494 std::cerr << reads[i].Seq << std::endl;
495 }
496 }
497
498 // peform the assembly
499 std::cerr << "...performing assembly" << std::endl;
500 f.PerformAssembly();
501
502 // retrieve the contigs
503 std::vector<std::string> contigs = f.GetContigs();
504
505 }
506
507
508 BOOST_AUTO_TEST_CASE( bam_header_stdout ) {
509
510 SeqLib::BamReader br;
511 br.Open("test_data/small.bam");
512 SeqLib::BamHeader h = br.Header();
513
514 std::cout << h.AsString() << std::endl;
515 }
516
517 BOOST_AUTO_TEST_CASE( bam_header_name2id ) {
518
519 SeqLib::BamReader br;
520 br.Open("test_data/small.bam");
521 SeqLib::BamHeader h = br.Header();
522
523 BOOST_CHECK_EQUAL(h.Name2ID("2"), 1);
524 BOOST_CHECK_EQUAL(h.Name2ID("23"), -1);
525
526 }
527
528 BOOST_AUTO_TEST_CASE( bam_header_id2name ) {
529
530 SeqLib::BamReader br;
531 br.Open("test_data/small.bam");
532 SeqLib::BamHeader h = br.Header();
533
534 BOOST_CHECK_EQUAL(h.IDtoName(2), "3");
535 BOOST_CHECK_THROW(h.IDtoName(100), std::out_of_range);
536 BOOST_CHECK_THROW(h.IDtoName(-1), std::invalid_argument);
537 BOOST_CHECK_THROW(SeqLib::BamHeader().IDtoName(1), std::out_of_range);
538 }
539
540 BOOST_AUTO_TEST_CASE( genomic_ranges_string_constructor) {
541
542 SeqLib::BamReader br;
543 br.Open("test_data/small.bam");
544 SeqLib::BamHeader h = br.Header();
545
546 const std::string in = "2:1,000,000-2,000,000";
547 SeqLib::GenomicRegion gr(in, h);
548 BOOST_CHECK_EQUAL(gr.chr, 1);
549 BOOST_CHECK_EQUAL(gr.pos1, 1000000);
550 BOOST_CHECK_EQUAL(gr.pos2, 2000000);
551
552 BOOST_CHECK_THROW(SeqLib::GenomicRegion(in, SeqLib::BamHeader()), std::invalid_argument);
553
554 BOOST_CHECK_EQUAL(gr.ChrName(h), "2");
555 BOOST_CHECK_EQUAL(gr.ChrName(SeqLib::BamHeader()), "2");
556 gr.chr = 1000;
557 BOOST_CHECK_THROW(gr.ChrName(h), std::invalid_argument);
558
559 }
560
561 BOOST_AUTO_TEST_CASE( genomic_region_less_than ) {
562
563 SeqLib::GenomicRegion gr1(0, 1, 2);
564 SeqLib::GenomicRegion gr2(1, 1, 2);
565 SeqLib::GenomicRegion gr3(1, 2, 2);
566 SeqLib::GenomicRegion gr4(1, 6, 6);
567
568 BOOST_CHECK(gr1 < gr2);
569 BOOST_CHECK(gr2 > gr1);
570 BOOST_CHECK(!(gr1 > gr2));
571
572 BOOST_CHECK(gr2 < gr3);
573 BOOST_CHECK(gr3 > gr2);
574 BOOST_CHECK(!(gr2 > gr3));
575
576 BOOST_CHECK(gr3 < gr4);
577 BOOST_CHECK(!(gr4 == gr3));
578 BOOST_CHECK(!(gr3 > gr4));
579 BOOST_CHECK(gr4 > gr3);
580
581 BOOST_CHECK(!(gr1 < gr1));
582 BOOST_CHECK(!(gr1 > gr1));
583
584 BOOST_CHECK(!(gr1 != gr1));
585 BOOST_CHECK(gr2 != gr1);
586 BOOST_CHECK(gr3 != gr1);
587 BOOST_CHECK(gr4 != gr3);
588
589 BOOST_CHECK(gr1 >= gr1);
590 BOOST_CHECK(gr2 >= gr2);
591 BOOST_CHECK(gr3 >= gr3);
592 BOOST_CHECK(gr4 >= gr4);
593
594 BOOST_CHECK(gr1 <= gr1);
595 BOOST_CHECK(gr2 <= gr2);
596 BOOST_CHECK(gr3 <= gr3);
597 BOOST_CHECK(gr4 <= gr4);
598
599 BOOST_CHECK(gr1 <= gr2);
600 BOOST_CHECK(gr2 >= gr1);
601
602 BOOST_CHECK(gr2 <= gr3);
603 BOOST_CHECK(gr3 >= gr2);
604
605 }
606
607 BOOST_AUTO_TEST_CASE( genomic_region_distance ) {
608
609 SeqLib::GenomicRegion gr1(0, 10, 100);
610 SeqLib::GenomicRegion gr2(0, 10, 200);
611 SeqLib::GenomicRegion gr3(1, 10, 100);
612 SeqLib::GenomicRegion gr4(0, 100, 100);
613
614 BOOST_CHECK_EQUAL(gr1.DistanceBetweenEnds(gr3), -1);
615 BOOST_CHECK_EQUAL(gr1.DistanceBetweenEnds(gr1), 0);
616 BOOST_CHECK_EQUAL(gr1.DistanceBetweenEnds(gr2), 100);
617 BOOST_CHECK_EQUAL(gr1.DistanceBetweenEnds(gr4), 0);
618
619 BOOST_CHECK_EQUAL(gr1.DistanceBetweenStarts(gr3), -1);
620 BOOST_CHECK_EQUAL(gr1.DistanceBetweenStarts(gr1), 0);
621 BOOST_CHECK_EQUAL(gr1.DistanceBetweenStarts(gr2), 0);
622 BOOST_CHECK_EQUAL(gr1.DistanceBetweenStarts(gr4), 90);
623
624 }
625
626 BOOST_AUTO_TEST_CASE( small_trie_from_file) {
627
628 AbstractRule ar;
629 const bool inverted = false;
630 ar.addMotifRule("test_data/motif.txt", inverted);
631
632 ReadFilterCollection rfc;
633 ReadFilter rf;
634 rf.AddRule(ar);
635 rfc.AddReadFilter(rf);
636
637 SeqLib::BamReader br;
638 br.Open("test_data/small.bam");
639
640 SeqLib::BamRecord rec;
641 bool rule;
642 size_t count = 0;
643 while (br.GetNextRecord(rec) && count++ < 1000){
644 }
645
646 }
647
648 BOOST_AUTO_TEST_CASE( large_trie ) {
649
650 const std::string dictionary = "ACTG";
651
652 const int string_size = 20;
653 const int string_count = 10000;
654
655 SeqLib::Filter::AhoCorasick aho;
656
657 std::vector<std::string> k;
658
659 std::cerr << "...generating key" << std::endl;
660 for (int i = 0; i < string_count; ++i) {
661 char* c = (char*) malloc(string_size + 1);
662 for (int j = 0; j < string_size; ++j)
663 c[j] = dictionary.at(rand() % 4);
664 c[string_size] = '\0';
665 k.push_back(std::string(c));
666 free(c);
667 }
668 std::cerr << "...done with key" << std::endl;
669
670 std::cerr << "...generating trie" << std::endl;
671 for (auto& i : k)
672 aho.AddMotif(i);
673 std::cerr << "...done generating trie" << std::endl;
674
675 std::cerr << "...querying trie" << std::endl;
676 auto result = aho.aho_trie->parse_text(k[0]);
677 std::cerr << "...querying trie fast" << std::endl;
678 for (int i = 0; i < string_count; ++i) {
679 //if (i % 20000 == 0)
680 // std::cerr << "... " << i << std::endl;
681 auto result = aho.aho_trie->parse_text(k[i]);
682 }
683
684 }
685
686 BOOST_AUTO_TEST_CASE( genomic_region_constructors ) {
687
688 // GenomicRegion Constructors
689 SeqLib::GenomicRegion gr(0, 0, 10, '+');
690 BOOST_CHECK_EQUAL(gr.Width(), 11);
691
692 SeqLib::GenomicRegion gr_empty;
693 BOOST_TEST(gr_empty.IsEmpty());
694
695 SeqLib::GenomicRegion gr2("chrX", "0", "10", SeqLib::BamHeader());
696 BOOST_CHECK_EQUAL(gr2.Width(), 11);
697 BOOST_CHECK_EQUAL(gr2.chr, 22);
698
699 SeqLib::GenomicRegion gr3("X", "0", "10", SeqLib::BamHeader());
700 BOOST_TEST(gr2 == gr3);
701
702 BOOST_CHECK_THROW(SeqLib::GenomicRegion gr3("X", "a", "10", SeqLib::BamHeader()), std::invalid_argument);
703 BOOST_CHECK_THROW(SeqLib::GenomicRegion gr3("X", "1000000000000000000000000000000000000000000000000000000000000000000000000000000", "10", SeqLib::BamHeader()), std::out_of_range);
704
705 BOOST_CHECK_EQUAL(gr.DistanceBetweenStarts(gr2), -1);
706 BOOST_CHECK_EQUAL(gr2.DistanceBetweenStarts(gr), -1);
707
708 SeqLib::BamReader br;
709 br.Open("test_data/small.bam");
710 BOOST_CHECK_EQUAL(SeqLib::GenomicRegion("X","1","100", br.Header()).chr, 22);
711
712 // check negative inputs
713 SeqLib::GenomicRegion grn(-1,-11,-10);
714 BOOST_CHECK_EQUAL(grn.chr, -1);
715 BOOST_CHECK_EQUAL(grn.pos1, -11);
716 BOOST_CHECK_EQUAL(grn.pos2, -10);
717
718 // check strand constructions
719 SeqLib::GenomicRegion gra(0,0,0);
720 SeqLib::GenomicRegion grb(0,10000,10001, '+');
721 SeqLib::GenomicRegion grc(0,0,3, '-');
722 BOOST_CHECK_EQUAL(gra.strand, '*');
723 BOOST_CHECK_EQUAL(grb.strand, '+');
724 BOOST_CHECK_EQUAL(grc.strand, '-');
725
726 // check point string
727 BOOST_CHECK_EQUAL(grb.PointString(br.Header()), "1:10,000(+)");
728
729 // check pretty string
730 std::stringstream ss;
731 ss << grb;
732 BOOST_CHECK_EQUAL(ss.str(), "1:10,000-10,001(+)");
733
734 }
735
736 BOOST_AUTO_TEST_CASE( genomic_region_bad_inputs ) {
737
738 BOOST_CHECK_THROW(SeqLib::GenomicRegion(0, 10, 9), std::invalid_argument);
739
740 BOOST_CHECK_THROW(SeqLib::GenomicRegion(0,0,0,'P'), std::invalid_argument);
741
742 }
743
744 // BOOST_AUTO_TEST_CASE( genomic_region_random ) {
745
746 // SeqLib::GenomicRegion gr;
747 // std::srand(42);
748 // gr.random();
749 // BOOST_CHECK_EQUAL(gr.pointString(), "9:69,477,830(*)");
750
751 // }
752
753 BOOST_AUTO_TEST_CASE( genomic_region_range_operations ) {
754
755 SeqLib::GenomicRegion gr(0,1,10);
756 SeqLib::GenomicRegion gr2(0,1,11);
757 gr.Pad(3);
758 gr2.Pad(-3);
759 BOOST_CHECK_EQUAL(gr.pos1,-2);
760 BOOST_CHECK_EQUAL(gr.pos2,13);
761 BOOST_CHECK_EQUAL(gr2.pos1,4);
762 BOOST_CHECK_EQUAL(gr2.pos2,8);
763
764 BOOST_CHECK_THROW(gr.Pad(-10), std::out_of_range);
765
766 }
767
768 BOOST_AUTO_TEST_CASE( genomic_check_overlaps ) {
769
770 SeqLib::GenomicRegion gr1(0, 0, 10, '+');
771 SeqLib::GenomicRegion gr2(1, 0, 10, '+');
772
773 SeqLib::GenomicRegion gr3(0, 10, 20, '+');
774 SeqLib::GenomicRegion gr4(1, 4, 10, '+');
775
776 SeqLib::GenomicRegion gr5(1, 11, 12, '+');
777
778 // partial overlaps should be one
779 BOOST_CHECK_EQUAL(gr1.GetOverlap(gr3), 1);
780
781 // argument contained gets 2
782 BOOST_CHECK_EQUAL(gr2.GetOverlap(gr4), 2);
783
784 // object contained gets 3
785 BOOST_CHECK_EQUAL(gr4.GetOverlap(gr2), 3);
786
787 // same chr, no overlap
788 BOOST_CHECK_EQUAL(gr4.GetOverlap(gr5), 0);
789 BOOST_CHECK_EQUAL(gr5.GetOverlap(gr4), 0);
790
791 }
792
793 BOOST_AUTO_TEST_CASE( bwa_wrapper ) {
794
795 SeqLib::BWAWrapper bwa;
796
797 // Set some options
798 bwa.SetGapOpen(32);
799 bwa.SetGapExtension(1);
800 bwa.SetMismatchPenalty(18);
801 bwa.SetAScore(2);
802 bwa.SetZDropoff(100);
803 bwa.Set3primeClippingPenalty(5);
804 bwa.Set5primeClippingPenalty(5);
805 bwa.SetBandwidth(1000);
806 bwa.SetReseedTrigger(1.5);
807
808 BOOST_CHECK_THROW(bwa.SetGapOpen(-1), std::invalid_argument);
809 BOOST_CHECK_THROW(bwa.SetGapExtension(-1), std::invalid_argument);
810 BOOST_CHECK_THROW(bwa.SetMismatchPenalty(-18), std::invalid_argument);
811 BOOST_CHECK_THROW(bwa.SetAScore(-2), std::invalid_argument);
812 BOOST_CHECK_THROW(bwa.SetZDropoff(-100), std::invalid_argument);
813 BOOST_CHECK_THROW(bwa.Set3primeClippingPenalty(-5), std::invalid_argument);
814 BOOST_CHECK_THROW(bwa.Set5primeClippingPenalty(-5), std::invalid_argument);
815 BOOST_CHECK_THROW(bwa.SetBandwidth(-1000), std::invalid_argument);
816 BOOST_CHECK_THROW(bwa.SetReseedTrigger(-1.5), std::invalid_argument);
817
818 // no index loaded yet
819 BOOST_CHECK_THROW(bwa.ChrIDToName(1), std::runtime_error);
820
821 // load a test index
822 BOOST_TEST(SeqLib::read_access_test(TREF));
823 bwa.LoadIndex(TREF);
824
825 BOOST_CHECK_EQUAL(bwa.NumSequences(), 2);
826
827 BOOST_CHECK_EQUAL(bwa.ChrIDToName(0), "ref1");
828 BOOST_CHECK_EQUAL(bwa.ChrIDToName(1), "ref2");
829 BOOST_CHECK_THROW(bwa.ChrIDToName(2), std::out_of_range);
830
831 BOOST_CHECK(!bwa.LoadIndex("test_data/small.bam"));
832
833 SeqLib::BamHeader hh = bwa.HeaderFromIndex();
834 BOOST_CHECK_EQUAL(hh.NumSequences(), 2);
835
836 // error check the index construction
837 SeqLib::UnalignedSequenceVector usv_bad1, usv_bad2, usv;;
838 usv_bad1.push_back(SeqLib::UnalignedSequence("ref1","ACATGGCGAGCACTTCTAGCATCAGCTAGCTACGATCGATCGATCGATCGTAGC", std::string()));
839 usv_bad1.push_back(SeqLib::UnalignedSequence("ref4", std::string(), std::string()));
840 usv_bad1.push_back(SeqLib::UnalignedSequence("ref5","CGATCGTAGCTAGCTGATGCTAGAAGTGCTCGCCATGT", std::string()));
841 usv_bad2.push_back(SeqLib::UnalignedSequence(std::string(), "ACATGGCGAGCACTTCTAGCATCAGCTAGCTACGATCGATCGATCGATCGTAGC", std::string()));
842 usv_bad2.push_back(SeqLib::UnalignedSequence("ref4","ACCATCGCAGCAGCTATCTATTATATCGGCAGCATCTAGC", std::string()));
843 usv_bad2.push_back(SeqLib::UnalignedSequence("ref5","CGATCGTAGCTAGCTGATGCTAGAAGTGCTCGCCATGT", std::string()));
844 BOOST_CHECK_THROW(bwa.ConstructIndex(usv_bad1), std::invalid_argument);
845 BOOST_CHECK_THROW(bwa.ConstructIndex(usv_bad2), std::invalid_argument);
846
847 // construct a normal index
848 usv.push_back(SeqLib::UnalignedSequence("ref3","ACATGGCGAGCACTTCTAGCATCAGCTAGCTACGATCGATCGATCGATCGTAGC", std::string()));
849 usv.push_back(SeqLib::UnalignedSequence("ref4","CTACTTTATCATCTACACACTGCCTGACTGCGGCGACGAGCGAGCAGCTACTATCGACT", std::string()));
850 usv.push_back(SeqLib::UnalignedSequence("ref5","CGATCGTAGCTAGCTGATGCTAGAAGTGCTCGCCATGT", std::string()));
851 usv.push_back(SeqLib::UnalignedSequence("ref6","TATCTACTGCGCGCGATCATCTAGCGCAGGACGAGCATC" + std::string(100,'N') + "CGATCGTTATTATCGAGCGACGATCTACTACGT", std::string()));
852
853 bwa.ConstructIndex(usv);
854
855 BOOST_CHECK_EQUAL(bwa.NumSequences(), 4);
856 bwa.ChrIDToName(1);
857
858 BOOST_CHECK_THROW(bwa.ChrIDToName(-1), std::out_of_range);
859 BOOST_CHECK_THROW(bwa.ChrIDToName(10000), std::out_of_range);
860
861 std::cout << bwa.ChrIDToName(2) << std::endl;
862
863 BOOST_CHECK_EQUAL(bwa.ChrIDToName(0), "ref3");
864 BOOST_CHECK_EQUAL(bwa.ChrIDToName(1), "ref4");
865 BOOST_CHECK_EQUAL(bwa.ChrIDToName(2), "ref5");
866 BOOST_CHECK_EQUAL(bwa.ChrIDToName(3), "ref6");
867 BOOST_CHECK_THROW(bwa.ChrIDToName(4), std::out_of_range);
868
869 // write the index
870 BOOST_CHECK(bwa.WriteIndex(OREF));
871
872 // write the fasta
873 std::ofstream os;
874 os.open(OREF);
875 os << ">" << usv[0].Name << std::endl << usv[0].Seq <<
876 std::endl << ">" << usv[1].Name << std::endl << usv[1].Seq <<
877 std::endl << ">" << usv[2].Name << std::endl << usv[2].Seq <<
878 std::endl << ">" << usv[3].Name << std::endl << usv[3].Seq <<
879 std::endl;
880 os.close();
881
882 // read it back
883 BOOST_CHECK(bwa.LoadIndex(OREF));
884
885 // check that its good
886 BOOST_CHECK_EQUAL(bwa.ChrIDToName(0), "ref3");
887 BOOST_CHECK_EQUAL(bwa.ChrIDToName(1), "ref4");
888
889 // try aligning a sequence
890 std::cerr << "...aligning sequences" << std::endl;
891 SeqLib::BamRecordVector brv, brv2;
892 bool hardclip = false;
893 bwa.AlignSequence("ACATGGCGAGCACTTCTAGCATCAGCTAGCTACGATCG", "name", brv, false, 0.9, 1);
894 // reverse complement
895 bwa.AlignSequence("CGATCGTAGCTAGCTGATGCTAGAAGTGCTCGC", "name", brv2, false, 0.9, 2);
896
897 BOOST_CHECK_EQUAL(brv[0].Qname(), "name");
898 BOOST_CHECK_EQUAL(brv[0].ChrID(), 2);
899 BOOST_CHECK_EQUAL(brv[0].Sequence(), "CGATCGTAGCTAGCTGATGCTAGAAGTGCTCGCCATGT");
900 std::cerr << " brv[0].GetCigar() " << brv[0].GetCigar() << std::endl;
901 BOOST_CHECK_EQUAL(brv[0].GetCigar()[0].Type(), 'M');
902 BOOST_CHECK_EQUAL(brv[0].GetCigar()[0].Length(), 38);
903
904 // check from iterator
905 SeqLib::Cigar ccc = brv[0].GetCigar();
906
907 //SeqLib::Cigar::const_iterator f = brv[0].GetCigar().begin();
908 BOOST_CHECK_EQUAL(ccc.begin()->Length(), 38);
909
910 // check that it got both alignments
911 BOOST_CHECK_EQUAL(brv2.size(), 2);
912
913 // print info
914 std::cerr << bwa << std::endl;
915 }
916
917 BOOST_AUTO_TEST_CASE( bam_reader ) {
918
919 // print empty read
920 std::cerr << SeqLib::BamRecord() << std::endl;
921
922 SeqLib::BamReader bw;
923 bw.Open(SBAM);
924
925 // open index
926 bw.SetRegion(SeqLib::GenomicRegion(22, 1000000, 1001000));
927
928 // make a set of locations
929 SeqLib::GRC grc;
930 for (size_t i = 0; i < 24; ++i)
931 grc.add(SeqLib::GenomicRegion(i, 1, 100));
932
933 // set regions
934 bw.SetMultipleRegions(grc);
935
936 // write index of new bam
937 // should print a warning since no write bam is specified
938 //bw.BuildIndex();
939
940 // open an output BAM
941 //bw.OpenWriteBam(OBAM);
942
943 // set tags to strip
944 //bw.setStripTags("OQ,BI");
945
946 // loop through and grab some reads
947 SeqLib::BamRecord r;
948 size_t count = 0;
949 while (bw.GetNextRecord(r)) {
950 //if (++count % 10 == 0)
951 // bw.WriteRecord(r);
952 }
953
954 // display info about BAM
955 std::cerr << bw << std::endl;
956
957 // write index of new bam
958 //bw.BuildIndex();
959
960 // reset the walker
961 bw.Reset();
962
963 // set a smaller region
964 bw.SetRegion(SeqLib::GenomicRegion(0, 1, 100));
965 std::cerr << bw << std::endl;
966
967 // write as a cram
968 //bw.OpenWriteBam(OCRAM);
969
970 //
971 //bw.setCram(OCRAM, HGREF);
972
973 // print cram writer
974 //std::cerr << bw << std::endl;
975 // write the CRAM
976 //while (bw.GetNextRecord(r, rule)) {
977 // if (++count % 10 == 0) {
978 // std::cerr << count << std::endl;
979 // bw.WriteRecord(r);
980 // }
981 //}
982
983 }
984
985 BOOST_AUTO_TEST_CASE( set_qualities ) {
986
987 SeqLib::BamReader br;
988 br.Open("test_data/small.bam");
989
990
991 SeqLib::BamRecord r;
992 while (br.GetNextRecord(r)) {
993 r.SetQualities("", 0);
994 BOOST_CHECK_EQUAL(r.Qualities().at(0), '!');
995 r.SetQualities(std::string(r.Length(), '#'), 33);
996 BOOST_CHECK_EQUAL(r.Qualities(), std::string(r.Length(), '#'));
997 BOOST_CHECK_THROW(r.SetQualities(std::string(8, '#'), 0), std::invalid_argument);
998 break;
999 }
1000 }
1001
1002 BOOST_AUTO_TEST_CASE( header_constructor ) {
1003
1004 SeqLib::HeaderSequenceVector hsv;
1005 hsv.push_back(SeqLib::HeaderSequence("1", 1000));
1006 hsv.push_back(SeqLib::HeaderSequence("chr2", 1200));
1007 SeqLib::BamHeader hdr(hsv);
1008
1009 }
1010
1011 BOOST_AUTO_TEST_CASE( overlapping_coverage ) {
1012
1013 SeqLib::BamReader br;
1014 br.Open("test_data/small.bam");
1015 SeqLib::BamRecordVector brv;
1016 size_t count = 0;
1017 SeqLib::BamRecord r;
1018 while(br.GetNextRecord(r) && ++count < 4) {
1019 std::cout << " r " << r << std::endl;
1020 brv.push_back(r);
1021 }
1022 BOOST_CHECK_EQUAL(brv[0].OverlappingCoverage(brv[2]), 78);
1023
1024 }
1025
1026 BOOST_AUTO_TEST_CASE( gr_chr_region_set) {
1027
1028 SeqLib::BamReader br;
1029 br.Open("test_data/small.bam");
1030
1031
1032 SeqLib::GenomicRegion gr("1", br.Header());
1033 BOOST_CHECK_EQUAL(gr.chr, 0);
1034 BOOST_CHECK_EQUAL(gr.pos2, 249250621);
1035 BOOST_CHECK_EQUAL(gr.pos1, 1);
1036
1037 BOOST_CHECK_THROW(SeqLib::GenomicRegion gr2("-1", br.Header()), std::invalid_argument);
1038
1039 }
1040
1041 BOOST_AUTO_TEST_CASE( sequtils ) {
1042
1043 std::string seq = "actgACGTnTCN";
1044
1045 SeqLib::rcomplement(seq);
1046
1047 BOOST_CHECK_EQUAL(seq, "NGAnACGTcagt");
1048
1049 }
1050
1051 BOOST_AUTO_TEST_CASE( bam_write ) {
1052
1053
1054 SeqLib::BamReader br;
1055 br.Open("test_data/small.bam");
1056 SeqLib::BamHeader h = br.Header();
1057
1058 SeqLib::BamRecord rec;
1059
1060 // empty constructor
1061 SeqLib::BamWriter w;
1062
1063 BOOST_CHECK(!w.WriteHeader());
1064 BOOST_CHECK(!w.Close());
1065 BOOST_CHECK(!w.BuildIndex());
1066 BOOST_CHECK(!w.WriteRecord(rec));
1067
1068 w.Open("tmp_out.bam");
1069
1070 // check that set CRAM fails
1071 BOOST_CHECK(!w.SetCramReference("dummy"));
1072 BOOST_CHECK(!w.WriteHeader());
1073
1074 w.SetHeader(h);
1075
1076 w.WriteHeader();
1077
1078 size_t count = 0;
1079
1080 while(br.GetNextRecord(rec) && count++ < 10000)
1081 w.WriteRecord(rec);
1082
1083 BOOST_CHECK(!w.BuildIndex());
1084 w.Close();
1085
1086 w.BuildIndex();
1087
1088 // check that write header now fails
1089 BOOST_CHECK(!w.WriteHeader());
1090
1091 // check that set CRAM fails
1092 BOOST_CHECK(!w.SetCramReference("badref"));
1093
1094 // print some info
1095 std::cerr << w << std::endl;
1096
1097 }
1098
1099 BOOST_AUTO_TEST_CASE( bam_record_more ) {
1100
1101 SeqLib::BamReader br;
1102 br.Open("test_data/small.bam");
1103 SeqLib::BamHeader h = br.Header();
1104
1105 SeqLib::BamRecord rec;
1106 size_t count = 0;
1107
1108 while(br.GetNextRecord(rec) && count++ < 100) {
1109 rec.ClearSeqQualAndTags();
1110 assert(rec.Sequence().empty());
1111 assert(rec.Qualities().empty());
1112 //int32_t nm;
1113 //rec.GetIntTag("NM", nm);
1114 //assert(!nm);
1115 std::string xa;
1116 rec.GetZTag("XA", xa);
1117 BOOST_CHECK_EQUAL(xa.empty(), rec.CountBWASecondaryAlignments()==0);
1118 rec.CountBWAChimericAlignments();
1119 }
1120
1121 br.Reset();
1122
1123 SeqLib::Filter::ReadFilterCollection rf;
1124
1125 }
1126
1127 BOOST_AUTO_TEST_CASE( bam_record_manipulation ) {
1128
1129 SeqLib::Cigar cig;
1130
1131 // manually construct a cigar
1132 cig.add(SeqLib::CigarField('M', 10));
1133 cig.add(SeqLib::CigarField('I', 1));
1134 cig.add(SeqLib::CigarField('M', 10));
1135 cig.add(SeqLib::CigarField('D', 1));
1136 cig.add(SeqLib::CigarField('M', 10));
1137 cig.add(SeqLib::CigarField('S', 10));
1138
1139 // check that coversion to cigar data strutur (uint32_t) worked
1140 SeqLib::CigarField cm('M', 1);
1141 SeqLib::CigarField ci('I', 1);
1142 SeqLib::CigarField cd('D', 1);
1143 SeqLib::CigarField cn('N', 1);
1144 SeqLib::CigarField cs('S', 1);
1145 SeqLib::CigarField ch('H', 1);
1146 SeqLib::CigarField cp('P', 1);
1147 SeqLib::CigarField ce('=', 1);
1148 SeqLib::CigarField cx('X', 1);
1149 SeqLib::CigarField cb('B', 1);
1150
1151 BOOST_CHECK_EQUAL(cm.Type(), 'M');
1152 BOOST_CHECK_EQUAL(ci.Type(), 'I');
1153 BOOST_CHECK_EQUAL(cd.Type(), 'D');
1154 BOOST_CHECK_EQUAL(cn.Type(), 'N');
1155 BOOST_CHECK_EQUAL(cs.Type(), 'S');
1156 BOOST_CHECK_EQUAL(ch.Type(), 'H');
1157 BOOST_CHECK_EQUAL(cp.Type(), 'P');
1158 BOOST_CHECK_EQUAL(ce.Type(), '=');
1159 BOOST_CHECK_EQUAL(cx.Type(), 'X');
1160
1161 // check invalid constructions
1162 BOOST_CHECK_THROW(SeqLib::CigarField('L', 1), std::invalid_argument);
1163
1164 // make a sequence
1165 const std::string seq = std::string(10, 'A') + std::string(1, 'T') + std::string(10, 'C') + std::string(10, 'G') + std::string(10, 'A');
1166
1167 // check
1168 BOOST_CHECK_EQUAL(cig.NumQueryConsumed(), 41);
1169 BOOST_CHECK_EQUAL(cig.NumReferenceConsumed(), 31);
1170
1171 std::stringstream ss;
1172 ss << cig;
1173
1174 // cigar from string
1175 SeqLib::Cigar cig2(ss.str());
1176
1177 // check that the string from / to are consistent
1178 assert(cig == cig2);
1179 assert(!(cig != cig2));
1180 for (int i = 0; i < cig.size(); ++i)
1181 assert(cig[i] == cig2[i]);
1182 for (int i = 0; i < cig.size(); ++i)
1183 assert(!(cig[i] != cig2[i]));
1184
1185 // manually make a read
1186 SeqLib::GenomicRegion gr_wrong(0, 100, 131);
1187 SeqLib::GenomicRegion gr(0, 100, 130);
1188
1189 BOOST_CHECK_THROW(SeqLib::BamRecord("dumname", seq, &gr_wrong, cig), std::invalid_argument);
1190 BOOST_CHECK_THROW(SeqLib::BamRecord("dumname", seq + "A", &gr, cig), std::invalid_argument);
1191
1192 SeqLib::BamRecord br("dumname", seq, &gr, cig);
1193
1194 BOOST_CHECK_EQUAL(br.Sequence(), seq);
1195 BOOST_CHECK_EQUAL(br.GetCigar(), cig);
1196 BOOST_CHECK_EQUAL(br.Qname(), "dumname");
1197 BOOST_CHECK_EQUAL(br.Position(), 100);
1198 BOOST_CHECK_EQUAL(br.Length(), 41);
1199 BOOST_CHECK_EQUAL(br.ChrID(), 0);
1200
1201 }
1202
1203 BOOST_AUTO_TEST_CASE( change_bam_record ) {
1204
1205 // get a record
1206 SeqLib::BamReader br;
1207 br.Open("test_data/small.bam");
1208 SeqLib::BamRecord r;
1209
1210 SeqLib::BamRecordVector brv;
1211
1212 size_t count = 0;
1213 br.GetNextRecord(r);
1214
1215 SeqLib::Cigar c = r.GetCigar();
1216 std::cerr << c << std::endl;
1217
1218 // try replace with cigar of same size
1219 SeqLib::Cigar c2;
1220 c2.add(SeqLib::CigarField('S', 101));
1221 r.SetCigar(c2);
1222 std::cerr << r << std::endl;
1223
1224 // try replace with new cigar
1225 SeqLib::Cigar c3;
1226 c3.add(SeqLib::CigarField('S', 10));
1227 c3.add(SeqLib::CigarField('M', 91));
1228 r.SetCigar(c3);
1229 std::cerr << r << std::endl;
1230
1231 const std::string new_seq = "ACTGGACTACAC";
1232
1233 r.SetSequence(new_seq);
1234 std::cerr << r << std::endl;
1235
1236 r.SetQname("dummy_qname");
1237 std::cerr << r << std::endl;
1238
1239 }
1240
1241 BOOST_AUTO_TEST_CASE( stdinput ) {
1242
1243 #ifdef RUN_STDIN
1244 // read a BAM from stdin
1245 SeqLib::BamReader b;
1246 b.Open("-");
1247
1248 // write it back out
1249 SeqLib::BamRecord r;
1250 size_t count = 0;
1251 while(b.GetNextRecord(r) && count++ < 1) {
1252 std::cerr << " STDIN " << r << std::endl;
1253 }
1254 #endif
1255 }
1256
1257 BOOST_AUTO_TEST_CASE( cramin ) {
1258
1259 // read a BAM from stdin
1260 SeqLib::BamReader b;
1261 b.Open("test_data/small.cram");
1262
1263 SeqLib::BamRecord r;
1264 size_t count = 0;
1265 while(b.GetNextRecord(r) && count++ < 1) {
1266 std::cerr << "CRAM " << r << std::endl;
1267 }
1268 }
1269
1270 BOOST_AUTO_TEST_CASE( cramin_new_ref ) {
1271
1272 // read a BAM from stdin
1273 SeqLib::BamReader b;
1274 b.Open("test_data/small.cram");
1275
1276 SeqLib::BamRecord r;
1277 size_t count = 0;
1278 while(b.GetNextRecord(r) && count++ < 10) {
1279 std::cerr << "CRAM " << r << std::endl;
1280 }
1281
1282 b.Reset();
1283
1284 // should fail
1285
1286
1287 }
1288
1289
1290 BOOST_AUTO_TEST_CASE( bamin ) {
1291
1292 // read a BAM from stdin
1293 SeqLib::BamReader b;
1294 b.Open("test_data/small.bam");
1295
1296 SeqLib::BamRecord r;
1297 size_t count = 0;
1298 while(b.GetNextRecord(r) && count++ < 1) {
1299 std::cerr << "BAM " << r << std::endl;
1300 }
1301 }
1302
1303 BOOST_AUTO_TEST_CASE( samin ) {
1304
1305 // read a BAM from stdin
1306 SeqLib::BamReader b;
1307 b.Open("test_data/small.sam");
1308
1309 SeqLib::BamRecord r;
1310 size_t count = 0;
1311 while(b.GetNextRecord(r) && count++ < 1) {
1312 std::cerr << "SAM " << r << std::endl;
1313 }
1314 }
1315
1316 BOOST_AUTO_TEST_CASE( bamout ) {
1317
1318 // read a BAM from stdin
1319 SeqLib::BamReader b;
1320 b.Open("test_data/small.sam");
1321
1322 SeqLib::BamWriter w(SeqLib::BAM);
1323 //SeqLib::BamWriter w;
1324 w.Open("tmp_out.bam");
1325 w.SetHeader(b.Header());
1326 w.WriteHeader();
1327
1328 SeqLib::BamRecord r;
1329 size_t count = 0;
1330 while(b.GetNextRecord(r) && count++ < 1) {
1331 w.WriteRecord(r);
1332 }
1333 w.Close();
1334
1335 }
1336
1337 BOOST_AUTO_TEST_CASE( samout ) {
1338
1339 // read a BAM from stdin
1340 SeqLib::BamReader b;
1341 b.Open("test_data/small.sam");
1342
1343 SeqLib::BamWriter w(SeqLib::SAM);
1344 w.Open("tmp_out.sam");
1345 w.SetHeader(b.Header());
1346 w.WriteHeader();
1347
1348 SeqLib::BamRecord r;
1349 size_t count = 0;
1350 while(b.GetNextRecord(r) && count++ < 1) {
1351 w.WriteRecord(r);
1352 }
1353 w.Close();
1354 b.Close();
1355 }
1356
1357
1358 BOOST_AUTO_TEST_CASE( cramout ) {
1359
1360 SeqLib::BamReader b;
1361 b.Open("test_data/small.cram");
1362
1363 SeqLib::BamWriter w(SeqLib::CRAM);
1364 w.Open("tmp_out.cram");
1365 w.SetHeader(b.Header());
1366 w.WriteHeader();
1367
1368 SeqLib::BamRecord r;
1369 size_t count = 0;
1370 while(b.GetNextRecord(r) && count++ < 1) {
1371 w.WriteRecord(r);
1372 }
1373 w.Close();
1374
1375 }
1376
1377 BOOST_AUTO_TEST_CASE( samout_to_stdout ) {
1378
1379 #ifdef RUN_SAM_STDOUT
1380 // read a BAM from stdin
1381 SeqLib::BamReader b;
1382 b.Open("test_data/small.sam");
1383
1384 SeqLib::BamWriter w(SeqLib::SAM);
1385 w.Open("-");
1386 w.SetHeader(b.Header());
1387 w.WriteHeader();
1388
1389 SeqLib::BamRecord r;
1390 size_t count = 0;
1391 while(b.GetNextRecord(r) && count++ < 1) {
1392 w.WriteRecord(r);
1393 }
1394 w.Close();
1395 #endif
1396 }
1397
1398 BOOST_AUTO_TEST_CASE( bamout_to_stdout ) {
1399
1400 //
1401 // dont actually run every time
1402 // too much stdout-ing
1403 //
1404
1405 #ifdef RUN_BAM_STDOUT
1406 // read a BAM from stdin
1407 SeqLib::BamReader b;
1408 b.Open("test_data/small.sam");
1409
1410 SeqLib::BamWriter w(SeqLib::BAM);
1411 w.Open("-");
1412 w.SetHeader(b.Header());
1413 w.WriteHeader();
1414
1415 SeqLib::BamRecord r;
1416 size_t count = 0;
1417 while(b.GetNextRecord(r) && count++ < 1) {
1418 w.WriteRecord(r);
1419 }
1420 w.Close();
1421 #endif
1422
1423 }
1424
1425 BOOST_AUTO_TEST_CASE( bam_poly ) {
1426
1427 SeqLib::BamReader r;
1428
1429 BOOST_CHECK(r.Open("test_data/small.bam"));
1430 BOOST_CHECK(r.Open("test_data/small.cram"));
1431
1432 BOOST_CHECK(r.SetRegion(SeqLib::GenomicRegion(r.Header().Name2ID("X"),1001000, 1001100)));
1433 BOOST_CHECK(!r.SetRegion(SeqLib::GenomicRegion(1000, 1001000, 1001100))); // should fail
1434
1435 SeqLib::BamWriter w(SeqLib::BAM);
1436 w.Open("tmp_out_poly.bam");
1437 w.SetHeader(r.Header());
1438 w.WriteHeader();
1439
1440 SeqLib::BamRecord rec;
1441 while(r.GetNextRecord(rec)) {
1442 w.WriteRecord(rec);
1443 }
1444
1445 BOOST_CHECK(r.Reset("test_data/small.bam"));
1446 BOOST_CHECK(!r.Reset("dum"));
1447
1448 BOOST_CHECK(r.Close("test_data/small.bam"));
1449 BOOST_CHECK(r.Close("test_data/small.cram"));
1450
1451 // problematic here FIXME
1452 //SeqLib::BamReader r2;
1453 //BOOST_CHECK(r2.Open("tmp_out_poly.bam"));
1454 // should return false, no index
1455 //BOOST_CHECK(!r2.SetRegion(SeqLib::GenomicRegion(r.Header().Name2ID("X"),1001000, 1001100)));
1456
1457 }
1458
1459
1460 BOOST_AUTO_TEST_CASE( plot_test ) {
1461
1462 SeqLib::BamReader r;
1463 r.Open("test_data/small.bam");
1464
1465 // should return false on empty region
1466 BOOST_CHECK(!r.SetMultipleRegions(SeqLib::GRC()));
1467
1468 SeqLib::GenomicRegion gr("X:1,002,942-1,003,294", r.Header());
1469 r.SetRegion(gr);
1470
1471 SeqLib::SeqPlot s;
1472
1473 s.SetView(gr);
1474
1475 SeqLib::BamRecord rec;
1476 SeqLib::BamRecordVector brv;
1477 while(r.GetNextRecord(rec))
1478 if (!rec.CountNBases() && rec.MappedFlag())
1479 brv.push_back(rec);
1480
1481 s.SetPadding(20);
1482
1483 std::cout << s.PlotAlignmentRecords(brv);
1484
1485 }
1486
1487
1488 // CURRENTLY DOES NOT WORK
1489 // need to find how to do reset
1490 // BOOST_AUTO_TEST_CASE ( reset_works ) {
1491
1492 // SeqLib::BamReader r;
1493 // r.Open("test_data/small.bam");
1494 // //r.Open("test_data/small.cram");
1495
1496 // SeqLib::BamRecord rec1, rec2;
1497 // r.GetNextRecord(rec1);
1498 // r.Reset();
1499 // std::cerr << " AFTER RESET " << std::endl;
1500 // std::cerr << r.GetNextRecord(rec2) << std::endl;
1501
1502 // BOOST_CHECK_EQUAL(rec1.Qname(), rec2.Qname());
1503 // }
1504
1505 BOOST_AUTO_TEST_CASE (json_parse) {
1506
1507 SeqLib::BamReader r;
1508 r.Open("test_data/small.bam");
1509 ReadFilterCollection rfc(JSON1, r.Header());
1510
1511 ReadFilter rf;
1512 SeqLib::GRC g(VCFFILE, r.Header());
1513 rf.addRegions(g);
1514 AbstractRule ar;
1515 ar.isize = Range(10,100, false);
1516 rf.SetMateLinked(true);
1517 rf.AddRule(ar);
1518 rfc.AddReadFilter(rf);
1519
1520 std::cout << rfc << std::endl;
1521
1522 SeqLib::BamRecord rec;
1523 size_t count = 0;
1524 int start, end;
1525 while(r.GetNextRecord(rec) && ++count < 10) {
1526 rec.QualityTrimmedSequence(4, start, end); // phred trim first
1527 rfc.isValid(rec);
1528 }
1529
1530 // empty
1531 ReadFilterCollection rfc2("", r.Header());
1532
1533 }
1534
1535
1536 BOOST_AUTO_TEST_CASE ( ref_genome ) {
1537
1538 //SeqLib::RefGenome r("test_data/test_ref.fa");
1539 SeqLib::RefGenome r;
1540 r.LoadIndex("test_data/test_ref.fa");
1541
1542 BOOST_CHECK(!r.IsEmpty());
1543
1544 std::string out = r.QueryRegion("ref1", 0, 5);
1545 BOOST_CHECK_EQUAL(out, "ATCGAC");
1546
1547 BOOST_CHECK_THROW(r.QueryRegion("ref1", 5,4), std::invalid_argument);
1548 BOOST_CHECK_THROW(r.QueryRegion("ref1", -1,4), std::invalid_argument);
1549
1550 SeqLib::RefGenome r2;
1551 BOOST_CHECK_THROW(r2.QueryRegion("ref1",1,2), std::invalid_argument);
1552
1553 // reload
1554 r2.LoadIndex("test_data/test_ref.fa");
1555 }
1556
1557 BOOST_AUTO_TEST_CASE ( set_cigar ) {
1558
1559 SeqLib::BamReader rr;
1560 rr.Open(SBAM);
1561 SeqLib::BamRecord rec;
1562 size_t count = 0;
1563 while (rr.GetNextRecord(rec) && ++count < 10) {
1564 SeqLib::Cigar c;
1565 c.add(SeqLib::CigarField('M', 70));
1566 c.add(SeqLib::CigarField('I', 80));
1567 c.add(SeqLib::CigarField('M',1));
1568 rec.SetCigar(c);
1569 std::cerr << rec << std::endl;
1570 }
1571
1572
1573 }