|
| 1 | +package org.labkey.singlecell.pipeline.singlecell; |
| 2 | + |
| 3 | +import au.com.bytecode.opencsv.CSVReader; |
| 4 | +import org.json.JSONObject; |
| 5 | +import org.labkey.api.pipeline.PipelineJobException; |
| 6 | +import org.labkey.api.reader.Readers; |
| 7 | +import org.labkey.api.sequenceanalysis.SequenceOutputFile; |
| 8 | +import org.labkey.api.sequenceanalysis.pipeline.AbstractPipelineStepProvider; |
| 9 | +import org.labkey.api.sequenceanalysis.pipeline.PipelineContext; |
| 10 | +import org.labkey.api.sequenceanalysis.pipeline.SequenceOutputHandler; |
| 11 | +import org.labkey.api.singlecell.pipeline.SeuratToolParameter; |
| 12 | +import org.labkey.api.singlecell.pipeline.SingleCellStep; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Set; |
| 19 | +import java.util.stream.Collectors; |
| 20 | + |
| 21 | +public class RunVision extends AbstractCellMembraneStep |
| 22 | +{ |
| 23 | + public RunVision(PipelineContext ctx, RunVision.Provider provider) |
| 24 | + { |
| 25 | + super(provider, ctx); |
| 26 | + } |
| 27 | + |
| 28 | + public static class Provider extends AbstractPipelineStepProvider<SingleCellStep> |
| 29 | + { |
| 30 | + public Provider() |
| 31 | + { |
| 32 | + super("RunVision", "Run VISION", "VISION", "This will run VISION, a tool for functional interpretation of scRNA-seq data, saving the result to an RDS file.", Arrays.asList( |
| 33 | + SeuratToolParameter.create("metadataCols", "Field(s)", "This will subset the seuratObj to just these columns prior to running VISION.", "sequenceanalysis-trimmingtextarea", new JSONObject(){{ |
| 34 | + put("allowBlank", false); |
| 35 | + put("height", 200); |
| 36 | + put("delimiter", ","); |
| 37 | + }}, "nCount_RNA,ClusterNames_0.2,ClusterNames_0.4,ClusterNames_0.6,ClusterNames_0.8", "metadataCols", true, true).delimiter(",") |
| 38 | + ), Arrays.asList("/sequenceanalysis/field/TrimmingTextArea.js"), null); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public RunVision create(PipelineContext ctx) |
| 43 | + { |
| 44 | + return new RunVision(ctx, this); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public boolean createsSeuratObjects() |
| 50 | + { |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public String getFileSuffix() |
| 56 | + { |
| 57 | + return "vision"; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public Output execute(SequenceOutputHandler.JobContext ctx, List<SeuratObjectWrapper> inputObjects, String outputPrefix) throws PipelineJobException |
| 62 | + { |
| 63 | + Output output = super.execute(ctx, inputObjects, outputPrefix); |
| 64 | + |
| 65 | + File saved = new File(ctx.getOutputDir(), "visionFiles.txt"); |
| 66 | + if (!saved.exists()) |
| 67 | + { |
| 68 | + throw new PipelineJobException("Unable to find file: " + saved.getPath()); |
| 69 | + } |
| 70 | + |
| 71 | + try (CSVReader reader = new CSVReader(Readers.getReader(saved), '\t')) |
| 72 | + { |
| 73 | + String[] line; |
| 74 | + while ((line = reader.readNext()) != null) |
| 75 | + { |
| 76 | + File rds = new File(ctx.getOutputDir(), line[1]); |
| 77 | + if (!rds.exists()) |
| 78 | + { |
| 79 | + throw new PipelineJobException("Unable to find file: " + rds.getPath()); |
| 80 | + } |
| 81 | + |
| 82 | + final String datasetId = line[0]; |
| 83 | + Set<SeuratObjectWrapper> wrappers = inputObjects.stream().filter(x -> datasetId.equals(x.getDatasetId())).collect(Collectors.toSet()); |
| 84 | + if (wrappers.size() == 0) |
| 85 | + { |
| 86 | + throw new PipelineJobException("Unable to find seurat object wrapper for: " + datasetId); |
| 87 | + } |
| 88 | + else if (wrappers.size() > 1) |
| 89 | + { |
| 90 | + throw new PipelineJobException("More than one seurat object wrapper matched: " + datasetId + ", found: " + wrappers.stream().map(SeuratObjectWrapper::getDatasetId).collect(Collectors.joining(", "))); |
| 91 | + } |
| 92 | + |
| 93 | + SeuratObjectWrapper wrapper = wrappers.iterator().next(); |
| 94 | + |
| 95 | + SequenceOutputFile so = new SequenceOutputFile(); |
| 96 | + so.setFile(rds); |
| 97 | + so.setCategory("VISION Analysis"); |
| 98 | + so.setLibrary_id(ctx.getSequenceSupport().getCachedGenomes().iterator().next().getGenomeId()); |
| 99 | + so.setReadset(wrapper.getReadsetId()); |
| 100 | + so.setName(wrapper.getDatasetName() == null ? wrapper.getDatasetId() : wrapper.getDatasetName() + ": VISION Analysis"); |
| 101 | + |
| 102 | + ctx.getFileManager().addSequenceOutput(so); |
| 103 | + } |
| 104 | + } |
| 105 | + catch (IOException e) |
| 106 | + { |
| 107 | + throw new PipelineJobException(e); |
| 108 | + } |
| 109 | + |
| 110 | + ctx.getFileManager().addIntermediateFile(saved); |
| 111 | + |
| 112 | + return output; |
| 113 | + } |
| 114 | +} |
0 commit comments