annotate mytools/resize.py @ 0:39217fa39ff2

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