#editor_options:
  #chunk_output_type: inline

load packages and data

library(conos)
library(parallel)
library(ggplot2)
library(Matrix)
library(data.table)
library(pagoda2)
library(cowplot)
library(dplyr)
library(abind)
library(tidyr)
require(Rtsne)
devtools::load_all('/home/larsc/SecretUtils')

con <- readRDS(file.path('/home/larsc/data/10x_preproced_graphed.rds'))
con <- Conos$new(con)
annot <- readRDS(file.path('/home/demharters/R/projects/UPF9_14_17_19_22_23_24_32_33/metadata_10x_final.rds'))
fraction_palette_eps <- c(epilepsy='hotpink', healthy='seagreen') 

'Fractional' changes.

FractionalPlot(annot$sample, annot$subtype, annot$condition, fraction.palette = fraction_palette_eps)
freq_df <- FractionalPlot(annot$sample, annot$subtype, annot$condition, return.plot=F) %>% 
  mutate(cond.sub=paste(condition, subtype, sep='_'))
ggplot(na.omit(freq_df),aes(x=subtype,y=freq, col=patient))+ 
  geom_point() + theme(axis.text.x = element_text(angle = 90, hjust = 1), axis.text.y = element_text(angle = 90, hjust = 0.5)) +
    xlab("") +ylab("fraction of total cells")+ theme(legend.position="right")

Distance matrix tsne plot

annot2 <- annot %>% mutate(cellid=rownames(annot))
source('/home/larsc/SecretUtils/R/peter_code_utils.R')
sub_dist_mat <- Makesubdistmat(con, annot2$sample, annot2$subtype, annot2$cellid)

## some weird shit
#testannot <- bind_cols(list(annot2$sample, annot2$subtype, annot2$cellid))
#tf_test <- setNames(testannot[, 2], testannot[, 3]) %>% as.factor # does not work
#tf_test <- setNames(testannot[[2]], testannot[[3]]) %>% as.factor
## end weird shit
PlotDistanceMatRed(sub_dist_mat, annot2$sample, annot2$subtype, annot2$sample, 
                   annot2$cellid, annot2$condition, perplexity=2, max_iter=1e4) # add labels

check the mat

PlotDistanceMatRed(sub_dist_mat, annot2$sample, annot2$subtype, annot2$sample, 
                   annot2$cellid, annot2$condition, perplexity=2, max_iter=1e4, get.mat=TRUE)

Condition distance distribution

ConditionDistanceDensity(sub_dist_mat, annot$sample, annot$subtype, annot$sample, annot2$cellid, annot$condition,
                         notch=F, fraction.palette = fraction_palette_eps)

Distribution distances per cell type (using ctdm so must be unweighted)

devtools::load_all('/home/larsc/SecretUtils')
min_cells=0
SecretUtils::PlotCellTypeDists(sub_dist_mat, min_cells, annot$sample, annot$condition)

Replacing JS distance matrix with paga metric Initialize some variables

annot$subtype_sample <- paste0(annot$subtype, '-', annot$sample)
membership_vec_subsamp <- as.numeric(factor(annot$subtype_sample))
membership_vec_sample <- as.numeric(factor(annot$sample))
raw_cm <- RbindRaw(con)
unaligned_graph <- SecretUtils::GenerateUnalignedAdj(raw_cm, annot2$cellid)
connectivities <- GetPagaMatrix(unaligned_graph, membership_vec_sample, scale=F)
mem_levels <- factor(annot$subtype_sample) %>% levels
subtype_order <- gsub("-.*","", mem_levels)
sample_order <- gsub(".*-","", mem_levels)
sample_order2 <- factor(annot$sample) %>% levels # sometimes care must be taken with special characters in the names
connectivities <- GetPagaMatrix(unaligned_graph, membership_vec_sample, scale=F) %>% as.matrix
rownames(connectivities) <- sample_order %>% unique; colnames(connectivities) <- sample_order %>% unique
SecretUtils::PlotDistanceMatRed(log(connectivities), annot2$sample, annot2$subtype, annot2$sample, 
                   annot2$cellid, annot2$condition, perplexity=2, max_iter=1e5, by.subtype=F)
SecretUtils::ConditionDistanceDensity(connectivities, annot$sample, annot$subtype, annot$sample, annot2$cellid, annot$condition,
                         notch=F, fraction.palette = fraction_palette_eps, by.subtype=F)

Between conditions intersample distance per subtype, log transformed. Some -Inf because of 0 values being logged

connectivities <- GetPagaMatrix(unaligned_graph, membership_vec_subsamp, scale=F) %>% as.matrix
sub_paga_mats <- SecretUtils::SubsetPAGABySubtype(connectivities, mem_levels, sample_order, subtype_order)
# pad the matrices to have same dim
sub_paga_mats_padded <- sub_paga_mats %>% lapply(PadMatZeroes, sample_order %>% unique) %>% lapply(as.matrix)

SecretUtils::PlotCellTypeDists(sub_paga_mats_padded %>% lapply(log), min_cells, annot$sample, annot$condition, cell.count.attr=F)
between_conds_df <- SecretUtils::PlotCellTypeDists(sub_paga_mats_padded %>% lapply(log), min_cells, annot$sample, annot$condition, plot=F, cell.count.attr=F)


githubz0r/SecretUtils documentation built on May 15, 2021, 10:29 p.m.