forked from BimberLab/DiscvrLabKeyModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLiftoverHandler.java
More file actions
423 lines (366 loc) · 18.3 KB
/
LiftoverHandler.java
File metadata and controls
423 lines (366 loc) · 18.3 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package org.labkey.sequenceanalysis.analysis;
import au.com.bytecode.opencsv.CSVWriter;
import htsjdk.samtools.liftover.LiftOver;
import htsjdk.samtools.util.Interval;
import htsjdk.tribble.AbstractFeatureReader;
import htsjdk.tribble.CloseableTribbleIterator;
import htsjdk.tribble.FeatureReader;
import htsjdk.tribble.annotation.Strand;
import htsjdk.tribble.bed.BEDCodec;
import htsjdk.tribble.bed.BEDFeature;
import htsjdk.tribble.bed.FullBEDFeature;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.json.JSONObject;
import org.labkey.api.data.Container;
import org.labkey.api.exp.api.ExpData;
import org.labkey.api.exp.api.ExperimentService;
import org.labkey.api.module.Module;
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.pipeline.file.FileAnalysisJobSupport;
import org.labkey.api.security.User;
import org.labkey.api.sequenceanalysis.SequenceAnalysisService;
import org.labkey.api.sequenceanalysis.SequenceOutputFile;
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.VariantProcessingStep;
import org.labkey.api.sequenceanalysis.run.SelectVariantsWrapper;
import org.labkey.api.util.FileType;
import org.labkey.api.util.FileUtil;
import org.labkey.api.view.ActionURL;
import org.labkey.api.writer.PrintWriters;
import org.labkey.sequenceanalysis.SequenceAnalysisModule;
import org.labkey.sequenceanalysis.pipeline.ProcessVariantsHandler;
import org.labkey.sequenceanalysis.run.util.LiftoverBcfToolsWrapper;
import org.labkey.sequenceanalysis.run.util.LiftoverVcfWrapper;
import org.labkey.sequenceanalysis.util.SequenceUtil;
import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
/**
* Created by bimber on 8/26/2014.
*/
public class LiftoverHandler implements SequenceOutputHandler<SequenceOutputHandler.SequenceOutputProcessor>, VariantProcessingStep.SupportsScatterGather
{
private final FileType _bedFileType = new FileType(".bed", false);
//private FileType _gffFileType = new FileType("gff", false);
private final FileType _vcfFileType = new FileType(Arrays.asList(".vcf", ".bcf"), ".vcf", false, FileType.gzSupportLevel.SUPPORT_GZ);
public LiftoverHandler()
{
}
@Override
public boolean doSortAfterMerge()
{
return true;
}
@Override
public String getName()
{
return "Lift To Alternate Genome";
}
@Override
public String getDescription()
{
return "This will translate (liftover) VCF or BED files from one reference genome to another. This is often useful to compare data aligned against different genome builds, or to compare between closely related species.";
}
@Override
public String getButtonJSHandler()
{
return "SequenceAnalysis.window.LiftoverWindow.buttonHandler";
}
@Override
public ActionURL getButtonSuccessUrl(Container c, User u, List<Integer> outputFileIds)
{
return null;
}
@Override
public Module getOwningModule()
{
return ModuleLoader.getInstance().getModule(SequenceAnalysisModule.class);
}
@Override
public LinkedHashSet<String> getClientDependencies()
{
return new LinkedHashSet<>(Arrays.asList("sequenceanalysis/window/OutputHandlerWindow.js", "sequenceanalysis/window/LiftoverWindow.js"));
}
@Override
public boolean useWorkbooks()
{
return true;
}
@Override
public boolean doRunRemote()
{
return true;
}
@Override
public boolean doRunLocal()
{
return false;
}
@Override
public boolean canProcess(SequenceOutputFile f)
{
return f.getFile() != null && (
_bedFileType.isType(f.getFile()) ||
_vcfFileType.isType(f.getFile()));
}
@Override
public SequenceOutputProcessor getProcessor()
{
return new Processor();
}
@Override
public boolean doSplitJobs()
{
return false;
}
public class Processor implements SequenceOutputProcessor
{
@Override
public void init(JobContext ctx, List<SequenceOutputFile> inputFiles, List<RecordedAction> actions, List<SequenceOutputFile> outputsToCreate) throws UnsupportedOperationException, PipelineJobException
{
Integer chainFileId = ctx.getParams().getInt("chainFileId");
ExpData chainData = ExperimentService.get().getExpData(chainFileId);
if (chainData == null || !chainData.getFile().exists())
{
throw new PipelineJobException("Unable to find chain file: " + chainFileId);
}
ctx.getSequenceSupport().cacheExpData(chainData);
int targetGenomeId = ctx.getParams().getInt("targetGenomeId");
ReferenceGenome targetGenome = SequenceAnalysisService.get().getReferenceGenome(targetGenomeId, ctx.getJob().getUser());
ctx.getSequenceSupport().cacheGenome(targetGenome);
}
@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
{
PipelineJob job = ctx.getJob();
JSONObject params = ctx.getParams();
boolean dropGenotypes = params.optBoolean("dropGenotypes", false);
boolean useBcfTools = params.optBoolean("useBcfTools", false);
boolean doNotRetainUnmapped = params.optBoolean("doNotRetainUnmapped", false);
if (!doNotRetainUnmapped && !useBcfTools)
{
ctx.getLogger().debug("Picard LiftoverVcf requires an output file for rejected sites, so setting doNotRetainUnmapped to true");
doNotRetainUnmapped = true;
}
int chainFileId = params.getInt("chainFileId");
File chainFile = ctx.getSequenceSupport().getCachedData(chainFileId);
int targetGenomeId = params.getInt("targetGenomeId");
for (SequenceOutputFile f : inputFiles)
{
job.getLogger().info("processing output: " + f.getFile().getName());
RecordedAction action = new RecordedAction(getName());
action.setStartTime(new Date());
boolean isGzip = f.getFile().getPath().toLowerCase().endsWith("gz");
int dots = isGzip ? 2 : 1;
String baseName = FileUtil.getBaseName(f.getFile(), dots);
double pct = params.has("pct") ? params.getDouble("pct") : 0.95;
job.getLogger().info("using minimum percent match: " + pct);
action.addInput(f.getFile(), "Input File");
action.addInput(chainFile, "Chain File");
File outDir = ((FileAnalysisJobSupport) job).getAnalysisDirectory();
String ext = null;
if (_bedFileType.isType(f.getFile()))
{
ext = ".bed";
}
else if (_vcfFileType.isType(f.getFile()))
{
ext = ".vcf.gz";
}
else
{
throw new UnsupportedOperationException("Unsupported file type: " + f.getFile().getName());
}
File lifted = new File(outDir, baseName + ".lifted-" + targetGenomeId + ext);
File unmappedOutput = doNotRetainUnmapped ? null : new File(outDir, baseName + ".unmapped-" + targetGenomeId + ext);
try
{
if (_bedFileType.isType(f.getFile()))
{
liftOverBed(chainFile, f.getFile(), lifted, unmappedOutput, job, pct);
}
else if (_vcfFileType.isType(f.getFile()))
{
ReferenceGenome targetGenome = ctx.getSequenceSupport().getCachedGenome(targetGenomeId);
ReferenceGenome sourceGenome = ctx.getSequenceSupport().getCachedGenome(f.getLibrary_id());
liftOverVcf(ctx, targetGenome, sourceGenome, chainFile, f.getFile(), lifted, unmappedOutput, job, pct, dropGenotypes, useBcfTools);
}
}
catch (Exception e)
{
throw new PipelineJobException(e);
}
job.getLogger().info("adding outputs");
action.addOutput(lifted, "Lifted Features", lifted.exists(), true);
if (lifted.exists())
{
job.getLogger().info("adding lifted features: " + lifted.getName());
SequenceOutputFile so1 = new SequenceOutputFile();
so1.setName(f.getName() + " (lifted)");
so1.setDescription("Contains features from " + f.getName() + " after liftover");
//ExpData liftedData = ExperimentService.get().createData(job.getContainer(), new DataType("Liftover Output"));
//liftedData.setDataFileURI(lifted.toURI());
//liftedData.setName(lifted.getName());
//liftedData.save(job.getUser());
//so1.setDataId(liftedData.getRowId());
so1.setFile(lifted);
so1.setLibrary_id(targetGenomeId);
so1.setReadset(f.getReadset());
so1.setAnalysis_id(f.getAnalysis_id());
so1.setCategory(f.getCategory());
so1.setContainer(job.getContainerId());
so1.setCreated(new Date());
so1.setModified(new Date());
ctx.addSequenceOutput(so1);
}
if (unmappedOutput == null)
{
// skip
}
else if (!unmappedOutput.exists())
{
job.getLogger().info("no unmapped intervals");
}
else if (!SequenceUtil.hasLineCount(unmappedOutput))
{
job.getLogger().info("no unmapped intervals");
unmappedOutput.delete();
}
else
{
job.getLogger().info("adding unmapped features: " + unmappedOutput.getName());
action.addOutput(unmappedOutput, "Unmapped features", false, true);
SequenceOutputFile so2 = new SequenceOutputFile();
so2.setName(f.getName() + " (lifted/unmapped)");
so2.setDescription("Contains the unmapped features after attempted liftover of " + f.getName());
//ExpData unmappedData = ExperimentService.get().createData(job.getContainer(), new DataType("Liftover Output"));
//unmappedData.setName(unmappedOutput.getName());
//unmappedData.setDataFileURI(unmappedOutput.toURI());
//unmappedData.save(job.getUser());
//so2.setDataId(unmappedData.getRowId());
so2.setFile(unmappedOutput);
so2.setLibrary_id(f.getLibrary_id());
so2.setReadset(f.getReadset());
so2.setAnalysis_id(f.getAnalysis_id());
so2.setCategory(f.getCategory());
so2.setContainer(job.getContainerId());
so2.setCreated(new Date());
so2.setModified(new Date());
ctx.addSequenceOutput(so2);
}
action.setEndTime(new Date());
ctx.addActions(action);
}
}
}
public void liftOverVcf(JobContext ctx, ReferenceGenome targetGenome, ReferenceGenome sourceGenome, File chain, File input, File output, @Nullable File unmappedOutput, PipelineJob job, double pct, boolean dropGenotypes, boolean useBcfTools) throws IOException, PipelineJobException
{
File currentVCF = input;
if (dropGenotypes)
{
ctx.getLogger().info("creating VCF wihtout genotypes");
File outputFile = new File(output.getParentFile(), SequenceAnalysisService.get().getUnzippedBaseName(currentVCF.getName()) + ".noGenotypes.vcf.gz");
if (new File(outputFile.getPath() + ".tbi").exists())
{
ctx.getLogger().info("resuming from file: " + outputFile.getPath());
}
else
{
SelectVariantsWrapper wrapper = new SelectVariantsWrapper(job.getLogger());
wrapper.execute(sourceGenome.getWorkingFastaFile(), currentVCF, outputFile, List.of("--sites-only-vcf-output"));
}
currentVCF = outputFile;
ctx.getFileManager().addIntermediateFile(outputFile);
ctx.getFileManager().addIntermediateFile(new File(outputFile.getPath() + ".tbi"));
}
if (useBcfTools)
{
LiftoverBcfToolsWrapper wrapper = new LiftoverBcfToolsWrapper(job.getLogger());
wrapper.doLiftover(currentVCF, chain, sourceGenome.getWorkingFastaFile(), targetGenome.getWorkingFastaFile(), unmappedOutput, output);
SequencePipelineService.get().sortVcf(output, null, targetGenome.getSequenceDictionary(), ctx.getLogger());
}
else
{
LiftoverVcfWrapper wrapper = new LiftoverVcfWrapper(job.getLogger());
wrapper.doLiftover(currentVCF, chain, targetGenome.getWorkingFastaFile(), unmappedOutput, output, pct);
}
Long mapped = null;
if (output.exists())
{
String mappedStr = ProcessVariantsHandler.getVCFLineCount(output, job.getLogger(), false, true);
mapped = StringUtils.trimToNull(mappedStr) == null ? 0L : Long.parseLong(mappedStr);
job.getLogger().info("total variants mapped: " + mappedStr);
job.getLogger().info("passing variants mapped: " + ProcessVariantsHandler.getVCFLineCount(output, job.getLogger(), true, false));
SequenceAnalysisService.get().ensureVcfIndex(output, job.getLogger());
}
Long unmapped = 0L;
if (unmappedOutput != null && unmappedOutput.exists())
{
String unmappedStr = ProcessVariantsHandler.getVCFLineCount(unmappedOutput, job.getLogger(), false, true);
unmapped = StringUtils.trimToNull(unmappedStr) == null ? 0L : Long.parseLong(unmappedStr);
job.getLogger().info("total unmapped variants: " + unmappedStr);
job.getLogger().info("passing unmapped variants: " + ProcessVariantsHandler.getVCFLineCount(unmappedOutput, job.getLogger(), true, false));
SequenceAnalysisService.get().ensureVcfIndex(unmappedOutput, job.getLogger());
}
if (mapped != null)
{
Double fraction = (double)mapped / (mapped + unmapped);
job.getLogger().info("fraction mapped of total: " + fraction);
}
}
public void liftOverBed(File chain, File input, File output, @Nullable File unmappedOutput, PipelineJob job, double pct) throws IOException, PipelineJobException
{
LiftOver lo = new LiftOver(chain);
try (CSVWriter writer = new CSVWriter(PrintWriters.getPrintWriter(output), '\t', CSVWriter.NO_QUOTE_CHARACTER);CSVWriter unmappedWriter = unmappedOutput == null ? null : new CSVWriter(PrintWriters.getPrintWriter(unmappedOutput), '\t', CSVWriter.NO_QUOTE_CHARACTER))
{
try (FeatureReader<BEDFeature> reader = AbstractFeatureReader.getFeatureReader(input.getAbsolutePath(), new BEDCodec(), false))
{
try (CloseableTribbleIterator<BEDFeature> i = reader.iterator())
{
int mapped = 0;
int unmapped = 0;
while (i.hasNext())
{
FullBEDFeature f = (FullBEDFeature)i.next();
Interval iv = new Interval(f.getContig(), f.getStart(), f.getEnd(), f.getStrand() == Strand.POSITIVE, StringUtils.isEmpty(f.getName()) ? null : f.getName());
Interval lifted = lo.liftOver(iv, pct);
String score = ((Float)f.getScore()).isNaN() ? "0" : String.valueOf(f.getScore());
if (lifted != null)
{
writer.writeNext(new String[]{lifted.getContig(), String.valueOf(lifted.getStart() - 1), String.valueOf(lifted.getEnd()), f.getName(), score, (lifted.isNegativeStrand() ? "-" : lifted.isPositiveStrand() ? "+" : null), ("Source: " + f.getContig() + ":" + f.getStart() + "-" + f.getEnd())});
mapped++;
}
else
{
if (unmappedWriter != null)
unmappedWriter.writeNext(new String[]{f.getContig(), String.valueOf(f.getStart() - 1), String.valueOf(f.getEnd()), f.getName(), score, (f.getStrand() == Strand.NEGATIVE ? "-" : f.getStrand() == Strand.POSITIVE ? "+" : null)});
unmapped++;
}
}
NumberFormat pctFormat = NumberFormat.getPercentInstance();
pctFormat.setMaximumFractionDigits(1);
double total = (double)mapped + unmapped;
job.getLogger().info("total mapped: " + mapped + (total > 0 ? " (" + pctFormat.format(mapped / total) + "%)" : ""));
job.getLogger().info("total unmapped: " + unmapped + (total > 0 ? " (" + pctFormat.format(unmapped / total) + "%)" : ""));
}
}
}
//sort resulting file
SequenceUtil.sortROD(output, job.getLogger(), 2);
}
}