Skip to content

Commit

Permalink
Added new functions
Browse files Browse the repository at this point in the history
  • Loading branch information
salsalsal97 committed Oct 17, 2024
1 parent 106b64f commit ba487cc
Show file tree
Hide file tree
Showing 16 changed files with 1,017 additions and 13 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ Imports:
utils,
gtools,
stringr,
gridExtra
gridExtra,
ggcorrplot
Suggests:
knitr,
rmarkdown,
Expand Down
8 changes: 7 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ importFrom(edgeR,calcNormFactors)
importFrom(edgeR,estimateDisp)
importFrom(edgeR,glmFit)
importFrom(edgeR,glmLRT)
importFrom(ggcorrplot,ggcorrplot)
importFrom(ggplot2,aes)
importFrom(ggplot2,element_blank)
importFrom(ggplot2,element_text)
importFrom(ggplot2,facet_wrap)
importFrom(ggplot2,geom_bar)
importFrom(ggplot2,geom_boxplot)
importFrom(ggplot2,geom_histogram)
importFrom(ggplot2,geom_hline)
importFrom(ggplot2,geom_jitter)
importFrom(ggplot2,geom_point)
Expand All @@ -33,6 +35,7 @@ importFrom(ggplot2,ggsave)
importFrom(ggplot2,ggtitle)
importFrom(ggplot2,labs)
importFrom(ggplot2,scale_colour_manual)
importFrom(ggplot2,scale_fill_gradient2)
importFrom(ggplot2,scale_shape_manual)
importFrom(ggplot2,stat_summary)
importFrom(ggplot2,theme)
Expand All @@ -45,11 +48,14 @@ importFrom(qs,qread)
importFrom(reshape2,melt)
importFrom(scales,percent)
importFrom(stats,as.formula)
importFrom(stats,complete.cases)
importFrom(stats,cor)
importFrom(stats,median)
importFrom(stats,model.matrix)
importFrom(stats,p.adjust)
importFrom(stats,quantile)
importFrom(stats,reshape)
importFrom(stringr,str_sub)
importFrom(utils,globalVariables)
importFrom(viridis,scale_colour_viridis)
importFrom(viridis,scale_fill_viridis)
importFrom(viridis,scale_fill_viridis)
84 changes: 84 additions & 0 deletions R/compute_downsampled_corr.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Define global variables
utils::globalVariables(c(".","dataset"))

#' For a given down-sampled DE output, computes the correlation of the log-foldchange of the DEGs (at specified p-value) for a given dataset (celltype)

#' @importFrom ggcorrplot ggcorrplot
#' @importFrom data.table rbindlist setkey
#' @importFrom stats reshape complete.cases cor
#' @importFrom ggplot2 theme element_text scale_fill_gradient2 labs

#' @param allstudies - a list containing DGE analysis outputs (subset to "celltype_all_genes" and the specified cell type) at each down-sampled point
#' @param sampled - downsampling carried out based on what (either "individuals" or "cells")

#' @return correlation matrix, plot and the number of DEGs at the specified p-value

compute_downsampled_corr <- function(allstudies,
sampled="individuals"){

# check input parameters are fine
if(class(allstudies)!="list"){
stop("Error: allstudies should be a list.")
}

# variable to redefine allstudies
allstudies_new <- list()
j <- 1
# select data for each down-sampling point
for(sampling_point in names(allstudies)){
# redefine allstudies so each element only contains data for that down-sampled point
allstudies_new[[j]] <- allstudies[[sampling_point]]
names(allstudies_new)[[j]] <- sampling_point
j <- j+1
}
# reshape data so "dataset" is now a variable
allstudies_dt <- rbindlist(allstudies_new,idcol="dataset")
#filter dataset
setkey(allstudies_dt,name)

# make matrix for corr() - cols will be [datset, name, logFC]
mat_lfc <- allstudies_dt[,.(dataset,name,logFC)]
# reshape so cols now are [(gene) name, logFC.dataset1, logFC.dataset2,...,logFC.datasetN] (N being length(names(allstudies)))
mat_lfc <-
reshape(mat_lfc, idvar = "name", timevar = "dataset",
direction = "wide")
# remove logFC. from name (now cols are just [name, dataset1, dataset2,...,datasetN] with logFC values in each column)
colnames(mat_lfc)<-
c(colnames(mat_lfc)[1],
substr(colnames(mat_lfc[,2:ncol(mat_lfc)]),
7,nchar(colnames(mat_lfc[,2:ncol(mat_lfc)]))))
# remove NA rows
mat_lfc <- mat_lfc[complete.cases(mat_lfc), ]

# get correlation matrix
corr_lfc <- cor(mat_lfc[,2:ncol(mat_lfc)],method = "spearman")

# plot correlation matrix
if(sampled=="individuals"){
corr_plot.plot <- ggcorrplot(round(corr_lfc,2),
hc.order = F, insig="pch",pch=5,pch.col = "grey",
pch.cex=9,
title=paste0("LogFC Correlation Matrix when downsampling individuals"),
colors = c("#FC4E07", "white", "#00AFBB"),
outline.color = "white", lab = TRUE, lab_size=3.5,
sig.level=0.05) +
theme(plot.title = element_text(hjust = 0.7)) +
scale_fill_gradient2(breaks=c(0, 1), limit=c(0, 1)) +
labs(fill="Corr")
}else{
corr_plot.plot <- ggcorrplot(round(corr_lfc,2),
hc.order = F, insig="pch",pch=5,pch.col = "grey",
pch.cex=9,
title=paste0("LogFC Correlation Matrix when downsampling cells"),
colors = c("#FC4E07", "white", "#00AFBB"),
outline.color = "white", lab = TRUE, lab_size=3.5,
sig.level=0.05) +
theme(plot.title = element_text(hjust = 0.7)) +
scale_fill_gradient2(breaks=c(0, 1), limit=c(0, 1)) +
labs(fill="Corr")
}

# output matrix, plot
return(list(corr_lfc, corr_plot.plot))

}
2 changes: 1 addition & 1 deletion R/downsampling_DEanalysis.r
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ utils::globalVariables(c("PValue","name"))
#' @param pval_adjust_method the adjustment method for the p-value in the differential expression analysis. Default is benjamini hochberg "BH". See stats::p.adjust for available options
#' @param rmv_zero_count_genes whether genes with no count values in any cell should be removed. Default is TRUE

#' @return saves all DE outputs for downsampled files as well as a summary table of results showing number of true DEGs detected at each number of samples/cells
#' Saves all DE outputs for downsampled files as well as a summary table of results showing number of true DEGs detected at each number of samples/cells

downsampling_DEanalysis <- function(data,
range_downsampled="placeholder",
Expand Down
Loading

0 comments on commit ba487cc

Please sign in to comment.