@@ -48,11 +48,6 @@ public void extract(BEDCoord read) {
4848
4949 int BEDSTART = (int )read .getStart ();
5050 int BEDSTOP = (int )read .getStop ();
51- //Correct for '-' strand BED coord so they align with '+' strand
52- if (read .getDir ().equals ("-" )) {
53- BEDSTART ++;
54- BEDSTOP ++;
55- }
5651
5752 //Correct Window Size for proper transformations
5853 int WINDOW = (BEDSTOP - BEDSTART ) + ((param .getBin () / 2 ) * 2 );
@@ -81,24 +76,29 @@ else if(param.getTrans() == 2) {
8176 if ((sr .getFirstOfPairFlag () && param .getRead () == 0 ) || (!sr .getFirstOfPairFlag () && param .getRead () == 1 ) || param .getRead () == 2 || (sr .getFirstOfPairFlag () && param .getRead () == 3 )) {
8277 int FivePrime = sr .getUnclippedStart () - 1 ;
8378 if (sr .getReadNegativeStrandFlag ()) {
84- FivePrime = sr .getUnclippedEnd ();
79+ FivePrime = sr .getUnclippedEnd () - 1 ;
8580 FivePrime -= SHIFT ; //SHIFT DATA HERE IF NECCESSARY
8681 } else { FivePrime += SHIFT ; }
8782
8883 if (sr .getProperPairFlag ()) { //prevent cases where non-properly paired Read1 gets to this point
89- int recordStart = sr .getUnclippedStart () - 1 ;
90- int recordStop = sr .getMateAlignmentStart () + sr .getReadLength () - 1 ;
91- if (sr .getMateAlignmentStart () - 1 < recordStart ) {
92- recordStart = sr .getMateAlignmentStart () - 1 ;
93- recordStop = sr .getUnclippedEnd ();
84+ //Find midpoint if read flag == 3
85+ if (param .getRead () == 3 ) {
86+ if (sr .getInferredInsertSize ()>0 ) {
87+ FivePrime = sr .getAlignmentStart () - 1 + (sr .getInferredInsertSize () / 2 );
88+ } else if (sr .getInferredInsertSize ()<0 ) {
89+ FivePrime = sr .getMateAlignmentStart () - 1 - (sr .getInferredInsertSize () / 2 );
90+ } else {
91+ //Most aligners will flag records with an insert size of zero as improper pairs
92+ System .err .println ("This statement should never print (insert size=0 when finding midpoint in PileupExtract)" );
93+ continue ;
94+ }
95+ // Correction to ensure that even insert size mark reoriented for negative strands
96+ if (sr .getInferredInsertSize () % 2 == 0 && read .getDir ().equals ("-" ) ) { FivePrime --; }
9497 }
95-
96- //Find midpoint is read flag == 3
97- if (param .getRead () == 3 ) { FivePrime = (recordStart + recordStop ) / 2 ; }
98-
99- if (recordStop - recordStart < param .getMinInsert () && param .getMinInsert () != -9999 ) { FivePrime = -1 ; } //Test for MIN insert size cutoff here
100- if (recordStop - recordStart > param .getMaxInsert () && param .getMaxInsert () != -9999 ) { FivePrime = -1 ; } //Test for MAX insert size cutoff here
101- } else if (param .getRead () == 3 ) { FivePrime = -1 ; } // Make sure that midpoint pileup must come from properly paired read
98+ // Apply insert size filters
99+ if (Math .abs (sr .getInferredInsertSize ()) < param .getMinInsert () && param .getMinInsert () != -9999 ) { continue ; } //Test for MIN insert size cutoff here
100+ if (Math .abs (sr .getInferredInsertSize ()) > param .getMaxInsert () && param .getMaxInsert () != -9999 ) { continue ; } //Test for MAX insert size cutoff here
101+ } else if (param .getRead () == 3 ) { continue ; } // Make sure that midpoint pileup must come from properly paired read
102102
103103 //Adjust tag start to be within array reference
104104 FivePrime -= (BEDSTART - QUERYWINDOW );
@@ -119,7 +119,7 @@ else if(param.getTrans() == 2) {
119119 } else if (param .getRead () == 0 || param .getRead () == 2 ) { //Also outputs if not paired-end since by default it is read-1
120120 int FivePrime = sr .getUnclippedStart () - 1 ;
121121 if (sr .getReadNegativeStrandFlag ()) {
122- FivePrime = sr .getUnclippedEnd ();
122+ FivePrime = sr .getUnclippedEnd () - 1 ;
123123 FivePrime -= SHIFT ; //SHIFT DATA HERE IF NECCESSARY
124124 } else { FivePrime += SHIFT ; }
125125 FivePrime -= (BEDSTART - QUERYWINDOW );
0 commit comments