11package org .labkey .sequenceanalysis .analysis ;
22
3+ import com .google .common .io .Files ;
34import htsjdk .samtools .SAMFileHeader ;
45import htsjdk .samtools .SAMFileWriter ;
56import htsjdk .samtools .SAMFileWriterFactory ;
2223import org .labkey .api .sequenceanalysis .model .Readset ;
2324import org .labkey .api .sequenceanalysis .pipeline .AbstractParameterizedOutputHandler ;
2425import org .labkey .api .sequenceanalysis .pipeline .BcftoolsRunner ;
26+ import org .labkey .api .sequenceanalysis .pipeline .ReferenceGenome ;
2527import org .labkey .api .sequenceanalysis .pipeline .SequenceAnalysisJobSupport ;
2628import org .labkey .api .sequenceanalysis .pipeline .SequenceOutputHandler ;
2729import org .labkey .api .sequenceanalysis .pipeline .SequencePipelineService ;
@@ -111,6 +113,10 @@ else if (SequenceUtil.FILETYPE.gvcf.getFileType().isType(so.getFile()) | Sequenc
111113 {
112114 getAndValidateHeaderForVcf (so , newRsName );
113115 }
116+ else
117+ {
118+ throw new PipelineJobException ("Unexpected file type: " + so .getFile ().getPath ());
119+ }
114120
115121 ctx .getSequenceSupport ().cacheObject ("readsetId" , newRsName );
116122 }
@@ -207,6 +213,18 @@ private void reheaderVcf(SequenceOutputFile so, JobContext ctx, String newRsName
207213 String existingSample = header .getGenotypeSamples ().get (0 );
208214
209215 File sampleNamesFile = new File (ctx .getWorkingDirectory (), "sampleNames.txt" );
216+ if (!sampleNamesFile .exists ())
217+ {
218+ try
219+ {
220+ Files .touch (sampleNamesFile );
221+ }
222+ catch (IOException e )
223+ {
224+ throw new PipelineJobException (e );
225+ }
226+ }
227+
210228 try (PrintWriter writer = PrintWriters .getPrintWriter (sampleNamesFile , StandardOpenOption .APPEND ))
211229 {
212230 writer .println (newRsName );
@@ -225,11 +243,19 @@ private void reheaderVcf(SequenceOutputFile so, JobContext ctx, String newRsName
225243 try
226244 {
227245 File outputIdx = SequenceAnalysisService .get ().ensureVcfIndex (outputVcf , ctx .getLogger (), false );
228- FileUtils .moveFile (outputVcf , so .getFile (), StandardCopyOption .REPLACE_EXISTING );
246+ if (so .getFile ().exists ())
247+ {
248+ so .getFile ().delete ();
249+ }
250+ FileUtils .moveFile (outputVcf , so .getFile ());
229251
230252 FileType gz = new FileType (".gz" );
231253 File inputIndex = gz .isType (so .getFile ()) ? new File (so .getFile ().getPath () + ".tbi" ) : new File (so .getFile ().getPath () + FileExtensions .TRIBBLE_INDEX );
232- FileUtils .moveFile (outputIdx , inputIndex , StandardCopyOption .REPLACE_EXISTING );
254+ if (inputIndex .exists ())
255+ {
256+ inputIndex .delete ();
257+ }
258+ FileUtils .moveFile (outputIdx , inputIndex );
233259
234260 addTracker (so , existingSample , newRsName );
235261 }
@@ -243,6 +269,11 @@ private void addTracker(SequenceOutputFile so, String existingSample, String new
243269 {
244270 File tracker = new File (so .getFile ().getParentFile (), "reheaderHistory.txt" );
245271 boolean preExisting = tracker .exists ();
272+ if (!preExisting )
273+ {
274+ Files .touch (tracker );
275+ }
276+
246277 try (PrintWriter writer = PrintWriters .getPrintWriter (tracker , StandardOpenOption .APPEND ))
247278 {
248279 if (!preExisting )
@@ -279,20 +310,36 @@ private void reheaderBamOrCram(SequenceOutputFile so, JobContext ctx, String new
279310 throw new PipelineJobException ("Expected header was not created: " + headerBam .getPath ());
280311 }
281312
313+ ReferenceGenome rg = ctx .getSequenceSupport ().getCachedGenome (so .getLibrary_id ());
314+ if (rg == null )
315+ {
316+ throw new PipelineJobException ("Unable to find genome: " + so .getLibrary_id ());
317+ }
318+
282319 ctx .getFileManager ().addIntermediateFile (headerBam );
283320 ctx .getFileManager ().addIntermediateFile (SequencePipelineService .get ().getExpectedIndex (headerBam ));
284321
285322 File output = new File (ctx .getWorkingDirectory (), so .getFile ().getName ());
286- new ReplaceSamHeaderWrapper (ctx .getLogger ()).execute (so .getFile (), output , headerBam );
323+ new ReplaceSamHeaderWrapper (ctx .getLogger ()).execute (so .getFile (), output , headerBam , rg );
287324 if (!output .exists ())
288325 {
289326 throw new PipelineJobException ("Missing file: " + output .getPath ());
290327 }
291328
292329 File outputIdx = SequencePipelineService .get ().ensureBamIndex (output , ctx .getLogger (), false );
293330
294- FileUtils .moveFile (output , so .getFile (), StandardCopyOption .REPLACE_EXISTING );
295- FileUtils .moveFile (outputIdx , SequenceAnalysisService .get ().getExpectedBamOrCramIndex (so .getFile ()), StandardCopyOption .REPLACE_EXISTING );
331+ if (so .getFile ().exists ())
332+ {
333+ so .getFile ().delete ();
334+ }
335+ FileUtils .moveFile (output , so .getFile ());
336+
337+ File inputIndex = SequenceAnalysisService .get ().getExpectedBamOrCramIndex (so .getFile ());
338+ if (inputIndex .exists ())
339+ {
340+ inputIndex .delete ();
341+ }
342+ FileUtils .moveFile (outputIdx , inputIndex );
296343
297344 addTracker (so , existingSample , newRsName );
298345 }
@@ -315,7 +362,7 @@ protected String getToolName()
315362 return "ReplaceSamHeader" ;
316363 }
317364
318- public void execute (File input , File output , File headerBam ) throws PipelineJobException
365+ public void execute (File input , File output , File headerBam , ReferenceGenome genome ) throws PipelineJobException
319366 {
320367 List <String > params = new ArrayList <>(getBaseArgs ());
321368
@@ -328,6 +375,9 @@ public void execute(File input, File output, File headerBam) throws PipelineJobE
328375 params .add ("--HEADER" );
329376 params .add (headerBam .getPath ());
330377
378+ params .add ("-R" );
379+ params .add (genome .getWorkingFastaFile ().getPath ());
380+
331381 execute (params );
332382 }
333383 }
0 commit comments