2323import org .labkey .api .sequenceanalysis .model .Readset ;
2424import org .labkey .api .sequenceanalysis .pipeline .AbstractParameterizedOutputHandler ;
2525import org .labkey .api .sequenceanalysis .pipeline .BcftoolsRunner ;
26+ import org .labkey .api .sequenceanalysis .pipeline .ReferenceGenome ;
2627import org .labkey .api .sequenceanalysis .pipeline .SequenceAnalysisJobSupport ;
2728import org .labkey .api .sequenceanalysis .pipeline .SequenceOutputHandler ;
2829import org .labkey .api .sequenceanalysis .pipeline .SequencePipelineService ;
@@ -242,11 +243,19 @@ private void reheaderVcf(SequenceOutputFile so, JobContext ctx, String newRsName
242243 try
243244 {
244245 File outputIdx = SequenceAnalysisService .get ().ensureVcfIndex (outputVcf , ctx .getLogger (), false );
245- 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 ());
246251
247252 FileType gz = new FileType (".gz" );
248253 File inputIndex = gz .isType (so .getFile ()) ? new File (so .getFile ().getPath () + ".tbi" ) : new File (so .getFile ().getPath () + FileExtensions .TRIBBLE_INDEX );
249- FileUtils .moveFile (outputIdx , inputIndex , StandardCopyOption .REPLACE_EXISTING );
254+ if (inputIndex .exists ())
255+ {
256+ inputIndex .delete ();
257+ }
258+ FileUtils .moveFile (outputIdx , inputIndex );
250259
251260 addTracker (so , existingSample , newRsName );
252261 }
@@ -301,20 +310,36 @@ private void reheaderBamOrCram(SequenceOutputFile so, JobContext ctx, String new
301310 throw new PipelineJobException ("Expected header was not created: " + headerBam .getPath ());
302311 }
303312
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+
304319 ctx .getFileManager ().addIntermediateFile (headerBam );
305320 ctx .getFileManager ().addIntermediateFile (SequencePipelineService .get ().getExpectedIndex (headerBam ));
306321
307322 File output = new File (ctx .getWorkingDirectory (), so .getFile ().getName ());
308- new ReplaceSamHeaderWrapper (ctx .getLogger ()).execute (so .getFile (), output , headerBam );
323+ new ReplaceSamHeaderWrapper (ctx .getLogger ()).execute (so .getFile (), output , headerBam , rg );
309324 if (!output .exists ())
310325 {
311326 throw new PipelineJobException ("Missing file: " + output .getPath ());
312327 }
313328
314329 File outputIdx = SequencePipelineService .get ().ensureBamIndex (output , ctx .getLogger (), false );
315330
316- FileUtils .moveFile (output , so .getFile (), StandardCopyOption .REPLACE_EXISTING );
317- 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 );
318343
319344 addTracker (so , existingSample , newRsName );
320345 }
@@ -337,7 +362,7 @@ protected String getToolName()
337362 return "ReplaceSamHeader" ;
338363 }
339364
340- 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
341366 {
342367 List <String > params = new ArrayList <>(getBaseArgs ());
343368
@@ -350,6 +375,9 @@ public void execute(File input, File output, File headerBam) throws PipelineJobE
350375 params .add ("--HEADER" );
351376 params .add (headerBam .getPath ());
352377
378+ params .add ("-R" );
379+ params .add (genome .getWorkingFastaFile ().getPath ());
380+
353381 execute (params );
354382 }
355383 }
0 commit comments