1212import java .util .Iterator ;
1313import java .util .List ;
1414import java .util .Map ;
15- import java .util .concurrent .atomic .AtomicLong ;
16- import java .util .regex .Pattern ;
15+ import java .util .concurrent .atomic .LongAdder ;
1716
1817import htsjdk .samtools .SamReader ;
1918import htsjdk .samtools .SAMRecord ;
@@ -33,44 +32,39 @@ class CoverageJob implements Job {
3332 private final ChrPosition cp ;
3433 private final QLogger logger ;
3534 private final QueryExecutor filter ;
36- private final HashSet <SamReader > fileReaders = new HashSet <SamReader >();
35+ private final HashSet <File > fileReaders = new HashSet <>();
3736 private final Algorithm alg ;
38- private final AtomicLong counterIn ;
39- private final AtomicLong counterOut ;
37+ private final LongAdder counterIn ;
38+ private final LongAdder counterOut ;
4039 private final List <ChrPosition > includes ;
4140 private final List <ChrPosition > excludes ;
4241 private Map <ChrPosition , RegionCounter > chrSpecificRegions ;
4342 private int windowSize = 1000000 ; // default to a mill
4443 private final AbstractQueue <SAMRecord > outputQueue ;
44+ String validation ;
4545
4646 CoverageJob (final ChrPosition cp ,
4747 final HashSet <Pair <File , File >> filePairs , final QueryExecutor filter ,
48- final Algorithm algorithm , final AtomicLong counterIn ,final AtomicLong counterOut ,
49- Integer windowSize , Pattern regex , AbstractQueue <SAMRecord > outputQueue ) throws Exception {
50- this (cp , filePairs , filter , algorithm , counterIn , counterOut , null , windowSize , outputQueue , null , null );
51- }
52- CoverageJob (final ChrPosition cp ,
53- final HashSet <Pair <File , File >> filePairs , final QueryExecutor filter ,
54- final Algorithm algorithm , final AtomicLong counterIn , final AtomicLong counterOut , final String validation ,
48+ final Algorithm algorithm , final LongAdder counterIn , final LongAdder counterOut , final String validation ,
5549 Integer windowSize , AbstractQueue <SAMRecord > outputQueue ,
56- List <ChrPosition > includes , List <ChrPosition > excludes ) throws Exception {
50+ List <ChrPosition > includes , List <ChrPosition > excludes ) {
5751 assert (cp .getLength () > -1 );
5852 this .cp = cp ;
5953 this .alg = algorithm ;
54+ this .validation = validation ;
6055 this .counterIn = counterIn ;
6156 this .counterOut = counterOut ;
6257 this .filter = filter ;
6358 this .logger = QLoggerFactory .getLogger (CoverageJob .class );
6459 this .includes = includes ;
6560 this .excludes = excludes ;
6661 if (null != windowSize ) {
67- this .windowSize = windowSize . intValue () ;
62+ this .windowSize = windowSize ;
6863 }
6964 this .outputQueue = outputQueue ;
7065 for (final Pair <File , File > pair : filePairs ) {
7166 File bamFile = pair .left ();
72- SamReader reader = SAMFileReaderFactory .createSAMFileReader (bamFile , validation );
73- fileReaders .add (reader );
67+ fileReaders .add (bamFile );
7468 }
7569 logger .debug ("Length of sequence to be processed by job '" + cp .toIGVString () + " : " + cp .getLength ());
7670 }
@@ -110,15 +104,15 @@ void constructCoverageMap() {
110104 }
111105
112106 private void performCoverage () throws Exception {
113- for (final SamReader fileReader : fileReaders ) {
114-
107+ for (final File file : fileReaders ) {
108+ SamReader fileReader = SAMFileReaderFactory . createSAMFileReader ( file , validation );
115109 Iterator <SAMRecord > iter = "unmapped" .equals (cp .getChromosome ()) ? fileReader .queryUnmapped () : fileReader .query (cp .getChromosome (), cp .getStartPosition (), cp .getEndPosition (), true );
116110 long recordCounterIn = 0 ;
117111 long recordCounterOut = 0 ;
118112 while (iter .hasNext ()) {
119113 SAMRecord read = iter .next ();
120114
121- counterIn .incrementAndGet (); //count input read number
115+ counterIn .increment (); //count input read number
122116
123117 if (++recordCounterIn % 10000000 == 0 ) {
124118 logger .debug ("Hit " + recordCounterIn + " record for " + cp .toIGVString ());
@@ -127,26 +121,25 @@ private void performCoverage() throws Exception {
127121 if (MotifConstants .UNMAPPED .equals (cp .getChromosome ()) || read .getReferenceName ().equals (cp .getChromosome ())) {
128122 if (null == filter ) {
129123 recordCounterOut ++;
130- counterOut .incrementAndGet (); //count output read number
124+ counterOut .increment (); //count output read number
131125 if (alg .applyTo (read , chrSpecificRegions )) outputQueue .add (read );
132126 } else if (filter .Execute (read )) {
133127 recordCounterOut ++;
134- counterOut .incrementAndGet (); //count output read number
128+ counterOut .increment (); //count output read number
135129 if (alg .applyTo (read , chrSpecificRegions )) outputQueue .add (read );
136130 }
137131 } else {
138132 logger .info ("ref names did not match!" );
139133 }
140134 }
141135 fileReader .close ();
136+
137+ String sb = cp .toIGVString () + ":\n " +
138+ "read " + recordCounterIn + " records, of which " + recordCounterOut + " satisfied the query\n " +
139+ "number in counterIn instance is: " + counterIn .longValue () + "\n " +
140+ "number in counterOut instance is: " + counterOut .longValue () + "\n " ;
142141
143- StringBuilder sb = new StringBuilder ();
144- sb .append (cp .toIGVString ()).append (":\n " );
145- sb .append ("read " ).append (recordCounterIn ).append (" records, of which " ).append (recordCounterOut ).append (" satisfied the query\n " );
146- sb .append ("number in counterIn instance is: " ).append (counterIn .get ()).append ("\n " );
147- sb .append ("number in counterOut instance is: " ).append (counterOut .get ()).append ("\n " );
148-
149- logger .info (sb .toString ());
142+ logger .info (sb );
150143 }
151144 }
152145}
0 commit comments