-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbed_center_region_2_bed.py
51 lines (38 loc) · 1.23 KB
/
bed_center_region_2_bed.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
import re, os, sys, shutil
from math import *
from string import *
from optparse import OptionParser
import operator
import bisect
plus = re.compile("\+");
minus = re.compile("\-");
Dir = os.getcwd();
'''
This module is to separate BED file into 2, according to the size of each region.
'''
def main(argv):
parser = OptionParser()
parser.add_option("-b", "--sample", action="store", type="string", dest="infile", metavar="<file>", help="sample name")
parser.add_option("-e", "--halfsize", action="store", type="int", dest="shift", metavar="<int>", help="half of region size")
parser.add_option("-o", "--output", action="store", type="string", dest="out_file", metavar="<file>", help="name of output file")
(opt, args) = parser.parse_args(argv)
if len(argv) < 4:
parser.print_help()
sys.exit(1)
f = open(opt.infile, 'r')
u = open(opt.out_file, 'w')
for line in f:
if not re.search("#", line):
line = line.strip()
sline = line.split()
start = atoi(sline[1])
end = atoi(sline[2])
center = (start+end) / 2
sline[1] = str(center - opt.shift)
sline[2] = str(center + opt.shift)
u.write('\t'.join(sline) + '\n')
f.close()
u.close()
if __name__ == "__main__":
main(sys.argv)