• LUHMES data
  • T cells data
  • Session Information

Last updated: 2022-08-31

Checks: 7 0

Knit directory: GSFA_analysis/

This reproducible R Markdown analysis was created with workflowr (version 1.7.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20220524) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version b709053. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/

Untracked files:
    Untracked:  Rplots.pdf
    Untracked:  analysis/check_Tcells_datasets.Rmd
    Untracked:  analysis/interpret_gsfa_LUHMES.Rmd
    Untracked:  analysis/interpret_gsfa_TCells.Rmd
    Untracked:  analysis/spca_LUHMES_data.Rmd
    Untracked:  analysis/test_seurat.Rmd
    Untracked:  code/gsfa_negctrl_job.sbatch
    Untracked:  code/music_LUHMES_Yifan.R
    Untracked:  code/plotting_functions.R
    Untracked:  code/run_gsfa_2groups_negctrl.R
    Untracked:  code/run_gsfa_negctrl.R
    Untracked:  code/run_music_LUHMES.R
    Untracked:  code/run_music_LUHMES_data.sbatch
    Untracked:  code/run_sceptre_LUHMES_data.sbatch
    Untracked:  code/run_sceptre_Tcells_stimulated_data.sbatch
    Untracked:  code/run_sceptre_Tcells_unstimulated_data.sbatch
    Untracked:  code/run_spca_LUHMES.R
    Untracked:  code/run_spca_TCells.R
    Untracked:  code/run_unguided_gsfa_LUHMES.R
    Untracked:  code/run_unguided_gsfa_LUHMES.sbatch
    Untracked:  code/run_unguided_gsfa_Tcells.R
    Untracked:  code/run_unguided_gsfa_Tcells.sbatch
    Untracked:  code/sceptre_LUHMES_data.R
    Untracked:  code/sceptre_Tcells_stimulated_data.R
    Untracked:  code/sceptre_Tcells_unstimulated_data.R
    Untracked:  code/seurat_sim_fpr_tpr.R
    Untracked:  code/unguided_GFSA_mixture_normal_prior.cpp

Unstaged changes:
    Modified:   analysis/sceptre_LUHMES_data.Rmd
    Modified:   code/run_sceptre_cropseq_data.sbatch
    Modified:   code/sceptre_analysis.R

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/check_gsfa_deg_exp.Rmd) and HTML (docs/check_gsfa_deg_exp.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd b709053 kevinlkx 2022-08-31 checked the distribution of DEGs by expression bins

Load necessary packages and data

library(data.table)
library(Matrix)
library(tidyverse)
library(ggplot2)
theme_set(theme_bw() + theme(plot.title = element_text(size = 14, hjust = 0.5),
                             axis.title = element_text(size = 14),
                             axis.text = element_text(size = 12),
                             legend.title = element_text(size = 13),
                             legend.text = element_text(size = 12),
                             panel.grid.minor = element_blank())
)
library(gridExtra)
library(ComplexHeatmap)
library(kableExtra)
library(WebGestaltR)

source("/project2/xinhe/yifan/Factor_analysis/analysis_website_for_Kevin/scripts/plotting_functions.R")

Set directories

res_dir <- "/project2/xinhe/kevinluo/GSFA/DEGs_by_expression_level"
dir.create(res_dir, recursive = TRUE, showWarnings = FALSE)

LUHMES data

Load processed gene expression matrix

data_folder <- "/project2/xinhe/yifan/Factor_analysis/LUHMES/"
scaled.gene_exp <- readRDS(paste0(data_folder,"processed_data/deviance_residual.merged_top_6k.corrected_4.scaled.rds"))

Load GSFA result

fit <- readRDS(paste0(data_folder,
                      "gsfa_output_detect_01/use_negctrl/All.gibbs_obj_k20.svd_negctrl.seed_14314.light.rds"))
gibbs_PM <- fit$posterior_means
lfsr_mat <- fit$lfsr[, -ncol(fit$lfsr)]
total_effect <- fit$total_effect[, -ncol(fit$total_effect)]
KO_names <- colnames(lfsr_mat)
guides <- KO_names[KO_names!="Nontargeting"]
if(!all.equal(rownames(lfsr_mat), rownames(scaled.gene_exp))){stop("Gene names not match!")}

mean_gene_exp <- rowMeans(scaled.gene_exp)
exp_breaks <- quantile(mean_gene_exp, probs = seq(0,1,0.2))
gene_exp_bins <- cut(mean_gene_exp, breaks = exp_breaks, labels = 1:5)
gene_exp_bins[is.na(gene_exp_bins)] <- 1
gene_exp_bins.df <- data.frame(geneID = rownames(scaled.gene_exp), mean_exp = mean_gene_exp, exp_bin = gene_exp_bins)
table(gene_exp_bins.df$exp_bin)

1 2 3 4 5 1200 1200 1200 1200 1200

lfsr_signif_num_bins <- data.frame()
for(l in 1:5){
  curr_genes <- gene_exp_bins.df[gene_exp_bins.df$exp_bin == l, ]$geneID
  cat(length(curr_genes), "genes in bin", l, "\n")
  curr_lfsr_mat <- lfsr_mat[curr_genes, ]
  curr_lfsr_signif_num <- colSums(curr_lfsr_mat < 0.05)
  lfsr_signif_num_bins <- rbind(lfsr_signif_num_bins, 
                                c(bin = l, curr_lfsr_signif_num))
}

1200 genes in bin 1 1200 genes in bin 2 1200 genes in bin 3 1200 genes in bin 4 1200 genes in bin 5

colnames(lfsr_signif_num_bins) <- c("bin", colnames(lfsr_mat))

lfsr_signif_num_bins.df <- tidyr::gather(lfsr_signif_num_bins[,c("bin", guides)], guide, num_genes, guides, factor_key=TRUE)
lfsr_signif_num_bins.df$bin <- factor(lfsr_signif_num_bins.df$bin, levels = 5:1, 
                                      labels = c("highest", "mid-high", "mid", "low-mid", "lowest"))

# pdf(file.path(res_dir, "LUHMES_stimulated_lfsr_signif_num_by_exp_bins.pdf"), width = 14, height = 3)
ggplot(lfsr_signif_num_bins.df, aes(x=guide, y=num_genes, fill=bin)) + 
  geom_bar(position="stack", stat="identity") +
  scale_fill_brewer(palette = "Greens") + 
  guides(fill=guide_legend(title="Gene expression level")) +
  labs(x = "Target genes",
       y = "Number of DEGs",
       title = "Number of DEGs detected by gene expression level") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 12),
        legend.position = "right",
        legend.text = element_text(size = 13))

# dev.off()

T cells data

Load processed gene expression matrix

data_folder <- "/project2/xinhe/yifan/Factor_analysis/Stimulated_T_Cells/"
scaled.gene_exp <- readRDS(paste0(data_folder,"processed_data/deviance_residual.all_T_cells_merged_top_6k.batch_uncorrected.rds"))

Load GSFA result

fit <- readRDS(paste0(data_folder,
                      "gsfa_output_detect_01/all_uncorrected_by_group.use_negctrl/All.gibbs_obj_k20.svd_negctrl.restart.light.rds"))
gibbs_PM <- fit$posterior_means
lfsr_mat1 <- fit$lfsr1[, -ncol(fit$lfsr1)]
lfsr_mat0 <- fit$lfsr0[, -ncol(fit$lfsr0)]
total_effect1 <- fit$total_effect1[, -ncol(fit$total_effect1)]
total_effect0 <- fit$total_effect0[, -ncol(fit$total_effect0)]
KO_names <- colnames(lfsr_mat1)
guides <- KO_names[KO_names!="NonTarget"]
lfsr_mat <- lfsr_mat1

if(!all.equal(rownames(lfsr_mat), rownames(scaled.gene_exp))){stop("Gene names not match!")}

mean_gene_exp <- rowMeans(scaled.gene_exp)
exp_breaks <- quantile(mean_gene_exp, probs = seq(0,1,0.2))
gene_exp_bins <- cut(mean_gene_exp, breaks = exp_breaks, labels = 1:5)
gene_exp_bins[is.na(gene_exp_bins)] <- 1
gene_exp_bins.df <- data.frame(geneID = rownames(scaled.gene_exp), mean_exp = mean_gene_exp, exp_bin = gene_exp_bins)
table(gene_exp_bins.df$exp_bin)

1 2 3 4 5 1200 1200 1200 1200 1200

lfsr_signif_num_bins <- data.frame()
for(l in 1:5){
  curr_genes <- gene_exp_bins.df[gene_exp_bins.df$exp_bin == l, ]$geneID
  cat(length(curr_genes), "genes in bin", l, "\n")
  curr_lfsr_mat <- lfsr_mat[curr_genes, ]
  curr_lfsr_signif_num <- colSums(curr_lfsr_mat < 0.05)
  lfsr_signif_num_bins <- rbind(lfsr_signif_num_bins, 
                                c(bin = l, curr_lfsr_signif_num))
}

1200 genes in bin 1 1200 genes in bin 2 1200 genes in bin 3 1200 genes in bin 4 1200 genes in bin 5

colnames(lfsr_signif_num_bins) <- c("bin", colnames(lfsr_mat))

lfsr_signif_num_bins.df <- tidyr::gather(lfsr_signif_num_bins[,c("bin", guides)], guide, num_genes, guides, factor_key=TRUE)
lfsr_signif_num_bins.df$bin <- factor(lfsr_signif_num_bins.df$bin, levels = 5:1, 
                                      labels = c("highest", "mid-high", "mid", "low-mid", "lowest"))

# pdf(file.path(res_dir, "Tcells_stimulated_lfsr_signif_num_by_exp_bins.pdf"), width = 14, height = 3)
ggplot(lfsr_signif_num_bins.df, aes(x=guide, y=num_genes, fill=bin)) + 
  geom_bar(position="stack", stat="identity") +
  scale_fill_brewer(palette = "Greens") + 
  guides(fill=guide_legend(title="Gene expression level")) +
  labs(x = "Target genes",
       y = "Number of DEGs",
       title = "Number of DEGs detected by gene expression level") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 12),
        legend.position = "right",
        legend.text = element_text(size = 13))

# dev.off()

Session Information

sessionInfo()
R version 4.0.4 (2021-02-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Scientific Linux 7.4 (Nitrogen)

Matrix products: default
BLAS/LAPACK: /software/openblas-0.3.13-el7-x86_64/lib/libopenblas_haswellp-r0.3.13.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
 [1] lattice_0.20-45      WebGestaltR_0.4.4    kableExtra_1.3.4    
 [4] ComplexHeatmap_2.6.2 gridExtra_2.3        forcats_0.5.1       
 [7] stringr_1.4.0        dplyr_1.0.8          purrr_0.3.4         
[10] readr_2.1.2          tidyr_1.2.0          tibble_3.1.6        
[13] ggplot2_3.3.5        tidyverse_1.3.1      Matrix_1.4-1        
[16] data.table_1.14.2    workflowr_1.7.0     

loaded via a namespace (and not attached):
 [1] matrixStats_0.62.0  fs_1.5.2            lubridate_1.8.0    
 [4] doParallel_1.0.17   webshot_0.5.2       RColorBrewer_1.1-3 
 [7] httr_1.4.2          rprojroot_2.0.2     doRNG_1.8.2        
[10] tools_4.0.4         backports_1.4.1     bslib_0.3.1        
[13] utf8_1.2.2          R6_2.5.1            DBI_1.1.3          
[16] BiocGenerics_0.36.1 colorspace_2.0-3    GetoptLong_1.0.5   
[19] withr_2.5.0         tidyselect_1.1.2    processx_3.5.3     
[22] compiler_4.0.4      git2r_0.30.1        cli_3.3.0          
[25] rvest_1.0.2         Cairo_1.6-0         xml2_1.3.3         
[28] labeling_0.4.2      sass_0.4.1          scales_1.2.0       
[31] callr_3.7.0         apcluster_1.4.10    systemfonts_1.0.4  
[34] digest_0.6.29       svglite_2.0.0       rmarkdown_2.13     
[37] pkgconfig_2.0.3     htmltools_0.5.3     highr_0.9          
[40] dbplyr_2.1.1        fastmap_1.1.0       rlang_1.0.4        
[43] GlobalOptions_0.1.2 readxl_1.4.0        rstudioapi_0.13    
[46] farver_2.1.1        shape_1.4.6         jquerylib_0.1.4    
[49] generics_0.1.3      jsonlite_1.8.0      magrittr_2.0.3     
[52] Rcpp_1.0.9          munsell_0.5.0       S4Vectors_0.28.1   
[55] fansi_1.0.3         lifecycle_1.0.1     stringi_1.7.6      
[58] whisker_0.4         yaml_2.3.5          parallel_4.0.4     
[61] promises_1.2.0.1    crayon_1.5.1        haven_2.5.0        
[64] pander_0.6.5        circlize_0.4.15     hms_1.1.1          
[67] knitr_1.38          ps_1.7.1            pillar_1.8.0       
[70] igraph_1.3.4        rjson_0.2.21        rngtools_1.5.2     
[73] codetools_0.2-18    stats4_4.0.4        reprex_2.0.1       
[76] glue_1.6.2          evaluate_0.16       getPass_0.2-2      
[79] modelr_0.1.8        foreach_1.5.2       png_0.1-7          
[82] vctrs_0.4.1         tzdb_0.3.0          httpuv_1.6.5       
[85] cellranger_1.1.0    gtable_0.3.0        clue_0.3-60        
[88] assertthat_0.2.1    xfun_0.30           broom_0.8.0        
[91] later_1.3.0         viridisLite_0.4.0   iterators_1.0.14   
[94] IRanges_2.24.1      cluster_2.1.3       ellipsis_0.3.2     

sessionInfo()

R version 4.0.4 (2021-02-15) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Scientific Linux 7.4 (Nitrogen)

Matrix products: default BLAS/LAPACK: /software/openblas-0.3.13-el7-x86_64/lib/libopenblas_haswellp-r0.3.13.so

locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages: [1] grid stats graphics grDevices utils datasets methods
[8] base

other attached packages: [1] lattice_0.20-45 WebGestaltR_0.4.4 kableExtra_1.3.4
[4] ComplexHeatmap_2.6.2 gridExtra_2.3 forcats_0.5.1
[7] stringr_1.4.0 dplyr_1.0.8 purrr_0.3.4
[10] readr_2.1.2 tidyr_1.2.0 tibble_3.1.6
[13] ggplot2_3.3.5 tidyverse_1.3.1 Matrix_1.4-1
[16] data.table_1.14.2 workflowr_1.7.0

loaded via a namespace (and not attached): [1] matrixStats_0.62.0 fs_1.5.2 lubridate_1.8.0
[4] doParallel_1.0.17 webshot_0.5.2 RColorBrewer_1.1-3 [7] httr_1.4.2 rprojroot_2.0.2 doRNG_1.8.2
[10] tools_4.0.4 backports_1.4.1 bslib_0.3.1
[13] utf8_1.2.2 R6_2.5.1 DBI_1.1.3
[16] BiocGenerics_0.36.1 colorspace_2.0-3 GetoptLong_1.0.5
[19] withr_2.5.0 tidyselect_1.1.2 processx_3.5.3
[22] compiler_4.0.4 git2r_0.30.1 cli_3.3.0
[25] rvest_1.0.2 Cairo_1.6-0 xml2_1.3.3
[28] labeling_0.4.2 sass_0.4.1 scales_1.2.0
[31] callr_3.7.0 apcluster_1.4.10 systemfonts_1.0.4
[34] digest_0.6.29 svglite_2.0.0 rmarkdown_2.13
[37] pkgconfig_2.0.3 htmltools_0.5.3 highr_0.9
[40] dbplyr_2.1.1 fastmap_1.1.0 rlang_1.0.4
[43] GlobalOptions_0.1.2 readxl_1.4.0 rstudioapi_0.13
[46] farver_2.1.1 shape_1.4.6 jquerylib_0.1.4
[49] generics_0.1.3 jsonlite_1.8.0 magrittr_2.0.3
[52] Rcpp_1.0.9 munsell_0.5.0 S4Vectors_0.28.1
[55] fansi_1.0.3 lifecycle_1.0.1 stringi_1.7.6
[58] whisker_0.4 yaml_2.3.5 parallel_4.0.4
[61] promises_1.2.0.1 crayon_1.5.1 haven_2.5.0
[64] pander_0.6.5 circlize_0.4.15 hms_1.1.1
[67] knitr_1.38 ps_1.7.1 pillar_1.8.0
[70] igraph_1.3.4 rjson_0.2.21 rngtools_1.5.2
[73] codetools_0.2-18 stats4_4.0.4 reprex_2.0.1
[76] glue_1.6.2 evaluate_0.16 getPass_0.2-2
[79] modelr_0.1.8 foreach_1.5.2 png_0.1-7
[82] vctrs_0.4.1 tzdb_0.3.0 httpuv_1.6.5
[85] cellranger_1.1.0 gtable_0.3.0 clue_0.3-60
[88] assertthat_0.2.1 xfun_0.30 broom_0.8.0
[91] later_1.3.0 viridisLite_0.4.0 iterators_1.0.14
[94] IRanges_2.24.1 cluster_2.1.3 ellipsis_0.3.2