|
| 1 | +package org.labkey.sequenceanalysis.run.preprocessing; |
| 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.pipeline.PipelineJobService; |
| 8 | +import org.labkey.api.sequenceanalysis.SequenceAnalysisService; |
| 9 | +import org.labkey.api.sequenceanalysis.pipeline.AbstractPipelineStepProvider; |
| 10 | +import org.labkey.api.sequenceanalysis.pipeline.CommandLineParam; |
| 11 | +import org.labkey.api.sequenceanalysis.pipeline.PipelineContext; |
| 12 | +import org.labkey.api.sequenceanalysis.pipeline.PipelineStepProvider; |
| 13 | +import org.labkey.api.sequenceanalysis.pipeline.PreprocessingStep; |
| 14 | +import org.labkey.api.sequenceanalysis.pipeline.SequencePipelineService; |
| 15 | +import org.labkey.api.sequenceanalysis.pipeline.ToolParameterDescriptor; |
| 16 | +import org.labkey.api.sequenceanalysis.run.AbstractCommandPipelineStep; |
| 17 | +import org.labkey.api.sequenceanalysis.run.AbstractCommandWrapper; |
| 18 | +import org.labkey.api.sequenceanalysis.run.SimpleScriptWrapper; |
| 19 | +import org.labkey.api.util.Compress; |
| 20 | +import org.labkey.api.util.FileUtil; |
| 21 | +import org.labkey.api.util.Pair; |
| 22 | + |
| 23 | +import java.io.File; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +public class Kraken2Step extends AbstractCommandPipelineStep<Kraken2Step.Kraken2Wrapper> implements PreprocessingStep |
| 29 | +{ |
| 30 | + private static final String DB_PARAM = "db"; |
| 31 | + private static final String MODE_PARAM = "mode"; |
| 32 | + |
| 33 | + public Kraken2Step(PipelineStepProvider<?> provider, PipelineContext ctx) |
| 34 | + { |
| 35 | + super(provider, ctx, new Kraken2Wrapper(ctx.getLogger())); |
| 36 | + } |
| 37 | + |
| 38 | + public static class Provider extends AbstractPipelineStepProvider<PreprocessingStep> |
| 39 | + { |
| 40 | + public Provider() |
| 41 | + { |
| 42 | + super("Kraken2", "Kraken2", "Kraken2", "This step aligns input reads against a reference using BWA-mem and will only return read pairs without a passing hit in either read.", Arrays.asList( |
| 43 | + ToolParameterDescriptor.create(DB_PARAM, "Database", "This determines the DB for positive or negative selection", "ldk-simplecombo", new JSONObject(){{ |
| 44 | + put("storeValues", "kraken2_bv;kraken2_standard"); |
| 45 | + put("multiSelect", false); |
| 46 | + put("allowBlank", false); |
| 47 | + put("joinReturnValue", true); |
| 48 | + put("delimiter", ";"); |
| 49 | + }}, "kraken2_bv"), |
| 50 | + ToolParameterDescriptor.create(MODE_PARAM, "Reads To Retain", "This determines which set of reads is passed to the next step. If 'Retain Classified' is selected, then reads matching the DB are retained. if 'Retain Unclassified' is selected, then reads that do not match the DB are retained", "ldk-simplecombo", new JSONObject(){{ |
| 51 | + put("storeValues", "Classified;Unclassified"); |
| 52 | + put("multiSelect", false); |
| 53 | + put("allowBlank", false); |
| 54 | + put("joinReturnValue", true); |
| 55 | + put("delimiter", ";"); |
| 56 | + }}, null), |
| 57 | + ToolParameterDescriptor.createCommandLineParam(CommandLineParam.createSwitch("--memory-mapping"), "memoryMapping", "Memory Mapping", "If checked, the DB will not be read into memory, reducing RAM", "checkbox", null, false), |
| 58 | + ToolParameterDescriptor.createCommandLineParam(CommandLineParam.create("--minimum-hit-groups"), "minimumHitGroups", "Minimum Hit Groups", "Minimum number of hit groups (overlapping k-mers sharing the same minimizer) needed to make a call", "ldk-integerfield", new JSONObject(){{ |
| 59 | + put("minValue", 0); |
| 60 | + }}, 2), |
| 61 | + ToolParameterDescriptor.createCommandLineParam(CommandLineParam.create("--confidence"), "confidence", "Confidence", "Confidence score threshold (0-1)", "ldk-numberfield", new JSONObject(){{ |
| 62 | + put("minValue", 0); |
| 63 | + put("maxValue", 1); |
| 64 | + put("decimalPrecision", 2); |
| 65 | + }}, 0) |
| 66 | + ), null, "https://github.com/DerrickWood/kraken2"); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public Kraken2Step create(PipelineContext context) |
| 71 | + { |
| 72 | + return new Kraken2Step(this, context); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public Output processInputFile(File inputFile, @Nullable File inputFile2, File outputDir) throws PipelineJobException |
| 78 | + { |
| 79 | + PreprocessingOutputImpl output = new PreprocessingOutputImpl(inputFile, inputFile2); |
| 80 | + |
| 81 | + List<String> args = new ArrayList<>(); |
| 82 | + args.add(getWrapper().getExe().getPath()); |
| 83 | + |
| 84 | + if (inputFile2 != null) |
| 85 | + { |
| 86 | + args.add("--paired"); |
| 87 | + } |
| 88 | + |
| 89 | + if (inputFile.getName().toLowerCase().endsWith(".gz")) |
| 90 | + { |
| 91 | + args.add("--gzip-compressed"); |
| 92 | + } |
| 93 | + |
| 94 | + Integer threads = SequencePipelineService.get().getMaxThreads(getPipelineCtx().getLogger()); |
| 95 | + if (threads != null) |
| 96 | + { |
| 97 | + args.add("--threads"); |
| 98 | + args.add(threads.toString()); |
| 99 | + } |
| 100 | + |
| 101 | + String dbName = getProvider().getParameterByName(DB_PARAM).extractValue(getPipelineCtx().getJob(), getProvider(), getStepIdx(), String.class); |
| 102 | + if (dbName == null) |
| 103 | + { |
| 104 | + throw new PipelineJobException("Missing DB name"); |
| 105 | + } |
| 106 | + |
| 107 | + File binDir = FileUtil.appendName(new File(PipelineJobService.get().getAppProperties().getToolsDirectory()), "kraken2_dbs"); |
| 108 | + if (!binDir.exists()) |
| 109 | + { |
| 110 | + throw new PipelineJobException("Unable to find kraken2 DB dir, expected: " + binDir.getAbsolutePath()); |
| 111 | + } |
| 112 | + |
| 113 | + File dbDir = FileUtil.appendName(binDir, dbName); |
| 114 | + if (!dbDir.exists()) |
| 115 | + { |
| 116 | + throw new PipelineJobException("Unable to find kraken2 DB dir, expected: " + dbDir.getAbsolutePath()); |
| 117 | + } |
| 118 | + |
| 119 | + args.add("--use-names"); |
| 120 | + |
| 121 | + args.add("--db"); |
| 122 | + args.add(dbDir.getAbsolutePath()); |
| 123 | + |
| 124 | + args.addAll(getClientCommandArgs()); |
| 125 | + |
| 126 | + args.add("--output"); |
| 127 | + args.add("-"); |
| 128 | + |
| 129 | + String mode = getProvider().getParameterByName(MODE_PARAM).extractValue(getPipelineCtx().getJob(), getProvider(), getStepIdx(), String.class); |
| 130 | + |
| 131 | + File classifiedOutputBase = FileUtil.appendName(outputDir, SequenceAnalysisService.get().getUnzippedBaseName(inputFile.getName()) + ".classified"); |
| 132 | + File unclassifiedOutputBase = FileUtil.appendName(outputDir, SequenceAnalysisService.get().getUnzippedBaseName(inputFile.getName()) + ".unclassified"); |
| 133 | + if ("Classified".equals(mode)) |
| 134 | + { |
| 135 | + args.add("--classified-out"); |
| 136 | + args.add(classifiedOutputBase.getPath() + "#.fq"); |
| 137 | + } |
| 138 | + else |
| 139 | + { |
| 140 | + args.add("--unclassified-out"); |
| 141 | + args.add(unclassifiedOutputBase.getPath() + "#.fq"); |
| 142 | + } |
| 143 | + |
| 144 | + File reportFile = FileUtil.appendName(outputDir, SequencePipelineService.get().getUnzippedBaseName(inputFile.getName()) + ".kraken2.report.txt"); |
| 145 | + args.add("--report"); |
| 146 | + args.add(reportFile.getPath()); |
| 147 | + |
| 148 | + args.add(inputFile.getPath()); |
| 149 | + if (inputFile2 != null) |
| 150 | + { |
| 151 | + args.add(inputFile2.getPath()); |
| 152 | + } |
| 153 | + |
| 154 | + getWrapper().execute(args); |
| 155 | + |
| 156 | + if ("Classified".equals(mode)) |
| 157 | + { |
| 158 | + File classified1 = new File(classifiedOutputBase.getPath() + "_1.fq"); |
| 159 | + File classified2 = inputFile2 == null ? null : new File(classifiedOutputBase.getPath() + "_2.fq"); |
| 160 | + if (!classified1.exists()) |
| 161 | + { |
| 162 | + throw new PipelineJobException("Classified file does not exist: " + classified1.getAbsolutePath()); |
| 163 | + } |
| 164 | + |
| 165 | + File compressed1 = Compress.compressGzip(classified1); |
| 166 | + output.addIntermediateFile(classified1); |
| 167 | + |
| 168 | + File compressed2 = classified2 == null ? null : Compress.compressGzip(classified2); |
| 169 | + if (classified2 != null) |
| 170 | + { |
| 171 | + output.addIntermediateFile(classified2); |
| 172 | + } |
| 173 | + |
| 174 | + output.setProcessedFastq(Pair.of(compressed1, compressed2)); |
| 175 | + } |
| 176 | + else |
| 177 | + { |
| 178 | + File unclassified1 = new File(unclassifiedOutputBase.getPath() + "_1.fq"); |
| 179 | + File unclassified2 = inputFile2 == null ? null : new File(unclassifiedOutputBase.getPath() + "_2.fq"); |
| 180 | + if (!unclassified1.exists()) |
| 181 | + { |
| 182 | + throw new PipelineJobException("Unclassified file does not exist: " + unclassified1.getAbsolutePath()); |
| 183 | + } |
| 184 | + |
| 185 | + File compressed1 = Compress.compressGzip(unclassified1); |
| 186 | + output.addIntermediateFile(unclassified1); |
| 187 | + |
| 188 | + File compressed2 = unclassified2 == null ? null : Compress.compressGzip(unclassified2); |
| 189 | + if (unclassified2 != null) |
| 190 | + { |
| 191 | + output.addIntermediateFile(unclassified2); |
| 192 | + } |
| 193 | + |
| 194 | + output.setProcessedFastq(Pair.of(compressed1, compressed2)); |
| 195 | + } |
| 196 | + |
| 197 | + return output; |
| 198 | + } |
| 199 | + |
| 200 | + public static class Kraken2Wrapper extends AbstractCommandWrapper |
| 201 | + { |
| 202 | + public Kraken2Wrapper(Logger log) |
| 203 | + { |
| 204 | + super(log); |
| 205 | + } |
| 206 | + |
| 207 | + public File getExe() |
| 208 | + { |
| 209 | + return SimpleScriptWrapper.resolveFileInPath("kraken2", null, true); |
| 210 | + } |
| 211 | + } |
| 212 | +} |
0 commit comments