-
-
Save haluk/d357aab571f83c1d1470477af23f5e7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import org.jcvi.jillion.core.datastore.DataStoreException; | |
import org.jcvi.jillion.core.datastore.DataStoreProviderHint; | |
import org.jcvi.jillion.core.util.iter.StreamingIterator; | |
import org.jcvi.jillion.trace.fastq.FastqDataStore; | |
import org.jcvi.jillion.trace.fastq.FastqFileDataStoreBuilder; | |
import org.jcvi.jillion.trace.fastq.FastqRecord; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.stream.IntStream; | |
/** | |
* Created by hd on 4/3/16. | |
*/ | |
public class Sample { | |
private String name; | |
private static long numberOfReads = 0L; | |
private static long numberOfBases = 0L; | |
private static long numberOfHighQualityBases = 0L; | |
public Sample() { | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public long getNumberOfReads() { | |
return numberOfReads; | |
} | |
public void setNumberOfReads(long numberOfReads) { | |
this.numberOfReads = numberOfReads; | |
} | |
public long getNumberOfBases() { | |
return numberOfBases; | |
} | |
public void setNumberOfBases(long numberOfBases) { | |
this.numberOfBases = numberOfBases; | |
} | |
public long getNumberOfHighQualityBases() { | |
return numberOfHighQualityBases; | |
} | |
public void setNumberOfHighQualityBases(long numberOfHighQualityBases) { | |
this.numberOfHighQualityBases = numberOfHighQualityBases; | |
} | |
@Override | |
public String toString() { | |
return "Sample{" + | |
"name='" + name + '\'' + | |
", numberOfReads=" + numberOfReads + | |
", numberOfBases=" + numberOfBases + | |
", numberOfHighQualityBases=" + numberOfHighQualityBases + | |
'}'; | |
} | |
private void count(FastqRecord read, int high_quality) { | |
incrementReadNumber(1); | |
final byte[] quality = read.getQualitySequence().toArray(); | |
incrementNumberOfBases(quality.length); | |
IntStream is = IntStream.range(0, quality.length).map(i -> quality[i]); | |
is = is.parallel().filter(b -> b >= high_quality); | |
incrementNumberOfHighQualityBases(is.toArray().length); | |
} | |
private synchronized void incrementReadNumber(int x) { | |
numberOfReads += x; | |
} | |
private synchronized void incrementNumberOfBases(int x) { | |
numberOfBases += x; | |
} | |
private synchronized void incrementNumberOfHighQualityBases(int x) { | |
numberOfHighQualityBases += x; | |
} | |
public void calcStat(File fqFile, int highQualityThr) { | |
try { | |
FastqDataStore datastore = new FastqFileDataStoreBuilder(fqFile) | |
.hint(DataStoreProviderHint.ITERATION_ONLY) | |
.build(); | |
StreamingIterator<FastqRecord> iter = datastore.iterator(); | |
iter.toStream().parallel().forEach(read -> count(read, highQualityThr)); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} catch (DataStoreException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment