annotate tools/mytools/plotmatrix.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 import sys,os
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 infile = sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 outfile = sys.argv[2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 uselog = sys.argv[3]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 subset = sys.argv[4]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 reorder = sys.argv[5]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 color = sys.argv[6]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 scale = sys.argv[7] # rescale each row
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 cols = sys.argv[8]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 rscript = open('tmp.r','w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 rscript.write("x <- read.table('"+infile+"')\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 rscript.write("x <- x[,c("+cols+")]\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 rscript.write("nr <- nrow(x) \n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 rscript.write("nc <- ncol(x)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 rscript.write("rowsum <- apply(x,1,sum)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 if subset =='subset':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 rscript.write("if (nr*nc > 100000) {\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 rscript.write(" nr2 <- as.integer(100000/nc)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 rscript.write(" subind <- sample(seq(nr),nr2)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 rscript.write(" x <- x[subind,]\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 rscript.write(" rowsum <- rowsum[subind]\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 rscript.write("}\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 rscript.write("pdf('"+outfile+"')\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 if uselog == 'uselog':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 rscript.write("x <- -(log(as.matrix(x,nc=nc)))\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 rscript.write("x <- -as.matrix(x,nc=nc)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 if scale == 'scale':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 rscript.write("x <- scale(x)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 if reorder == 'average':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 rscript.write("hc <- hclust(dist(x),method= 'average')\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 rscript.write("x <- x[hc$order,]\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 elif reorder == 'centroid':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 rscript.write("hc <- hclust(dist(x),method= 'centroid')\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 rscript.write("x <- x[hc$order,]\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 elif reorder == 'complete':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 rscript.write("hc <- hclust(dist(x),method= 'complete')\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 rscript.write("x <- x[hc$order,]\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 elif reorder == 'single':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 rscript.write("hc <- hclust(dist(x),method= 'single')\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 rscript.write("x <- x[hc$order,]\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 elif reorder == 'median':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 rscript.write("hc <- hclust(dist(x),method= 'median')\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 rscript.write("x <- x[hc$order,]\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 elif reorder == 'sort_by_total':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 rscript.write("srt <- sort(rowsum,index.return=T)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 rscript.write("x <- x[srt$ix,]\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 elif reorder == 'sort_by_center':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 rscript.write("srt <- sort(x[,as.integer(nc/2)],index.return=T)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 rscript.write("x <- x[srt$ix,]\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 if color == 'heat':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 rscript.write("colormap = heat.colors(1000)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 elif color == 'topo':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 rscript.write("colormap = topo.colors(1000)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 elif color == 'rainbow':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 rscript.write("colormap = rainbow(1000)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 elif color == 'terrain':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 rscript.write("colormap = terrain.colors(1000)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 rscript.write("colormap = gray.colors(1000)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 #rscript.write("qt <- quantile(as.vector(x),probs=c(0.1,0.9))\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 #rscript.write("breaks <- c(min(as.vector(x)),seq(qt[1],qt[2],length.out=99),max(as.vector(x)))\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 #rscript.write("image(t(x),col=colormap,breaks=breaks,axes=F)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 rscript.write("image(t(x),col=colormap,axes=F)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 rscript.write("dev.off()\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 rscript.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 os.system("R --slave < tmp.r")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 os.system("rm tmp.r")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77