From 7240a53c38916d343dd0fa9db88ec438a9611ead Mon Sep 17 00:00:00 2001 From: Kelvin <26215587+zktuong@users.noreply.github.com> Date: Fri, 24 Jun 2022 22:34:34 +0100 Subject: [PATCH] update documentations --- R/plot_cpdb2.R | 2 +- R/plot_cpdb3.R | 2 +- README.md | 149 ++++++------------------------------------------- 3 files changed, 20 insertions(+), 133 deletions(-) diff --git a/R/plot_cpdb2.R b/R/plot_cpdb2.R index 1e5d815..3e5e92a 100644 --- a/R/plot_cpdb2.R +++ b/R/plot_cpdb2.R @@ -38,7 +38,7 @@ plot_cpdb2 <- function(cell_type1, cell_type2, scdata, idents, means, pvals, dec desiredInteractions = NULL, interaction_grouping = NULL, edge_group_colors = NULL, node_group_colors = NULL, version3 = FALSE, return_df = FALSE, ...) { if (class(scdata) == "Seurat") { - stop("Sorry not supported yet. Please use a SingleCellExperiment object.") + stop("Sorry not supported. Please use a SingleCellExperiment object.") } if (length(separator) > 0) { sep = separator diff --git a/R/plot_cpdb3.R b/R/plot_cpdb3.R index 15f6637..4cb7992 100644 --- a/R/plot_cpdb3.R +++ b/R/plot_cpdb3.R @@ -41,7 +41,7 @@ plot_cpdb3 <- function(cell_type1, cell_type2, scdata, idents, means, pvals, dec alpha = 0.5, edge_colors = NULL, grid_colors = NULL, show_legend = TRUE, legend.pos.x = 20, legend.pos.y = 20, ...) { if (class(scdata) == "Seurat") { - stop("Sorry not supported yet. Please use a SingleCellExperiment object.") + stop("Sorry not supported. Please use a SingleCellExperiment object.") } if (length(separator) > 0) { sep = separator diff --git a/README.md b/README.md index f915b72..5877b4a 100644 --- a/README.md +++ b/README.md @@ -102,29 +102,6 @@ if ```genes``` and ```gene.family``` are both not specified, the function will t Specifying ```keep_significant_only``` will only keep those that are p<0.05 (which you can try to adjust with ```p.adjust.method```). -### plot_cpdb3 -Generates a chord diagram inspired from CellChat's way of showing the data! - -```R -library(ktplots) -data(kidneyimmune) -data(cpdb_output2) - -p <- plot_cpdb3(cell_type1 = 'B cell', cell_type2 = 'CD4T cell|MNPd', - scdata = kidneyimmune, - idents = 'celltype', # column name where the cell ids are located in the metadata - means = means2, - pvals = pvals2, - deconvoluted = decon2, # new options from here on specific to plot_cpdb3 - keep_significant_only = TRUE, - standard_scale = TRUE, - remove_self = TRUE - ) -p -``` - -![plot_cpdb3](exampleImages/plot_cpdb3.png) - ### plot_cpdb2 Generates a circos-style wire/arc/chord plot for cellphonedb results. @@ -215,119 +192,29 @@ test <- plot_cpdb2(cell_type1 = "CD4_Tem|CD4_Tcm|CD4_Treg", # same usage style a ``` ![plot_cpd2](exampleImages/plot_cpdb2.png) -## New feature - -### compare_cpdb/plot_compare_cpdb -These functions piggy-back on the original `plot_cpdb` function and generates the results from cellphonedb that are run separately on replicates. The goal of this function is to able to compare interactions between groups. - -I've provided example datasets from randomnly selected `Healthy` and `Severe` samples from [Stephenson et al. COVID-19 single cell data set published in Nature Medicine 2021](https://www.nature.com/articles/s41591-021-01329-2). This should get it running and for you to see how the input data folder should be organised: -```R -data(covid_sample_metadata) -data(covid_cpdb_meta) -file <- system.file("extdata", "covid_cpdb.tar.gz", package = "ktplots") -# copy and unpack wherever you want this to end up -file.copy(file, ".") -system("tar -xzf covid_cpdb.tar.gz") -``` - -It requires 1) an input table like so: -```R -> covid_cpdb_meta - sample_id cellphonedb_folder sce_file -1 MH9143325 covid_cpdb/MH9143325/out covid_cpdb/MH9143325/sce.rds -2 MH9143320 covid_cpdb/MH9143320/out covid_cpdb/MH9143320/sce.rds -3 MH9143274 covid_cpdb/MH9143274/out covid_cpdb/MH9143274/sce.rds -4 MH8919226 covid_cpdb/MH8919226/out covid_cpdb/MH8919226/sce.rds -5 MH8919227 covid_cpdb/MH8919227/out covid_cpdb/MH8919227/sce.rds -6 newcastle49 covid_cpdb/newcastle49/out covid_cpdb/newcastle49/sce.rds -7 MH9179822 covid_cpdb/MH9179822/out covid_cpdb/MH9179822/sce.rds -8 MH8919178 covid_cpdb/MH8919178/out covid_cpdb/MH8919178/sce.rds -9 MH8919177 covid_cpdb/MH8919177/out covid_cpdb/MH8919177/sce.rds -10 MH8919176 covid_cpdb/MH8919176/out covid_cpdb/MH8919176/sce.rds -11 MH8919179 covid_cpdb/MH8919179/out covid_cpdb/MH8919179/sce.rds -12 MH9179826 covid_cpdb/MH9179826/out covid_cpdb/MH9179826/sce.rds -``` -where each row is a sample, the location of the `out` folder generated by cellphonedb and a single-cell object used to generate the cellphonedb result. If cellphonedb was ran with a `.h5ad` the sce_file would be the path to the `.h5ad` file. - -and 2) a sample metadata data frame for the tests to run: -```R -> covid_sample_metadata - sample_id Status_on_day_collection_summary -MH9143325 MH9143325 Severe -MH9143320 MH9143320 Severe -MH9143274 MH9143274 Severe -MH8919226 MH8919226 Healthy -MH8919227 MH8919227 Healthy -newcastle49 newcastle49 Severe -MH9179822 MH9179822 Severe -MH8919178 MH8919178 Healthy -MH8919177 MH8919177 Healthy -MH8919176 MH8919176 Healthy -MH8919179 MH8919179 Healthy -MH9179826 MH9179826 Severe -``` - -So if I want to compare between Severe vs Healthy, I would specify the function as follows: -```R -## set up the levels -covid_sample_metadata$Status_on_day_collection_summary <- factor(covid_sample_metadata$Status_on_day_collection_summary, levels = c('Healthy', 'Severe')) - -out <- compare_cpdb(cpdb_meta = covid_cpdb_meta, - sample_metadata = covid_sample_metadata, - celltypes = c("B_cell", "CD14", "CD16", "CD4", "CD8", "DCs", "MAIT", "NK_16hi", "NK_56hi", "Plasmablast", "Platelets", "Treg", "gdT", "pDC"), # the actual celltypes you want to test - celltype_col = "initial_clustering", # the column that holds the cell type annotation in the sce object - groupby = "Status_on_day_collection_summary") # the column in the sample_metadata that holds the column where you want to do the comparison. In this example, it's Severe vs Healthy -``` -This returns a list of dataframes (for each contrast found) with which you can use to plot the results. - -`plot_compare_cpdb` is a simple function to achieve that but you can always just make a custom plotting function based on what you want. - -```R -plot_compare_cpdb(out) # red is significantly increased in Severe compared to Healthy. -``` -![plot_compare_cpdb](exampleImages/plot_compare_cpdb_example.png) - -If there are multiple contrasts and groups, you can facet the plot by specifying `groups = c('group1', 'group2')`. +### plot_cpdb3 +Generates a chord diagram inspired from [CellChat](https://github.com/sqjin/CellChat)'s way of showing the data! +Usage is similar to `plot_cpdb2` but with reduced options. Additional kwargs are passed to `plot_cpdb`. ```R -# let's mock up a new contrast like this -covid_sample_metadata$Status_on_day_collection_summary <- c(rep('Severe', 3), rep('Healthy', 2), rep('notSevere', 2), rep('Healthy', 4), 'notSevere') -out <- compare_cpdb(cpdb_meta = covid_cpdb_meta, - sample_metadata = covid_sample_metadata, - celltypes = c("B_cell", "CD14", "CD16", "CD4", "CD8", "DCs", "MAIT", "NK_16hi", "NK_56hi", "Plasmablast", "Platelets", "Treg", "gdT", "pDC"), # the actual celltypes you want to test - celltype_col = "initial_clustering", # the column that holds the cell type annotation in the sce object - groupby = "Status_on_day_collection_summary") -plot_compare_cpdb(out, alpha = .1, groups = names(out)) # there's no significant hit at 0.05 in this dummy example -``` -![plot_compare_cpdb4](exampleImages/plot_compare_cpdb_example4.png) - -The default method uses a pairwise `wilcox.test`. Alternatives are pairwise Welch's `t.test` or a linear mixed model with `lmer`. - -To run the linear mixed effect model, it expects that the input data is paired (i.e an individual with multiple samples corresponding to multiple timepoints): +library(ktplots) +data(kidneyimmune) +data(cpdb_output2) -```R -# just as a dummy example, lets say the samples are matched where there are two samples per individual -covid_sample_metadata$individual <- rep(c("A", "B", "C", "D", "E", "F"), 2) - -# actually run it -out <- compare_cpdb(cpdb_meta = covid_cpdb_meta, - sample_metadata = covid_sample_metadata, - celltypes = c("B_cell", "CD14", "CD16", "CD4", "CD8", "DCs", "MAIT", "NK_16hi", - "NK_56hi", "Plasmablast", "Platelets", "Treg", "gdT", "pDC"), - celltype_col = "initial_clustering", - groupby = "Status_on_day_collection_summary", - formula = "~ Status_on_day_collection_summary + (1|individual)", # formula passed to lmer - method = "lmer") - -plot_compare_cpdb(out, contrast = 'Status_on_day_collection_summarySevere') # use the colnames(out) to pick the right column. +p <- plot_cpdb3(cell_type1 = 'B cell', cell_type2 = 'CD4T cell|MNPd', + scdata = kidneyimmune, + idents = 'celltype', # column name where the cell ids are located in the metadata + means = means2, + pvals = pvals2, + deconvoluted = decon2, # new options from here on specific to plot_cpdb3 + keep_significant_only = TRUE, + standard_scale = TRUE, + remove_self = TRUE + ) +p ``` -![plot_compare_cpdb2](exampleImages/plot_compare_cpdb_example2.png) -Specifying `cluster = TRUE` will move the rows and columns to make it look a bit more ordered. -```R -plot_compare_cpdb(out, contrast = 'Status_on_day_collection_summarySevere', cluster = TRUE) -``` -![plot_compare_cpdb3](exampleImages/plot_compare_cpdb_example3.png) +![plot_cpdb3](exampleImages/plot_cpdb3.png) ## Other useful functions