annotate mytools/alignvis.py @ 9:87eb5c5ddfe9

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