comparison scripts/dendrogram.R @ 0:10bf0f89035f draft

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit 8beed1a19fcd9dc59f7746e1dfa735a2d5f29784"
author bgruening
date Thu, 10 Feb 2022 12:53:49 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:10bf0f89035f
1 ##
2 suppressWarnings(suppressPackageStartupMessages(library(xbioc)))
3 suppressWarnings(suppressPackageStartupMessages(library(MuSiC)))
4 suppressWarnings(suppressPackageStartupMessages(library(reshape2)))
5 suppressWarnings(suppressPackageStartupMessages(library(cowplot)))
6 ## We use this script to generate a clustering dendrogram of cell
7 ## types, using the prior labelling from scRNA.
8
9 read_list <- function(lfile) {
10 if (lfile == "None") {
11 return(NULL)
12 }
13 return(read.table(file = lfile, header = FALSE, check.names = FALSE,
14 stringsAsFactors = FALSE)$V1)
15 }
16
17 args <- commandArgs(trailingOnly = TRUE)
18 source(args[1])
19
20
21 ## Perform the estimation
22 ## Produce the first step information
23 sub.basis <- music_basis(scrna_eset, clusters = celltypes_label,
24 samples = samples_label,
25 select.ct = celltypes)
26
27 ## Plot the dendrogram of design matrix and cross-subject mean of
28 ## realtive abundance
29 ## Hierarchical clustering using Complete Linkage
30 d1 <- dist(t(log(sub.basis$Disgn.mtx + 1e-6)), method = "euclidean")
31 hc1 <- hclust(d1, method = "complete")
32 ## Hierarchical clustering using Complete Linkage
33 d2 <- dist(t(log(sub.basis$M.theta + 1e-8)), method = "euclidean")
34 hc2 <- hclust(d2, method = "complete")
35
36
37 if (length(data.to.use) > 0) {
38 ## We then perform bulk tissue cell type estimation with pre-grouping
39 ## of cell types: C, list_of_cell_types, marker genes name, marker
40 ## genes list.
41 ## data.to.use = list(
42 ## "C1" = list(cell.types = c("Neutro"),
43 ## marker.names=NULL,
44 ## marker.list=NULL),
45 ## "C2" = list(cell.types = c("Podo"),
46 ## marker.names=NULL,
47 ## marker.list=NULL),
48 ## "C3" = list(cell.types = c("Endo","CD-PC","LOH","CD-IC","DCT","PT"),
49 ## marker.names = "Epithelial",
50 ## marker.list = read_list("../test-data/epith.markers")),
51 ## "C4" = list(cell.types = c("Macro","Fib","B lymph","NK","T lymph"),
52 ## marker.names = "Immune",
53 ## marker.list = read_list("../test-data/immune.markers"))
54 ## )
55 grouped_celltypes <- lapply(data.to.use, function(x) {
56 x$cell.types
57 })
58 marker_groups <- lapply(data.to.use, function(x) {
59 x$marker.list
60 })
61 names(marker_groups) <- names(data.to.use)
62
63
64 cl_type <- as.character(scrna_eset[[celltypes_label]])
65
66 for (cl in seq_len(length(grouped_celltypes))) {
67 cl_type[cl_type %in%
68 grouped_celltypes[[cl]]] <- names(grouped_celltypes)[cl]
69 }
70 pData(scrna_eset)[[clustertype_label]] <- factor(
71 cl_type, levels = c(names(grouped_celltypes),
72 "CD-Trans", "Novel1", "Novel2"))
73
74 est_bulk <- music_prop.cluster(
75 bulk.eset = bulk_eset, sc.eset = scrna_eset,
76 group.markers = marker_groups, clusters = celltypes_label,
77 groups = clustertype_label, samples = samples_label,
78 clusters.type = grouped_celltypes
79 )
80
81 estimated_music_props <- est_bulk$Est.prop.weighted.cluster
82 ## NNLS is not calculated here
83
84 ## Show different in estimation methods
85 ## Jitter plot of estimated cell type proportions
86 methods_list <- c("MuSiC")
87
88 jitter_fig <- Jitter_Est(
89 list(data.matrix(estimated_music_props)),
90 method.name = methods_list, title = "Jitter plot of Est Proportions",
91 size = 2, alpha = 0.7) +
92 theme_minimal() +
93 labs(x = element_blank(), y = element_blank()) +
94 theme(axis.text = element_text(size = 6),
95 axis.text.x = element_blank(),
96 legend.position = "none")
97
98 plot_box <- Boxplot_Est(list(
99 data.matrix(estimated_music_props)),
100 method.name = methods_list) +
101 theme_minimal() +
102 labs(x = element_blank(), y = element_blank()) +
103 theme(axis.text = element_text(size = 6),
104 axis.text.x = element_blank(),
105 legend.position = "none")
106
107 plot_hmap <- Prop_heat_Est(list(
108 data.matrix(estimated_music_props)),
109 method.name = methods_list) +
110 labs(x = element_blank(), y = element_blank()) +
111 theme(axis.text.y = element_text(size = 6),
112 axis.text.x = element_text(angle = -90, size = 5),
113 plot.title = element_text(size = 9),
114 legend.key.width = unit(0.15, "cm"),
115 legend.text = element_text(size = 5),
116 legend.title = element_text(size = 5))
117
118 }
119
120 pdf(file = outfile_pdf, width = 8, height = 8)
121 par(mfrow = c(1, 2))
122 plot(hc1, cex = 0.6, hang = -1, main = "Cluster log(Design Matrix)")
123 plot(hc2, cex = 0.6, hang = -1, main = "Cluster log(Mean of RA)")
124 if (length(data.to.use) > 0) {
125 plot_grid(jitter_fig, plot_box, plot_hmap, ncol = 2, nrow = 2)
126 }
127 message(dev.off())
128
129 if (length(data.to.use) > 0) {
130 write.table(estimated_music_props,
131 file = outfile_tab, quote = F, col.names = NA, sep = "\t")
132 }