Skip to content

Commit

Permalink
Revising examine_pbmc68k_more.R to use k = 7 results.
Browse files Browse the repository at this point in the history
  • Loading branch information
pcarbo committed Aug 10, 2024
1 parent d305149 commit e739630
Showing 1 changed file with 29 additions and 55 deletions.
84 changes: 29 additions & 55 deletions analysis/examine_pbmc68k_more.R
Original file line number Diff line number Diff line change
@@ -1,77 +1,51 @@
# For paper, highlight results with K = 11.
#
# Or maybe highlight K = 7 instead?
#
# For paper, highlight results with K = 7.
library(fastTopics)
library(cowplot)
set.seed(1)
k <- 11
k <- 7
load("../data/pbmc_68k.RData")
rm(counts)
fit1 <- readRDS("../output/pbmc68k/rds/fit-pbmc68k-em-k=11.rds")$fit
fit2 <- readRDS("../output/pbmc68k/rds/fit-pbmc68k-scd-ex-k=11.rds")$fit
lda1 <- readRDS("../output/pbmc68k/rds/lda-pbmc68k-em-k=11.rds")$lda
lda2 <- readRDS("../output/pbmc68k/rds/lda-pbmc68k-scd-ex-k=11.rds")$lda
fit1 <- readRDS("../output/pbmc68k/rds/fit-pbmc68k-em-k=7.rds")$fit
fit2 <- readRDS("../output/pbmc68k/rds/fit-pbmc68k-scd-ex-k=7.rds")$fit
lda1 <- readRDS("../output/pbmc68k/rds/lda-pbmc68k-em-k=7.rds")$lda
lda2 <- readRDS("../output/pbmc68k/rds/lda-pbmc68k-scd-ex-k=7.rds")$lda
fit1 <- poisson2multinom(fit1)
fit2 <- poisson2multinom(fit2)
k <- 6
plot(lda1@gamma[,k],lda2@gamma[,k],pch = 20)
diag(cor(fit1$L,lda1@gamma))
diag(cor(fit2$L,lda2@gamma))
diag(cor(lda1@gamma,lda2@gamma))
diag(cor(fit1$F,fit2$F))

n <- nrow(fit1$L)
rows <- sample(n,2000)
fit1 <- select_loadings(fit1,rows)
fit2 <- select_loadings(fit2,rows)

# Try to cluster the cells.
L <- lda2@gamma
n <- nrow(L)
clusters <- rep("T cells + other",n)
names(clusters) <- rownames(fit1$L)
clusters[L[,4] > 0.3] <- "NK cells"
clusters[L[,8] > 0.4] <- "B cells"
clusters[L[,10] > 0.1] <- "dendritic"
clusters[L[,11] > 0.25] <- "myeloid"
clusters <- factor(clusters)
## L <- lda2@gamma
## n <- nrow(L)
## clusters <- rep("T cells + other",n)
## names(clusters) <- rownames(fit1$L)
## clusters[L[,4] > 0.3] <- "NK cells"
## clusters[L[,8] > 0.4] <- "B cells"
## clusters[L[,10] > 0.1] <- "dendritic"
## clusters[L[,11] > 0.25] <- "myeloid"
## clusters <- factor(clusters)

set.seed(1)
k <- 11
p1 <- structure_plot(lda1@gamma[rows,],topics = 1:k,
grouping = clusters[rows],gap = 10)
p2 <- structure_plot(lda2@gamma[rows,],topics = 1:k,
grouping = clusters[rows],gap = 10)
k <- 7
p1 <- structure_plot(fit1,topics = 1:k,gap = 10)
p2 <- structure_plot(fit2,topics = 1:k,gap = 10)
plot_grid(p1,p2,nrow = 2,ncol = 1)

# NK cells.
k <- 4
dat <- data.frame(gene = genes$symbol,
f0 = exp(apply(lda2@beta[-k,],2,max)),
f1 = exp(lda1@beta[k,]),
f2 = exp(lda2@beta[k,]))
dat <- transform(dat,lfc = log2(f2/f0))
subset(dat,lfc > 2.5 & f2 > 0.001)

# B cells.
k <- 8
dat <- data.frame(gene = genes$symbol,
f0 = exp(apply(lda2@beta[-k,],2,max)),
f1 = exp(lda1@beta[k,]),
f2 = exp(lda2@beta[k,]))
dat <- transform(dat,lfc = log2(f2/f0))
subset(dat,lfc > 2 & f2 > 0.0005)

# dendritic cells.
k <- 10
dat <- data.frame(gene = genes$symbol,
f0 = exp(apply(lda2@beta[-k,],2,max)),
f1 = exp(lda1@beta[k,]),
f2 = exp(lda2@beta[k,]))
dat <- transform(dat,lfc = log2(f2/f0))
subset(dat,lfc > 2 & f2 > 0.001)
stop()

# myeloid cells.
k <- 11
k <- 6
dat <- data.frame(gene = genes$symbol,
f0 = exp(apply(lda2@beta[-k,],2,max)),
f1 = exp(lda1@beta[k,]),
f2 = exp(lda2@beta[k,]))
f0 = apply(fit2$L[,-k],2,max),
f1 = fit1$L[,k],
f2 = fit2$L[,k])
dat <- transform(dat,lfc = log2(f2/f0))
subset(dat,lfc > 2.5 & f2 > 0.001)
subset(dat,lfc > 0.5 & f2 > 0.001)

0 comments on commit e739630

Please sign in to comment.