Debian Codegraph @

src/ReadFilter.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #include "SeqLib/ReadFilter.h"
2
3 #include <cassert>
4 #include "htslib/htslib/khash.h"
5
6 //#define QNAME "D0EN0ACXX111207:7:2306:6903:136511"
7 //#define QFLAG -1
8
9 //#define DEBUG_MINI 1
10
11 #ifdef QNAME
12 #define DEBUGIV(msg, read) \
13 if (read.Qname() == QNAME && (read.AlignmentFlag() == QFLAG || QFLAG == -1)) { std::cerr << (msg) << " read " << r << std::endl; }
14 #else
15 #define DEBUGIV(msg, read)
16 #endif
17
18 namespace SeqLib {
19
20 namespace Filter {
21 // return if this rule accepts all reads
22 bool AbstractRule::isEvery() const {
23 return read_group.empty() && ins.isEvery() && del.isEvery() && isize.isEvery() &&
24 mapq.isEvery() && len.isEvery() && clip.isEvery() && nm.isEvery() &&
25 nbases.isEvery() && fr.isEvery() &&
26 (subsam_frac >= 1) && xp.isEvery()
27 #ifdef HAVE_C11
28 && !aho.count
29 #endif
30 ;
31 }
32
33 bool ReadFilter::isValid(const BamRecord &r) {
34
35 // empty default is pass
36 if (!m_abstract_rules.size())
37 return true;
38
39 for (std::vector<AbstractRule>::iterator it = m_abstract_rules.begin();
40 it != m_abstract_rules.end(); ++it)
41 if (it->isValid(r)) {
42 ++it->m_count; //update this rule counter
43 ++m_count;
44 return true; // it is includable in at least one.
45 }
46
47 return false;
48
49 }
50
51 int FlagRule::parse_json_int(const Json::Value& v) {
52
53 if (v.asInt())
54 return v.asInt();
55 else
56 throw std::invalid_argument("Failed to parse int tag in JSON");
57 return 0;
58 }
59
60 bool convert_to_bool(const Json::Value& value, const std::string& name) {
61
62 Json::Value null(Json::nullValue);
63 Json::Value v = value.get(name, null);
64 if (v != null) {
65 if (v.asBool())
66 return true;
67 else if (!v.asBool())
68 return false;
69 }
70
71 return false;
72
73 }
74
75 // check whether a BamAlignment (or optionally it's mate) is overlapping the regions
76 // contained in these rules
77 bool ReadFilter::isReadOverlappingRegion(const BamRecord &r) const {
78
79 // if this is a whole genome rule, it overlaps
80 if (!m_grv.size())
81 return true;
82
83 if (m_grv.CountOverlaps(GenomicRegion(r.ChrID(), r.Position(), r.PositionEnd())))
84 return true;
85
86 if (!m_applies_to_mate)
87 return false;
88 if (m_grv.CountOverlaps(GenomicRegion(r.MateChrID(), r.MatePosition(), r.MatePosition() + r.Length())))
89 return true;
90
91 return false;
92 }
93
94 // checks which rule a read applies to (using the hiearchy stored in m_regions).
95 // if a read does not satisfy a rule it is excluded.
96 bool ReadFilterCollection::isValid(const BamRecord &r) {
97
98 ++m_count_seen;
99
100 DEBUGIV(r, "starting RFC isValid")
101
102 if (m_regions.size() == 0)
103 return true;
104
105 DEBUGIV(r, "starting RFC isValid with non-empty regions")
106
107 bool is_valid = false;
108 bool exclude_hit = false;
109
110 for (std::vector<ReadFilter>::iterator it = m_regions.begin(); it != m_regions.end(); ++it) {
111
112 // only check read validity if it overlaps region
113 if (!it->isReadOverlappingRegion(r))
114 continue;
115
116 // check the region with all its rules
117 if (it->isValid(r)) {
118
119 // if this is excluder region, exclude read
120 if (it->excluder)
121 exclude_hit = true;
122
123 // in case we do fall through, track that we passed here
124 is_valid = true;
125
126 }
127 }
128
129 // found a hit in a rule
130 if (is_valid && !exclude_hit) {
131 ++m_count;
132 return true;
133 }
134
135 return false;
136 }
137
138 void ReadFilter::AddRule(const AbstractRule& ar) {
139 m_abstract_rules.push_back(ar);
140 }
141
142 // constructor to make a ReadFilterCollection from a rules file.
143 // This will reduce each individual BED file and make the
144 // GenomicIntervalTreeMap
145 ReadFilterCollection::ReadFilterCollection(const std::string& script, const BamHeader& hdr) : m_count(0), m_count_seen(0) {
146
147 // if is a file, read into a string
148 std::ifstream iscript(script.c_str());
149 std::stringstream ss;
150 std::string tscript = script;
151 if (iscript.is_open()) {
152 char c = iscript.get();
153 while (iscript.good()) {
154 ss << c;
155 c = iscript.get();
156 }
157 tscript = ss.str();;
158 }
159
160 // set up JsonCPP reader and attempt to parse script
161 Json::Value root;
162 //warning: 'Reader' is deprecated: Use CharReader and CharReaderBuilder instead
163
164 //new way JSON 1.9.4
165 Json::CharReaderBuilder builder;
166 const auto rawJsonLength = static_cast<int>(tscript.length());
167 const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
168 JSONCPP_STRING err;
169 if (!reader->parse(tscript.c_str(), tscript.c_str() + rawJsonLength, &root,
170 &err)) {
171
172 if (script.empty()) {
173 std::cerr << "JSON script is empty. Setting default to filter all reads" << std::endl;
174 return;
175 }
176 throw std::invalid_argument("ERROR: failed to parse JSON script");
177 }
178
179 // old way: Json::CharReader reader;
180 /*
181 Json::Reader reader;
182 if ( !reader.parse(tscript, root)) {
183 if (script.empty()) {
184 std::cerr << "JSON script is empty. Setting default to filter all reads" << std::endl;
185 return;
186 }
187 throw std::invalid_argument("ERROR: failed to parse JSON script");
188 }
189 */
190 Json::Value null(Json::nullValue);
191
192 int level = 1;
193
194 // assign the global rule if there is one
195 // remove from the rest of the rules
196 Json::Value glob = root.get("global", null);
197 root.removeMember("global");
198 if (!glob.isNull())
199 rule_all.parseJson(glob);
200
201 // iterator over regions
202 for (Json::ValueConstIterator regions = root.begin(); regions != root.end(); ++regions) {
203
204 ReadFilter mr;
205
206 // check if mate applies
207 mr.m_applies_to_mate = convert_to_bool(*regions, "matelink");
208
209 // check for region padding
210 int pad = regions->get("pad", 0).asInt();
211
212 // set the region
213 std::string reg;
214 Json::Value v = regions->get("region", null);
215 if (v != null) {
216 reg = v.asString();
217 mr.id = mr.id + reg;
218 }
219
220 // actually parse the region
221 if (reg == "WG" || reg.empty())
222 mr.m_grv.clear(); // ensure it is whole-genome
223 else {
224 GRC regr(reg, hdr);
225 regr.Pad(pad);
226 mr.setRegions(regr);
227 }
228 // debug mr.setRegionFromFile(reg, hdr);
229
230 // check if its excluder region
231 mr.excluder = false; // default is no exclude
232 v = regions->get("exclude", null);
233 if (v != null) {
234 mr.excluder = v.asBool();
235 if (mr.excluder)
236 mr.id = mr.id + "_exclude";
237 }
238
239 // set the rules
240 v = regions->get("rules", null);
241 if (!v.size()) {
242 //std::cerr << " !!!! RULES size is zero. !!!! " << std::endl;
243 //exit(EXIT_FAILURE);
244 }
245
246 // loop through the rules
247 for (Json::ValueIterator vv = v.begin(); vv != v.end(); ++vv) {
248 if (*vv != null) {
249 AbstractRule ar = rule_all; // always start with the global rule
250 ar.parseJson(*vv);
251 // add the rule to the region
252 mr.m_abstract_rules.push_back(ar);
253 }
254 }
255
256 // check that the regions have at least one rule
257 // if it it doesn't, give it the global WG all
258 if (!mr.m_abstract_rules.size())
259 mr.m_abstract_rules.push_back(rule_all);
260
261 mr.id = tostring(level);
262
263 m_regions.push_back(mr);
264
265 }
266
267 // make sure that there is at least one includer region
268 this->CheckHasIncluder();
269
270 }
271
272 void ReadFilterCollection::CheckHasIncluder() {
273
274 // check that there is at least one non-excluder region.
275 // if not, give global includer
276 bool has_includer = false;
277 for (std::vector<ReadFilter>::const_iterator kk = m_regions.begin(); kk != m_regions.end(); ++kk)
278 if (!kk->excluder)
279 has_includer = true;
280 if (!has_includer) {
281 ReadFilter mr;
282 mr.m_abstract_rules.push_back(rule_all);
283 mr.id = "WG_includer";
284 m_regions.push_back(mr);
285 }
286
287 }
288
289 void ReadFilter::setRegions(const GRC& g) {
290 m_grv = g;
291 m_grv.CreateTreeMap();
292 }
293
294 void ReadFilter::addRegions(const GRC& g) {
295 m_grv.Concat(g);
296 m_grv.MergeOverlappingIntervals();
297 m_grv.CreateTreeMap();
298 }
299
300
301 // print the ReadFilterCollection
302 std::ostream& operator<<(std::ostream &out, const ReadFilterCollection &mr) {
303
304 out << "----------ReadFilterCollection-------------" << std::endl;
305
306 for (std::vector<ReadFilter>::const_iterator it = mr.m_regions.begin(); it != mr.m_regions.end(); ++it)
307 out << *it << std::endl;
308 out << "------------------------------------------";
309 return out;
310
311 }
312
313 // print a ReadFilter information
314 std::ostream& operator<<(std::ostream &out, const ReadFilter &mr) {
315
316 std::string file_print = !mr.m_grv.size() ? "WHOLE GENOME" : mr.m_region_file;
317 out << (mr.excluder ? "--Exclude Region: " : "--Include Region: ") << file_print;
318 if (mr.m_grv.size()) {
319 //out << " --Size: " << AddCommas<int>(mr.m_width);
320 out << " Matelink: " << (mr.m_applies_to_mate ? "ON" : "OFF");
321 if (mr.m_grv.size() == 1)
322 out << " Region : " << mr.m_grv[0] << std::endl;
323 else
324 out << " " << mr.m_grv.size() << " regions" << std::endl;
325 } else {
326 out << std::endl;
327 }
328
329 for (std::vector<AbstractRule>::const_iterator it = mr.m_abstract_rules.begin(); it != mr.m_abstract_rules.end(); ++it)
330 out << *it << std::endl;
331 return out;
332 }
333
334 void ReadFilterCollection::AddReadFilter(const ReadFilter& rf) {
335 m_regions.push_back(rf);
336 }
337
338 ReadFilter::~ReadFilter() {}
339
340 bool Flag::parseJson(const Json::Value& value, const std::string& name) {
341
342 if (value.isMember(name.c_str())) {
343 convert_to_bool(value, name) ? setOn() : setOff();
344 return true;
345 }
346
347 return false;
348
349 }
350
351 void FlagRule::parseJson(const Json::Value& value) {
352
353 Json::Value null(Json::nullValue);
354 if (value.isMember("allflag"))
355 setAllOnFlag(parse_json_int(value.get("allflag", null)));
356 if (value.isMember("!allflag"))
357 setAllOffFlag(parse_json_int(value.get("!allflag", null)));
358 if (value.isMember("anyflag"))
359 setAnyOnFlag(parse_json_int(value.get("anyflag", null)));
360 if (value.isMember("!anyflag"))
361 setAnyOffFlag(m_any_off_flag = parse_json_int(value.get("!anyflag", null)));
362
363 // have to set the every if find flag so that rule knows it cant skip checking
364 if (dup.parseJson(value, "duplicate")) every = false;
365 if (supp.parseJson(value, "supplementary")) every = false;
366 if (qcfail.parseJson(value, "qcfail")) every = false;
367 if (hardclip.parseJson(value, "hardclip")) every = false;
368 if (fwd_strand.parseJson(value, "fwd_strand")) every = false;
369 if (mate_rev_strand.parseJson(value, "mate_rev")) every = false;
370 if (mate_fwd_strand.parseJson(value, "mate_fwd")) every = false;
371 if (mate_mapped.parseJson(value, "mate_mapped")) every = false;
372 if (mapped.parseJson(value, "mapped")) every = false;
373 if (ff.parseJson(value, "ff")) every = false;
374 if (fr.parseJson(value, "fr")) every = false;
375 if (rf.parseJson(value, "rf")) every = false;
376 if (rr.parseJson(value, "rr")) every = false;
377 if (ic.parseJson(value, "ic")) every = false;
378
379 }
380
381 void Range::parseJson(const Json::Value& value, const std::string& name) {
382 Json::Value null(Json::nullValue);
383 Json::Value v = value.get(name, null);
384
385 if (v != null) {
386 if (v.size() > 2) {
387 std::cerr << " ERROR. Not expecting array size " << v.size() << " for Range " << name << std::endl;
388 } else {
389 m_every = false;
390 m_inverted = false;
391
392 if (v.isArray()) {
393 m_min = v[0].asInt();
394 m_max = v[1].asInt();
395 } else if (v.isInt()) {
396 m_min = v.asInt();
397 m_max = INT_MAX;
398 } else if (v.isBool()) {
399 m_min = v.asBool() ? 1 : INT_MAX; // if true, [1,MAX], if false [MAX,1] (not 1-MAX)
400 m_max = v.asBool() ? INT_MAX : 1;
401 } else {
402 throw std::invalid_argument("Unexpected type for range flag: " + name);
403 }
404
405 if (m_min > m_max) {
406 m_inverted = true;
407 std::swap(m_min, m_max); // make min always lower
408 }
409 }
410
411 }
412 }
413
414 void AbstractRule::parseJson(const Json::Value& value) {
415
416 // parse read group
417 const std::string rg = "rg";
418 if (value.isMember(rg.c_str())) {
419 Json::Value null(Json::nullValue);
420 Json::Value v = value.get(rg, null);
421 assert(v != null);
422 read_group = v.asString();
423 }
424
425 // set the ID
426 std::vector<std::string> mn = value.getMemberNames();
427 for (std::vector<std::string>::const_iterator i = mn.begin(); i != mn.end(); ++i)
428 id += *i + ";";
429
430 // not necessary, not c++98 compatible
431 //if (id.length())
432 // id.pop_back();
433
434 // parse the flags
435 fr.parseJson(value);
436
437 isize.parseJson(value, "isize");
438 mapq.parseJson(value, "mapq");
439 len.parseJson(value, "length");
440 clip.parseJson(value, "clip");
441 nbases.parseJson(value, "nbases");
442 ins.parseJson(value, "ins");
443 del.parseJson(value, "del");
444 nm.parseJson(value, "nm");
445 xp.parseJson(value, "xp");
446
447 // parse the subsample data
448 parseSubLine(value);
449
450 // parse the motif line
451 parseSeqLine(value);
452
453 }
454
455
456 // main function for determining if a read is valid
457 bool AbstractRule::isValid(const BamRecord &r) {
458
459 DEBUGIV(r, "starting AR:isValid")
460
461 // check if its keep all or none
462 if (isEvery())
463 return true;
464
465 // check if it is a subsample
466 if (subsam_frac < 1) {
467 uint32_t k = __ac_Wang_hash(__ac_X31_hash_string(r.QnameChar()) ^ subsam_seed);
468 if ((double)(k&0xffffff) / 0x1000000 >= subsam_frac)
469 return false;
470 }
471
472 // check if is discordant
473 bool isize_pass = isize.isValid(r.FullInsertSize());
474
475 DEBUGIV("isize_pass: " + isize_pass, r)
476
477 if (!isize_pass) {
478 return false;
479 }
480
481 // check for valid read name
482 if (!read_group.empty()) {
483 std::string RG = r.ParseReadGroup();
484 if (!RG.empty() && RG != read_group)
485 return false;
486 }
487
488 // check for valid mapping quality
489 if (!mapq.isEvery())
490 if (!mapq.isValid(r.MapQuality()))
491 return false;
492
493 DEBUGIV(r, "mapq pass")
494
495 // check for valid flags
496 if (!fr.isValid(r))
497 return false;
498
499 DEBUGIV(r, "flag pass")
500
501 // check the CIGAR
502 if (!ins.isEvery() || !del.isEvery()) {
503 if (!ins.isValid(r.MaxInsertionBases()))
504 return false;
505 if (!del.isValid(r.MaxDeletionBases()))
506 return false;
507 }
508
509 DEBUGIV(r, "cigar pass")
510
511 // get the sequence as trimmed
512 std::string tseq = r.QualitySequence(); //AddZTag("GV", r.Sequence().substr(startpoint, new_len));
513
514 #ifdef HAVE_C11
515 // check for aho corasick motif match
516 if (aho.count) {
517 if (!aho.QueryText(tseq))
518 return false;
519 DEBUGIV(r, "aho pass")
520 }
521 #endif
522
523 // check for valid NM
524 if (!nm.isEvery()) {
525 int32_t nm_val = 0;
526 r.GetIntTag("NM", nm_val);
527 if (!nm.isValid(nm_val))
528 return false;
529 DEBUGIV(r, "NM pass")
530 }
531
532 // check the N bases
533 if (!nbases.isEvery()) {
534 size_t n = r.CountNBases();
535 if (!nbases.isValid(n))
536 return false;
537 DEBUGIV(r, "N bases pass")
538 }
539
540 // check for valid length
541 if (!len.isValid(tseq.length())) {
542 return false;
543 DEBUGIV(r, "len pass")
544 }
545
546 // check for valid clip
547 int new_clipnum = r.NumClip() - (r.Length() - tseq.length()); // get clips, minus amount trimmed off
548 if (!clip.isValid(new_clipnum)) {
549 return false;
550 DEBUGIV(r, "clip pass with clip size " + tostring(new_clipnum))
551 }
552
553 // check for secondary alignments
554 if (!xp.isEvery()) {
555 if (!xp.isValid(r.CountBWASecondaryAlignments())) {
556 return false;
557 }
558 DEBUGIV(r, "XP pass")
559 }
560
561 DEBUGIV(r, "**** READ ACCEPTED IN AR:ISVALID")
562 return true;
563 }
564
565 bool FlagRule::isValid(const BamRecord &r) {
566
567 DEBUGIV(r, "flagrule start")
568
569 if (isEvery())
570 return true;
571
572 // if have on or off flag, use that
573 // 0001100 - all flag
574 // 0101000 - flag
575 // -------
576 // 0001000 - should fail all flag. Should pass any flag
577 if (m_all_on_flag && !( (r.AlignmentFlag() & m_all_on_flag) == m_all_on_flag) ) // if all on, pass
578 return false;
579 if (m_all_off_flag && ( (r.AlignmentFlag() & m_all_off_flag) == m_all_off_flag) ) // if all on, fail
580 return false;
581
582 // if have on or off flag, use that
583 if (m_any_on_flag && !(r.AlignmentFlag() & m_any_on_flag) ) // if ANY on, pass
584 return false;
585 if (m_any_off_flag && (r.AlignmentFlag() & m_any_off_flag)) // if ANY on, fail
586 return false;
587
588 DEBUGIV(r, "FlagRule::isValid checking named flags")
589
590 if (!dup.isNA())
591 if ((dup.isOff() && r.DuplicateFlag()) || (dup.isOn() && !r.DuplicateFlag()))
592 return false;
593 if (!supp.isNA())
594 if ((supp.isOff() && r.SecondaryFlag()) || (supp.isOn() && !r.SecondaryFlag()))
595 return false;
596 if (!qcfail.isNA())
597 if ((qcfail.isOff() && r.QCFailFlag()) || (qcfail.isOn() && !r.QCFailFlag()))
598 return false;
599 if (!mapped.isNA())
600 if ( (mapped.isOff() && r.MappedFlag()) || (mapped.isOn() && !r.MappedFlag()))
601 return false;
602 if (!mate_mapped.isNA())
603 if ( (mate_mapped.isOff() && r.MateMappedFlag()) || (mate_mapped.isOn() && !r.MateMappedFlag()) )
604 return false;
605
606 // check for hard clips
607 if (!hardclip.isNA()) {// check that we want to chuck hard clip
608 if (r.CigarSize() > 1) {
609 bool ishclipped = r.NumHardClip() > 0;
610 if ( (ishclipped && hardclip.isOff()) || (!ishclipped && hardclip.isOn()) )
611 return false;
612 }
613 }
614
615 // check for orientation
616 // check first if we need to even look for orientation
617 bool ocheck = !ff.isNA() || !fr.isNA() || !rf.isNA() || !rr.isNA() || !ic.isNA();
618
619 // now its an orientation pair check. If not both mapped, chuck
620 if (!r.PairMappedFlag() && ocheck)
621 return false;
622
623 if ( ocheck ) {
624
625 // bool first = r.Position() < r.MatePosition();
626 //bool bfr = (first && (!r.ReverseFlag() && r.MateReverseFlag())) || (!first && r.ReverseFlag() && !r.MateReverseFlag());
627 //bool brr = r.ReverseFlag() && r.MateReverseFlag();
628 //bool brf = (first && (r.ReverseFlag() && !r.MateReverseFlag())) || (!first && !r.ReverseFlag() && r.MateReverseFlag());
629 //bool bff = !r.ReverseFlag() && !r.MateReverseFlag();
630
631 bool bic = r.Interchromosomal();
632
633 int PO = r.PairOrientation();
634
635 // its FR and it CANT be FR (off) or its !FR and it MUST be FR (ON)
636 // orienation not defined for inter-chrom, so exclude these with !ic
637 if (!bic) { // PROCEED IF INTRA-CHROMOSOMAL
638 //if ( (bfr && fr.isOff()) || (!bfr && fr.isOn()))
639 if ( (PO == FRORIENTATION && fr.isOff()) || (PO != FRORIENTATION && fr.isOn()))
640 return false;
641 //if ( (brr && rr.isOff()) || (!brr && rr.isOn()))
642 if ( (PO == RRORIENTATION && rr.isOff()) || (PO != RRORIENTATION && rr.isOn()))
643 return false;
644 //if ( (brf && rf.isOff()) || (!brf && rf.isOn()))
645 if ( (PO == RFORIENTATION && rf.isOff()) || (PO != RFORIENTATION&& rf.isOn()))
646 return false;
647 //if ( (bff && ff.isOff()) || (!bff && ff.isOn()))
648 if ( (PO == FFORIENTATION && ff.isOff()) || (PO != FFORIENTATION && ff.isOn()))
649 return false;
650 }
651 if ( (bic && ic.isOff()) || (!bic && ic.isOn()))
652 return false;
653
654 }
655
656 return true;
657
658 }
659
660 // define how to print
661 std::ostream& operator<<(std::ostream &out, const AbstractRule &ar) {
662
663 out << " Rule: ";
664 if (ar.isEvery()) {
665 out << " ALL";
666 } else {
667 if (!ar.read_group.empty())
668 out << "Read Group: " << ar.read_group << " -- ";
669 if (!ar.isize.isEvery())
670 out << "isize:" << ar.isize << " -- " ;
671 if (!ar.mapq.isEvery())
672 out << "mapq:" << ar.mapq << " -- " ;
673 if (!ar.len.isEvery())
674 out << "length:" << ar.len << " -- ";
675 if (!ar.clip.isEvery())
676 out << "clip:" << ar.clip << " -- ";
677 if (!ar.nm.isEvery())
678 out << "nm:" << ar.nm << " -- ";
679 if (!ar.xp.isEvery())
680 out << "xp:" << ar.xp << " -- ";
681 if (!ar.nbases.isEvery())
682 out << "nbases:" << ar.nbases << " -- ";
683 if (!ar.ins.isEvery())
684 out << "ins:" << ar.ins << " -- ";
685 if (!ar.del.isEvery())
686 out << "del:" << ar.del << " -- ";
687 if (ar.subsam_frac < 1)
688 out << "sub:" << ar.subsam_frac << " -- ";
689 #ifdef HAVE_C11
690 if (ar.aho.count)
691 out << "motif: " << ar.aho.file << " -- ";
692 #endif
693 out << ar.fr;
694 }
695 return out;
696 }
697
698 // define how to print
699 std::ostream& operator<<(std::ostream &out, const FlagRule &fr) {
700
701 if (fr.isEvery()) {
702 out << "Flag: ALL";
703 return out;
704 }
705
706 std::string keep = "Flag ON: ";
707 std::string remo = "Flag OFF: ";
708
709 if (fr.m_all_on_flag)
710 keep += "[(all)" + tostring(fr.m_all_on_flag) + "],";
711 if (fr.m_all_off_flag)
712 remo += "[(all)" + tostring(fr.m_all_off_flag) + "],";
713
714 if (fr.m_any_on_flag)
715 keep += "[(any)" + tostring(fr.m_any_on_flag) + "],";
716 if (fr.m_any_off_flag)
717 remo += "[(any)" + tostring(fr.m_any_off_flag) + "],";
718
719 if (fr.dup.isOff())
720 remo += "duplicate,";
721 if (fr.dup.isOn())
722 keep += "duplicate,";
723
724 if (fr.supp.isOff())
725 remo += "supplementary,";
726 if (fr.supp.isOn())
727 keep += "supplementary,";
728
729 if (fr.qcfail.isOff())
730 remo += "qcfail,";
731 if (fr.qcfail.isOn())
732 keep += "qcfail,";
733
734 if (fr.hardclip.isOff())
735 remo += "hardclip,";
736 if (fr.hardclip.isOn())
737 keep += "hardclip,";
738
739 if (fr.paired.isOff())
740 remo += "paired,";
741 if (fr.paired.isOn())
742 keep += "paired,";
743
744
745
746 if (fr.ic.isOff())
747 remo += "ic,";
748 if (fr.ic.isOn())
749 keep += "ic,";
750
751 if (fr.ff.isOff())
752 remo += "ff,";
753 if (fr.ff.isOn())
754 keep += "ff,";
755
756 if (fr.fr.isOff())
757 remo += "fr,";
758 if (fr.fr.isOn())
759 keep += "fr,";
760
761 if (fr.rr.isOff())
762 remo += "rr,";
763 if (fr.rr.isOn())
764 keep += "rr,";
765
766 if (fr.rf.isOff())
767 remo += "rf,";
768 if (fr.rf.isOn())
769 keep += "rf,";
770
771 if (fr.mapped.isOff())
772 remo += "mapped,";
773 if (fr.mapped.isOn())
774 keep += "mapped,";
775
776 if (fr.mate_mapped.isOff())
777 remo += "mate_mapped,";
778 if (fr.mate_mapped.isOn())
779 keep += "mate_mapped,";
780
781 keep = keep.length() > 10 ? keep.substr(0, keep.length() - 1) : ""; // remove trailing comment
782 remo = remo.length() > 10 ? remo.substr(0, remo.length() - 1) : ""; // remove trailing comment
783
784 if (!keep.empty() && !remo.empty())
785 out << keep << " -- " << remo;
786 else if (!keep.empty())
787 out << keep;
788 else
789 out << remo;
790
791 return out;
792 }
793
794 // define how to print
795 std::ostream& operator<<(std::ostream &out, const Range &r) {
796 if (r.isEvery())
797 out << "ALL";
798 else
799 out << (r.m_inverted ? "NOT " : "") << "[" << r.m_min << "," << (r.m_max == INT_MAX ? "MAX" : tostring(r.m_max)) << "]";
800 return out;
801 }
802
803 void AbstractRule::parseSeqLine(const Json::Value& value) {
804
805 #ifdef HAVE_C11
806 bool i = false; // invert motif?
807 #endif
808 std::string motif_file;
809 Json::Value null(Json::nullValue);
810 if (value.get("motif", null) != null)
811 motif_file = value.get("motif", null).asString();
812 else if (value.get("!motif", null) != null) {
813 motif_file = value.get("!motif", null).asString();
814 #ifdef HAVE_C11
815 i = true;
816 #endif
817 }
818 else
819 return;
820 #ifdef HAVE_C11
821 addMotifRule(motif_file, i);
822 #else
823 if (!motif_file.empty())
824 std::cerr << "WARNING: Need to compile with C++11 for Aho-Corasick matching" << std::endl;
825 #endif
826
827 return;
828
829 }
830
831 #ifdef HAVE_C11
832 void AbstractRule::addMotifRule(const std::string& f, bool inverted) {
833 std::cerr << "...making the AhoCorasick trie from " << f << std::endl;
834 aho.TrieFromFile(f);
835 std::cerr << "...finished making AhoCorasick trie with " << AddCommas(aho.count) << " motifs" << std::endl;
836 aho.inv = inverted;
837 }
838
839 void AhoCorasick::TrieFromFile(const std::string& f) {
840
841 file = f;
842
843 // open the sequence file
844 std::ifstream iss(f.c_str());
845 if (!iss || !read_access_test(f))
846 throw std::runtime_error("AhoCorasick::TrieFromFile - Cannot read file: " + f);
847
848 // make the Aho-Corasick trie
849 std::string pat;
850 while (getline(iss, pat, '\n')) {
851 ++count;
852 AddMotif(pat);
853 }
854 }
855 #endif
856
857 void AbstractRule::parseSubLine(const Json::Value& value) {
858 Json::Value null(Json::nullValue);
859 if (value.get("subsample", null) != null)
860 subsam_frac = value.get("subample", null).asDouble();
861 }
862
863 GRC ReadFilterCollection::getAllRegions() const
864 {
865 GRC out;
866
867 for (std::vector<ReadFilter>::const_iterator i = m_regions.begin(); i != m_regions.end(); ++i)
868 out.Concat(i->m_grv);
869
870 return out;
871 }
872
873 #ifdef HAVE_C11
874 int AhoCorasick::QueryText(const std::string& t) const {
875 auto matches = aho_trie->parse_text(t);
876 return matches.size();
877 return 0;
878 }
879 #endif
880
881 }
882 }