@@ -43,7 +43,7 @@ int JunctionsExtractor::parse_options(int argc, char *argv[]) {
4343 optind = 1 ; // Reset before parsing again.
4444 int c;
4545 stringstream help_ss;
46- while ((c = getopt (argc, argv, " ha:m:M:o:r:t:s:b:" )) != -1 ) {
46+ while ((c = getopt (argc, argv, " ha:m:M:f:F:q: o:r:t:s:b:" )) != -1 ) {
4747 switch (c) {
4848 case ' h' :
4949 usage (help_ss);
@@ -57,6 +57,15 @@ int JunctionsExtractor::parse_options(int argc, char *argv[]) {
5757 case ' M' :
5858 max_intron_length_ = atoi (optarg);
5959 break ;
60+ case ' f' :
61+ require_flags_ = atoi (optarg);
62+ break ;
63+ case ' F' :
64+ filter_flags_ = atoi (optarg);
65+ break ;
66+ case ' q' :
67+ min_map_qual_ = atoi (optarg);
68+ break ;
6069 case ' o' :
6170 output_file_ = string (optarg);
6271 break ;
@@ -108,10 +117,17 @@ int JunctionsExtractor::parse_options(int argc, char *argv[]) {
108117 throw runtime_error (" Strandness mode 'intron-motif' requires a fasta file!\n\n " );
109118 }
110119 }
120+ if ( (require_flags_ & filter_flags_) != 0 ) {
121+ usage ();
122+ throw runtime_error (" No overlap allowed between '-f' and '-F' options (same flag filtered and required)!\n\n " );
123+ }
111124
112125 cerr << " Minimum junction anchor length: " << min_anchor_length_ << endl;
113126 cerr << " Minimum intron length: " << min_intron_length_ << endl;
114127 cerr << " Maximum intron length: " << max_intron_length_ << endl;
128+ cerr << " Require alignment flags: " << require_flags_ << endl;
129+ cerr << " Filter alignment flags: " << filter_flags_ << endl;
130+ cerr << " Minimum alignment mapping quality: " << int (min_map_qual_) << endl;
115131 cerr << " Alignment: " << bam_ << endl;
116132 cerr << " Output file: " << output_file_ << endl;
117133 if (output_barcodes_file_ != " NA" ){
@@ -130,6 +146,9 @@ int JunctionsExtractor::usage(ostream& out) {
130146 << " \t\t\t " << " anchor length on both sides are reported. [8]" << endl;
131147 out << " \t\t " << " -m INT\t Minimum intron length. [70]" << endl;
132148 out << " \t\t " << " -M INT\t Maximum intron length. [500000]" << endl;
149+ out << " \t\t " << " -f INT\t Only use alignments where all flag bits set here are set. [0]" << endl;
150+ out << " \t\t " << " -F INT\t Only use alignments where no flag bits set here are set. [0]" << endl;
151+ out << " \t\t " << " -q INT\t Only use alignments with this mapping quality or above. [0]" << endl;
133152 out << " \t\t " << " -o FILE\t The file to write output to. [STDOUT]" << endl;
134153 out << " \t\t " << " -r STR\t The region to identify junctions \n "
135154 << " \t\t\t " << " in \" chr:start-end\" format. Entire BAM by default." << endl;
@@ -358,7 +377,7 @@ void JunctionsExtractor::set_junction_strand(bam1_t *aln, Junction& j1, string i
358377 return ;
359378}
360379
361- // Get the the barcode
380+ // Get the barcode
362381void JunctionsExtractor::set_junction_barcode (bam1_t *aln, Junction& j1) {
363382 uint8_t *p = bam_aux_get (aln, barcode_tag_.c_str ());
364383 if (p != NULL ) {
@@ -373,6 +392,14 @@ void JunctionsExtractor::set_junction_barcode(bam1_t *aln, Junction& j1) {
373392 }
374393}
375394
395+ // Filters alignments based on filtering flags and mapping quality
396+ bool JunctionsExtractor::filter_alignment (bam_hdr_t *header, bam1_t *aln) {
397+ if ((aln->core .flag & filter_flags_) != 0 ) return true ;
398+ if ((aln->core .flag | require_flags_) != aln->core .flag ) return true ;
399+ if (aln->core .qual < min_map_qual_) return true ;
400+ return false ;
401+ }
402+
376403// Parse junctions from the read and store in junction map
377404int JunctionsExtractor::parse_alignment_into_junctions (bam_hdr_t *header, bam1_t *aln) {
378405 int n_cigar = aln->core .n_cigar ;
@@ -523,6 +550,7 @@ int JunctionsExtractor::identify_junctions_from_BAM() {
523550 // Initiate the alignment record
524551 bam1_t *aln = bam_init1 ();
525552 while (sam_itr_next (in, iter, aln) >= 0 ) {
553+ if (filter_alignment (header, aln)) continue ;
526554 parse_alignment_into_junctions (header, aln);
527555 }
528556 hts_itr_destroy (iter);
0 commit comments