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