Mercurial > repos > xuebing > sharplab_interval_analysis
comparison getGenomicScore.py @ 20:16ba480adf96
Uploaded
author | xuebing |
---|---|
date | Sat, 31 Mar 2012 08:31:22 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
19:d325683ec368 | 20:16ba480adf96 |
---|---|
1 import random,string,os,sys | |
2 | |
3 | |
4 def getScore(intvfile,outfile,summary_type,bwfilepath,nbin,strand,outplot,span): | |
5 f = open(intvfile) | |
6 tmpsh = "".join(random.sample(string.letters+string.digits, 8)) | |
7 tmpout = "".join(random.sample(string.letters+string.digits, 8)) | |
8 tmp = open(tmpsh,'w') | |
9 if os.path.isdir(bwfilepath): | |
10 for line in f: | |
11 flds = line.strip().split('\t') | |
12 cmd = 'bigWigSummary -type='+summary_type+' '+bwfilepath+'/'+flds[0]+'.bw '+flds[0]+' '+flds[1]+' '+flds[2]+' '+nbin+' >> '+tmpout+' 2>>'+tmpout+'\n' | |
13 tmp.write(cmd) | |
14 else: | |
15 for line in f: | |
16 flds = line.strip().split('\t') | |
17 cmd = 'bigWigSummary -type='+summary_type+' '+bwfilepath+' '+flds[0]+' '+flds[1]+' '+flds[2]+' '+nbin+' >> '+tmpout+' 2>>'+tmpout+'\n' | |
18 tmp.write(cmd) | |
19 f.close() | |
20 # remove blank lines | |
21 tmp.write("sed '/^$/d' "+tmpout+'>'+tmpout+".1\n") | |
22 tmp.write("sed '/^Can/d' "+tmpout+".1 >"+tmpout+".2\n") | |
23 # set n/a to 0 | |
24 tmp.write("sed 's/n\/a/0/g' "+tmpout+".2 >"+tmpout+".3\n") | |
25 # replace text with 0 | |
26 zeros = ''.join(['0\t']*int(nbin)) | |
27 tmp.write("sed 's/^[a-zA-Z]/"+zeros+"/' "+tmpout+".3 >"+tmpout+".4\n") | |
28 # cut the first nbin columns | |
29 tmp.write("cut -f 1-"+nbin+" "+tmpout+".4 > "+tmpout+".5\n") | |
30 tmp.write("paste "+intvfile+" "+tmpout+".5 >"+outfile+"\n") | |
31 tmp.close() | |
32 os.system('chmod +x '+tmpsh) | |
33 os.system('./'+tmpsh) | |
34 #os.system('rm '+tmpout+'*') | |
35 #os.system('rm '+tmpsh) | |
36 | |
37 # strandness: need to reverse bins for - strand | |
38 if nbin > 1 and strand > 0: | |
39 strand = strand - 1 # python is 0 based | |
40 os.system('mv '+outfile+' '+tmpout) | |
41 f = open(tmpout) | |
42 out = open(outfile,'w') | |
43 for line in f: | |
44 flds=line.strip().split('\t') | |
45 if flds[strand] == '+': | |
46 out.write(line) | |
47 else: | |
48 scores = flds[-int(nbin):] | |
49 scores.reverse() | |
50 flds = flds[:-int(nbin)]+scores | |
51 out.write('\t'.join(flds)+'\n') | |
52 os.system('rm '+tmpout) | |
53 f.close() | |
54 out.close() | |
55 # plot | |
56 if int(nbin) > 1: | |
57 rscript = open(tmpsh,"w") | |
58 rscript.write("options(warn=-1)\n") | |
59 rscript.write("x <- read.table('"+outfile+"',sep='\t')\n") | |
60 rscript.write("x <- x[,(ncol(x)+1-"+nbin+"):ncol(x)]\n") | |
61 rscript.write("pdf('"+outplot+"')\n") | |
62 rscript.write("avg <- apply(x,2,mean)\n") | |
63 rscript.write("err <- apply(x,2,sd)/sqrt(nrow(x))\n") | |
64 rscript.write("ylim=c(min(avg-err),max(avg+err))\n") | |
65 rscript.write("xticks <- seq(ncol(x))-(1+ncol(x))/2\n") | |
66 if span >= 0.1: | |
67 rscript.write("avg = loess(avg~xticks,span="+str(span)+")$fitted\n") | |
68 rscript.write("err = loess(err~xticks,span="+str(span)+")$fitted\n") | |
69 rscript.write("par(cex=1.5)\n") | |
70 rscript.write("plot(xticks,avg,ylab='average conservation score',xlab='relative position (bin)',type='l',lwd=0,ylim=ylim)\n") | |
71 rscript.write("polygon(c(xticks,rev(xticks)),c(avg+err,rev(avg-err)),col='slateblue1',border=NA)\n") | |
72 rscript.write("lines(xticks,avg,type='l',lwd=1)\n") | |
73 rscript.write("dev.off()\n") | |
74 rscript.close() | |
75 os.system("R --vanilla < "+tmpsh) | |
76 os.system("rm "+tmpsh) | |
77 | |
78 getScore(sys.argv[1],sys.argv[2],sys.argv[3],sys.argv[4],sys.argv[5],int(sys.argv[6]),sys.argv[7],float(sys.argv[8])) |