comparison scripts/dendrogram.R.orig @ 5:2b58bd6becc7 draft

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