Skip to content

Commit

Permalink
more steps for full Seurat4 support in exportToCellbrowser
Browse files Browse the repository at this point in the history
  • Loading branch information
maximilianh committed Jun 23, 2021
1 parent 4cc59db commit 8909195
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
22 changes: 16 additions & 6 deletions src/cbPyLib/cellbrowser/R/cellbrowser.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# Build a UCSC cell browser website from a \code{Seurat} object
#
NULL
#require(reticulate)
#require(Matrix)
#require(R.utils)

#' Used by \code{ExportToCellbrowser}:
#' Write a big sparse matrix to a .tsv.gz file by writing chunks, concating them with the Unix cat command,
#' then gziping the result. This does not work on Windows, we'd have to use the copy /b command there.
Expand Down Expand Up @@ -64,7 +60,6 @@ findMatrices = function(object, slotNames ) {
slotMatrices = list()
slots <- list()
for (slotName in slotNames) {
message("!!", slotName)
mat <- GetAssayData(object = object, slot = slotName)
if (slotName == "scale.data")
slotName <- "scale" # dots in filenames are not good
Expand All @@ -88,6 +83,9 @@ saveMatrix <- function(counts, dir, prefix, use.mtx) {
# Export expression matrix
message("Writing matrix with prefix ",prefix," to directory ",dir, ", use.mtx is", use.mtx)
too.big = ((((ncol(counts)/1000)*(nrow(counts)/1000))>2000) && is(counts, 'sparseMatrix'))
if (prefix!="" && !endsWith(prefix, "_"))
prefix <- paste0(prefix, "_")

if (use.mtx || too.big) {
# we have to write the matrix to an mtx file
matrixPath <- file.path(dir, paste(prefix, "matrix.mtx", sep=""))
Expand Down Expand Up @@ -349,7 +347,14 @@ ExportToCellbrowser <- function(
if (length(levels(idents)) > 1) {
markers.helper <- function(x) {
partition <- markers[x,]
ord <- order(partition$p_val_adj < 0.05, -partition$avg_logFC)

# Seurat4 has changed the field name! grrrr...
if ("avg_log2FC" %in% colnames(markers))
avgs <- -partition$avg_log2FC
else
avgs <- -partition$avg_logFC

ord <- order(partition$p_val_adj < 0.05, avgs)
res <- x[ord]
naCount <- max(0, length(x) - markers.n)
res <- c(res[1:markers.n], rep(NA, naCount))
Expand Down Expand Up @@ -390,6 +395,11 @@ ExportToCellbrowser <- function(
else
matSep = "_"

# we assume that any slotname is possible. (in the wild, only 'counts' and 'scale.data' seem to occur, but we tolerate others)
matrixNames <- names(slotMatrices)
matrixLabels <- matrixNames
matrixLabels[matrixLabels=="counts"] <- "read counts"
matrixLabels[matrixLabels=="scale.data"] <- "scaled"
matrices.conf <- sprintf(" {'label':'%s','fileName':'%s_exprMatrix.tsv.gz'}", names(slotMatrices), names(slotMatrices))

if (length(slotMatrices)==1)
Expand Down
17 changes: 4 additions & 13 deletions src/cbPyLib/cellbrowser/seurat.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,22 +454,13 @@ def readExportScript(cmds):
blockFound = False
for line in open(fname):
cmds.append(line.rstrip("\n"))
#if line=="# ---\n":
#blockFound = (not blockFound)
#continue
#if blockFound:
#cmds.append(line.rstrip("\n"))

# the exactly same R export source file is also part of seurat-wrappers now.
# we want to have only a single source code file, and seurat-wrappers code
# cannot use require, so we add the require commands here
cmds.insert(0, "require(Matrix)")
cmds.insert(0, "require(R.utils)")
cmds.insert(0, "require(reticulate)")
#require(Matrix)
#require(R.utils)

# the R export function is also part of seurat-wrappers.
# we want to have only a single source code file, and seurat-wrappers code
# cannot use require, so it's commented out there.
#cmds = [l.replace("#require(", "require(") for l in cmds]
assert(len(cmds)!=0)
return cmds

Expand Down Expand Up @@ -533,7 +524,7 @@ def cbImportSeurat(inFname, outDir, datasetName, options):
skipMarkerStr = str(skipMarkers).upper()

if isDebugMode():
cmds.append("debug(ExportToCellbrowser)")
cmds.append("debugonce(ExportToCellbrowser)")

if clusterField is None:
clusterStr = 'NULL'
Expand Down

0 comments on commit 8909195

Please sign in to comment.