Skip to content

Commit 65bdfe5

Browse files
committed
Fix handling of idxdepth/paraGRAPH coverage
1 parent 2fa2097 commit 65bdfe5

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

SequenceAnalysis/src/org/labkey/sequenceanalysis/run/alignment/ParagraphStep.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package org.labkey.sequenceanalysis.run.alignment;
22

3+
import htsjdk.samtools.SAMFileHeader;
4+
import htsjdk.samtools.SamReader;
5+
import htsjdk.samtools.SamReaderFactory;
6+
import org.apache.commons.io.FileUtils;
37
import org.json.JSONObject;
48
import org.labkey.api.module.ModuleLoader;
59
import org.labkey.api.pipeline.PipelineJob;
@@ -15,11 +19,14 @@
1519
import org.labkey.api.sequenceanalysis.run.AbstractCommandWrapper;
1620
import org.labkey.api.sequenceanalysis.run.SimpleScriptWrapper;
1721
import org.labkey.api.util.FileUtil;
22+
import org.labkey.api.writer.PrintWriters;
1823
import org.labkey.sequenceanalysis.SequenceAnalysisModule;
1924
import org.labkey.sequenceanalysis.util.SequenceUtil;
2025

2126
import java.io.File;
2227
import java.io.IOException;
28+
import java.io.PrintWriter;
29+
import java.nio.charset.Charset;
2330
import java.util.ArrayList;
2431
import java.util.Arrays;
2532
import java.util.List;
@@ -95,9 +102,9 @@ else if (!svVcf.exists())
95102
depthArgs.add("-b");
96103
depthArgs.add(so.getFile().getPath());
97104

98-
File coverageFile = new File(ctx.getWorkingDirectory(), "coverage.txt");
105+
File coverageJson = new File(ctx.getWorkingDirectory(), "coverage.json");
99106
depthArgs.add("-o");
100-
depthArgs.add(coverageFile.getPath());
107+
depthArgs.add(coverageJson.getPath());
101108

102109
depthArgs.add("-r");
103110
depthArgs.add(ctx.getSequenceSupport().getCachedGenome(so.getLibrary_id()).getWorkingFastaFile().getPath());
@@ -110,14 +117,41 @@ else if (!svVcf.exists())
110117

111118
new SimpleScriptWrapper(ctx.getLogger()).execute(depthArgs);
112119

113-
if (!coverageFile.exists())
120+
if (!coverageJson.exists())
114121
{
115-
throw new PipelineJobException("Missing file: " + coverageFile.getPath());
122+
throw new PipelineJobException("Missing file: " + coverageJson.getPath());
116123
}
124+
ctx.getFileManager().addIntermediateFile(coverageJson);
117125

118126
// Should produce a simple text file:
119127
// id path depth read length
120128
// TNPRC-IB18 ../IB18.cram 29.77 150
129+
File coverageFile = new File(ctx.getWorkingDirectory(), "coverage.txt");
130+
try (PrintWriter writer = PrintWriters.getPrintWriter(coverageFile); SamReader reader = SamReaderFactory.makeDefault().open(so.getFile()))
131+
{
132+
SAMFileHeader header = reader.getFileHeader();
133+
if (header.getReadGroups().size() == 0)
134+
{
135+
throw new PipelineJobException("No read groups found in input BAM");
136+
}
137+
else if (header.getReadGroups().size() > 1)
138+
{
139+
throw new PipelineJobException("More than one read group found in BAM");
140+
}
141+
142+
String rgId = header.getReadGroups().get(0).getSample();
143+
144+
JSONObject json = new JSONObject(FileUtils.readFileToString(coverageFile, Charset.defaultCharset()));
145+
writer.println("id\tpath\tdepth\tread length");
146+
double depth = json.getJSONObject("autosome").getDouble("depth");
147+
double readLength = json.getInt("read_length");
148+
writer.println(rgId + "\t" + so.getFile().getPath() + "\t" + depth + "\t" + readLength);
149+
}
150+
catch (IOException e)
151+
{
152+
throw new PipelineJobException(e);
153+
}
154+
ctx.getFileManager().addIntermediateFile(coverageFile);
121155

122156
List<String> paragraphArgs = new ArrayList<>();
123157
paragraphArgs.add(AbstractCommandWrapper.resolveFileInPath("multigrmpy.py", null, true).getPath());

0 commit comments

Comments
 (0)