Skip to content

Commit 8117d04

Browse files
committed
Allow many steps to operate on either BAM or CRAM
1 parent c69a9ae commit 8117d04

File tree

7 files changed

+41
-56
lines changed

7 files changed

+41
-56
lines changed

SequenceAnalysis/api-src/org/labkey/api/sequenceanalysis/pipeline/SamSorter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ else if (sortOrder != SAMFileHeader.SortOrder.coordinate)
9999
input.delete();
100100

101101
//note: if there is a pre-existing index, we need to delete this since it is out of date
102-
File idx = new File(input.getPath() + ".bai");
102+
File idx = SequencePipelineService.get().getExpectedIndex(output);
103103
if (idx.exists())
104104
{
105105
getLogger().debug("deleting old BAM index");

SequenceAnalysis/api-src/org/labkey/api/sequenceanalysis/pipeline/SamtoolsIndexer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public SamtoolsIndexer(Logger log)
2222

2323
public File execute(File inputBam) throws PipelineJobException
2424
{
25-
getLogger().info("Sorting SAM/BAM: " + inputBam.getPath());
25+
getLogger().info("Indexing SAM/BAM: " + inputBam.getPath());
2626

27-
File idx = new File(inputBam.getPath() + ".bai");
27+
File idx = SequencePipelineService.get().getExpectedIndex(inputBam);
2828
if (idx.exists())
2929
{
3030
getLogger().debug("deleting existing index: " + idx.getPath());

SequenceAnalysis/api-src/org/labkey/api/sequenceanalysis/pipeline/SequencePipelineService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ static public void setInstance(SequencePipelineService instance)
112112

113113
abstract public long getLineCount(File f) throws PipelineJobException;
114114

115+
abstract public File getExpectedIndex(File bamOrCram);
116+
115117
abstract public File ensureBamIndex(File f, Logger log, boolean forceDeleteExisting) throws PipelineJobException;
116118

117119
abstract public SAMFileHeader.SortOrder getBamSortOrder(File bam) throws IOException;

SequenceAnalysis/api-src/org/labkey/api/sequenceanalysis/pipeline/SortSamWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public File execute(File inputFile, @Nullable File outputFile, SAMFileHeader.Sor
6161
FileUtils.moveFile(outputBam, inputFile);
6262

6363
//note: if there is a pre-existing index, we need to delete this since it is out of date
64-
File expectedIndex = new File(inputFile.getPath() + ".bai");
64+
File expectedIndex = SequencePipelineService.get().getExpectedIndex(inputFile);
6565
if (expectedIndex.exists())
6666
{
6767
getLogger().info("deleting out of date index: " + expectedIndex.getPath());

SequenceAnalysis/src/org/labkey/sequenceanalysis/SequencePipelineServiceImpl.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.labkey.api.sequenceanalysis.pipeline.PipelineStepCtx;
1818
import org.labkey.api.sequenceanalysis.pipeline.PipelineStepProvider;
1919
import org.labkey.api.sequenceanalysis.pipeline.PreprocessingStep;
20+
import org.labkey.api.sequenceanalysis.pipeline.SamtoolsIndexer;
2021
import org.labkey.api.sequenceanalysis.pipeline.SequencePipelineService;
2122
import org.labkey.api.sequenceanalysis.pipeline.TaskFileManager;
2223
import org.labkey.api.sequenceanalysis.run.AbstractCommandWrapper;
@@ -437,10 +438,16 @@ public long getLineCount(File f) throws PipelineJobException
437438
return SequenceUtil.getLineCount(f);
438439
}
439440

441+
@Override
442+
public File getExpectedIndex(File bamOrCram)
443+
{
444+
return SequenceUtil.getExpectedIndex(bamOrCram);
445+
}
446+
440447
@Override
441448
public File ensureBamIndex(File inputBam, Logger log, boolean forceDeleteExisting) throws PipelineJobException
442449
{
443-
File expectedIndex = new File(inputBam.getPath() + ".bai");
450+
File expectedIndex = SequenceUtil.getExpectedIndex(inputBam);
444451
if (expectedIndex.exists() && (expectedIndex.lastModified() < inputBam.lastModified() || forceDeleteExisting))
445452
{
446453
log.info("deleting out of date index: " + expectedIndex.getPath());
@@ -449,10 +456,8 @@ public File ensureBamIndex(File inputBam, Logger log, boolean forceDeleteExistin
449456

450457
if (!expectedIndex.exists())
451458
{
452-
log.debug("\tcreating temp index for BAM: " + inputBam.getName());
453-
BuildBamIndexWrapper buildBamIndexWrapper = new BuildBamIndexWrapper(log);
454-
buildBamIndexWrapper.setStringency(ValidationStringency.SILENT);
455-
buildBamIndexWrapper.executeCommand(inputBam);
459+
log.debug("\tcreating index for BAM: " + inputBam.getName());
460+
new SamtoolsIndexer(log).execute(inputBam);
456461

457462
return expectedIndex;
458463
}

SequenceAnalysis/src/org/labkey/sequenceanalysis/run/util/HaplotypeCallerWrapper.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import org.apache.commons.io.FileUtils;
44
import org.apache.logging.log4j.Logger;
55
import org.labkey.api.pipeline.PipelineJobException;
6+
import org.labkey.api.sequenceanalysis.pipeline.SamtoolsIndexer;
67
import org.labkey.api.sequenceanalysis.run.AbstractGatk4Wrapper;
78
import org.labkey.sequenceanalysis.pipeline.ReblockGvcfHandler;
9+
import org.labkey.sequenceanalysis.util.SequenceUtil;
810

911
import java.io.File;
1012
import java.io.IOException;
@@ -21,25 +23,22 @@ public HaplotypeCallerWrapper(Logger log)
2123
super(log);
2224
}
2325

24-
public void execute(File inputBam, File referenceFasta, File outputFile, List<String> options) throws PipelineJobException
26+
public void execute(File inputBamOrCram, File referenceFasta, File outputFile, List<String> options) throws PipelineJobException
2527
{
26-
execute(inputBam, referenceFasta, outputFile, options, true);
28+
execute(inputBamOrCram, referenceFasta, outputFile, options, true);
2729
}
2830

29-
public void execute(File inputBam, File referenceFasta, File outputFile, List<String> options, boolean reblockGVCF) throws PipelineJobException
31+
public void execute(File inputBamOrCram, File referenceFasta, File outputFile, List<String> options, boolean reblockGVCF) throws PipelineJobException
3032
{
31-
getLogger().info("Running GATK 4 HaplotypeCaller for: " + inputBam.getName());
33+
getLogger().info("Running GATK 4 HaplotypeCaller for: " + inputBamOrCram.getName());
3234

3335
ensureDictionary(referenceFasta);
3436

35-
File expectedIndex = new File(inputBam.getPath() + ".bai");
36-
boolean doDeleteIndex = false;
37+
File expectedIndex = SequenceUtil.getExpectedIndex(inputBamOrCram);
3738
if (!expectedIndex.exists())
3839
{
39-
getLogger().debug("\tcreating temp index for BAM: " + inputBam.getName());
40-
new BuildBamIndexWrapper(getLogger()).executeCommand(inputBam);
41-
42-
doDeleteIndex = true;
40+
getLogger().debug("\tcreating index for BAM: " + inputBamOrCram.getName());
41+
new SamtoolsIndexer(getLogger()).execute(inputBamOrCram);
4342
}
4443
else
4544
{
@@ -51,7 +50,7 @@ public void execute(File inputBam, File referenceFasta, File outputFile, List<St
5150
args.add("-R");
5251
args.add(referenceFasta.getPath());
5352
args.add("-I");
54-
args.add(inputBam.getPath());
53+
args.add(inputBamOrCram.getPath());
5554
args.add("-O");
5655
args.add(outputFile.getPath());
5756
if (options != null)
@@ -71,12 +70,6 @@ public void execute(File inputBam, File referenceFasta, File outputFile, List<St
7170
throw new PipelineJobException("Expected output not found: " + outputFile.getPath());
7271
}
7372

74-
if (doDeleteIndex)
75-
{
76-
getLogger().debug("\tdeleting temp BAM index: " + expectedIndex.getPath());
77-
expectedIndex.delete();
78-
}
79-
8073
if (reblockGVCF)
8174
{
8275
getLogger().info("Running GATK 4 ReblockGVCF for: " + outputFile.getName());

SequenceAnalysis/src/org/labkey/sequenceanalysis/util/SequenceUtil.java

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public static enum FILETYPE
8282
fastq(Arrays.asList(".fastq", ".fq"), true),
8383
fasta(Arrays.asList(".fasta", ".fa", ".fna"), true),
8484
bam(".bam"),
85+
cram(".cram"),
8586
sff(".sff"),
8687
gtf(Collections.singletonList(".gtf"), true),
8788
gff(Arrays.asList(".gff", ".gff3"), true),
@@ -394,36 +395,6 @@ public static JSONArray getReadGroupsForBam(File bam) throws IOException
394395
return ret;
395396
}
396397

397-
public static void deleteBamAndIndex(File bam)
398-
{
399-
bam.delete();
400-
401-
File idx = new File(bam.getPath() + ".bai");
402-
if (idx.exists())
403-
{
404-
idx.delete();
405-
}
406-
}
407-
408-
public static void recreateOldBamIndex(File bam, boolean forceRecreate, @Nullable Logger log) throws PipelineJobException
409-
{
410-
File idx = new File(bam.getPath() + ".bai");
411-
412-
//delete out of date index
413-
if (idx.exists() && (forceRecreate || idx.lastModified() < bam.lastModified()))
414-
{
415-
if (log != null)
416-
log.info("deleting existing BAM index");
417-
418-
idx.delete();
419-
}
420-
421-
if (!idx.exists())
422-
{
423-
new BuildBamIndexWrapper(log).executeCommand(bam);
424-
}
425-
}
426-
427398
public static void sortROD(File input, Logger log, Integer startColumnIdx) throws IOException, PipelineJobException
428399
{
429400
boolean isCompressed = input.getPath().endsWith(".gz");
@@ -622,4 +593,18 @@ public static List<Interval> parseAndSortIntervals(String intervalString) throws
622593

623594
return intervals;
624595
}
596+
597+
public static File getExpectedIndex(File bamOrCram)
598+
{
599+
if (FILETYPE.bam.getFileType().isType(bamOrCram))
600+
{
601+
return new File(bamOrCram.getPath() + ".bai");
602+
}
603+
else if (FILETYPE.cram.getFileType().isType(bamOrCram))
604+
{
605+
return new File(bamOrCram.getPath() + ".crai");
606+
}
607+
608+
throw new IllegalArgumentException("Must provide either a .bam or .cram file");
609+
}
625610
}

0 commit comments

Comments
 (0)