comparison mytools/resize.py @ 0:39217fa39ff2

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