Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correlation plots #97

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions workflow/rules/statistic/correlation.smk
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@ rule statistic_correlation_calculate:
if "label_file" in config["experiments"][wc.project]
else ""
),
plot_width=29,
plot_height=17,
legend_nrow=6,
log:
temp(
"results/logs/statistic/correlation/calculate.{project}.{condition}.{config}.{assignment}.log"
Expand All @@ -323,6 +326,9 @@ rule statistic_correlation_calculate:
--files {params.files} \
--replicates {params.replicates} \
--threshold {params.thresh} \
--plot_width {params.plot_width} \
--plot_height {params.plot_height} \
--legend_nrow {params.legend_nrow} \
--outdir {params.outdir} &> {log}
"""

Expand Down
19 changes: 10 additions & 9 deletions workflow/scripts/count/plot_perBCCounts_correlation.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,21 @@ plot_correlations_dna <- function(data, plot_data, condition, r1, r2, name) {
geom_point() +
xlab(sprintf(paste("log2 Normalized DNA count per barcode,\n replicate", r1))) +
ylab(sprintf(paste("log2 Normalized DNA count per barcode,\n replicate", r2))) +
geom_text(x = min + 0.5, y = max - 0.5, label = sprintf(" r = %.2f", cor(data$DNA_normalized_log2.x, data$DNA_normalized_log2.y, method = "pearson")), size = 10) +
geom_text(x = min + 0.5, y = max - 1.0, label = sprintf("rho = %.2f", cor(data$DNA_normalized.x, data$DNA_normalized.y, method = "spearman")), size = 10) +
geom_text(x = -Inf, y = Inf, hjust=0, vjust=1, label = sprintf(" r = %.2f", cor(data$DNA_normalized_log2.x, data$DNA_normalized_log2.y, method = "pearson")), size = 10) +
geom_text(x = -Inf, y = Inf, hjust=0, vjust=2.1, label = sprintf("rho = %.2f", cor(data$DNA_normalized.x, data$DNA_normalized.y, method = "spearman")), size = 10) +
geom_abline(intercept = 0, slope = 1) +
theme_classic(base_size = 30)
return(dna_p)
}
plot_correlations_rna <- function(data, plot_data, condition, r1, r2, name) {
max <- max(data$`RNA_normalized.y_log2`)
min <- min(data$`RNA_normalized.x_log2`)
max <- max(data$`RNA_normalized_log2.y`)
min <- min(data$`RNA_normalized_log2.x`)
rna_p <- ggplot(plot_data, aes(RNA_normalized_log2.x, RNA_normalized_log2.y)) +
geom_point() +
xlab(sprintf(paste("log2 Normalized RNA count per barcode,\n replicate", r1))) +
ylab(sprintf(paste("log2 Normalized RNA count per barcode,\n replicate", r2))) +
geom_text(x = min + 0.5, y = max - 0.5, label = sprintf(" r = %.2f", cor(data$RNA_normalized_log2.x, data$RNA_normalized_log2.y, method = "pearson")), size = 10) +
geom_text(x = min + 0.5, y = max - 1.0, label = sprintf("rho = %.2f", cor(data$RNA_normalized.x, data$RNA_normalized.y, method = "spearman")), size = 10) +
geom_text(x = -Inf, y = Inf, hjust=0, vjust=1, label = sprintf(" r = %.2f", cor(data$RNA_normalized_log2.x, data$RNA_normalized_log2.y, method = "pearson")), size = 10) +
geom_text(x = -Inf, y = Inf, hjust=0, vjust=2.1, label = sprintf("rho = %.2f", cor(data$RNA_normalized.x, data$RNA_normalized.y, method = "spearman")), size = 10) +
geom_abline(intercept = 0, slope = 1) +
theme_classic(base_size = 30)
return(rna_p)
Expand All @@ -111,8 +111,8 @@ plot_correlations_ratio <- function(data, plot_data, condition, r1, r2, name) {
geom_point() +
xlab(sprintf(paste("log2 RNA/DNA per barcode,\n replicate", r1))) +
ylab(sprintf(paste("log2 RNA/DNA per barcode,\n replicate", r2))) +
geom_text(x = min + 0.5, y = max - 0.5, label = sprintf(" r = %.2f", cor(data$Ratio_log2.x, res$Ratio_log2.y, method = "pearson")), size = 10) +
geom_text(x = min + 0.5, y = max - 1.0, label = sprintf("rho = %.2f", cor(data$Ratio.x, data$Ratio.y, method = "spearman")), size = 10) +
geom_text(x = -Inf, y = Inf, hjust=0, vjust=1, label = sprintf(" r = %.2f", cor(data$Ratio_log2.x, res$Ratio_log2.y, method = "pearson")), size = 10) +
geom_text(x = -Inf, y = Inf, hjust=0, vjust=2.1, label = sprintf("rho = %.2f", cor(data$Ratio.x, data$Ratio.y, method = "spearman")), size = 10) +
geom_abline(intercept = 0, slope = 1) +
theme_classic(base_size = 30)
return(ratio_p)
Expand Down Expand Up @@ -225,4 +225,5 @@ if (data %>% nrow() > 1) {
writeCorrelationPlots(plots_correlations_rna, sprintf("%s_barcode_RNA_pairwise.png", outdir))
writeCorrelationPlots(plots_correlations_ratio, sprintf("%s_barcode_Ratio_pairwise.png", outdir))

}
}

53 changes: 42 additions & 11 deletions workflow/scripts/count/plot_perInsertCounts_correlation.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,31 @@ option_list <- list(
type = "integer",
default = 10,
help = "Number of required barcodes (default 10)"
),
make_option(
c("-w", "--plot_width"),
type = "integer",
default = 29,
help = "Width of the plots created by this script (default 29)"
),
make_option(
c("-h", "--plot_height"),
type = "integer",
default = 17,
help = "Height of the plots created by this script (default 17)"
),
make_option(
c("-n", "--legend_nrow"),
type = "integer",
default = 6,
help = "Number of rows in the legend of the plots created by this script (default 6)"
),
make_option(c("-o", "--outdir"),
type = "character",
help = "Outdir of the plots and table.")
)

parser <- OptionParser(option_list = option_list)
parser <- OptionParser(add_help_option=FALSE, option_list = option_list)
arguments <- parse_args(parser, positional_arguments = TRUE)
opt <- arguments$options

Expand Down Expand Up @@ -72,6 +90,10 @@ if ("label" %in% names(opt)) {
use_labels <- FALSE
}

# plot configuration
plot_width <- opt$plot_width
plot_height <- opt$plot_height
legend_nrow <- opt$legend_nrow

# replicates and count files
files <- strsplit(opt$files, ",")[[1]]
Expand All @@ -85,13 +107,12 @@ data["Condition"] <- cond

print(data)

# pairwise comparison only if more than one replicate
thresh <- opt$threshold

plot_correlations_dna <- function(data, condition, r1, r2, name) {
dna_p <-
ggplot(data, aes(dna_normalized_log2.x, dna_normalized_log2.y)) +
geom_point(aes(colour = label.x), show.legend = TRUE) +
geom_point(aes(colour = label.x)) +
xlim(-5, 5) +
ylim(-5, 5) +
xlab(sprintf(
Expand Down Expand Up @@ -126,13 +147,16 @@ plot_correlations_dna <- function(data, condition, r1, r2, name) {
size = 10
) +
geom_abline(intercept = 0, slope = 1) +
theme_classic(base_size = 30)
theme_classic(base_size = 30) +
theme(legend.position="bottom") + # show legend below the plot
guides(fill=guide_legend(nrow=legend_nrow, byrow=TRUE)) + # show labels in rows
labs(color = "label\n") # legend name
return(dna_p)
}
plot_correlations_rna <- function(data, condition, r1, r2, name) {
rna_p <-
ggplot(data, aes(rna_normalized_log2.x, rna_normalized_log2.y)) +
geom_point(aes(colour = label.x), show.legend = TRUE) +
geom_point(aes(colour = label.x)) +
xlim(-5, 5) +
ylim(-5, 5) +
xlab(sprintf(
Expand Down Expand Up @@ -165,12 +189,15 @@ plot_correlations_rna <- function(data, condition, r1, r2, name) {
size = 10
) +
geom_abline(intercept = 0, slope = 1) +
theme_classic(base_size = 30)
theme_classic(base_size = 30) +
theme(legend.position="bottom") + # show legend below the plot
guides(fill=guide_legend(nrow=legend_nrow, byrow=TRUE)) + # show labels in rows
labs(color = "label\n") # legend name
return(rna_p)
}
plot_correlations_ratio <- function(data, condition, r1, r2, name) {
ratio_p <- ggplot(data, aes(ratio_log2.x, ratio_log2.y)) +
geom_point(aes(colour = label.x), show.legend = TRUE) +
geom_point(aes(colour = label.x)) +
xlim(-5, 5) +
ylim(-5, 5) +
xlab(sprintf(paste(
Expand Down Expand Up @@ -198,7 +225,10 @@ plot_correlations_ratio <- function(data, condition, r1, r2, name) {
size = 10
) +
geom_abline(intercept = 0, slope = 1) +
theme_classic(base_size = 30)
theme_classic(base_size = 30) +
theme(legend.position="bottom") + # show legend below the plot
guides(fill=guide_legend(nrow=legend_nrow, byrow=TRUE)) + # show labels in rows
labs(color = "label\n") # legend name
return(ratio_p)
}

Expand Down Expand Up @@ -268,8 +298,9 @@ write_correlation_plots <- function(plots, name) {

ggplot2::ggsave(name,
correlation_plots,
width = 15,
height = 10 * length(plots))
width = plot_width,
height = plot_height * length(plots),
limitsize = FALSE)
}

write_correlation <- function(correlations, name) {
Expand Down Expand Up @@ -324,7 +355,7 @@ if (use_labels) {
all$label <- "NA"
}
}

# pairwise comparison only if more than one replicate
if (data %>% nrow() > 1 && nrow(all) > 1) {
print("Pairwise comparisons")
# make pairwise combinations
Expand Down
Loading