Skip to content

Commit 4517fce

Browse files
committed
Bugfix to SamtoolsCramConverter
1 parent 53bfea1 commit 4517fce

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public SamtoolsCramConverter(Logger log)
2121
public File convert(File inputBam, File outputCram, File gzippedFasta, boolean doIndex, @Nullable Integer threads, boolean archivalMode) throws PipelineJobException
2222
{
2323
getLogger().info("Converting SAM/BAM to CRAM: " + inputBam.getPath());
24+
if (inputBam.equals(outputCram))
25+
{
26+
throw new PipelineJobException("Input/output files are the same");
27+
}
2428

2529
List<String> params = new ArrayList<>();
2630
params.add(getSamtoolsPath().getPath());

SequenceAnalysis/src/org/labkey/sequenceanalysis/pipeline/ConvertToCramHandler.java

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -113,29 +113,30 @@ public void processFilesRemote(List<SequenceOutputFile> inputFiles, JobContext c
113113
for (SequenceOutputFile so : inputFiles)
114114
{
115115
ReferenceGenome genome = ctx.getSequenceSupport().getCachedGenome(so.getLibrary_id());
116-
File cram = new File(so.getFile().getParentFile(), FileUtil.getBaseName(so.getFile()) + ".cram");
117-
File cramIdx = SamtoolsCramConverter.getExpectedCramIndex(cram);
116+
File outputFile = new File(ctx.getWorkingDirectory(), FileUtil.getBaseName(so.getFile()) + ".cram");
118117
if (!so.getFile().exists())
119118
{
120-
if (replaceOriginal && cramIdx.exists())
119+
File inputAsCram = new File(so.getFile().getParentFile(), FileUtil.getBaseName(so.getFile()) + ".cram");
120+
File inputAsCramIdx = SamtoolsCramConverter.getExpectedCramIndex(inputAsCram);
121+
if (replaceOriginal && SequenceUtil.FILETYPE.bam.getFileType().isType(so.getFile()) && inputAsCram.exists() && inputAsCramIdx.exists())
121122
{
122123
ctx.getLogger().debug("BAM does not exist, but CRAM index does. Proceeding on the assumption this is a resume of a failed job.");
123124
}
124125
else
125126
{
126-
throw new PipelineJobException("Unable to find BAM: " + so.getFile().getPath());
127+
throw new PipelineJobException("Unable to find input CRAM/BAM: " + so.getFile().getPath());
127128
}
128129
}
129130
else
130131
{
131-
new SamtoolsCramConverter(ctx.getLogger()).convert(so.getFile(), cram, genome.getWorkingFastaFileGzipped(), true, threads, doCramArchivalMode);
132+
new SamtoolsCramConverter(ctx.getLogger()).convert(so.getFile(), outputFile, genome.getWorkingFastaFileGzipped(), true, threads, doCramArchivalMode);
132133
}
133134

134135
checkCramAndIndex(so);
135136

136137
if (replaceOriginal)
137138
{
138-
ctx.getLogger().info("Deleting original BAM: " + so.getFile().getPath());
139+
ctx.getLogger().info("Deleting original BAM/CRAM: {}", so.getFile().getPath());
139140
if (so.getFile().exists())
140141
{
141142
SequenceAnalysisService.get().getExpectedBamOrCramIndex(so.getFile()).delete();
@@ -174,6 +175,7 @@ public void complete(JobContext ctx, List<SequenceOutputFile> inputs, List<Seque
174175
return(row);
175176
}).collect(Collectors.toList());
176177

178+
boolean doCramArchivalMode = ctx.getParams().optBoolean("doCramArchivalMode", false);
177179
for (SequenceOutputFile so : inputs)
178180
{
179181
File cram = new File(so.getFile().getParentFile(), FileUtil.getBaseName(so.getFile()) + ".cram");
@@ -185,26 +187,43 @@ public void complete(JobContext ctx, List<SequenceOutputFile> inputs, List<Seque
185187
d.setName(cram.getName());
186188
d.save(ctx.getJob().getUser());
187189

190+
Map<String, Object> row = new CaseInsensitiveHashMap<>();
191+
row.put("rowid", so.getRowid());
192+
row.put("container", so.getContainer());
193+
boolean doUpdate = false;
194+
String description = so.getDescription();
188195
if (so.getName().contains(".bam"))
189196
{
190-
Map<String, Object> row = new CaseInsensitiveHashMap<>();
191-
row.put("rowid", so.getRowid());
192-
row.put("container", so.getContainer());
193197
row.put("name", so.getName().replaceAll("\\.bam", "\\.cram"));
194-
row.put("description", (so.getDescription() == null ? "" : so.getDescription() + "\n") + "Converted from BAM to CRAM");
198+
description = (description == null ? "" : description + "\n") + "Converted from BAM to CRAM";
199+
row.put("description", description);
200+
doUpdate = true;
201+
}
202+
203+
if (doCramArchivalMode)
204+
{
205+
description = (description == null ? "" : description + "\n") + "CRAM Archival Mode";
206+
row.put("description", description);
207+
doUpdate = true;
208+
}
209+
210+
if (doUpdate)
211+
{
195212
toUpdate.add(row);
196213
}
197214
}
198215

199-
try
200-
{
201-
Container target = ctx.getJob().getContainer().isWorkbook() ? ctx.getJob().getContainer().getParent() : ctx.getJob().getContainer();
202-
QueryService.get().getUserSchema(ctx.getJob().getUser(), target, SequenceAnalysisSchema.SCHEMA_NAME).getTable(SequenceAnalysisSchema.TABLE_OUTPUTFILES).getUpdateService().updateRows(ctx.getJob().getUser(), target, toUpdate, oldKeys, null, null);
203-
}
204-
catch (QueryUpdateServiceException | InvalidKeyException | BatchValidationException | SQLException e)
216+
if (!toUpdate.isEmpty())
205217
{
206-
throw new PipelineJobException(e);
207-
218+
try
219+
{
220+
Container target = ctx.getJob().getContainer().isWorkbook() ? ctx.getJob().getContainer().getParent() : ctx.getJob().getContainer();
221+
QueryService.get().getUserSchema(ctx.getJob().getUser(), target, SequenceAnalysisSchema.SCHEMA_NAME).getTable(SequenceAnalysisSchema.TABLE_OUTPUTFILES).getUpdateService().updateRows(ctx.getJob().getUser(), target, toUpdate, oldKeys, null, null);
222+
}
223+
catch (QueryUpdateServiceException | InvalidKeyException | BatchValidationException | SQLException e)
224+
{
225+
throw new PipelineJobException(e);
226+
}
208227
}
209228
}
210229
}

0 commit comments

Comments
 (0)