Skip to content

Commit 63a4474

Browse files
committed
Allow subset to use dplyr
1 parent e39489a commit 63a4474

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

singlecell/src/org/labkey/singlecell/pipeline/singlecell/SubsetSeurat.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import org.labkey.api.singlecell.pipeline.SingleCellStep;
1010

1111
import java.util.ArrayList;
12-
import java.util.Arrays;
1312
import java.util.Collection;
1413
import java.util.HashSet;
1514
import java.util.List;
@@ -34,7 +33,8 @@ public Provider()
3433
put("height", 150);
3534
put("width", 600);
3635
put("delimiter", DELIM);
37-
}}, null)
36+
}}, null),
37+
ToolParameterDescriptor.create("useDplyr", "Use dplyr", "If checked, the subset will be executed using dplyr::filter rather than Seurat::subset. This should allow more complex expressions to be used, including negations", "checkbox", null, false)
3838
), List.of("/sequenceanalysis/field/TrimmingTextArea.js"), null);
3939
}
4040

@@ -71,6 +71,9 @@ protected List<String> loadChunkFromFile() throws PipelineJobException
7171

7272
final String[] values = val.split(DELIM);
7373

74+
ToolParameterDescriptor pd2 = getProvider().getParameterByName("useDplyr");
75+
final boolean useDplyr = pd2.extractValue(getPipelineCtx().getJob(), getProvider(), getStepIdx(), Boolean.class, false);
76+
7477
List<String> ret = new ArrayList<>();
7578
for (String line : super.loadChunkFromFile())
7679
{
@@ -82,15 +85,23 @@ protected List<String> loadChunkFromFile() throws PipelineJobException
8285

8386
ret.add("\tif (!is.null(seuratObj)) {");
8487
ret.add("\tprint(paste0('Subsetting dataset: ', datasetId, ' with the expression: " + subsetEscaped + "'))");
85-
ret.add("\t\tcells <- c()");
86-
ret.add("\t\ttryCatch({");
87-
ret.add("\t\t\tcells <- WhichCells(seuratObj, expression = " + subset + ")");
88-
ret.add("\t\t}, error = function(e){");
89-
ret.add("\t\t\tif (!is.null(e) && e$message == 'Cannot find cells provided') {");
90-
ret.add("\t\t\t\tprint(paste0('There were no cells remaining after the subset: ', '" + subsetEscaped + "'))");
91-
ret.add("\t\t\t}");
92-
ret.add("\t\t})");
93-
ret.add("");
88+
if (useDplyr)
89+
{
90+
ret.add("\t\t\tcells <- rownames(seuratObj@meta.data %>% dplyr::filter( " + subset + " ))");
91+
}
92+
else
93+
{
94+
ret.add("\t\tcells <- c()");
95+
ret.add("\t\ttryCatch({");
96+
ret.add("\t\t\tcells <- WhichCells(seuratObj, expression = " + subset + ")");
97+
ret.add("\t\t}, error = function(e){");
98+
ret.add("\t\t\tif (!is.null(e) && e$message == 'Cannot find cells provided') {");
99+
ret.add("\t\t\t\tprint(paste0('There were no cells remaining after the subset: ', '" + subsetEscaped + "'))");
100+
ret.add("\t\t\t}");
101+
ret.add("\t\t})");
102+
ret.add("");
103+
}
104+
94105
ret.add("\t\tif (length(cells) == 0) {");
95106
ret.add("\t\t\tprint(paste0('There were no cells after subsetting for dataset: ', datasetId, ', with subset: ', '" + subsetEscaped + "'))");
96107
ret.add("\t\t\tseuratObj <- NULL");

0 commit comments

Comments
 (0)