forked from BimberLab/DiscvrLabKeyModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSelectVariantsStep.java
More file actions
270 lines (239 loc) · 12.9 KB
/
SelectVariantsStep.java
File metadata and controls
270 lines (239 loc) · 12.9 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package org.labkey.sequenceanalysis.run.variant;
import htsjdk.samtools.util.Interval;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.json.JSONArray;
import org.json.JSONObject;
import org.labkey.api.exp.api.ExpData;
import org.labkey.api.exp.api.ExperimentService;
import org.labkey.api.pipeline.PipelineJob;
import org.labkey.api.pipeline.PipelineJobException;
import org.labkey.api.sequenceanalysis.pipeline.AbstractVariantProcessingStepProvider;
import org.labkey.api.sequenceanalysis.pipeline.CommandLineParam;
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.SequenceAnalysisJobSupport;
import org.labkey.api.sequenceanalysis.pipeline.ToolParameterDescriptor;
import org.labkey.api.sequenceanalysis.pipeline.VariantProcessingStep;
import org.labkey.api.sequenceanalysis.pipeline.VariantProcessingStepOutputImpl;
import org.labkey.api.sequenceanalysis.run.AbstractCommandPipelineStep;
import org.labkey.api.sequenceanalysis.run.SelectVariantsWrapper;
import org.labkey.sequenceanalysis.pipeline.SequenceTaskHelper;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.labkey.sequenceanalysis.run.variant.SelectSNVsStep.SELECT_TYPE_TO_EXCLUDE;
import static org.labkey.sequenceanalysis.run.variant.SelectSNVsStep.SELECT_TYPE_TO_INCLUDE;
/**
* User: bimber
* Date: 6/15/2014
* Time: 12:39 PM
*/
public class SelectVariantsStep extends AbstractCommandPipelineStep<SelectVariantsWrapper> implements VariantProcessingStep
{
public static String INTERVALS = "intervals";
public SelectVariantsStep(PipelineStepProvider<?> provider, PipelineContext ctx)
{
super(provider, ctx, new SelectVariantsWrapper(ctx.getLogger()));
}
public static class Provider extends AbstractVariantProcessingStepProvider<SelectVariantsStep> implements VariantProcessingStep.SupportsScatterGather
{
public Provider()
{
super("SelectVariantsStep", "GATK SelectVariants", "GATK", "Select variants using GATK SelectVariants", Arrays.asList(
ToolParameterDescriptor.create("selects", "Select Expressions", "Filter expressions that can be used to subset variants.", "sequenceanalysis-variantfilterpanel", new JSONObject(){{
put("mode", "SELECT");
put("showFilterName", false);
put("title", "Select Expressions");
}}, null),
ToolParameterDescriptor.createCommandLineParam(CommandLineParam.createSwitch("--exclude-filtered"), "excludeFiltered", "Exclude Filtered", "If selected, any filtered sites will be removed", "checkbox", null, null),
ToolParameterDescriptor.createCommandLineParam(CommandLineParam.createSwitch("--exclude-non-variants"), "excludeNonVariant", "Exclude Non-Variant", "If selected, any non-variant sites will be removed", "checkbox", null, null),
ToolParameterDescriptor.createCommandLineParam(CommandLineParam.createSwitch("--preserve-alleles"), "noTrim", "Preserve Original Alleles", "If selected, the all alleles from the input will be retained, even if not used by any remaining genotypes.", "checkbox", null, null),
ToolParameterDescriptor.createCommandLineParam(CommandLineParam.createSwitch("--remove-unused-alternates"), "trimAlternates", "Remove Unused Alternates", "If selected, any alternate alleles not used in any genotypes will be trimmed.", "checkbox", null, null),
ToolParameterDescriptor.createCommandLineParam(CommandLineParam.createSwitch("--sites-only-vcf-output"), "sitesOnly", "Sites Only VCF Output", "If selected, the resulting VCF will omit samples and include only the site information.", "checkbox", null, null),
ToolParameterDescriptor.createCommandLineParam(CommandLineParam.createSwitch("--allow-nonoverlapping-command-line-samples"), "allowNnonoverlappingSamples", "Allow non-overlapping Samples", "Normally the job will fail is samples are selected that do not exist in the VCF. If checked, this will be allowed.", "checkbox", null, null),
ToolParameterDescriptor.createCommandLineParam(CommandLineParam.createSwitch("--set-filtered-gt-to-nocall"), "setFilteredGtToNocall", "Set Filtered Genotypes to No-Call", "If selected, any filtered genotypes will be converted to no-call.", "checkbox", new JSONObject(){{
put("checked", true);
}}, true),
ToolParameterDescriptor.create(SELECT_TYPE_TO_INCLUDE, "Select Type(s) To Include", "Only variants of the selected type(s) will be included", "ldk-simplecombo", new JSONObject(){{
put("storeValues", SelectSNVsStep.getSelectTypes());
put("multiSelect", true);
}}, "SNV"),
ToolParameterDescriptor.create(SELECT_TYPE_TO_EXCLUDE, "Select Type(s) To Exclude", "Variants of the selected type(s) will be excluded", "ldk-simplecombo", new JSONObject(){{
put("storeValues", SelectSNVsStep.getSelectTypes());
put("multiSelect", true);
}}, null),
new IntervalParameterDescriptor(),
ToolParameterDescriptor.create(SelectSamplesStep.SAMPLE_INCLUDE, "Select Sample(s) Include", "Only variants of the selected type(s) will be included", "sequenceanalysis-trimmingtextarea", null, null),
ToolParameterDescriptor.create(SelectSamplesStep.SAMPLE_EXCLUDE, "Select Samples(s) To Exclude", "Variants of the selected type(s) will be excluded", "sequenceanalysis-trimmingtextarea", null, null)
), Arrays.asList("sequenceanalysis/panel/VariantFilterPanel.js", "sequenceanalysis/panel/IntervalPanel.js", "/sequenceanalysis/field/TrimmingTextArea.js"), "");
}
@Override
public SelectVariantsStep create(PipelineContext ctx)
{
return new SelectVariantsStep(this, ctx);
}
}
@Override
public Output processVariants(File inputVCF, File outputDirectory, ReferenceGenome genome, @Nullable List<Interval> intervals) throws PipelineJobException
{
VariantProcessingStepOutputImpl output = new VariantProcessingStepOutputImpl();
List<String> options = new ArrayList<>();
String toInclude = getProvider().getParameterByName(SELECT_TYPE_TO_INCLUDE).extractValue(getPipelineCtx().getJob(), getProvider(), getStepIdx(), String.class);
addSelectTypeOptions(toInclude, options, "--select-type-to-include");
String toExclude = getProvider().getParameterByName(SELECT_TYPE_TO_EXCLUDE).extractValue(getPipelineCtx().getJob(), getProvider(), getStepIdx(), String.class);
addSelectTypeOptions(toExclude, options, "--select-type-to-exclude");
String samplesToInclude = getProvider().getParameterByName(SelectSamplesStep.SAMPLE_INCLUDE).extractValue(getPipelineCtx().getJob(), getProvider(), getStepIdx(), String.class);
SelectVariantsStep.addSubjectSelectOptions(samplesToInclude, options, "-sn");
String samplesToExclude = getProvider().getParameterByName(SelectSamplesStep.SAMPLE_EXCLUDE).extractValue(getPipelineCtx().getJob(), getProvider(), getStepIdx(), String.class);
SelectVariantsStep.addSubjectSelectOptions(samplesToExclude, options, "-xl-sn");
//intervals:
String intervalText = getProvider().getParameterByName(INTERVALS).extractValue(getPipelineCtx().getJob(), getProvider(), getStepIdx(), String.class, null);
options.addAll(getIntervalOptions(intervalText, getPipelineCtx().getSequenceSupport()));
//JEXL
String selectText = getProvider().getParameterByName("selects").extractValue(getPipelineCtx().getJob(), getProvider(), getStepIdx(), String.class, null);
if (selectText != null)
{
JSONArray filterArr = new JSONArray(selectText);
for (int i = 0; i < filterArr.length(); i++)
{
JSONArray arr = filterArr.getJSONArray(i);
if (arr.length() < 2)
{
throw new PipelineJobException("Improper select expression: " + filterArr.getString(i));
}
options.add("-select");
options.add(arr.getString(1));
//if (arr.length() > 2 && arr.optBoolean(2))
//{
// params.add("-invfilter");
//}
}
}
options.addAll(getClientCommandArgs());
if (intervals != null)
{
intervals.forEach(interval -> {
options.add("-L");
options.add(interval.getContig() + ":" + interval.getStart() + "-" + interval.getEnd());
});
}
File outputVcf = new File(outputDirectory, SequenceTaskHelper.getUnzippedBaseName(inputVCF) + ".selectVariants.vcf.gz");
getWrapper().execute(genome.getWorkingFastaFile(), inputVCF, outputVcf, options);
if (!outputVcf.exists())
{
throw new PipelineJobException("output not found: " + outputVcf);
}
output.setVcf(outputVcf);
return output;
}
public static void addSubjectSelectOptions(String text, List<String> options, String argName)
{
text = StringUtils.trimToNull(text);
if (text != null)
{
String[] names = text.split(";");
for (String name : names)
{
options.add(argName);
options.add(name);
}
}
}
public static void addSelectTypeOptions(String text, List<String> options, String argName)
{
if (StringUtils.trimToNull(text) != null)
{
if (text.startsWith("["))
{
JSONArray arr = new JSONArray(text);
for (Object o : arr.toList())
{
if (o == null)
{
continue;
}
options.add(argName);
options.add(o.toString());
}
}
else
{
for (String s : StringUtils.split(text, ";"))
{
options.add(argName);
options.add(s);
}
}
}
}
private static class IntervalParameterDescriptor extends ToolParameterDescriptor implements ToolParameterDescriptor.CachableParam
{
public IntervalParameterDescriptor()
{
super(null, INTERVALS, "Intervals", "Only variants spanning these intervals will be included", "sequenceanalysis-intervalpanel", null, null);
}
@Override
public void doCache(PipelineJob job, Object value, SequenceAnalysisJobSupport support) throws PipelineJobException
{
job.getLogger().debug("caching param " + getName() + ": " + value);
if (value != null)
{
JSONObject json;
if (!(value instanceof JSONObject))
{
json = new JSONObject(value.toString());
}
else
{
json = (JSONObject)value;
}
if (!json.isNull("fileId"))
{
ExpData d = ExperimentService.get().getExpData(json.getInt("fileId"));
if (d != null)
{
support.cacheExpData(d);
}
}
}
}
}
public static List<String> getIntervalOptions(String intervalText, SequenceAnalysisJobSupport support) throws PipelineJobException
{
List<String> options = new ArrayList<>();
if (intervalText != null)
{
JSONObject intervalJson = new JSONObject(intervalText);
if (intervalJson.get("source") == null)
{
throw new PipelineJobException("Improper interval data: " + intervalText);
}
if (!intervalJson.isNull("fileId"))
{
File d = support.getCachedData(intervalJson.getInt("fileId"));
if (d.exists())
{
options.add("-L");
options.add(d.getPath());
}
else
{
throw new PipelineJobException("Unable to find file: " + d.getPath());
}
}
else if (!intervalJson.isNull("intervals"))
{
String[] intervals = intervalJson.getString("intervals").split(";");
for (String i : intervals)
{
options.add("-L");
options.add(i);
}
}
}
return options;
}
}