Initialization

library(ggplot2)
library(ggrastr)
library(dplyr)
library(parallel)
library(dropestr)
library(dropEstAnalysis)

theme_set(theme_base)

kDataFolder <- '../../data/dropest/'
kEstFolder <- paste0(kDataFolder, 'SCG29/')
kSubfolders <- c(human='est_2018_01_24_human/cell.counts.rds', 
                 mouse='est_2018_01_24_mouse/cell.counts.rds')

FillNa <- function(data, value=0) {
  data[is.na(data)] <- value
  return(data)
}
holders <- mclapply(paste0(kEstFolder, kSubfolders), readRDS, mc.cores=2) %>% 
  setNames(names(kSubfolders))

umis_per_cb <- lapply(holders, function(h) Matrix::colSums(h$cm_raw) %>% 
                        as.data.frame() %>% tibble::rownames_to_column("Barcode"))

umis_per_cb <- dplyr::full_join(umis_per_cb$human, umis_per_cb$mouse, "Barcode") %>% 
  `colnames<-`(c('Barcode', 'Human', 'Mouse')) %>% FillNa()

reads_per_chr <- lapply(holders, function(h) 
  FillNa(h$reads_per_chr_per_cells$Exon[umis_per_cb$Barcode,]))

mixture_df <- umis_per_cb %>%
  mutate(MitReads = reads_per_chr$human$chrM + reads_per_chr$mouse$chrM,
         TotalReads = rowSums(reads_per_chr$human) + rowSums(reads_per_chr$mouse),
         MitochondrionFraction = MitReads / TotalReads,
         Total = Human + Mouse, Organism=ifelse(Human > Mouse, "Human", "Mouse"),
         IsReal = order(Total, decreasing=T) <= 1000)

Origin of mixed reads

ggplot(mixture_df) + 
  geom_point(aes(x=Total, y=pmin(Human, Mouse) / Total, color=Organism), size=0.3, alpha=0.2) +
  scale_x_log10(name='Real UMIs', limits=c(10, 2e5)) + annotation_logticks() + ylab('Fraction of mixed UMIs') +
  theme_pdf(legend.pos=c(1, 1))
plot_frac <- mixture_df$MitochondrionFraction %>% setNames(mixture_df$Barcode)
smoothScatter(plot_frac[mixture_df$Barcode[order(mixture_df$Total, decreasing=T)]][1:10000])

# plot_frac[plot_frac > 0.2] <- 0.2
ggplot(mixture_df) + 
  geom_point(aes(x=Mouse, y=Human, color=plot_frac[Barcode]), size=0.2, alpha=0.3) +
  geom_abline(aes(slope=1, intercept=0), linetype='dashed', alpha=0.5) +
  scale_x_log10() + scale_y_log10() + annotation_logticks() +
  scale_color_gradientn(colours=c("#1200ba", "#347fff", "#cc4000", "#ff3333"), values=scales::rescale(c(0, 0.1, 0.3, 0.8))) +
  guides(color=guide_colorbar(direction="horizontal", title="Mitochondrial\nfraction", barwidth=unit(1.3, units="in"))) +
  theme_pdf() + theme(legend.margin=margin(l=3, r=3, unit="pt"), legend.position="bottom")


VPetukhov/dropEstAnalysis documentation built on Dec. 28, 2019, 8:16 p.m.