forked from BimberLab/DiscvrLabKeyModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSampleRenameStep.java
More file actions
175 lines (151 loc) · 7.52 KB
/
SampleRenameStep.java
File metadata and controls
175 lines (151 loc) · 7.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
package org.labkey.sequenceanalysis.run.variant;
import htsjdk.samtools.util.CloseableIterator;
import htsjdk.samtools.util.Interval;
import htsjdk.variant.utils.SAMSequenceDictionaryExtractor;
import htsjdk.variant.variantcontext.VariantContext;
import htsjdk.variant.variantcontext.writer.Options;
import htsjdk.variant.variantcontext.writer.VariantContextWriter;
import htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder;
import htsjdk.variant.vcf.VCFFileReader;
import htsjdk.variant.vcf.VCFHeader;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.json.JSONObject;
import org.labkey.api.pipeline.PipelineJobException;
import org.labkey.api.sequenceanalysis.SequenceAnalysisService;
import org.labkey.api.sequenceanalysis.pipeline.AbstractVariantProcessingStepProvider;
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.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.VariantFiltrationWrapper;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by bimber on 3/28/2017.
*/
public class SampleRenameStep extends AbstractCommandPipelineStep<VariantFiltrationWrapper> implements VariantProcessingStep
{
public SampleRenameStep(PipelineStepProvider provider, PipelineContext ctx)
{
super(provider, ctx, new VariantFiltrationWrapper(ctx.getLogger()));
}
public static class Provider extends AbstractVariantProcessingStepProvider<SampleRenameStep> implements VariantProcessingStep.SupportsScatterGather
{
public Provider()
{
super("SampleRenameStep", "Rename Samples", "GATK", "Rename samples in a VCF file", Arrays.asList(
ToolParameterDescriptor.create("sampleMap", "Sample Rename", "A list of the pairs of samples to rename. It should contain two entries per line, the first being the original sample name and the second being the name to replace it. They sould be separated by a comma.", "textarea", new JSONObject(){{
put("height", 200);
put("allowBlank", false);
}}, null),
ToolParameterDescriptor.create("enforceChangeAll", "Change All Samples", "This is a check if you expect to change every sample in the VCF. If the VCF contains a sample name, but no updated name is provided, the job will fail.", "checkbox", null, false)
), null, "");
}
@Override
public SampleRenameStep create(PipelineContext ctx)
{
return new SampleRenameStep(this, ctx);
}
}
@Override
public Output processVariants(File inputVCF, File outputDirectory, ReferenceGenome genome, @Nullable List<Interval> intervals) throws PipelineJobException
{
boolean enforceChangeAll = getProvider().getParameterByName("enforceChangeAll").extractValue(getPipelineCtx().getJob(), getProvider(), getStepIdx(), Boolean.class, false);
String sampleMapString = getProvider().getParameterByName("sampleMap").extractValue(getPipelineCtx().getJob(), getProvider(), getStepIdx(), String.class);
Map<String, String> sampleMap = parseSampleMap(sampleMapString);
File outputFile = new File(outputDirectory, SequenceAnalysisService.get().getUnzippedBaseName(inputVCF.getName()) + ".renamed.vcf.gz");
VariantContextWriterBuilder builder = new VariantContextWriterBuilder();
builder.setReferenceDictionary(SAMSequenceDictionaryExtractor.extractDictionary(genome.getSequenceDictionary()));
builder.setOutputFile(outputFile);
builder.setOption(Options.USE_ASYNC_IO);
try (VCFFileReader reader = new VCFFileReader(inputVCF); VariantContextWriter writer = builder.build())
{
VCFHeader header = reader.getFileHeader();
List<String> samples = header.getGenotypeSamples();
List<String> remappedSamples = new ArrayList<>();
int totalRenamed = 0;
for (String sample : samples)
{
if (sampleMap.containsKey(sample))
{
remappedSamples.add(sampleMap.get(sample));
totalRenamed++;
}
else if (enforceChangeAll)
{
throw new PipelineJobException("No alternate name provided for sample: " + sample);
}
else
{
remappedSamples.add(sample);
}
}
getPipelineCtx().getLogger().info("renamed " + totalRenamed + " of " + samples.size() + " samples");
if (remappedSamples.size() != samples.size())
{
throw new PipelineJobException("The number of renamed samples does not equal starting samples: " + samples.size() + " / " + remappedSamples.size());
}
writer.writeHeader(new VCFHeader(header.getMetaDataInInputOrder(), remappedSamples));
List<Interval> queryIntervals = intervals;
if (queryIntervals == null || queryIntervals.isEmpty())
{
queryIntervals = Collections.singletonList(null);
}
int i = 0;
for (Interval interval : queryIntervals)
{
try (CloseableIterator<VariantContext> it = (interval == null ? reader.iterator() : reader.query(interval.getContig(), interval.getStart(), interval.getEnd())))
{
while (it.hasNext())
{
i++;
if (i % 100000 == 0)
{
getPipelineCtx().getLogger().info("processed " + i + " variants");
}
writer.add(it.next());
}
}
}
}
VariantProcessingStepOutputImpl output = new VariantProcessingStepOutputImpl();
output.addInput(inputVCF, "Input VCF");
output.addInput(genome.getWorkingFastaFile(), "Reference Genome");
output.addOutput(outputFile, "Renamed VCF");
output.setVcf(outputFile);
return output;
}
private Map<String, String> parseSampleMap(String sampleMapString) throws PipelineJobException
{
sampleMapString = StringUtils.trimToNull(sampleMapString);
if (sampleMapString == null)
{
throw new PipelineJobException("No sample map provided");
}
Map<String, String> ret = new HashMap<>();
for (String line : sampleMapString.split("\\r?\\n"))
{
line = StringUtils.trimToNull(line);
if (line == null)
{
continue;
}
String[] tokens = line.split(",");
if (tokens.length != 2)
{
throw new PipelineJobException("Improper line: [" + line + "]. Each line should contain two IDs, separated by a comma");
}
ret.put(StringUtils.trimToNull(tokens[0]), StringUtils.trimToNull(tokens[1]));
}
return ret;
}
}