|
| 1 | +package org.labkey.mgap.pipeline; |
| 2 | + |
| 3 | +import htsjdk.samtools.util.Interval; |
| 4 | +import org.apache.logging.log4j.Logger; |
| 5 | +import org.json.old.JSONObject; |
| 6 | +import org.labkey.api.data.SimpleFilter; |
| 7 | +import org.labkey.api.data.TableSelector; |
| 8 | +import org.labkey.api.pipeline.PipelineJob; |
| 9 | +import org.labkey.api.pipeline.PipelineJobException; |
| 10 | +import org.labkey.api.query.FieldKey; |
| 11 | +import org.labkey.api.sequenceanalysis.SequenceAnalysisService; |
| 12 | +import org.labkey.api.sequenceanalysis.SequenceOutputFile; |
| 13 | +import org.labkey.api.sequenceanalysis.pipeline.AbstractVariantProcessingStepProvider; |
| 14 | +import org.labkey.api.sequenceanalysis.pipeline.PipelineContext; |
| 15 | +import org.labkey.api.sequenceanalysis.pipeline.PipelineStepProvider; |
| 16 | +import org.labkey.api.sequenceanalysis.pipeline.ReferenceGenome; |
| 17 | +import org.labkey.api.sequenceanalysis.pipeline.SequenceAnalysisJobSupport; |
| 18 | +import org.labkey.api.sequenceanalysis.pipeline.ToolParameterDescriptor; |
| 19 | +import org.labkey.api.sequenceanalysis.pipeline.VariantProcessingStep; |
| 20 | +import org.labkey.api.sequenceanalysis.pipeline.VariantProcessingStepOutputImpl; |
| 21 | +import org.labkey.api.sequenceanalysis.run.AbstractCommandPipelineStep; |
| 22 | +import org.labkey.api.sequenceanalysis.run.AbstractDiscvrSeqWrapper; |
| 23 | +import org.labkey.api.util.PageFlowUtil; |
| 24 | +import org.labkey.mgap.mGAPSchema; |
| 25 | + |
| 26 | +import javax.annotation.Nullable; |
| 27 | +import java.io.File; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +/** |
| 33 | + * User: bimber |
| 34 | + * Date: 6/15/2014 |
| 35 | + * Time: 12:39 PM |
| 36 | + */ |
| 37 | +public class mGapReleaseAnnotateNovelSitesStep extends AbstractCommandPipelineStep<mGapReleaseAnnotateNovelSitesStep.AnnotateNovelSitesWrapper> implements VariantProcessingStep |
| 38 | +{ |
| 39 | + public static final String VERSION_ROWID = "versionRowId"; |
| 40 | + public static final String PRIOR_RELEASE_LABEL = "priorReleaseLabel"; |
| 41 | + public static final String SITES_ONLY_DATA = "sitesOnlyVcfData"; |
| 42 | + |
| 43 | + public mGapReleaseAnnotateNovelSitesStep(PipelineStepProvider<?> provider, PipelineContext ctx) |
| 44 | + { |
| 45 | + super(provider, ctx, new AnnotateNovelSitesWrapper(ctx.getLogger())); |
| 46 | + } |
| 47 | + |
| 48 | + public static class Provider extends AbstractVariantProcessingStepProvider<mGapReleaseAnnotateNovelSitesStep> implements SupportsScatterGather |
| 49 | + { |
| 50 | + public Provider() |
| 51 | + { |
| 52 | + super("mGapAnnotateNovelSites", "Annotate Novel Sites Against mGAP Release", "AnnotateNovelSites", "Compare the VCF to the specified mGAP release VCF, producing TSV/VCF reports with site- and genotype-level concordance.", Arrays.asList( |
| 53 | + ToolParameterDescriptor.create(VERSION_ROWID, "mGAP Release", "The mGAP release VCF to use for comparison", "ldk-simplelabkeycombo", new JSONObject(){{ |
| 54 | + put("allowBlank", false); |
| 55 | + put("width", 400); |
| 56 | + put("schemaName", "mgap"); |
| 57 | + put("queryName", "variantCatalogReleases"); |
| 58 | + put("containerPath", "js:Laboratory.Utils.getQueryContainerPath()"); |
| 59 | + put("displayField", "version"); |
| 60 | + put("valueField", "rowid"); |
| 61 | + put("doNotIncludeInTemplates", true); |
| 62 | + }}, null), |
| 63 | + ToolParameterDescriptor.create("releaseVersion", "mGAP Version", "This string will be used to tag novel variants.", "textfield", new JSONObject(){{ |
| 64 | + put("allowBlank", false); |
| 65 | + put("doNotIncludeInTemplates", true); |
| 66 | + }}, null) |
| 67 | + ), PageFlowUtil.set("sequenceanalysis/field/SequenceOutputFileSelectorField.js"), null); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public mGapReleaseAnnotateNovelSitesStep create(PipelineContext ctx) |
| 72 | + { |
| 73 | + return new mGapReleaseAnnotateNovelSitesStep(this, ctx); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public Output processVariants(File inputVCF, File outputDirectory, ReferenceGenome genome, @Nullable List<Interval> intervals) throws PipelineJobException |
| 79 | + { |
| 80 | + VariantProcessingStepOutputImpl output = new VariantProcessingStepOutputImpl(); |
| 81 | + getPipelineCtx().getLogger().info("Annotating VCF by mGAP Release"); |
| 82 | + |
| 83 | + String releaseVersion = getProvider().getParameterByName("releaseVersion").extractValue(getPipelineCtx().getJob(), getProvider(), getStepIdx(), String.class, "0.0"); |
| 84 | + String priorReleaseLabel = getPipelineCtx().getSequenceSupport().getCachedObject(PRIOR_RELEASE_LABEL, String.class); |
| 85 | + int sitesOnlyExpDataId = getPipelineCtx().getSequenceSupport().getCachedObject(SITES_ONLY_DATA, Integer.class); |
| 86 | + File sitesOnlyVcf = getPipelineCtx().getSequenceSupport().getCachedData(sitesOnlyExpDataId); |
| 87 | + if (!sitesOnlyVcf.exists()) |
| 88 | + { |
| 89 | + throw new PipelineJobException("Unable to find file: " + sitesOnlyVcf); |
| 90 | + } |
| 91 | + |
| 92 | + List<String> extraArgs = new ArrayList<>(); |
| 93 | + if (intervals != null) |
| 94 | + { |
| 95 | + intervals.forEach(interval -> { |
| 96 | + extraArgs.add("-L"); |
| 97 | + extraArgs.add(interval.getContig() + ":" + interval.getStart() + "-" + interval.getEnd()); |
| 98 | + }); |
| 99 | + |
| 100 | + extraArgs.add("--ignore-variants-starting-outside-interval"); |
| 101 | + } |
| 102 | + |
| 103 | + extraArgs.add("-dv"); |
| 104 | + extraArgs.add(priorReleaseLabel); |
| 105 | + |
| 106 | + File annotatedVCF = new File(outputDirectory, SequenceAnalysisService.get().getUnzippedBaseName(inputVCF.getName()) + ".comparison.vcf.gz"); |
| 107 | + getWrapper().execute(inputVCF, sitesOnlyVcf, genome.getWorkingFastaFile(), releaseVersion, annotatedVCF, extraArgs); |
| 108 | + if (!annotatedVCF.exists()) |
| 109 | + { |
| 110 | + throw new PipelineJobException("Unable to find output: " + annotatedVCF.getPath()); |
| 111 | + } |
| 112 | + |
| 113 | + output.addInput(inputVCF, "Input VCF"); |
| 114 | + output.addInput(sitesOnlyVcf, "Reference VCF"); |
| 115 | + |
| 116 | + output.addOutput(annotatedVCF, "VCF Annotated by mGAP Version"); |
| 117 | + output.setVcf(annotatedVCF); |
| 118 | + |
| 119 | + return output; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void init(PipelineJob job, SequenceAnalysisJobSupport support, List<SequenceOutputFile> inputFiles) throws PipelineJobException |
| 124 | + { |
| 125 | + Integer versionRowId = getProvider().getParameterByName(VERSION_ROWID).extractValue(getPipelineCtx().getJob(), getProvider(), getStepIdx(), Integer.class); |
| 126 | + String version = new TableSelector(mGAPSchema.getInstance().getSchema().getTable(mGAPSchema.TABLE_VARIANT_CATALOG_RELEASES), PageFlowUtil.set("version"), new SimpleFilter(FieldKey.fromString("rowId"), versionRowId), null).getObject(String.class); |
| 127 | + if (version == null) |
| 128 | + { |
| 129 | + throw new PipelineJobException("Unable to find release for release: " + versionRowId); |
| 130 | + } |
| 131 | + |
| 132 | + Integer referenceVcfOutputId = new TableSelector(mGAPSchema.getInstance().getSchema().getTable(mGAPSchema.TABLE_VARIANT_CATALOG_RELEASES), PageFlowUtil.set("sitesOnlyVcfId"), new SimpleFilter(FieldKey.fromString("rowId"), versionRowId), null).getObject(Integer.class); |
| 133 | + if (referenceVcfOutputId == null) |
| 134 | + { |
| 135 | + getPipelineCtx().getLogger().debug("Sites-only VCF not found, using primary VCF"); |
| 136 | + referenceVcfOutputId = new TableSelector(mGAPSchema.getInstance().getSchema().getTable(mGAPSchema.TABLE_VARIANT_CATALOG_RELEASES), PageFlowUtil.set("vcfId"), new SimpleFilter(FieldKey.fromString("rowId"), versionRowId), null).getObject(Integer.class); |
| 137 | + } |
| 138 | + |
| 139 | + if (referenceVcfOutputId == null) |
| 140 | + { |
| 141 | + throw new PipelineJobException("Unable to find sites-only VCF for release: " + versionRowId); |
| 142 | + } |
| 143 | + |
| 144 | + SequenceOutputFile sitesOnly = SequenceOutputFile.getForId(referenceVcfOutputId); |
| 145 | + if (sitesOnly == null) |
| 146 | + { |
| 147 | + throw new PipelineJobException("Unable to find sites-only VCF output file for fileId: " + referenceVcfOutputId); |
| 148 | + } |
| 149 | + |
| 150 | + support.cacheExpData(sitesOnly.getExpData()); |
| 151 | + |
| 152 | + support.cacheObject(SITES_ONLY_DATA, sitesOnly.getDataId()); |
| 153 | + support.cacheObject(PRIOR_RELEASE_LABEL, version); |
| 154 | + } |
| 155 | + |
| 156 | + public static class AnnotateNovelSitesWrapper extends AbstractDiscvrSeqWrapper |
| 157 | + { |
| 158 | + public AnnotateNovelSitesWrapper(Logger log) |
| 159 | + { |
| 160 | + super(log); |
| 161 | + } |
| 162 | + |
| 163 | + public File execute(File vcf, File referenceVcf, File fasta, String versionString, File vcfOutput, List<String> extraArgs) throws PipelineJobException |
| 164 | + { |
| 165 | + List<String> args = new ArrayList<>(getBaseArgs()); |
| 166 | + args.add("AnnotateNovelSites"); |
| 167 | + args.add("-R"); |
| 168 | + args.add(fasta.getPath()); |
| 169 | + |
| 170 | + args.add("-V"); |
| 171 | + args.add(vcf.getPath()); |
| 172 | + args.add("-rv"); |
| 173 | + args.add(referenceVcf.getPath()); |
| 174 | + |
| 175 | + args.add("-an"); |
| 176 | + args.add("mGAPV"); |
| 177 | + args.add("-ad"); |
| 178 | + args.add("The first mGAP version where variants at this site appeared"); |
| 179 | + args.add("-av"); |
| 180 | + args.add(versionString); |
| 181 | + |
| 182 | + args.add("-O"); |
| 183 | + args.add(vcfOutput.getPath()); |
| 184 | + |
| 185 | + if (extraArgs != null) |
| 186 | + { |
| 187 | + args.addAll(extraArgs); |
| 188 | + } |
| 189 | + |
| 190 | + execute(args); |
| 191 | + |
| 192 | + return vcfOutput; |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments