forked from BimberLab/DiscvrLabKeyModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSawfishAnalysis.java
More file actions
163 lines (135 loc) · 5.66 KB
/
SawfishAnalysis.java
File metadata and controls
163 lines (135 loc) · 5.66 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
package org.labkey.sequenceanalysis.run.analysis;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;
import org.labkey.api.pipeline.PipelineJobException;
import org.labkey.api.sequenceanalysis.model.AnalysisModel;
import org.labkey.api.sequenceanalysis.model.Readset;
import org.labkey.api.sequenceanalysis.pipeline.AbstractAnalysisStepProvider;
import org.labkey.api.sequenceanalysis.pipeline.AbstractPipelineStep;
import org.labkey.api.sequenceanalysis.pipeline.AnalysisOutputImpl;
import org.labkey.api.sequenceanalysis.pipeline.AnalysisStep;
import org.labkey.api.sequenceanalysis.pipeline.PipelineContext;
import org.labkey.api.sequenceanalysis.pipeline.PipelineStepProvider;
import org.labkey.api.sequenceanalysis.pipeline.ReferenceGenome;
import org.labkey.api.sequenceanalysis.pipeline.SamtoolsIndexer;
import org.labkey.api.sequenceanalysis.pipeline.SamtoolsRunner;
import org.labkey.api.sequenceanalysis.pipeline.SequencePipelineService;
import org.labkey.api.sequenceanalysis.run.SimpleScriptWrapper;
import org.labkey.sequenceanalysis.util.SequenceUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class SawfishAnalysis extends AbstractPipelineStep implements AnalysisStep
{
public SawfishAnalysis(PipelineStepProvider<?> provider, PipelineContext ctx)
{
super(provider, ctx);
}
public static class Provider extends AbstractAnalysisStepProvider<SawfishAnalysis>
{
public Provider()
{
super("sawfish", "Sawfish Analysis", null, "This will run sawfish SV dicvoery and calling on the selected BAMs", List.of(), null, null);
}
@Override
public SawfishAnalysis create(PipelineContext ctx)
{
return new SawfishAnalysis(this, ctx);
}
}
@Override
public Output performAnalysisPerSampleRemote(Readset rs, File inputBam, ReferenceGenome referenceGenome, File outputDir) throws PipelineJobException
{
AnalysisOutputImpl output = new AnalysisOutputImpl();
File inputFile = inputBam;
if (SequenceUtil.FILETYPE.cram.getFileType().isType(inputFile))
{
CramToBam samtoolsRunner = new CramToBam(getPipelineCtx().getLogger());
File bam = new File(getPipelineCtx().getWorkingDirectory(), inputFile.getName().replaceAll(".cram$", ".bam"));
File bamIdx = new File(bam.getPath() + ".bai");
if (!bamIdx.exists())
{
samtoolsRunner.convert(inputFile, bam, referenceGenome.getWorkingFastaFile(), SequencePipelineService.get().getMaxThreads(getPipelineCtx().getLogger()));
new SamtoolsIndexer(getPipelineCtx().getLogger()).execute(bam);
}
else
{
getPipelineCtx().getLogger().debug("BAM index exists, will not re-convert CRAM");
}
inputFile = bam;
output.addIntermediateFile(bam);
output.addIntermediateFile(bamIdx);
}
List<String> args = new ArrayList<>();
args.add(getExe().getPath());
args.add("discover");
args.add("--bam");
args.add(inputFile.getPath());
args.add("--ref");
args.add(referenceGenome.getWorkingFastaFile().getPath());
File svOutDir = new File(outputDir, "sawfish");
args.add("--output-dir");
args.add(svOutDir.getPath());
Integer maxThreads = SequencePipelineService.get().getMaxThreads(getPipelineCtx().getLogger());
if (maxThreads != null)
{
args.add("--threads");
args.add(String.valueOf(maxThreads));
}
File bcf = new File(svOutDir, "candidate.sv.bcf");
File bcfIdx = new File(bcf.getPath() + ".csi");
if (bcfIdx.exists())
{
getPipelineCtx().getLogger().debug("BCF index already exists, reusing output");
}
else
{
new SimpleScriptWrapper(getPipelineCtx().getLogger()).execute(args);
}
if (!bcf.exists())
{
throw new PipelineJobException("Unable to find file: " + bcf.getPath());
}
output.addSequenceOutput(bcf, rs.getName() + ": sawfish", "Sawfish SV Discovery", rs.getReadsetId(), null, referenceGenome.getGenomeId(), null);
return output;
}
@Override
public Output performAnalysisPerSampleLocal(AnalysisModel model, File inputBam, File referenceFasta, File outDir) throws PipelineJobException
{
return null;
}
private File getExe()
{
return SequencePipelineService.get().getExeForPackage("SAWFISHPATH", "sawfish");
}
private static class CramToBam extends SamtoolsRunner
{
public CramToBam(Logger log)
{
super(log);
}
public void convert(File inputCram, File outputBam, File fasta, @Nullable Integer threads) throws PipelineJobException
{
getLogger().info("Converting CRAM to BAM");
execute(getParams(inputCram, outputBam, fasta, threads));
}
private List<String> getParams(File inputCram, File outputBam, File fasta, @Nullable Integer threads)
{
List<String> params = new ArrayList<>();
params.add(getSamtoolsPath().getPath());
params.add("view");
params.add("-b");
params.add("-T");
params.add(fasta.getPath());
params.add("-o");
params.add(outputBam.getPath());
if (threads != null)
{
params.add("-@");
params.add(String.valueOf(threads));
}
params.add(inputCram.getPath());
return params;
}
}
}