comparison R/heatmap.R @ 3:e42d30da7a74 draft

Uploaded
author dereeper
date Thu, 30 May 2024 11:52:25 +0000
parents
children
comparison
equal deleted inserted replaced
2:97e4e3e818b6 3:e42d30da7a74
1 #!/usr/local/R-4.1.2/bin/R
2
3
4 library(dendextend)
5 library("optparse")
6 library(reshape2)
7 library(ape)
8
9
10 #args = commandArgs(trailingOnly=TRUE)
11
12 option_list = list(
13 make_option(c("-f", "--file"), type="character", default=NULL,
14 help="dataset file name", metavar="character"),
15 make_option(c("-o", "--out"), type="character", default="out.txt",
16 help="output file name [default= %default]", metavar="character")
17 );
18 opt_parser = OptionParser(option_list=option_list);
19 opt = parse_args(opt_parser);
20
21 if (is.null(opt$file)){
22 print_help(opt_parser)
23 stop("At least one argument must be supplied (input file).\n", call.=FALSE)
24 }
25
26 if (is.null(opt$out)){
27 print_help(opt_parser)
28 stop("At least one argument must be supplied (out file).\n", call.=FALSE)
29 }
30
31 #svglite(opt$out,width = 31, height = 28)
32 pdf(opt$out,width = 31,height = 28)
33
34 mydata <- read.table(opt$file, header=TRUE,sep="\t", row.names="Gene")
35
36 iris <- mydata
37
38 #dend_r <- iris %>% dist(method = "man") %>% hclust(method = "ward.D") %>% as.dendrogram %>% ladderize
39 #dend_r <- iris %>% dist(method = "man") %>% hclust(method = "com") %>% as.dendrogram %>% ladderize
40
41 #dend_c <- t(iris) %>% dist(method = "man") %>% hclust(method = "com") %>% as.dendrogram %>% ladderize
42 #dend_c <- t(iris) %>% dist(method = "man") %>% hclust(method = "ward.D") %>% as.dendrogram %>% ladderize
43
44 #dend_c
45
46
47 distance_matrix = t(iris) %>% dist(method = "man")
48 h = distance_matrix %>% hclust(method = "ward.D")
49
50 tree = as.phylo(h)
51
52
53 dend_c = h %>% as.dendrogram
54
55
56 write.tree(tree, file=paste(opt$out, "distance_matrix.hclust.newick", sep="."))
57
58 dist_df <- melt(as.matrix(distance_matrix), varnames = c("row", "col"))
59 write.table(
60 dist_df,
61 paste(opt$out, "distance_matrix.txt", sep=".")
62 )
63
64 mat <- as.matrix(t(iris-1))
65 out <- gplots::heatmap.2(mat,
66 main = "",
67 scale="none",
68 srtCol=NULL,
69 Rowv = dend_c,
70 #Colv = dend_r,
71 key = FALSE,
72 margins =c(20,20),
73 trace="row", hline = NA, tracecol = NA
74 )
75
76 write.table(
77 data.frame(gene = rownames(mat)[out$rowInd]),
78 paste(opt$out, "rows.csv", sep="."),
79 row.names = FALSE,
80 quote = FALSE,
81 sep = ',')
82
83 write.table(
84 data.frame(gene = colnames(mat)[out$colInd]),
85 paste(opt$out, "cols.csv", sep="."),
86 row.names = FALSE,
87 quote = FALSE,
88 sep = ',')
89
90 dev.off()
91