|
| 1 | +#--------------------------- |
| 2 | +# Annotate DNA lineage |
| 3 | +# 1. quantify any signature for single-cell using UCELL for the ciceroRNA (preferred) or the iRNA assay |
| 4 | +# 2. visualize signature on the DNA lineage tree ---- |
| 5 | + |
| 6 | +# Can use either iRNA or ciceroRNA |
| 7 | +# Can use either UCell or Seurat's addmodulescore |
| 8 | +#--------------------------- |
| 9 | +suppressPackageStartupMessages({ |
| 10 | + library(Seurat) |
| 11 | + library(Signac) |
| 12 | + library(tidyverse); library(forcats) |
| 13 | + library(fs) |
| 14 | + library(copykit) |
| 15 | + library(VennDiagram) |
| 16 | + library(SummarizedExperiment) |
| 17 | + library(ggpubr); library(ruok); library(scales) |
| 18 | + library(ComplexHeatmap) |
| 19 | +}) |
| 20 | +setwd("/volumes/USR1/yyan/project/coda") |
| 21 | +source('/volumes/USR1/yyan/project/coda/rsrc/utils.R') |
| 22 | + |
| 23 | +options <- commandArgs(trailingOnly = TRUE) |
| 24 | +if (length(options)>0) { |
| 25 | + cat('Reading user parameters:\n') |
| 26 | + sample_name <- options[[1]] |
| 27 | + rna_assay_use <- options[[2]] |
| 28 | + module_method <- options[[3]] |
| 29 | +} else { |
| 30 | + sample_name = 'DCIS35T' |
| 31 | + rna_assay_use <- 'iRNA' |
| 32 | + rna_assay_use <- 'ciceroRNA' |
| 33 | + module_method <- 'ucell' |
| 34 | + module_method <- 'seurat' |
| 35 | +} |
| 36 | + |
| 37 | +#------ input / output ------ |
| 38 | + |
| 39 | +dir_coda <- file.path( |
| 40 | + '/volumes/USR1/yyan/project/coda', |
| 41 | + 'rds_coda_ready', sample_name, 'aneuploid_epi') |
| 42 | + |
| 43 | + |
| 44 | +load_coda(dir_coda) # obja; objd; df_meta |
| 45 | +# consensus_z <- 'clones' |
| 46 | +print(obja) |
| 47 | + |
| 48 | +#------ load ciceroRNA ------ |
| 49 | +f_crna <- file.path(dir_coda, 'cicero', 'ciceroRNA_assay.seurat.rds') |
| 50 | +crna <- try(read_rds(f_crna)) |
| 51 | + |
| 52 | +#------ choose RNA assay ------ |
| 53 | + |
| 54 | +if ( 'try-error' %in% class(crna)) { |
| 55 | + DefaultAssay(obja) <- 'iRNA' |
| 56 | +} else { |
| 57 | + stopifnot( identical(Cells(crna), Cells(obja)) ) |
| 58 | + obja[['ciceroRNA']] <- crna[['ciceroRNA']] |
| 59 | + DefaultAssay(obja) <- 'ciceroRNA' |
| 60 | +} |
| 61 | + |
| 62 | +try(DefaultAssay(obja) <- rna_assay_use) |
| 63 | + |
| 64 | +cat('ATAC uses RNA assay = ', DefaultAssay(obja), '\n') |
| 65 | +print(obja) |
| 66 | +#------ choose pathways ------ |
| 67 | + |
| 68 | +if (T) { |
| 69 | + library(msigdbr) |
| 70 | + db_msigdb_H <- msigdbr(species = "Homo sapiens", category = "H") %>% |
| 71 | + dplyr::select(gs_name, gene_symbol) |
| 72 | + db_msigdb_H <- deframe_to_list(as.data.frame(db_msigdb_H)) |
| 73 | + names(db_msigdb_H) <- make.names(names(db_msigdb_H)) |
| 74 | +} |
| 75 | + |
| 76 | +db = db_msigdb_H |
| 77 | +name_db <- 'msigdb_hallmark' |
| 78 | + |
| 79 | +#------ setup dir_res ------ |
| 80 | + |
| 81 | +dir_res <- file.path( |
| 82 | + dir_coda, sprintf('%s_%s_%s', |
| 83 | + module_method, |
| 84 | + name_db, |
| 85 | + DefaultAssay(obja))) |
| 86 | +fs::dir_create(dir_res) |
| 87 | + |
| 88 | +#------ run UCELL ------ |
| 89 | +if (module_method == 'ucell') { |
| 90 | + library(UCell) |
| 91 | + if (! file.exists(file.path(dir_res, 'addmodulescore.df.rds'))) { |
| 92 | + # if (T) { ## force to rerun |
| 93 | + message('Run AddModuleScore_UCell') |
| 94 | + obja <- AddModuleScore_UCell(obja, features = db, name="_UCell") |
| 95 | + names(db) <- paste0(names(db), '_UCell') |
| 96 | + df_signature_res <- obja[[]][, names(db)] |
| 97 | + print(colnames(df_signature_res)) |
| 98 | + write.csv(x=df_signature_res, file = file.path(dir_res, 'addmodulescore.df.csv')) |
| 99 | + write_rds(x=df_signature_res, file.path(dir_res, 'addmodulescore.df.rds')) |
| 100 | + } else { |
| 101 | + message('Load AddModuleScore_UCell') |
| 102 | + df_signature_res <- read_rds(file.path(dir_res, 'addmodulescore.df.rds')) |
| 103 | + stopifnot(identical(rownames(df_signature_res), Cells(obja))) |
| 104 | + obja <- AddMetaData(obja, metadata = df_signature_res) |
| 105 | + names(db) <- paste0(names(db), '_UCell') |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +#------ Run AddModulescore ------ |
| 110 | + |
| 111 | +if (module_method == 'seurat') { |
| 112 | + if (! file.exists(file.path(dir_res, 'addmodulescore.df.rds'))) { |
| 113 | + # if (T) { ## force to rerun |
| 114 | + message('Run AddModuleScore') |
| 115 | + obja <- AddModuleScore(obja, features = db, name='seuratmodule') |
| 116 | + for ( tmp in 1:length(db) ) { |
| 117 | + obja@meta.data[paste0(names(db)[tmp], '_seurat')] <- obja@meta.data[paste0('seuratmodule', tmp)] |
| 118 | + obja@meta.data[paste0('seuratmodule', tmp)] <- NULL |
| 119 | + } |
| 120 | + names(db) <- paste0(names(db), '_seurat') |
| 121 | + df_signature_res <- obja[[]][, names(db)] |
| 122 | + print(colnames(df_signature_res)) |
| 123 | + write.csv( |
| 124 | + x=df_signature_res, |
| 125 | + file = file.path(dir_res, 'addmodulescore.df.csv')) |
| 126 | + write_rds( |
| 127 | + x=df_signature_res, |
| 128 | + file.path(dir_res, 'addmodulescore.df.rds')) |
| 129 | + } else { |
| 130 | + message('Load AddModuleScore') |
| 131 | + df_signature_res <- read_rds(file.path(dir_res, 'addmodulescore.df.rds')) |
| 132 | + stopifnot(identical(rownames(df_signature_res), Cells(obja))) |
| 133 | + obja <- AddMetaData(obja, metadata = df_signature_res) |
| 134 | + names(db) <- paste0(names(db), '_seurat') |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +#------ viz Featureplot ------ |
| 141 | +update_coda(df_meta, obja, objd) |
| 142 | + |
| 143 | +pdf(file.path(dir_res, sprintf('coda_featureplot.birdview_%s.pdf', name_db)), |
| 144 | + width = 8, height = 5, onefile = T, useDingbats = F) |
| 145 | +for (i in seq_along(names(db))) { |
| 146 | + print(coda_featureplot2(df = df_meta, feature = names(db)[i], pal='Plasma')) |
| 147 | +} |
| 148 | +dev.off() |
| 149 | + |
| 150 | + |
| 151 | +#------ viz VlnPlot ------ |
| 152 | +pal_cna_clones = new_palette_D(levels(objd@colData$clones), pal = 'stallion') |
| 153 | +n_clones <- length(unique(objd@colData$clones)) |
| 154 | +for (z in c('clones')) { |
| 155 | + message(z) |
| 156 | + pdf(file.path(dir_res, sprintf('vlnplot.birdview_%s.%s.pdf', name_db, z)), |
| 157 | + width = n_clones*0.9, height = 4, useDingbats = F, onefile = T) |
| 158 | + for (i in seq_along(names(db))) { |
| 159 | + p <- VlnPlot(obja, feature = names(db)[i], |
| 160 | + group.by = z, pt.size = 0.5) & |
| 161 | + labs(x = z) & |
| 162 | + scale_fill_manual(values = pal_cna_clones) & |
| 163 | + stat_compare_means(label = "p.format") & |
| 164 | + stat_summary(fun = 'mean', colour = "cyan", size = 2, geom = "point", pch=4) |
| 165 | + print(p) |
| 166 | + |
| 167 | + } |
| 168 | + dev.off() |
| 169 | +} |
| 170 | +names(db) |
| 171 | + |
| 172 | +cat('Done: ', sample_name, '-', rna_assay_use, '-', module_method, '\n') |
| 173 | + |
0 commit comments