|
| 1 | +package org.labkey.sequenceanalysis.run.analysis; |
| 2 | + |
| 3 | +import htsjdk.samtools.SAMFileHeader; |
| 4 | +import org.apache.commons.io.FileUtils; |
| 5 | +import org.labkey.api.pipeline.PipelineJobException; |
| 6 | +import org.labkey.api.sequenceanalysis.model.AnalysisModel; |
| 7 | +import org.labkey.api.sequenceanalysis.model.Readset; |
| 8 | +import org.labkey.api.sequenceanalysis.pipeline.AbstractAnalysisStepProvider; |
| 9 | +import org.labkey.api.sequenceanalysis.pipeline.AnalysisOutputImpl; |
| 10 | +import org.labkey.api.sequenceanalysis.pipeline.AnalysisStep; |
| 11 | +import org.labkey.api.sequenceanalysis.pipeline.PipelineContext; |
| 12 | +import org.labkey.api.sequenceanalysis.pipeline.PipelineStepProvider; |
| 13 | +import org.labkey.api.sequenceanalysis.pipeline.ReferenceGenome; |
| 14 | +import org.labkey.api.sequenceanalysis.pipeline.SamSorter; |
| 15 | +import org.labkey.api.sequenceanalysis.pipeline.SamtoolsRunner; |
| 16 | +import org.labkey.api.sequenceanalysis.pipeline.SequencePipelineService; |
| 17 | +import org.labkey.api.sequenceanalysis.run.AbstractCommandPipelineStep; |
| 18 | +import org.labkey.api.sequenceanalysis.run.AbstractCommandWrapper; |
| 19 | +import org.labkey.api.sequenceanalysis.run.SimpleScriptWrapper; |
| 20 | +import org.labkey.api.util.FileUtil; |
| 21 | +import org.labkey.api.util.Path; |
| 22 | + |
| 23 | +import java.io.File; |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +public class SpecHlaAnalysis extends AbstractCommandPipelineStep<SimpleScriptWrapper> implements AnalysisStep |
| 30 | +{ |
| 31 | + public SpecHlaAnalysis(PipelineStepProvider<?> provider, PipelineContext ctx) |
| 32 | + { |
| 33 | + super(provider, ctx, new SimpleScriptWrapper(ctx.getLogger())); |
| 34 | + } |
| 35 | + |
| 36 | + public static class Provider extends AbstractAnalysisStepProvider<SpecHlaAnalysis> |
| 37 | + { |
| 38 | + public Provider() |
| 39 | + { |
| 40 | + super("SpecHlaStep", "SpecHLA", null, "This will run SpecHLA for HLA genotyping from WGS/WXS data. This should use a BAM aligned to a custom HLA DB, rather than aligned to the full genome", Arrays.asList( |
| 41 | + |
| 42 | + ), null, "https://github.com/deepomicslab/SpecHLA/"); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public SpecHlaAnalysis create(PipelineContext ctx) |
| 47 | + { |
| 48 | + return new SpecHlaAnalysis(this, ctx); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public Output performAnalysisPerSampleRemote(Readset rs, File inputBam, ReferenceGenome referenceGenome, File outputDir) throws PipelineJobException |
| 54 | + { |
| 55 | + AnalysisOutputImpl output = new AnalysisOutputImpl(); |
| 56 | + |
| 57 | + File gzippedFasta = referenceGenome.getWorkingFastaFileGzipped(); |
| 58 | + if (!gzippedFasta.exists()) |
| 59 | + { |
| 60 | + throw new PipelineJobException("Missing file: " + gzippedFasta.getPath()); |
| 61 | + } |
| 62 | + |
| 63 | + File doneFile = FileUtil.appendName(outputDir, FileUtil.getBaseName(inputBam) + ".subset.done"); |
| 64 | + output.addIntermediateFile(doneFile); |
| 65 | + |
| 66 | + File subsetBam = FileUtil.appendName(outputDir, FileUtil.getBaseName(inputBam) + ".subset.bam"); |
| 67 | + SamtoolsRunner sr = new SamtoolsRunner(getWrapper().getLogger()); |
| 68 | + if (doneFile.exists()) |
| 69 | + { |
| 70 | + getPipelineCtx().getLogger().debug("Done file exists, skipping samtools view"); |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + sr.execute(Arrays.asList( |
| 75 | + sr.getSamtoolsPath().getPath(), |
| 76 | + "view", |
| 77 | + "-h", |
| 78 | + "-F", "12", //This selects pairs where either mate is mapped |
| 79 | + "-T", gzippedFasta.getPath(), |
| 80 | + "-o", subsetBam.getPath(), |
| 81 | + inputBam.getPath() |
| 82 | + )); |
| 83 | + } |
| 84 | + output.addIntermediateFile(subsetBam); |
| 85 | + |
| 86 | + File queryNameSortBam = FileUtil.appendName(outputDir, FileUtil.getBaseName(inputBam) + ".querySort.bam"); |
| 87 | + if (doneFile.exists()) |
| 88 | + { |
| 89 | + getPipelineCtx().getLogger().debug("Done file exists, skipping samtools sort"); |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + new SamSorter(getPipelineCtx().getLogger()).execute(subsetBam, queryNameSortBam, SAMFileHeader.SortOrder.queryname); |
| 94 | + } |
| 95 | + output.addIntermediateFile(queryNameSortBam); |
| 96 | + |
| 97 | + File fq1 = FileUtil.appendName(outputDir, FileUtil.getBaseName(inputBam) + ".R1.fastq.gz"); |
| 98 | + File fq2 = FileUtil.appendName(outputDir, FileUtil.getBaseName(inputBam) + ".R2.fastq.gz"); |
| 99 | + if (doneFile.exists()) |
| 100 | + { |
| 101 | + getPipelineCtx().getLogger().debug("Done file exists, skipping samtools fastq"); |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + sr.execute(Arrays.asList( |
| 106 | + sr.getSamtoolsPath().getPath(), |
| 107 | + "fastq", |
| 108 | + "-1", |
| 109 | + fq1.getPath(), |
| 110 | + "-2", |
| 111 | + fq2.getPath(), |
| 112 | + queryNameSortBam.getPath() |
| 113 | + )); |
| 114 | + } |
| 115 | + output.addIntermediateFile(fq1); |
| 116 | + output.addIntermediateFile(fq2); |
| 117 | + |
| 118 | + try |
| 119 | + { |
| 120 | + FileUtils.touch(doneFile); |
| 121 | + } |
| 122 | + catch (IOException e) |
| 123 | + { |
| 124 | + throw new PipelineJobException(e); |
| 125 | + } |
| 126 | + |
| 127 | + File specHlaExe = AbstractCommandWrapper.resolveFileInPath("spechla", null, true); |
| 128 | + |
| 129 | + List<String> toRun = new ArrayList<>(Arrays.asList( |
| 130 | + specHlaExe.getPath(), |
| 131 | + "-n", |
| 132 | + "specHLA", |
| 133 | + "-u", |
| 134 | + "1", // 1 = exon. 0 = full-length |
| 135 | + "-1", |
| 136 | + fq1.getPath(), |
| 137 | + "-2", |
| 138 | + fq2.getPath(), |
| 139 | + "-o", |
| 140 | + outputDir.getPath() |
| 141 | + )); |
| 142 | + |
| 143 | + Integer maxThreads = SequencePipelineService.get().getMaxThreads(getWrapper().getLogger()); |
| 144 | + if (maxThreads != null) |
| 145 | + { |
| 146 | + toRun.add("-j"); |
| 147 | + toRun.add(maxThreads.toString()); |
| 148 | + } |
| 149 | + |
| 150 | + getWrapper().execute(toRun); |
| 151 | + |
| 152 | + File spechlaDir = FileUtil.appendName(outputDir, "specHLA"); |
| 153 | + File outFile = FileUtil.appendName(spechlaDir, "hla.result.txt"); |
| 154 | + if (!outFile.exists()) |
| 155 | + { |
| 156 | + throw new PipelineJobException("SpecHLA result file does not exist: " + outFile.getPath()); |
| 157 | + } |
| 158 | + |
| 159 | + output.addIntermediateFile(spechlaDir); |
| 160 | + File copiedFile = FileUtil.appendName(outputDir, outFile.getName()); |
| 161 | + try |
| 162 | + { |
| 163 | + FileUtils.copyFile(outFile, copiedFile); |
| 164 | + } |
| 165 | + catch (IOException e) |
| 166 | + { |
| 167 | + throw new PipelineJobException(e); |
| 168 | + } |
| 169 | + |
| 170 | + output.addSequenceOutput(copiedFile, FileUtil.getBaseName(inputBam) + ": HLA Typing", "specHLA Genotyping", rs.getReadsetId(), null, referenceGenome.getGenomeId(), null); |
| 171 | + |
| 172 | + return output; |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public Output performAnalysisPerSampleLocal(AnalysisModel model, File inputBam, File referenceFasta, File outDir) throws PipelineJobException |
| 177 | + { |
| 178 | + return null; |
| 179 | + } |
| 180 | +} |
0 commit comments