annotate R/heatmap.R @ 13:152d7c43478b draft default tip

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