forked from BimberLab/DiscvrLabKeyModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSawfishJointCallingHandler.java
More file actions
180 lines (150 loc) · 6.52 KB
/
SawfishJointCallingHandler.java
File metadata and controls
180 lines (150 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package org.labkey.sequenceanalysis.run.analysis;
import org.apache.commons.io.FileUtils;
import org.json.JSONObject;
import org.labkey.api.module.ModuleLoader;
import org.labkey.api.pipeline.PipelineJob;
import org.labkey.api.pipeline.PipelineJobException;
import org.labkey.api.pipeline.RecordedAction;
import org.labkey.api.sequenceanalysis.SequenceAnalysisService;
import org.labkey.api.sequenceanalysis.SequenceOutputFile;
import org.labkey.api.sequenceanalysis.pipeline.AbstractParameterizedOutputHandler;
import org.labkey.api.sequenceanalysis.pipeline.ReferenceGenome;
import org.labkey.api.sequenceanalysis.pipeline.SequenceAnalysisJobSupport;
import org.labkey.api.sequenceanalysis.pipeline.SequenceOutputHandler;
import org.labkey.api.sequenceanalysis.pipeline.SequencePipelineService;
import org.labkey.api.sequenceanalysis.pipeline.ToolParameterDescriptor;
import org.labkey.api.sequenceanalysis.run.SimpleScriptWrapper;
import org.labkey.sequenceanalysis.SequenceAnalysisModule;
import org.labkey.sequenceanalysis.util.SequenceUtil;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.stream.Collectors;
public class SawfishJointCallingHandler extends AbstractParameterizedOutputHandler<SequenceOutputHandler.SequenceOutputProcessor>
{
private static final String OUTPUT_CATEGORY = "Sawfish VCF";
public SawfishJointCallingHandler()
{
super(ModuleLoader.getInstance().getModule(SequenceAnalysisModule.NAME), "Sawfish Joint-Call", "Runs sawfish joint-call, which jointly calls SVs from PacBio CCS data", new LinkedHashSet<>(List.of("sequenceanalysis/panel/VariantScatterGatherPanel.js")), Arrays.asList(
ToolParameterDescriptor.create("fileName", "VCF Filename", "The name of the resulting file.", "textfield", new JSONObject(){{
put("allowBlank", false);
put("doNotIncludeInTemplates", true);
}}, null)
));
}
@Override
public boolean canProcess(SequenceOutputFile o)
{
return o.getFile() != null && SequenceUtil.FILETYPE.bcf.getFileType().isType(o.getFile());
}
@Override
public boolean doRunRemote()
{
return true;
}
@Override
public boolean doRunLocal()
{
return false;
}
@Override
public SequenceOutputProcessor getProcessor()
{
return new Processor();
}
public static class Processor implements SequenceOutputProcessor
{
@Override
public void processFilesOnWebserver(PipelineJob job, SequenceAnalysisJobSupport support, List<SequenceOutputFile> inputFiles, JSONObject params, File outputDir, List<RecordedAction> actions, List<SequenceOutputFile> outputsToCreate) throws UnsupportedOperationException, PipelineJobException
{
}
@Override
public void processFilesRemote(List<SequenceOutputFile> inputFiles, JobContext ctx) throws UnsupportedOperationException, PipelineJobException
{
List<File> filesToProcess = inputFiles.stream().map(SequenceOutputFile::getFile).collect(Collectors.toList());
ReferenceGenome genome = ctx.getSequenceSupport().getCachedGenomes().iterator().next();
String outputBaseName = ctx.getParams().getString("fileName");
if (!outputBaseName.toLowerCase().endsWith(".gz"))
{
outputBaseName = outputBaseName.replaceAll(".gz$", "");
}
if (!outputBaseName.toLowerCase().endsWith(".vcf"))
{
outputBaseName = outputBaseName.replaceAll(".vcf$", "");
}
File expectedFinalOutput = new File(ctx.getOutputDir(), outputBaseName + ".vcf.gz");
File ouputVcf = runSawfishCall(ctx, filesToProcess, genome, outputBaseName);
SequenceOutputFile so = new SequenceOutputFile();
so.setName("Sawfish call: " + outputBaseName);
so.setFile(ouputVcf);
so.setCategory(OUTPUT_CATEGORY);
so.setLibrary_id(genome.getGenomeId());
ctx.addSequenceOutput(so);
}
private File runSawfishCall(JobContext ctx, List<File> inputs, ReferenceGenome genome, String outputBaseName) throws PipelineJobException
{
if (inputs.isEmpty())
{
throw new PipelineJobException("No inputs provided");
}
List<String> args = new ArrayList<>();
args.add(getExe().getPath());
args.add("joint-call");
Integer maxThreads = SequencePipelineService.get().getMaxThreads(ctx.getLogger());
if (maxThreads != null)
{
args.add("--threads");
args.add(String.valueOf(maxThreads));
}
for (File sample : inputs)
{
args.add("--sample");
args.add(sample.getParentFile().getPath());
}
File outDir = new File(ctx.getOutputDir(), "sawfish");
args.add("--output-dir");
args.add(outDir.getPath());
new SimpleScriptWrapper(ctx.getLogger()).execute(args);
File vcfOut = new File(outDir, "genotyped.sv.vcf.gz");
if (!vcfOut.exists())
{
throw new PipelineJobException("Unable to find file: " + vcfOut.getPath());
}
File vcfOutFinal = new File(ctx.getOutputDir(), outputBaseName + ".vcf.gz");
try
{
if (vcfOutFinal.exists())
{
vcfOutFinal.delete();
}
FileUtils.moveFile(vcfOut, vcfOutFinal);
File targetIndex = new File(vcfOutFinal.getPath() + ".tbi");
if (targetIndex.exists())
{
targetIndex.delete();
}
File origIndex = new File(vcfOut.getPath() + ".tbi");
if (origIndex.exists())
{
FileUtils.moveFile(origIndex, targetIndex);
}
else
{
SequenceAnalysisService.get().ensureVcfIndex(vcfOutFinal, ctx.getLogger(), true);
}
}
catch (IOException e)
{
throw new PipelineJobException(e);
}
return vcfOutFinal;
}
private File getExe()
{
return SequencePipelineService.get().getExeForPackage("SAWFISHPATH", "sawfish");
}
}
}