annotate alignr.py @ 4:e0b0c3b5514c

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