11package 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 ;
37import org .json .JSONObject ;
48import org .labkey .api .module .ModuleLoader ;
59import org .labkey .api .pipeline .PipelineJob ;
1519import org .labkey .api .sequenceanalysis .run .AbstractCommandWrapper ;
1620import org .labkey .api .sequenceanalysis .run .SimpleScriptWrapper ;
1721import org .labkey .api .util .FileUtil ;
22+ import org .labkey .api .writer .PrintWriters ;
1823import org .labkey .sequenceanalysis .SequenceAnalysisModule ;
1924import org .labkey .sequenceanalysis .util .SequenceUtil ;
2025
2126import java .io .File ;
2227import java .io .IOException ;
28+ import java .io .PrintWriter ;
29+ import java .nio .charset .Charset ;
2330import java .util .ArrayList ;
2431import java .util .Arrays ;
2532import 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\t path\t depth\t read 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