|
| 1 | +package org.labkey.singlecell.run; |
| 2 | + |
| 3 | +import org.apache.logging.log4j.Logger; |
| 4 | +import org.jetbrains.annotations.Nullable; |
| 5 | +import org.json.JSONObject; |
| 6 | +import org.labkey.api.pipeline.PipelineJobException; |
| 7 | +import org.labkey.api.sequenceanalysis.model.Readset; |
| 8 | +import org.labkey.api.sequenceanalysis.pipeline.AbstractAlignmentStepProvider; |
| 9 | +import org.labkey.api.sequenceanalysis.pipeline.AlignmentOutputImpl; |
| 10 | +import org.labkey.api.sequenceanalysis.pipeline.AlignmentStep; |
| 11 | +import org.labkey.api.sequenceanalysis.pipeline.AlignmentStepProvider; |
| 12 | +import org.labkey.api.sequenceanalysis.pipeline.PipelineContext; |
| 13 | +import org.labkey.api.sequenceanalysis.pipeline.ReferenceGenome; |
| 14 | +import org.labkey.api.sequenceanalysis.pipeline.SequencePipelineService; |
| 15 | +import org.labkey.api.sequenceanalysis.pipeline.ToolParameterDescriptor; |
| 16 | +import org.labkey.api.sequenceanalysis.run.AbstractCommandWrapper; |
| 17 | +import org.labkey.api.sequenceanalysis.run.SimpleScriptWrapper; |
| 18 | +import org.labkey.api.util.PageFlowUtil; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.LinkedHashSet; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +public class VelocytoAlignmentStep extends AbstractCellRangerDependentStep |
| 27 | +{ |
| 28 | + public VelocytoAlignmentStep(AlignmentStepProvider provider, PipelineContext ctx, CellRangerWrapper wrapper) |
| 29 | + { |
| 30 | + super(provider, ctx, wrapper); |
| 31 | + } |
| 32 | + |
| 33 | + public static class Provider extends AbstractAlignmentStepProvider<AlignmentStep> |
| 34 | + { |
| 35 | + public Provider() |
| 36 | + { |
| 37 | + super("velocyto", "This will run velocyto to generate a supplemental feature count matrix", getCellRangerGexParams(Arrays.asList( |
| 38 | + ToolParameterDescriptor.createExpDataParam("gtf", "Gene File", "This is the ID of a GTF file containing genes from this genome.", "sequenceanalysis-genomefileselectorfield", new JSONObject() |
| 39 | + {{ |
| 40 | + put("extensions", Arrays.asList("gtf")); |
| 41 | + put("width", 400); |
| 42 | + put("allowBlank", false); |
| 43 | + }}, null), |
| 44 | + ToolParameterDescriptor.createExpDataParam("mask", "Mask File", "This is the ID of an optional GTF file containing repetitive regions to mask.", "sequenceanalysis-genomefileselectorfield", new JSONObject() |
| 45 | + {{ |
| 46 | + put("extensions", Arrays.asList("gtf")); |
| 47 | + put("width", 400); |
| 48 | + put("allowBlank", false); |
| 49 | + }}, null) |
| 50 | + )), new LinkedHashSet<>(PageFlowUtil.set("sequenceanalysis/field/GenomeFileSelectorField.js")), null, true, false, ALIGNMENT_MODE.MERGE_THEN_ALIGN); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public AlignmentStep create(PipelineContext context) |
| 55 | + { |
| 56 | + return new VelocytoAlignmentStep(this, context, new CellRangerWrapper(context.getLogger())); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public AlignmentOutput performAlignment(Readset rs, List<File> inputFastqs1, @Nullable List<File> inputFastqs2, File outputDirectory, ReferenceGenome referenceGenome, String basename, String readGroupId, @Nullable String platformUnit) throws PipelineJobException |
| 62 | + { |
| 63 | + AlignmentOutputImpl output = new AlignmentOutputImpl(); |
| 64 | + File localBam = runCellRanger(output, rs, inputFastqs1, inputFastqs2, outputDirectory, referenceGenome, basename, readGroupId, platformUnit); |
| 65 | + |
| 66 | + File gtf = getPipelineCtx().getSequenceSupport().getCachedData(getProvider().getParameterByName("gtf").extractValue(getPipelineCtx().getJob(), getProvider(), getStepIdx(), Integer.class)); |
| 67 | + if (gtf == null) |
| 68 | + { |
| 69 | + throw new PipelineJobException("Missing GTF file param"); |
| 70 | + } |
| 71 | + else if (!gtf.exists()) |
| 72 | + { |
| 73 | + throw new PipelineJobException("File not found: " + gtf.getPath()); |
| 74 | + } |
| 75 | + |
| 76 | + File mask = null; |
| 77 | + if (getProvider().getParameterByName("mask").extractValue(getPipelineCtx().getJob(), getProvider(), getStepIdx(), Integer.class) != null) |
| 78 | + { |
| 79 | + mask = getPipelineCtx().getSequenceSupport().getCachedData(getProvider().getParameterByName("mask").extractValue(getPipelineCtx().getJob(), getProvider(), getStepIdx(), Integer.class)); |
| 80 | + if (!mask.exists()) |
| 81 | + { |
| 82 | + throw new PipelineJobException("Missing file: " + mask.getPath()); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + File loom = new VelocytoWrapper(getPipelineCtx().getLogger()).runVelocytoFor10x(localBam, gtf, outputDirectory, mask); |
| 87 | + output.addSequenceOutput(loom, rs.getName() + ": velocyto", "Velocyto Counts", rs.getReadsetId(), null, referenceGenome.getGenomeId(), null); |
| 88 | + |
| 89 | + return output; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public IndexOutput createIndex(ReferenceGenome referenceGenome, File outputDir) throws PipelineJobException |
| 94 | + { |
| 95 | + return super.createIndex(referenceGenome, outputDir); |
| 96 | + } |
| 97 | + |
| 98 | + public static class VelocytoWrapper extends AbstractCommandWrapper |
| 99 | + { |
| 100 | + public VelocytoWrapper(Logger log) |
| 101 | + { |
| 102 | + super(log); |
| 103 | + } |
| 104 | + |
| 105 | + public File runVelocytoFor10x(File localBam, File gtf, File outputFolder, @Nullable File mask) throws PipelineJobException |
| 106 | + { |
| 107 | + // https://velocyto.org/velocyto.py/tutorial/cli.html#run10x-run-on-10x-chromium-samples |
| 108 | + // velocyto run10x -m repeat_msk.gtf mypath/sample01 somepath/refdata-cellranger-mm10-1.2.0/genes/genes.gtf |
| 109 | + |
| 110 | + SimpleScriptWrapper wrapper = new SimpleScriptWrapper(getLogger()); |
| 111 | + List<String> args = new ArrayList<>(); |
| 112 | + args.add(SequencePipelineService.get().getExeForPackage("VELOCYTOPATH", "velocyto").getPath()); |
| 113 | + args.add("run10x"); |
| 114 | + |
| 115 | + args.add("-o"); |
| 116 | + args.add(outputFolder.getPath()); |
| 117 | + |
| 118 | + Integer threads = SequencePipelineService.get().getMaxThreads(getLogger()); |
| 119 | + if (threads != null && threads > 1) |
| 120 | + { |
| 121 | + args.add("--samtools-threads"); |
| 122 | + args.add(String.valueOf(threads - 1)); |
| 123 | + } |
| 124 | + |
| 125 | + if (mask != null) |
| 126 | + { |
| 127 | + args.add("-m"); |
| 128 | + args.add(mask.getPath()); |
| 129 | + } |
| 130 | + |
| 131 | + // Input 10x. This is the top-level project output |
| 132 | + args.add(localBam.getParentFile().getParentFile().getPath()); |
| 133 | + |
| 134 | + args.add(gtf.getPath()); |
| 135 | + |
| 136 | + wrapper.execute(args); |
| 137 | + |
| 138 | + File loom = new File(outputFolder, "sample.loom"); |
| 139 | + if (!loom.exists()) |
| 140 | + { |
| 141 | + throw new PipelineJobException("Missing expected file: " + loom.getPath()); |
| 142 | + } |
| 143 | + |
| 144 | + return loom; |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments