annotate tools/mytools/alignr.py @ 2:c2a356708570

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:42 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 the scripts takes two files as input, and compute the coverage of
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 features in input 1 across features in input 2. Features in input 2 are
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 divided into bins and coverage is computed for each bin.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 please check the help information by typing:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 python align.py -h
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 requirement:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 please install the following tools first:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 bedtools: for read/region overlapping, http://code.google.com/p/bedtools/
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 import os,sys,os.path
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 from optparse import OptionParser
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 def lineCount(filename):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 with open(filename) as f:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 for i, l in enumerate(f):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 return i + 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 def combineFilename(f1,f2):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 fuse two file names into one
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 return f1.split('/')[-1]+'-'+f2.split('/')[-1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 def checkFormat(filename1,filename2,input1format):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 check the format of input files
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 # file1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 # read the first line, see how many filds
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 ncol1 = 6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 if input1format == "BED":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 f = open(filename1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 line = f.readline().strip().split('\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 ncol1 = len(line)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 if ncol1 < 3:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 print "ERROR: "+filename1+" has only "+str(ncol1)+" columns (>=3 required). Make sure it has NO header line and is TAB-delimited."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 sys.exit(1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 f.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 # file2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 f = open(filename2)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 line = f.readline().strip().split('\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 ncol2 = len(line)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 if ncol2 < 3:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 print "ERROR: "+filename2+" has only "+str(ncol2)+" columns (>=3 required). Make sure it has NO header line and is TAB-delimited."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 sys.exit(1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 return ncol1,ncol2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 def makeBed(filename,ncol):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 add up to 6 column
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 f = open(filename)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 outfile = filename+'.tmp.bed'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 outf = open(outfile,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 if ncol == 3:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 for line in f:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 outf.write(line.strip()+'\t.\t0\t+\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 elif ncol == 4:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 for line in f:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 outf.write(line.strip()+'\t0\t+\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 if ncol == 5:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 for line in f:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 outf.write(line.strip()+'\t+\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 f.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 outf.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 return outfile
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 def makeWindow(filename,window):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 outfile = filename+'-window='+str(window)+'.tmp.bed'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 if not os.path.exists(outfile):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 f=open(filename)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 out = open(outfile,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 lines = f.readlines()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 if 'track' in lines[0]:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 del lines[0]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 for line in lines:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 flds = line.strip().split('\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 #new position
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 center = (int(flds[1]) + int(flds[2]))/2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 start = center - window
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 end = center + window
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 if start >= 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 flds[1] = str(start)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 flds[2] = str(end)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 out.write('\t'.join(flds)+'\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 f.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 out.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 return outfile
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 def groupReadsMapped2aRegion(filename,ncol):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 read output from intersectBED
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 find all reads mapped to each region
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 f=open(filename)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 #If filename cannot be opened, print an error message and exit
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 except IOError:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 print "could not open",filename,"Are you sure this file exists?"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 sys.exit(1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 lines = f.readlines()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 allReadsStart = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 allReadsEnd = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 regionStrand = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 regionStart = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 regionEnd = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 for line in lines:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 flds = line.strip().split('\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 key = '_'.join(flds[ncol:(ncol+4)])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 if not allReadsStart.has_key(key):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127 allReadsStart[key] = list()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 allReadsEnd[key] = list()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 #print flds[ncol+0],flds[ncol+1],flds[ncol+2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 allReadsStart[key].append(int(flds[1]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131 allReadsEnd[key].append(int(flds[2]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 regionStrand[key] = flds[ncol+5]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 regionStart[key] = int(flds[ncol+1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 regionEnd[key] = int(flds[ncol+2])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135 return (allReadsStart,allReadsEnd,regionStrand,regionStart,regionEnd)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 def createRegionProfile(allReadsStart,allReadsEnd,regionStrand,regionStart,regionEnd,nbins):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140 each region is divided into nbins
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
141 compute the number of reads covering each bin for each region
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
142 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
143 RegionProfile = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
144 nRead = {} # num of all reads in the region
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
145 for region in allReadsStart.keys():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
146 RegionProfile[region] = [0]*nbins
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
147 nRead[region] = len(allReadsStart[region])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
148 #print region,nRead[region],allReadsStart[region]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
149 for i in range(nRead[region]):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
150 RegionProfile[region] = updateRegionCount(RegionProfile[region],allReadsStart[region][i],allReadsEnd[region][i],regionStart[region],regionEnd[region],regionStrand[region],nbins)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
151 return RegionProfile,nRead
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
152
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
153 def updateRegionCount(RegionCount,readStart,readEnd,regionStart,regionEnd,strand,nbins):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
154 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
155 each region is divided into nbins,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
156 add 1 to each bin covered by the read
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
157 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
158 L = regionEnd-regionStart
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
159 start = int(nbins*(readStart-regionStart)/L)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
160 end = int(nbins*(readEnd-regionStart)/L)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
161 if start < 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
162 start = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
163 if end > nbins:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
164 end = nbins
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
165 if strand == '-':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
166 for i in range(start,end):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
167 RegionCount[nbins-1-i] = RegionCount[nbins-1-i] + 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
168 else: # if the 6th column of the input is not strand, will treat as + strand by default
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
169 for i in range(start,end):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
170 RegionCount[i] = RegionCount[i] + 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
171 return RegionCount
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
172
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
173 def saveProfile(filename,Profile,nRegion):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
174 out = open(filename,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
175 for regionType in Profile.keys():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
176 #print Profile[regionType]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
177 out.write(regionType+'\t'+str(nRegion[regionType])+'\t'+'\t'.join(map(str,Profile[regionType]))+'\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
178
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
179 def saveSummary(filename,Profile,nbin):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
180 out = open(filename+'.summary','w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
181
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
182 nfeat = len(Profile)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
183 summaryprofile = [0]*nbin
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
184 for regionType in Profile.keys():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
185 for i in range(nbin):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
186 summaryprofile[i] += Profile[regionType][i]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
187 out.write(filename+'\t'+str(nfeat)+'\t'+'\t'.join(map(str,summaryprofile))+'\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
188 out.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
189 # calculate standard error
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
190 out = open(filename+'.standarderror','w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
191 sd = [0.0]*nbin
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
192 u = [0.0]*nbin
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
193 for i in range(nbin):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
194 u[i] = float(summaryprofile[i])/nfeat
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
195 for regionType in Profile.keys():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
196 sd[i] = sd[i] + (Profile[regionType][i] - u[i])**2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
197 sd[i] = sd[i]**0.5 / nfeat
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
198 out.write(filename+'\t'+str(nfeat)+'\t'+'\t'.join(map(str,sd))+'\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
199 out.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
200
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
201 def main():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
202 usage = "usage: %prog [options] -a inputA -b inputB"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
203 parser = OptionParser(usage)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
204 parser.add_option("-a", dest="inputA",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
205 help="(required) input file A, interval (first 3 columns are chrN, start and end) or BAM format. The script computes the depth of coverage of features in file A across the features in file B" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
206 parser.add_option("-b",dest="inputB",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
207 help="(required) input file B, interval file" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
208 parser.add_option("-f",dest="aformat",default="BED",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
209 help="Format of input file A. Can be BED (default) or BAM")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
210 parser.add_option("-w",type='int',dest="window",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
211 help="Generate new inputB by making a window of 2 x WINDOW bp (in total) flanking the center of each input feature" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
212 parser.add_option("-n", type="int", dest="nbins",default=100,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
213 help="number of bins. Features in B are binned, and the coverage is computed for each bin. Default is 100")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
214 parser.add_option("-s",action="store_true", dest="strandness",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
215 help="enforce strandness: require overlapping on the same strand. Default is off")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
216 parser.add_option("-p",action="store_true", dest="plot",default=False,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
217 help="load existed intersectBed outputfile")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
218 parser.add_option("-q", action="store_true", dest="quiet",default=False,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
219 help="suppress output on screen")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
220 parser.add_option("-o", dest="output_data",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
221 help="(optional) output coverage file (txt) name." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
222 parser.add_option("-v", dest="output_plot",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
223 help="(optional) output plot (pdf) file name." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
224 parser.add_option("-l", dest="plot_title", default="",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
225 help="(optional) output title of the plot." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
226 parser.add_option("--ylim", dest="ylim", default="min,max",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
227 help="(optional) ylim of the plot" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
228 parser.add_option("--summary-only", action="store_true", dest="summary_only",default=False,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
229 help="save profile summary only (no data for individual features)")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
230 parser.add_option("--compute-se", action="store_true", dest="compute_se",default=False,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
231 help="compute and plot standard deviation for each bin. used when --summary-only is on")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
232 parser.add_option("--profile-only", action="store_true", dest="profile_only",default=False,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
233 help="save profile only (no plot)")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
234 parser.add_option("--span", type="float", dest="span",default=0.1,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
235 help="loess span smooth parameter, 0.1 ~ 1")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
236
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
237 (options, args) = parser.parse_args()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
238
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
239 if options.inputA == None or options.inputB == None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
240 parser.error("Please specify two input files!!")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
241
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
242 if not options.quiet:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
243 print "checking input file format..."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
244
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
245 ncol,ncol2 = checkFormat(options.inputA ,options.inputB,options.aformat)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
246
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
247 if ncol2 < 6:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
248 options.inputB = makeBed(options.inputB,ncol2)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
249 if not options.quiet:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
250 print "fill up 6 columns"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
251
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
252 if options.window > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
253 if not options.quiet:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
254 print "making windows from "+options.inputB+"..."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
255 options.inputB = makeWindow(options.inputB,options.window)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
256
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
257 output = combineFilename(str(options.inputA),str(options.inputB))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
258
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
259 if not options.plot:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
260 if options.aformat == "BAM":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
261 cmd = "intersectBed -abam "+str(options.inputA)+" -b "+str(options.inputB) + ' -bed -split '
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
262 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
263 cmd = "intersectBed -a "+str(options.inputA)+" -b "+str(options.inputB)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
264 if options.strandness:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
265 cmd = cmd + ' -s'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
266 cmd = cmd +" -wo > "+ output+'-intersect.tmp.bed'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
267 if not options.quiet:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
268 print "search for overlappings: "+cmd
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
269 status = os.system(cmd)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
270 if status != 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
271 sys.exit(1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
272
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
273
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
274 if not options.quiet:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
275 print 'group reads mapped to the same region...'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
276
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
277 allReadsStart,allReadsEnd,regionStrand,regionStart,regionEnd = groupReadsMapped2aRegion(output+'-intersect.tmp.bed',ncol)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
278
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
279 if len(allReadsStart) == 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
280 if not options.quiet:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
281 print 'no overlap found!!'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
282 os.system('rm *tmp.*')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
283 sys.exit(1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
284
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
285 if not options.quiet:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
286 print 'count number of reads mapped to each bin...'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
287
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
288 RegionProfile,nRead = createRegionProfile(allReadsStart,allReadsEnd,regionStrand,regionStart,regionEnd,options.nbins)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
289
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
290 if options.output_data == None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
291 options.output_data = output+'.txt'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
292
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
293 if options.summary_only:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
294 saveSummary(options.output_data,RegionProfile,options.nbins)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
295
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
296 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
297 saveProfile(options.output_data,RegionProfile,nRead)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
298
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
299 if not options.quiet:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
300 print 'results saved to: '+ options.output_data
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
301
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
302 if not (options.summary_only or options.profile_only ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
303 # visualize
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
304
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
305 if options.window < 1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
306 xlab = 'relative position (bins)'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
307 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
308 xlab = 'relative position (bp)'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
309
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
310 if options.output_plot == None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
311 options.output_plot = output+'.pdf'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
312
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
313 title = options.plot_title+'\n n = '+str(len(RegionProfile))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
314
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
315 rscript = open("tmp.r","w")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
316 rscript.write("x <- read.table('"+options.output_data+"')\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
317 rscript.write("pdf('"+options.output_plot+"')\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
318 rscript.write("avg <- colSums(x[,3:ncol(x)])/nrow(x)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
319 rscript.write("err <- sd(x[,3:ncol(x)])/sqrt(nrow(x))\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
320
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
321 if options.window == 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
322 rscript.write("xticks <- seq("+str(options.nbins)+")\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
323 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
324 rscript.write("xticks <- seq("+str(-options.window)+","+str(options.window)+",length.out="+str(options.nbins)+")\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
325
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
326 if options.ylim != 'min,max':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
327 rscript.write("ylim=c("+options.ylim+")\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
328 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
329 rscript.write("ylim=c(min(avg-err),max(avg+err))\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
330 rscript.write("par(cex=1.5)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
331 #smooth
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
332 if options.span >= 0.1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
333 rscript.write("avg = loess(avg~xticks,span="+str(options.span)+")$fitted\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
334 rscript.write("err = loess(err~xticks,span="+str(options.span)+")$fitted\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
335 rscript.write("plot(xticks,avg,ylab='average coverage',main='"+title+"',xlab='"+xlab+"',type='l',lwd=0,ylim=ylim)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
336 rscript.write("polygon(c(xticks,rev(xticks)),c(avg+err,rev(avg-err)),col='slateblue1',border=NA)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
337 rscript.write("lines(xticks,avg,type='l',lwd=1)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
338 #rscript.write("xticks <- barplot(avg,names.arg=seq("+str(options.nbins)+"),ylab='average coverage',main='"+title+"',xlab='"+xlab+"',,ylim=c(min(avg-err),max(avg+err)))\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
339 #rscript.write("arrows(xticks,avg+err, xticks, avg-err, angle=90, code=3, length=0.0,col='green')\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
340 #rscript.write("lines(xticks,avg,lwd=2)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
341 #rscript.write("lines(xticks,avg-err,col='green')\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
342 #rscript.write("lines(xticks,avg+err,col='green')\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
343 rscript.write("dev.off()\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
344 rscript.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
345
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
346 os.system("R --vanilla < tmp.r")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
347
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
348 # remove intermediate output
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
349 os.system('rm *tmp.*')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
350
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
351
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
352 if __name__ == "__main__":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
353 main()