annotate resize.py @ 12:2f4ea569f048

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