comparison scripts/dendrogram.R @ 0:2cfd0db49bbc draft

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/music/ commit 08c6fd3885bdfbf8b5c3f4dcc2d04729b577e3e1"
author bgruening
date Sun, 12 Sep 2021 19:49:12 +0000
parents
children be91cb6f48e7
comparison
equal deleted inserted replaced
-1:000000000000 0:2cfd0db49bbc
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,
14 stringsAsFactors = FALSE)$V1)
15 }
16
17 args <- commandArgs(trailingOnly = TRUE)
18 source(args[1])
19
20 ## We then perform bulk tissue cell type estimation with pre-grouping
21 ## of cell types: C, list_of_cell_types, marker genes name, marker
22 ## genes list.
23 ## data.to.use = list(
24 ## "C1" = list(cell.types = c("Neutro"),
25 ## marker.names=NULL,
26 ## marker.list=NULL),
27 ## "C2" = list(cell.types = c("Podo"),
28 ## marker.names=NULL,
29 ## marker.list=NULL),
30 ## "C3" = list(cell.types = c("Endo","CD-PC","LOH","CD-IC","DCT","PT"),
31 ## marker.names = "Epithelial",
32 ## marker.list = read_list("../test-data/epith.markers")),
33 ## "C4" = list(cell.types = c("Macro","Fib","B lymph","NK","T lymph"),
34 ## marker.names = "Immune",
35 ## marker.list = read_list("../test-data/immune.markers"))
36 ## )
37 grouped_celltypes <- lapply(data.to.use, function(x) {
38 x$cell.types
39 })
40 marker_groups <- lapply(data.to.use, function(x) {
41 x$marker.list
42 })
43 names(marker_groups) <- names(data.to.use)
44
45
46 ## Perform the estimation
47 ## Produce the first step information
48 sub.basis <- music_basis(scrna_eset, clusters = celltypes_label,
49 samples = samples_label,
50 select.ct = celltypes)
51
52 ## Plot the dendrogram of design matrix and cross-subject mean of
53 ## realtive abundance
54 par(mfrow = c(1, 2))
55 d <- dist(t(log(sub.basis$Disgn.mtx + 1e-6)), method = "euclidean")
56 ## Hierarchical clustering using Complete Linkage
57 hc1 <- hclust(d, method = "complete")
58 ## Plot the obtained dendrogram
59 plot(hc1, cex = 0.6, hang = -1, main = "Cluster log(Design Matrix)")
60 d <- dist(t(log(sub.basis$M.theta + 1e-8)), method = "euclidean")
61 ## Hierarchical clustering using Complete Linkage
62 hc2 <- hclust(d, method = "complete")
63 ## Plot the obtained dendrogram
64 pdf(file = outfile_pdf, width = 8, height = 8)
65 plot(hc2, cex = 0.6, hang = -1, main = "Cluster log(Mean of RA)")
66
67 cl_type <- as.character(scrna_eset[[celltypes_label]])
68
69 for (cl in seq_len(length(grouped_celltypes))) {
70 cl_type[cl_type %in% grouped_celltypes[[cl]]] <- names(grouped_celltypes)[cl]
71 }
72 pData(scrna_eset)[[clustertype_label]] <- factor(
73 cl_type, levels = c(names(grouped_celltypes),
74 "CD-Trans", "Novel1", "Novel2"))
75
76 est_bulk <- music_prop.cluster(
77 bulk.eset = bulk_eset, sc.eset = scrna_eset,
78 group.markers = marker_groups, clusters = celltypes_label,
79 groups = clustertype_label, samples = samples_label,
80 clusters.type = grouped_celltypes)
81
82 write.table(est_bulk, file = outfile_tab, quote = F, col.names = NA, sep = "\t")
83 dev.off()