annotate resize.py @ 23:4e646baac551

Uploaded
author xuebing
date Sat, 31 Mar 2012 11:53:40 -0400
parents 16ba480adf96
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
1 '''
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
2 change start and end of interval files
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
3 '''
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
4
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
5 import sys
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
6
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
7 def resize(infile,outfile,expr_start,expr_end,strand):
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
8 fin = open(infile)
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
9 fout = open(outfile,'w')
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
10 if expr_start[0:3] == 'end':
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
11 c1 = 2
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
12 n1 = int(expr_start[3:])
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
13 else:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
14 c1 = 1
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
15 n1 = int(expr_start[5:])
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
16 if expr_end[0:3] == 'end':
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
17 c2 = 2
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
18 n2 = int(expr_end[3:])
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
19 else:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
20 c2 = 1
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
21 n2 = int(expr_end[5:])
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
22 if strand == 'ignore':
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
23 for line in fin:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
24 flds = line.strip().split('\t')
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
25 start = int(flds[c1]) + n1
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
26 if start >= 0:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
27 end = int(flds[c2]) + n2
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
28 if end >= start:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
29 flds[1] = str(start)
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
30 flds[2] = str(end)
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
31 fout.write('\t'.join(flds)+'\n')
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
32 else:# upstream downstream
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
33 for line in fin:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
34 flds = line.strip().split('\t')
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
35 if flds[5] == '+':
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
36 start = int(flds[c1]) + n1
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
37 if start >= 0:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
38 end = int(flds[c2]) + n2
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
39 if end >= start:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
40 flds[1] = str(start)
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
41 flds[2] = str(end)
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
42 fout.write('\t'.join(flds)+'\n')
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
43 else: # on the - strand
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
44 start = int(flds[3-c2]) - n2 # end=end+n2
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
45 if start >= 0:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
46 end = int(flds[3-c1]) - n1
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
47 if end >= start:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
48 flds[1] = str(start)
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
49 flds[2] = str(end)
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
50 fout.write('\t'.join(flds)+'\n')
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
51 fin.close()
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
52 fout.close()
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
53
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
54 if __name__ == "__main__":
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
55 resize(sys.argv[1],sys.argv[2],sys.argv[3],sys.argv[4],sys.argv[5])
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
56 # python resize.py in.bed out.bed start-3 end+5 both
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
57 # python resize.py <input.bed> <output.bed> expr_start expr_end strand(both/+/-)