Skip to content

Commit e633d5d

Browse files
committed
Add simple function to run AverageSeurat
1 parent f02cc58 commit e633d5d

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

singlecell/resources/chunks/AvgExpression.R

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
1+
GenerateAveragedData <- function(seuratObj, groupFields, addMetadata) {
2+
if (addMetadata && !'cDNA_ID' %in% names(seuratObj@meta.data)) {
3+
stop('A field names cDNA_ID must exist when addMetadata=TRUE')
4+
}
5+
6+
if (addMetadata && !'cDNA_ID' %in% groupFields) {
7+
stop('When addMetadata=TRUE, cDNA_ID must be part of groupFields')
8+
}
9+
10+
meta <- unique(seuratObj@meta.data[,groupFields, drop = F])
11+
rownames(meta) <- apply(meta, 1, function(y){
12+
return(paste0(y, collapse = '_'))
13+
})
14+
15+
Idents(seuratObj) <- rownames(meta)
16+
17+
a <- Seurat::AverageExpression(seuratObj, return.seurat = T, verbose = F)
18+
a <- Seurat::AddMetaData(a, meta)
19+
20+
if (addMetadata) {
21+
a <- Rdiscvr::QueryAndApplyMetadataUsingCDNA(a)
22+
}
23+
24+
return(a)
25+
}
26+
127
for (datasetId in names(seuratObjects)) {
228
printName(datasetId)
329
seuratObj <- readRDS(seuratObjects[[datasetId]])
430

5-
df <- CellMembrane::AvgExpression(seuratObj, groupField = groupField)
6-
write.table(df, file = paste0(outputPrefix, '.', makeLegalFileName(datasetId), '.avg.', groupField, '.txt'), sep = '\t', row.names = FALSE, quote = FALSE)
7-
rm(df)
31+
seuratObj <- GenerateAveragedData(seuratObj, groupFields = groupFields, addMetadata = addMetadata)
32+
saveData(seuratObj, datasetId)
833

934
# Cleanup
1035
rm(seuratObj)

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import java.util.Arrays;
1010

11-
public class AvgExpression extends AbstractCellMembraneStep
11+
public class AvgExpression extends AbstractRDiscvrStep
1212
{
1313
public AvgExpression(PipelineContext ctx, AvgExpression.Provider provider)
1414
{
@@ -19,10 +19,15 @@ public static class Provider extends AbstractPipelineStepProvider<SingleCellStep
1919
{
2020
public Provider()
2121
{
22-
super("AvgExpression", "Avg. Expression", "Seurat", "This will run AverageExpression on the raw counts, producing a matrix with the average per group. This matrix will have a row labeled TotalCells appended, which is the total cells per group.", Arrays.asList(
23-
SeuratToolParameter.create("groupField", "Grouping Field", "This field will be used to group cells of the seurat object. For each unique value of this field, count averages will be computed and saved into a matrix with one column per group. Any cells lacking a value in this field will be discarded.", "textfield", new JSONObject(){{
22+
super("AvgExpression", "Avg. Expression", "Seurat", "This will run AverageExpression on the raw counts, grouping using the provided fields. It will generate a seurat object with aggregate counts.", Arrays.asList(
23+
SeuratToolParameter.create("groupFields", "Grouping Field(s)", "This field will be used to group cells of the seurat object. For each unique value of this field, count averages will be computed and saved into a matrix with one column per group. Any cells lacking a value in this field will be discarded.", "textfield", new JSONObject(){{
2424
put("allowBlank", false);
25-
}}, null)
25+
put("height", 150);
26+
put("delimiter", ",");
27+
}}, null).delimiter(","),
28+
SeuratToolParameter.create("addMetadata", "Query Metadata?", "If checked, Rdiscvr::QueryAndApplyMetadataUsingCDNA will be run after aggregation. This requires a cDNA_ID column to exist.", "checkbox", new JSONObject(){{
29+
put("checked", true);
30+
}}, true)
2631
), null, null);
2732
}
2833

@@ -42,7 +47,7 @@ public String getFileSuffix()
4247
@Override
4348
public boolean createsSeuratObjects()
4449
{
45-
return false;
50+
return true;
4651
}
4752
}
4853

0 commit comments

Comments
 (0)