|
| 1 | +package org.labkey.sequenceanalysis.run.analysis; |
| 2 | + |
| 3 | +import htsjdk.samtools.SAMFileHeader; |
| 4 | +import org.apache.logging.log4j.Logger; |
| 5 | +import org.json.JSONObject; |
| 6 | +import org.labkey.api.pipeline.PipelineJobException; |
| 7 | +import org.labkey.api.sequenceanalysis.model.AnalysisModel; |
| 8 | +import org.labkey.api.sequenceanalysis.model.Readset; |
| 9 | +import org.labkey.api.sequenceanalysis.pipeline.AbstractAnalysisStepProvider; |
| 10 | +import org.labkey.api.sequenceanalysis.pipeline.AnalysisOutputImpl; |
| 11 | +import org.labkey.api.sequenceanalysis.pipeline.AnalysisStep; |
| 12 | +import org.labkey.api.sequenceanalysis.pipeline.CommandLineParam; |
| 13 | +import org.labkey.api.sequenceanalysis.pipeline.PipelineContext; |
| 14 | +import org.labkey.api.sequenceanalysis.pipeline.PipelineStepProvider; |
| 15 | +import org.labkey.api.sequenceanalysis.pipeline.ReferenceGenome; |
| 16 | +import org.labkey.api.sequenceanalysis.pipeline.SamSorter; |
| 17 | +import org.labkey.api.sequenceanalysis.pipeline.SequencePipelineService; |
| 18 | +import org.labkey.api.sequenceanalysis.pipeline.ToolParameterDescriptor; |
| 19 | +import org.labkey.api.sequenceanalysis.run.AbstractCommandPipelineStep; |
| 20 | +import org.labkey.api.sequenceanalysis.run.AbstractCommandWrapper; |
| 21 | +import org.labkey.api.util.FileUtil; |
| 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 GenrichStep extends AbstractCommandPipelineStep<GenrichStep.GenrichWrapper> implements AnalysisStep |
| 30 | +{ |
| 31 | + public GenrichStep(PipelineStepProvider<?> provider, PipelineContext ctx) |
| 32 | + { |
| 33 | + super(provider, ctx, new GenrichWrapper(ctx.getLogger())); |
| 34 | + } |
| 35 | + |
| 36 | + public static class Provider extends AbstractAnalysisStepProvider<GenrichStep> |
| 37 | + { |
| 38 | + public Provider() |
| 39 | + { |
| 40 | + super("GenrichStep", "Genrich", null, "This will run Genrich to calculate ATAC-seq peaks.", Arrays.asList( |
| 41 | + ToolParameterDescriptor.createCommandLineParam(CommandLineParam.createSwitch("-r"), "removeDuplicates", "Remove PCR Duplicates", "If checked, PCR duplicates will be removed", "checkbox", new JSONObject(){{ |
| 42 | + put("checked", true); |
| 43 | + }}, true), |
| 44 | + ToolParameterDescriptor.createCommandLineParam(CommandLineParam.create("-m"), "minMAPQ", "Min MAPQ", "Minimum MAPQ to keep an alignment", "ldk-integerfield", new JSONObject(){{ |
| 45 | + |
| 46 | + }}, 0) |
| 47 | + ), null, null); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public GenrichStep create(PipelineContext ctx) |
| 52 | + { |
| 53 | + return new GenrichStep(this, ctx); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public Output performAnalysisPerSampleRemote(Readset rs, File inputBam, ReferenceGenome referenceGenome, File outputDir) throws PipelineJobException |
| 59 | + { |
| 60 | + AnalysisOutputImpl output = new AnalysisOutputImpl(); |
| 61 | + |
| 62 | + File queryNameSortBam; |
| 63 | + try |
| 64 | + { |
| 65 | + if (SequencePipelineService.get().getBamSortOrder(inputBam) != SAMFileHeader.SortOrder.queryname) |
| 66 | + { |
| 67 | + queryNameSortBam = new SamSorter(getPipelineCtx().getLogger()).execute(inputBam, new File(outputDir, FileUtil.getBaseName(inputBam) + ".querySort.bam"), SAMFileHeader.SortOrder.queryname); |
| 68 | + output.addIntermediateFile(queryNameSortBam); |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + queryNameSortBam = inputBam; |
| 73 | + } |
| 74 | + } |
| 75 | + catch (IOException e) |
| 76 | + { |
| 77 | + throw new PipelineJobException(e); |
| 78 | + } |
| 79 | + |
| 80 | + File outputPeaks = new File(outputDir, FileUtil.getBaseName(inputBam) + ".narrowPeak"); |
| 81 | + |
| 82 | + List<String> extraArgs = getClientCommandArgs(); |
| 83 | + getWrapper().run(queryNameSortBam, outputPeaks, extraArgs); |
| 84 | + |
| 85 | + if (!outputPeaks.exists()) |
| 86 | + { |
| 87 | + throw new PipelineJobException("Unable to find file: " + outputPeaks.getPath()); |
| 88 | + } |
| 89 | + |
| 90 | + output.addSequenceOutput(outputPeaks, FileUtil.getBaseName(inputBam) + ": ATAC-seq Peaks", "ATAC-seq Peaks", rs.getReadsetId(), null, referenceGenome.getGenomeId(), null); |
| 91 | + |
| 92 | + return output; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public Output performAnalysisPerSampleLocal(AnalysisModel model, File inputBam, File referenceFasta, File outDir) throws PipelineJobException |
| 97 | + { |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + public static class GenrichWrapper extends AbstractCommandWrapper |
| 102 | + { |
| 103 | + public GenrichWrapper(Logger log) |
| 104 | + { |
| 105 | + super(log); |
| 106 | + } |
| 107 | + |
| 108 | + public void run(File inputBam, File output, List<String> extraArgs) throws PipelineJobException |
| 109 | + { |
| 110 | + List<String> args = new ArrayList<>(); |
| 111 | + args.add(getExe().getPath()); |
| 112 | + args.add("-t"); |
| 113 | + args.add(inputBam.getPath()); |
| 114 | + |
| 115 | + args.add("-o"); |
| 116 | + args.add(output.getPath()); |
| 117 | + |
| 118 | + args.add("-j"); |
| 119 | + |
| 120 | + if (extraArgs != null) |
| 121 | + { |
| 122 | + args.addAll(extraArgs); |
| 123 | + } |
| 124 | + |
| 125 | + execute(args); |
| 126 | + } |
| 127 | + |
| 128 | + private File getExe() |
| 129 | + { |
| 130 | + return resolveFileInPath("Genrich", "GENRICHPATH", true); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments