annotate bed_clean.py @ 8:c887ed5d2c51

Uploaded
author xuebing
date Sat, 31 Mar 2012 13:08:29 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
1 import sys
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
2
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
3 def readChrSize(filename):
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
4 f = open(filename)
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
5 chrSize = {}
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
6 for line in f:
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
7 chrom,size = line.strip().split()
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
8 chrSize[chrom]=int(size)
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
9 f.close()
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
10 return chrSize
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
11
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
12 def cleanFile(filename,chrSize,outfile):
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
13 f = open(filename)
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
14 out = open(outfile,'w')
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
15 i = 0
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
16 for line in f:
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
17 i = i + 1
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
18 flds = line.strip().split('\t')
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
19 if len(flds) < 3:
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
20 print 'line',i,'incomplete line:\n',line
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
21 elif chrSize.has_key(flds[0]):
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
22 if int(flds[1]) > int(flds[2]):
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
23 tmp = flds[1]
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
24 flds[1] = flds[2]
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
25 flds[2] = tmp
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
26 if int( flds[1]) < 0 or int(flds[2]) <0:
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
27 print 'line',i,'negative coordinates:\n',line
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
28 elif int(flds[2]) > chrSize[flds[0]]:
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
29 print 'line',i,'end larger than chr size:\n',line
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
30 else:
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
31 if flds[5] == '*':
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
32 flds[5] = '+'
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
33 print 'line',i,' strand * changed to +\n', line
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
34 out.write('\t'.join(flds)+'\n')
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
35 else:
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
36 print 'line',i,'chromosome',flds[0],'not found!\n',line
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
37 f.close()
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
38 out.close()
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
39
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
40 if len(sys.argv) < 4:
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
41 print "python bedClean.py in.bed chrsizefile out.bed"
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
42 exit()
c887ed5d2c51 Uploaded
xuebing
parents:
diff changeset
43 cleanFile(sys.argv[1],readChrSize(sys.argv[2]),sys.argv[3])