Skip to content

Commit

Permalink
formats: improve samtools/flagstat support, #187
Browse files Browse the repository at this point in the history
  • Loading branch information
jtarraga committed Jun 26, 2020
1 parent 705ca5c commit 534975d
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public class SamtoolsFlagstats {
*/
private int secondaryAlignments;

/**
* Total number of reads which are supplementary (0x800 bit set)
*/
private int supplementary;
/**
* Total number of reads which are duplicates (0x400 bit set)
*/
Expand All @@ -32,6 +36,17 @@ public class SamtoolsFlagstats {
*/
private int pairedInSequencing;

/**
* Total number of reads with both 0x1 and 0x40 bits set
*/
private int read1;


/**
* Total number reads with both 0x1 and 0x80 bits set
*/
private int read2;

/**
* Total number of reads which are properly paired (both 0x1 and 0x2 bits set and 0x4 bit not set)
*/
Expand Down Expand Up @@ -70,8 +85,11 @@ public String toString() {
sb.append(", totalQcPassed=").append(totalQcPassed);
sb.append(", mapped=").append(mapped);
sb.append(", secondaryAlignments=").append(secondaryAlignments);
sb.append(", supplementary=").append(supplementary);
sb.append(", duplicates=").append(duplicates);
sb.append(", pairedInSequencing=").append(pairedInSequencing);
sb.append(", read1=").append(read1);
sb.append(", read2=").append(read2);
sb.append(", properlyPaired=").append(properlyPaired);
sb.append(", selfAndMateMapped=").append(selfAndMateMapped);
sb.append(", singletons=").append(singletons);
Expand Down Expand Up @@ -117,6 +135,15 @@ public SamtoolsFlagstats setSecondaryAlignments(int secondaryAlignments) {
return this;
}

public int getSupplementary() {
return supplementary;
}

public SamtoolsFlagstats setSupplementary(int supplementary) {
this.supplementary = supplementary;
return this;
}

public int getDuplicates() {
return duplicates;
}
Expand All @@ -135,6 +162,24 @@ public SamtoolsFlagstats setPairedInSequencing(int pairedInSequencing) {
return this;
}

public int getRead1() {
return read1;
}

public SamtoolsFlagstats setRead1(int read1) {
this.read1 = read1;
return this;
}

public int getRead2() {
return read2;
}

public SamtoolsFlagstats setRead2(int read2) {
this.read2 = read2;
return this;
}

public int getProperlyPaired() {
return properlyPaired;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.opencb.biodata.formats.alignment.samtools.io;

import org.opencb.biodata.formats.alignment.samtools.SamtoolsFlagstats;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Path;

public class SamtoolsFlagstatsParser {

public static SamtoolsFlagstats parse(Path path) throws IOException {

SamtoolsFlagstats flagstats = new SamtoolsFlagstats();

FileReader fr = new FileReader(path.toFile());
BufferedReader br = new BufferedReader(fr);

String line;

while ((line = br.readLine()) != null) {
String[] splits = line.split(" ");
int passed = Integer.parseInt(splits[0]);
int failed = Integer.parseInt(splits[2]);
if (line.contains("QC-passed")) {
flagstats.setTotalQcPassed(passed);
flagstats.setTotalReads(passed + failed);
} else if (line.contains("secondary")) {
flagstats.setSecondaryAlignments(passed);
} else if (line.contains("supplementary")) {
flagstats.setSupplementary(passed);
} else if (line.contains("duplicates")) {
flagstats.setDuplicates(passed);
} else if (line.contains("mapped")) {
flagstats.setMapped(passed);
} else if (line.contains("paired in sequencing")) {
flagstats.setPairedInSequencing(passed);
} else if (line.contains("read1")) {
flagstats.setRead1(passed);
} else if (line.contains("read2")) {
flagstats.setRead2(passed);
} else if (line.contains("properly paired")) {
flagstats.setProperlyPaired(passed);
} else if (line.contains("with itself and mate mapped")) {
flagstats.setSelfAndMateMapped(passed);
} else if (line.contains("singletons")) {
flagstats.setSingletons(passed);
} else if (line.contains("mapQ>=5")) {
flagstats.setDiffChrMapQ5(passed);
} else if (line.contains("with mate mapped")) {
flagstats.setMateMappedToDiffChr(passed);
}
}

return flagstats;
}
}

0 comments on commit 534975d

Please sign in to comment.