annotate scripts/compare.R @ 3:192355cd1641 draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d5c7ca22af1d4f0eaa7a607886554bebb95e8c50
author bgruening
date Tue, 29 Oct 2024 13:39:12 +0000
parents 22232092be53
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
1 suppressWarnings(suppressPackageStartupMessages(library(xbioc)))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
2 suppressWarnings(suppressPackageStartupMessages(library(MuSiC)))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
3 suppressWarnings(suppressPackageStartupMessages(library(reshape2)))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
4 suppressWarnings(suppressPackageStartupMessages(library(cowplot)))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
5 ## We use this script to estimate the effectiveness of proportion methods
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
6
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
7 ## Load Conf
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
8 args <- commandArgs(trailingOnly = TRUE)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
9 source(args[1])
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
10
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
11 method_key <- list("MuSiC" = "est_music",
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
12 "NNLS" = "est_nnls")[[est_method]]
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
13
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
14 delim <- "::" ## separator bulk datasets and their samples
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
15
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
16 scale_yaxes <- function(gplot, value) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
17 if (is.na(value)) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
18 gplot
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
19 } else {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
20 gplot + scale_y_continuous(lim = c(0, value))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
21 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
22 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
23
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
24
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
25 set_factor_data <- function(bulk_data, factor_name = NULL) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
26 if (is.null(factor_name)) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
27 factor_name <- "None" ## change to something plottable
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
28 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
29 pdat <- pData(bulk_data)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
30 sam_fact <- NULL
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
31 if (factor_name %in% colnames(pdat)) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
32 sam_fact <- cbind(rownames(pdat),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
33 as.character(pdat[[factor_name]]))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
34 cat(paste0(" - factor: ", factor_name,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
35 " found in phenotypes\n"))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
36 } else {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
37 ## We assign this as the factor for the entire dataset
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
38 sam_fact <- cbind(rownames(pdat),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
39 factor_name)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
40 cat(paste0(" - factor: assigning \"", factor_name,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
41 "\" to whole dataset\n"))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
42 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
43 colnames(sam_fact) <- c("Samples", "Factors")
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
44 return(as.data.frame(sam_fact))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
45 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
46
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
47 ## Due to limiting sizes, we need to load and unload
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
48 ## possibly very large datasets.
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
49 process_pair <- function(sc_data, bulk_data,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
50 ctypes_label, samples_label, ctypes,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
51 factor_group) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
52 ## - Generate
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
53 est_prop <- music_prop(
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
54 bulk.eset = bulk_data, sc.eset = sc_data,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
55 clusters = ctypes_label,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
56 samples = samples_label, select.ct = ctypes, verbose = T)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
57 ## -
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
58 estimated_music_props <- est_prop$Est.prop.weighted
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
59 estimated_nnls_props <- est_prop$Est.prop.allgene
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
60 ## -
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
61 fact_data <- set_factor_data(bulk_data, factor_group)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
62 ## -
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
63 return(list(est_music = estimated_music_props,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
64 est_nnls = estimated_nnls_props,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
65 bulk_sample_totals = colSums(exprs(bulk_data)),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
66 plot_groups = fact_data))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
67 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
68
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
69 music_on_all <- function(files) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
70 results <- list()
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
71 for (sc_name in names(files)) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
72 cat(paste0("sc-group:", sc_name, "\n"))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
73 scgroup <- files[[sc_name]]
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
74 ## - sc Data
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
75 sc_est <- readRDS(scgroup$dataset)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
76 ## - params
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
77 celltypes_label <- scgroup$label_cell
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
78 samples_label <- scgroup$label_sample
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
79 celltypes <- scgroup$celltype
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
80
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
81 results[[sc_name]] <- list()
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
82 for (bulk_name in names(scgroup$bulk)) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
83 cat(paste0(" - bulk-group:", bulk_name, "\n"))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
84 bulkgroup <- scgroup$bulk[[bulk_name]]
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
85 ## - bulk Data
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
86 bulk_est <- readRDS(bulkgroup$dataset)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
87 ## - bulk params
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
88 pheno_facts <- bulkgroup$pheno_facts
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
89 pheno_excl <- bulkgroup$pheno_excl
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
90 ##
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
91 results[[sc_name]][[bulk_name]] <- process_pair(
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
92 sc_est, bulk_est,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
93 celltypes_label, samples_label,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
94 celltypes, bulkgroup$factor_group)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
95 ##
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
96 rm(bulk_est) ## unload
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
97 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
98 rm(sc_est) ## unload
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
99 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
100 return(results)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
101 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
102
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
103 plot_all_individual_heatmaps <- function(results) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
104 pdf(out_heatmulti_pdf, width = 8, height = 8)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
105 for (sc_name in names(results)) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
106 for (bk_name in names(results[[sc_name]])) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
107 res <- results[[sc_name]][[bk_name]]
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
108 plot_hmap <- Prop_heat_Est(
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
109 data.matrix(res[[method_key]]), method.name = est_method) +
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
110 ggtitle(paste0("[", est_method, "Cell type ",
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
111 "proportions in ",
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
112 bk_name, " (Bulk) based on ",
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
113 sc_name, " (scRNA)")) +
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
114 xlab("Cell Types (scRNA)") +
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
115 ylab("Samples (Bulk)") +
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
116 theme(axis.text.x = element_text(angle = -90),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
117 axis.text.y = element_text(size = 6))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
118 print(plot_hmap)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
119 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
120 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
121 dev.off()
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
122 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
123
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
124 merge_factors_spread <- function(grudat_spread, factor_groups) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
125 ## Generated
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
126 merge_it <- function(matr, plot_groups, valname) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
127 ren <- melt(lapply(matr, function(mat) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
128 mat["ct"] <- rownames(mat); return(mat)}))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
129 ## - Grab factors and merge into list
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
130 ren_new <- merge(ren, plot_groups, by.x = "variable", by.y = "Samples")
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
131 colnames(ren_new) <- c("Sample", "Cell", valname, "Bulk", "Factors")
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
132 return(ren_new)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
133 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
134 tab <- merge(merge_it(grudat$spread$prop, factor_groups, "value.prop"),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
135 merge_it(grudat$spread$scale, factor_groups, "value.scale"),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
136 by = c("Sample", "Cell", "Bulk", "Factors"))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
137 return(tab)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
138 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
139
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
140 unlist_names <- function(results, method, prepend_bkname=FALSE) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
141 unique(sort(
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
142 unlist(lapply(names(results), function(scname) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
143 lapply(names(results[[scname]]), function(bkname) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
144 res <- get(method)(results[[scname]][[bkname]][[method_key]])
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
145 if (prepend_bkname) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
146 ## We *do not* assume unique bulk sample names
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
147 ## across different bulk datasets.
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
148 res <- paste0(bkname, delim, res)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
149 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
150 return(res)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
151 })
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
152 }))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
153 ))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
154 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
155
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
156 summarized_matrix <- function(results) { # nolint
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
157 ## We assume that cell types MUST be unique, but that sample
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
158 ## names do not need to be. For this reason, we must prepend
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
159 ## the bulk dataset name to the individual sample names.
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
160 all_celltypes <- unlist_names(results, "colnames")
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
161 all_samples <- unlist_names(results, "rownames", prepend_bkname = TRUE)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
162
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
163 ## Iterate through all possible samples and populate a table.
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
164 ddff <- data.frame()
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
165 ddff_scale <- data.frame()
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
166 for (cell in all_celltypes) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
167 for (sample in all_samples) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
168 group_sname <- unlist(strsplit(sample, split = delim))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
169 bulk <- group_sname[1]
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
170 id_sample <- group_sname[2]
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
171 for (scgroup in names(results)) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
172 if (bulk %in% names(results[[scgroup]])) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
173 mat_prop <- results[[scgroup]][[bulk]][[method_key]]
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
174 vec_counts <- results[[scgroup]][[bulk]]$bulk_sample_totals
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
175 ## - We use sample instead of id_sample because we need to
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
176 ## extract bulk sets from the complete matrix later. It's
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
177 ## messy, yes.
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
178 if (cell %in% colnames(mat_prop)) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
179 ddff[cell, sample] <- mat_prop[id_sample, cell]
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
180 ddff_scale[cell, sample] <- mat_prop[id_sample, cell] * vec_counts[[id_sample]] #nolint
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
181 } else {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
182 ddff[cell, sample] <- 0
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
183 ddff_scale[cell, sample] <- 0
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
184 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
185 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
186 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
187 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
188 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
189 return(list(prop = ddff, scaled = ddff_scale))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
190 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
191
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
192 flatten_factor_list <- function(results) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
193 ## Get a 2d DF of all factors across all bulk samples.
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
194 res <- c()
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
195 for (scgroup in names(results)) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
196 for (bulkgroup in names(results[[scgroup]])) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
197 dat <- results[[scgroup]][[bulkgroup]]$plot_groups
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
198 dat$Samples <- paste0(bulkgroup, delim, dat$Samples) #nolint
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
199 res <- rbind(res, dat)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
200 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
201 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
202 return(res)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
203 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
204
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
205 group_by_dataset <- function(summat) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
206 bulk_names <- unlist(
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
207 lapply(names(files), function(x) names(files[[x]]$bulk)))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
208 mat_names <- colnames(summat$prop)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
209 bd <- list()
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
210 bd_scale <- list()
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
211 bd_spread_scale <- list()
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
212 bd_spread_prop <- list()
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
213 for (bname in bulk_names) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
214 subs <- mat_names[startsWith(mat_names, paste0(bname, delim))]
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
215 ## -
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
216 bd[[bname]] <- rowSums(summat$prop[, subs])
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
217 bd_scale[[bname]] <- rowSums(summat$scaled[, subs])
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
218 bd_spread_scale[[bname]] <- summat$scaled[, subs]
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
219 bd_spread_prop[[bname]] <- summat$prop[, subs]
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
220 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
221 return(list(prop = as.data.frame(bd),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
222 scaled = as.data.frame(bd_scale),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
223 spread = list(scale = bd_spread_scale,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
224 prop = bd_spread_prop)))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
225 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
226
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
227 do_cluster <- function(grudat_spread_melt, xaxis, yaxis, value_name,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
228 xlabs="", ylabs="", titled="",
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
229 order_col=T, order_row=T, size=11) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
230
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
231 data_m <- grudat_spread_melt
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
232 data_matrix <- {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
233 tmp <- dcast(data_m, formula(paste0(yaxis, " ~ ", xaxis)), value.var = value_name)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
234 rownames(tmp) <- tmp[[yaxis]]
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
235 tmp[[yaxis]] <- NULL
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
236 tmp
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
237 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
238 dist_method <- "euclidean"
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
239 clust_method <- "complete"
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
240
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
241 if (order_row) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
242 dd_row <- as.dendrogram(hclust(dist(data_matrix, method = dist_method), method = clust_method))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
243 row_ord <- order.dendrogram(dd_row)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
244 ordered_row_names <- row.names(data_matrix[row_ord, ])
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
245 data_m[[yaxis]] <- factor(data_m[[yaxis]], levels = ordered_row_names)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
246 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
247
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
248 if (order_col) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
249 dd_col <- as.dendrogram(hclust(dist(t(data_matrix), method = dist_method),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
250 method = clust_method))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
251 col_ord <- order.dendrogram(dd_col)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
252 ordered_col_names <- colnames(data_matrix[, col_ord])
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
253 data_m[[xaxis]] <- factor(data_m[[xaxis]], levels = ordered_col_names)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
254 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
255
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
256 heat_plot <- ggplot(data_m, aes_string(x = xaxis, y = yaxis, fill = value_name)) +
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
257 geom_tile(colour = "white") +
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
258 scale_fill_gradient2(low = "steelblue", high = "red", mid = "white",
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
259 name = element_blank()) +
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
260 scale_y_discrete(position = "right") +
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
261 theme(axis.text.x = element_text(angle = -90, hjust = 0,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
262 size = size)) +
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
263 ggtitle(label = titled) + xlab(xlabs) + ylab(ylabs)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
264
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
265 ## Graphics
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
266 dendro_linesize <- 0.5
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
267 dendro_colunit <- 0.2
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
268 dendro_rowunit <- 0.1
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
269 final_plot <- heat_plot
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
270
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
271 if (order_row) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
272 dendro_data_row <- ggdendro::dendro_data(dd_row, type = "rectangle")
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
273 dendro_row <- cowplot::axis_canvas(heat_plot, axis = "y", coord_flip = TRUE) +
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
274 ggplot2::geom_segment(data = ggdendro::segment(dendro_data_row),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
275 ggplot2::aes(y = -y, x = x, xend = xend, yend = -yend),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
276 size = dendro_linesize) + ggplot2::coord_flip()
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
277 final_plot <- cowplot::insert_yaxis_grob(
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
278 final_plot, dendro_row, grid::unit(dendro_colunit, "null"),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
279 position = "left")
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
280 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
281 if (order_col) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
282 dendro_data_col <- ggdendro::dendro_data(dd_col, type = "rectangle")
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
283 dendro_col <- cowplot::axis_canvas(heat_plot, axis = "x") +
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
284 ggplot2::geom_segment(data = ggdendro::segment(dendro_data_col),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
285 ggplot2::aes(x = x, y = y, xend = xend, yend = yend),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
286 size = dendro_linesize)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
287 final_plot <- cowplot::insert_xaxis_grob(
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
288 final_plot, dendro_col, grid::unit(dendro_rowunit, "null"),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
289 position = "top")
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
290 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
291 return(cowplot::ggdraw(final_plot))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
292 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
293
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
294 summarize_heatmaps <- function(grudat_spread_melt, do_factors, cluster="None") {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
295 ## - Cluster is either "Rows", "Cols", "Both", or "None"
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
296 do_single <- function(grudat_melted, yaxis, xaxis, fillval, title,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
297 ylabs = element_blank(), xlabs = element_blank(),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
298 use_log = TRUE, size = 11) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
299 ## Convert from matrix to long format
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
300 melted <- grudat_melted ## copy?
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
301 if (use_log) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
302 melted[[fillval]] <- log10(melted[[fillval]] + 1)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
303 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
304 if (cluster == "None") {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
305 return(ggplot(melted) +
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
306 geom_tile(aes_string(y = yaxis, x = xaxis, fill = fillval),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
307 colour = "white") +
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
308 scale_fill_gradient2(
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
309 low = "steelblue", high = "red", mid = "white",
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
310 name = element_blank()) +
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
311 theme(axis.text.x = element_text(
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
312 angle = -90, hjust = 0, size = size)) +
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
313 ggtitle(label = title) + xlab(xlabs) + ylab(ylabs))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
314 } else {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
315 return(do_cluster(grudat_spread_melt, xaxis, yaxis, fillval,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
316 xlabs, ylabs, title,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
317 (cluster %in% c("Cols", "Both")),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
318 (cluster %in% c("Rows", "Both"))))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
319 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
320 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
321
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
322 do_gridplot <- function(title, xvar, plot="both", ncol=2, size = 11) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
323 do_logged <- (plot %in% c("log", "both"))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
324 do_normal <- (plot %in% c("normal", "both"))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
325 plist <- list()
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
326 if (do_logged) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
327 plist[["1"]] <- do_single(grudat_spread_melt, "Cell", xvar,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
328 "value.scale", "Reads (log10+1)",
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
329 size = size)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
330 plist[["2"]] <- do_single(grudat_spread_melt, "Cell", xvar,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
331 "value.prop", "Sample (log10+1)",
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
332 size = size)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
333 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
334 if (do_normal) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
335 plist[["A"]] <- do_single(grudat_spread_melt, "Cell", xvar,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
336 "value.scale", "Reads", use_log = F,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
337 size = size)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
338 plist[["B"]] <- do_single(grudat_spread_melt, "Cell", xvar,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
339 "value.prop", "Sample", use_log = F,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
340 size = size)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
341 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
342 return(plot_grid(ggdraw() + draw_label(title, fontface = "bold"),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
343 plot_grid(plotlist = plist, ncol = ncol),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
344 ncol = 1, rel_heights = c(0.05, 0.95)))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
345 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
346
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
347 p1 <- do_gridplot("Cell Types vs Bulk Datasets", "Bulk", "both")
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
348 p2a <- do_gridplot("Cell Types vs Samples", "Sample", "normal",
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
349 ncol = 1, size = 8)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
350 p2b <- do_gridplot("Cell Types vs Samples (log10+1)", "Sample", "log",
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
351 ncol = 1, size = 8)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
352 p3 <- ggplot + theme_void()
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
353 if (do_factors) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
354 p3 <- do_gridplot("Cell Types vs Factors", "Factors", "both")
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
355 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
356 return(list(bulk = p1,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
357 samples = list(log = p2b, normal = p2a),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
358 factors = p3))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
359 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
360
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
361 summarize_boxplots <- function(grudat_spread, do_factors) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
362 common1 <- ggplot(grudat_spread, aes(x = value.prop)) + ggtitle("Sample") +
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
363 xlab(element_blank()) + ylab(element_blank())
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
364 common2 <- ggplot(grudat_spread, aes(x = value.scale)) + ggtitle("Reads") +
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
365 xlab(element_blank()) + ylab(element_blank())
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
366
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
367 A <- B <- list() #nolint
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
368 ## Cell type by sample
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
369 A$p1 <- common2 + geom_boxplot(aes(y = Cell, color = Bulk))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
370 A$p2 <- common1 + geom_boxplot(aes(y = Cell, color = Bulk))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
371 ## Sample by Cell type
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
372 B$p1 <- common2 + geom_boxplot(aes(y = Bulk, color = Cell)) +
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
373 ylab("Bulk Dataset")
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
374 B$p2 <- common1 + geom_boxplot(aes(y = Bulk, color = Cell)) +
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
375 ylab("Bulk Dataset")
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
376 ## -- Factor plots are optional
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
377 A$p3 <- B$p3 <- A$p4 <- B$p4 <- ggplot() + theme_void()
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
378
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
379 if (do_factors) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
380 A$p3 <- common1 + geom_boxplot(aes(y = Cell, color = Factors))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
381 A$p4 <- common2 + geom_boxplot(aes(y = Cell, color = Factors))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
382 B$p3 <- common1 + geom_boxplot(aes(y = Bulk, color = Factors)) +
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
383 ylab("Bulk Dataset")
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
384 B$p4 <- common2 + geom_boxplot(aes(y = Bulk, color = Factors)) +
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
385 ylab("Bulk Dataset")
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
386 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
387
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
388 title_a <- "Cell Types vs Bulk Datasets"
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
389 title_b <- "Bulk Datasets vs Cell Types"
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
390 if (do_factors) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
391 title_a <- paste0(title_a, " and Factors")
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
392 title_b <- paste0(title_b, " and Factors")
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
393 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
394
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
395 a_all <- plot_grid(ggdraw() + draw_label(title_a, fontface = "bold"),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
396 plot_grid(plotlist = A, ncol = 2),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
397 ncol = 1, rel_heights = c(0.05, 0.95))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
398 b_all <- plot_grid(ggdraw() + draw_label(title_b, fontface = "bold"),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
399 plot_grid(plotlist = B, ncol = 2),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
400 ncol = 1, rel_heights = c(0.05, 0.95))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
401 return(list(cell = a_all, bulk = b_all))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
402 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
403
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
404 filter_output <- function(grudat_spread_melt, out_filt) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
405 print_red <- function(comment, red_list) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
406 cat(paste(comment, paste(red_list, collapse = ", "), "\n"))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
407 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
408 grudat_filt <- grudat_spread_melt
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
409 print_red("Total Cell types:", unique(grudat_filt$Cell))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
410 if (!is.null(out_filt$cells)) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
411 grudat_filt <- grudat_filt[grudat_filt$Cell %in% out_filt$cells, ]
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
412 print_red(" - selecting:", out_filt$cells)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
413 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
414 print_red("Total Factors:", unique(grudat_spread_melt$Factors))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
415 if (!is.null(out_filt$facts)) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
416 grudat_filt <- grudat_filt[grudat_filt$Factors %in% out_filt$facts, ]
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
417 print_red(" - selecting:", out_filt$facts)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
418 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
419 return(grudat_filt)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
420 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
421
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
422 writable2 <- function(obj, prefix, title) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
423 write.table(obj,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
424 file = paste0("report_data/", prefix, "_",
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
425 title, ".tabular"),
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
426 quote = F, sep = "\t", col.names = NA)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
427 }
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
428
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
429
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
430 results <- music_on_all(files)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
431 summat <- summarized_matrix(results)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
432 grudat <- group_by_dataset(summat)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
433 grudat_spread_melt <- merge_factors_spread(grudat$spread,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
434 flatten_factor_list(results))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
435 grudat_spread_melt_filt <- filter_output(grudat_spread_melt, out_filt)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
436
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
437 plot_all_individual_heatmaps(results)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
438
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
439 ## The output filters ONLY apply to boxplots, since these take
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
440 do_factors <- (length(unique(grudat_spread_melt[["Factors"]])) > 1)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
441 box_plots <- summarize_boxplots(grudat_spread_melt_filt, do_factors)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
442 heat_maps <- summarize_heatmaps(grudat_spread_melt_filt, do_factors,
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
443 dendro_setting)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
444
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
445 pdf(out_heatsumm_pdf, width = 14, height = 14)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
446 print(heat_maps)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
447 print(box_plots)
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
448 dev.off()
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
449
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
450 ## Generate output tables
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
451 stats_prop <- lapply(grudat$spread$prop, function(x) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
452 t(apply(x, 1, summary))})
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
453 stats_scale <- lapply(grudat$spread$scale, function(x) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
454 t(apply(x, 1, summary))})
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
455
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
456 ## Make the value table printable
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
457 grudat_spread_melt$value.scale <- as.integer(grudat_spread_melt$value.scale) # nolint
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
458 colnames(grudat_spread_melt) <- c("Sample", "Cell", "Bulk", "Factors",
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
459 "CT Prop in Sample", "Number of Reads")
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
460
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
461 writable2(grudat_spread_melt, "values", "Data Table")
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
462 writable2(summat$prop, "values", "Matrix of Cell Type Sample Proportions")
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
463 writable2({
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
464 aa <- as.matrix(summat$scaled); mode(aa) <- "integer"; aa
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
465 }, "values", "Matrix of Cell Type Read Counts")
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
466
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
467 for (bname in names(stats_prop)) {
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
468 writable2(stats_prop[[bname]], "stats", paste0(bname, ": Sample Props"))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
469 writable2(stats_scale[[bname]], "stats", paste0(bname, ": Read Props"))
22232092be53 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit d007ae51743e621dc47524f681501e72ef3a2910"
bgruening
parents:
diff changeset
470 }