Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 41 additions & 32 deletions qprofiler2/src/org/qcmg/qprofiler2/bam/BamSummaryReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,33 +297,34 @@ private void createQual(Element parent, boolean isLongReadBam) {

private void createTLen(Element parent) {
//ISIZE
parent = XmlElementUtils.createSubElement(parent, XmlUtils.READGROUPS);
for (Entry<String, ReadGroupSummary> entry : rgSummaries.entrySet()) {
// output tLen inside pairSummary, eg. inward, f3f5
entry.getValue().pairTlen2Xml(XmlUtils.createReadGroupNode(parent, entry.getKey()));
}
parent = XmlElementUtils.createSubElement(parent, XmlUtils.READGROUPS);
List<String> sortedKeys = rgSummaries.keySet().stream().sorted().toList();
for (String key : sortedKeys) {
rgSummaries.get(key).pairTlen2Xml(XmlUtils.createReadGroupNode(parent, key));
}
}

private void createRLENGTH(Element parent) {
//Read length
parent = XmlElementUtils.createSubElement(parent, XmlUtils.READGROUPS);
for (Entry<String, ReadGroupSummary> entry : rgSummaries.entrySet()) {
// output ReadLength
entry.getValue().readLength2Xml(XmlUtils.createReadGroupNode(parent, entry.getKey()));
List<String> sortedKeys = rgSummaries.keySet().stream().sorted().toList();
for (String key : sortedKeys) {
rgSummaries.get(key).readLength2Xml(XmlUtils.createReadGroupNode(parent, key));
}
}

private void createCigar(Element parent) {
parent = XmlElementUtils.createSubElement(parent, XmlUtils.READGROUPS);
for (Entry<String, ReadGroupSummary> entry : rgSummaries.entrySet()) {
Element ele = XmlUtils.createMetricsNode(XmlUtils.createReadGroupNode(parent, entry.getKey()), null,
new Pair<String, Number>(ReadGroupSummary.READ_COUNT,entry.getValue().getCigarReadCount()));

// cigar string from reads including duplicateReads, notProperPairs and unmappedReads but excluding discardedReads (failed, secondary and supplementary).
Map<String, AtomicLong> tallys = new TreeMap<>(new CigarStringComparator());
tallys.putAll( entry.getValue().getCigarCount());
XmlUtils.outputTallyGroup(ele ,XmlUtils.CIGAR , tallys, true, false);
}
parent = XmlElementUtils.createSubElement(parent, XmlUtils.READGROUPS);
List<String> sortedKeys = rgSummaries.keySet().stream().sorted().toList();
for (String key : sortedKeys) {
Element ele = XmlUtils.createMetricsNode(XmlUtils.createReadGroupNode(parent, key), null,
new Pair<String, Number>(ReadGroupSummary.READ_COUNT,rgSummaries.get(key).getCigarReadCount()));

// cigar string from reads including duplicateReads, notProperPairs and unmappedReads but excluding discardedReads (failed, secondary and supplementary).
Map<String, AtomicLong> tallys = new TreeMap<>(new CigarStringComparator());
tallys.putAll( rgSummaries.get(key).getCigarCount());
XmlUtils.outputTallyGroup(ele ,XmlUtils.CIGAR , tallys, true, false);
}
}

private void createRNAME(Element parent) {
Expand Down Expand Up @@ -371,12 +372,13 @@ private void createMAPQ(Element parent) {

private void createPOS(Element parent) {
parent = XmlElementUtils.createSubElement(parent, XmlUtils.READGROUPS);

for (String rg : rgSummaries.keySet()) {

List<String> sortedKeys = rgSummaries.keySet().stream().sorted().toList();
for (String rg : sortedKeys) {
long readCount = rNamePosition.values().stream().mapToLong(x -> x.getTotalCountByRg(rg)).sum();
Element ele = XmlUtils.createMetricsNode(XmlUtils.createReadGroupNode(parent, rg) , null, new Pair<String, Number>(ReadGroupSummary.READ_COUNT, readCount));
rNamePosition.keySet().stream().sorted(new ReferenceNameComparator()).forEach(ref ->
XmlUtils.outputBins(ele, ref, rNamePosition.get(ref).getCoverageByRg(rg), PositionSummary.BUCKET_SIZE));
Element ele = XmlUtils.createMetricsNode(XmlUtils.createReadGroupNode(parent, rg) , null, new Pair<String, Number>(ReadGroupSummary.READ_COUNT, readCount));
rNamePosition.keySet().stream().sorted(new ReferenceNameComparator()).forEach(ref ->
XmlUtils.outputBins(ele, ref, rNamePosition.get(ref).getCoverageByRg(rg), PositionSummary.BUCKET_SIZE));
}
}

Expand Down Expand Up @@ -463,7 +465,7 @@ public void parseRecord(final SAMRecord record) {
}
}

private void summaryToXml(Element parent) {
public void summaryToXml(Element parent) {
Element summaryElement = XmlElementUtils.createSubElement(parent, XmlUtils.BAM_SUMMARY);

long discardReads = 0;
Expand All @@ -473,29 +475,30 @@ private void summaryToXml(Element parent) {
long noncanonicalBase = 0;
long trimBases = 0,overlappedBase = 0, softClippedBase = 0, hardClippedBase = 0;
long readCount = 0, lostBase = 0; // baseCount = 0,
Element rgsElement = XmlElementUtils.createSubElement(summaryElement, XmlUtils.READGROUPS);
for (ReadGroupSummary summary: rgSummaries.values()) {
Element rgsElement = XmlElementUtils.createSubElement(summaryElement, XmlUtils.READGROUPS);
List<String> sortedKeys = rgSummaries.keySet().stream().sorted().toList();
for (String key : sortedKeys) {
try {

ReadGroupSummary summary = rgSummaries.get(key);
Element rgEle = XmlUtils.createReadGroupNode(rgsElement, summary.getReadGroupId());
summary.readSummary2Xml(rgEle);
summary.pairSummary2Xml(rgEle);
summary.pairSummary2Xml(rgEle);
// presummary
lostBase += summary.getDuplicateBase() + summary.getUnmappedBase() + summary.getNotProperPairedBase()
+ summary.getTrimmedBase() + summary.getOverlappedBase() + summary.getSoftClippedBase() + summary.getHardClippedBase();
maxBases += summary.getReadCount() * summary.getMaxReadLength();
+ summary.getTrimmedBase() + summary.getOverlappedBase() + summary.getSoftClippedBase() + summary.getHardClippedBase();
maxBases += summary.getReadCount() * summary.getMaxReadLength();
duplicateBase += summary.getDuplicateBase();
unmappedBase += summary.getUnmappedBase();
noncanonicalBase += summary.getNotProperPairedBase();
trimBases += summary.getTrimmedBase();
overlappedBase += summary.getOverlappedBase();
softClippedBase += summary.getSoftClippedBase();
hardClippedBase += summary.getHardClippedBase();

discardReads += summary.getDiscardreads();
readCount += summary.getReadCount();
} catch (Exception e) {
logger.warn(e.getMessage());
logger.warn(e.getMessage());
}
}

Expand Down Expand Up @@ -578,6 +581,12 @@ public void setSamSequenceDictionary(SAMSequenceDictionary samSeqDictionary) {
public void setReadGroups(List<String> ids) {
readGroupIds = Stream.concat(ids.stream(), readGroupIds.stream()).collect(Collectors.toList());
}

public void setReadGroupSummaries(List<String> ids) {
for (String readGroupId : ids) {
rgSummaries.put(readGroupId, new ReadGroupSummary(readGroupId, isLongReadBam));
}
}

ConcurrentMap<String, PositionSummary> getRNamePosition() {
return rNamePosition;
Expand Down
60 changes: 53 additions & 7 deletions qprofiler2/test/org/qcmg/qprofiler2/bam/BamSummaryReportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

import static org.junit.Assert.*;

import java.io.BufferedWriter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.xml.parsers.ParserConfigurationException;



import org.qcmg.qprofiler2.summarise.*;
import org.w3c.dom.Element;
import htsjdk.samtools.SAMRecord;
Expand All @@ -22,14 +22,15 @@
import org.qcmg.common.string.StringUtils;
import org.qcmg.common.util.XmlElementUtils;
import org.qcmg.picard.BwaPair.Pair;
import org.qcmg.qprofiler2.bam.BamSummaryReport;
import org.qcmg.qprofiler2.util.XmlUtils;
import org.w3c.dom.NodeList;


public class BamSummaryReportTest {

@Rule
public TemporaryFolder testFolder = new TemporaryFolder();
public TemporaryFolder testFolder = new TemporaryFolder();


@Test
public void testParseRNameAndPos() throws Exception {
Expand Down Expand Up @@ -79,6 +80,8 @@ public void testParseRNameAndPos() throws Exception {
assertEquals(1, returnedSummary.getCoverageByRg(rg).size());

}



@Test
public void testCompareWithSAMUtils() {
Expand Down Expand Up @@ -358,13 +361,56 @@ public static Element createLongReadRoot(File input) throws Exception {
return root;
}

@Test
public void testReadGroupOrder() throws Exception {
BamSummaryReport report = new BamSummaryReport(3, false, false);
String[] rg = new String[] {"cd90dd75-8a1f-4fd0-a352-0364d8dd5300","69f81d0d-c430-4a6f-9ccd-05ea88b22c1d","374ed445-b8ee-4a1d-9337-f3fdd661f408"};
List list= Arrays.asList(rg);
report.setReadGroups(Arrays.asList(rg));

//Before sort
assertEquals(list.get(0),"cd90dd75-8a1f-4fd0-a352-0364d8dd5300");
assertEquals(list.get(1),"69f81d0d-c430-4a6f-9ccd-05ea88b22c1d");
assertEquals(list.get(2),"374ed445-b8ee-4a1d-9337-f3fdd661f408");

Element root = XmlElementUtils.createRootElement("root", null);
report.setReadGroupSummaries(list);
report.summaryToXml(root);

List<String> readGroupNames = getAllReadGroupNames(root);
assertTrue(readGroupNames.size() == 3);
assertEquals(readGroupNames.get(0),"374ed445-b8ee-4a1d-9337-f3fdd661f408");
assertEquals(readGroupNames.get(1),"69f81d0d-c430-4a6f-9ccd-05ea88b22c1d");
assertEquals(readGroupNames.get(2),"cd90dd75-8a1f-4fd0-a352-0364d8dd5300");

}

public List<String> getAllReadGroupNames(Element root) {
List<String> readGroupNames = new ArrayList<>();
NodeList bamSummaryList = root.getElementsByTagName("bamSummary");
for (int i = 0; i < bamSummaryList.getLength(); i++) {
Element bamSummary = (Element) bamSummaryList.item(i);
NodeList readGroupsList = bamSummary.getElementsByTagName("readGroups");
for (int j = 0; j < readGroupsList.getLength(); j++) {
Element readGroups = (Element) readGroupsList.item(j);
NodeList readGroupList = readGroups.getElementsByTagName("readGroup");
for (int k = 0; k < readGroupList.getLength(); k++) {
Element readGroup = (Element) readGroupList.item(k);
readGroupNames.add(readGroup.getAttribute("name"));
}
}
}
return readGroupNames;
}

@Test
public void unpairedTest() throws ParserConfigurationException {
BamSummaryReport report = new BamSummaryReport(3, false, false);

SAMRecord record = new SAMRecord(null);

record.setReadName("TESTDATA");

// first read
record.setReadBases("ACCCT AACCC CAACC CTAAC CNTAA CCCTA ACCCA AC".replace(" ","").getBytes());
report.parseRecord(record);// unapired
Expand Down