Skip to content

Commit a5d33db

Browse files
Add percent by pathology to columns
1 parent 4323416 commit a5d33db

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

R/deg.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,15 @@ deg.prepare <- function(se, pathology, case, control, sample.col, filter_only_ca
191191
logCPM = edgeR::cpmByGroup(pb, SummarizedExperiment::colData(pb)[[pathology]], log=TRUE)
192192
colnames(logCPM) = paste0("logCPM_", colnames(logCPM))
193193
SummarizedExperiment::rowData(se) = cbind(SummarizedExperiment::rowData(se), as.data.frame(logCPM))
194+
### Get % expressing info
195+
if ("Percent" %in% names(SummarizedExperiment::assays(pb))) {
196+
Percent <- SummarizedExperiment::assays(pb)$Percent %*% make_pseudobulk(SummarizedExperiment::colData(pb)[[pathology]])
197+
pf <- as.data.frame(as.matrix(Percent))
198+
colnames(pf) <- paste0("Percent_", colnames(pf))
199+
for (cn in names(pf)) {
200+
rowData(se)[[paste0("Percent", cn)]] <- pf[[cn]]
201+
}
202+
}
194203
### Convert pathology to factor
195204
SummarizedExperiment::colData(se)[[pathology]] = as.factor(as.character(cd[[pathology]]))
196205
### Check matrix

R/sc.R

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ se_make_pseudobulk <- function(se, on, missing.levels=FALSE, unlevel=TRUE, ncell
1515
P = lapply(A, function(mat) {
1616
mat %*% to_P
1717
})
18+
if ("counts" %in% names(A)) {
19+
Percent <- as.matrix((A$counts > 0) %*% to_P) %*% diag(100/colSums(to_P))
20+
dimnames(Percent) <- dimnames(P$counts)
21+
P$Percent <- Percent
22+
}
1823
cd[[on]] = factor(as.character(cd[[on]]), levels=colnames(to_P))
1924
pcd = data.frame(row.names=levels(cd[[on]]))
2025
pcd[[ncell_col]] <- Matrix::colSums(to_P)
@@ -270,9 +275,11 @@ se_concat <- function(se.list) {
270275

271276
se_as_Seurat <- function(sce) {
272277
C = SummarizedExperiment::assays(sce)$counts
278+
dimnames(C) <- dimnames(sce)
273279
cd = SummarizedExperiment::colData(sce)
280+
rownames(cd) <- colnames(sce)
274281
s.obj = Seurat::CreateSeuratObject(counts=C, meta.data=as.data.frame(cd))
275-
s.obj[["umap"]] <- Seurat::CreateDimReducObject(
282+
s.obj[["UMAP"]] <- Seurat::CreateDimReducObject(
276283
embeddings = SingleCellExperiment::reducedDims(sce)$X_umap,
277284
key = "UMAP_", assay = "RNA")
278285
s.obj[["X_pca"]] <- Seurat::CreateDimReducObject(
@@ -285,5 +292,6 @@ se_as_Seurat <- function(sce) {
285292
ad_as_Seurat <- function(h5ad) {
286293
s.obj = anndataR::read_h5ad(h5ad, to="Seurat")
287294
obsm = rhdf5::h5read(h5ad, "obsm")
288-
s.obj[["X_umap"]] <- Seurat::CreateDimReducObject(embeddings=t(obsm$X_umap), key="UMAP_", assay="RNA")
295+
colnames(obsm$X_umap) <- rhdf5::h5read(h5ad, "/obs/_index")
296+
s.obj[["UMAP"]] <- Seurat::CreateDimReducObject(embeddings=t(obsm$X_umap), key="UMAP_", assay="RNA")
289297
}

0 commit comments

Comments
 (0)