-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvert_stripmask_to_bed.py
More file actions
69 lines (58 loc) · 1.87 KB
/
Copy pathconvert_stripmask_to_bed.py
File metadata and controls
69 lines (58 loc) · 1.87 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
# This script takes a mask file generated by Genome STRiP ComputeGenomeMask:
# http://gatkforums.broadinstitute.org/gatk/discussion/1499/computegenomemask
# and outputs a BED3 file containing the masked regions to stdout
import sys
CURRENT_CHROMOSOME = None
CURRENT_POSITION = 0
IN_MASK_INTERVAL = False
IN_MASK_CHAR = "1"
NOT_IN_MASK_CHAR = "0"
CURRENT_INTERVAL = None
def main():
with open(sys.argv[1], "r") as input_file:
for line in input_file:
line = line.rstrip()
if line.startswith(">"):
start_new_chromosome(line)
else:
parse_mask_line(line)
if IN_MASK_INTERVAL:
exit_mask_interval_and_print()
def start_new_chromosome(line):
global CURRENT_CHROMOSOME
global CURRENT_POSITION
if IN_MASK_INTERVAL:
exit_mask_interval_and_print()
CURRENT_CHROMOSOME = get_chromosome_name_from_line(line)
CURRENT_POSITION = 0
def get_chromosome_name_from_line(line):
return line.rstrip()[1:]
def parse_mask_line(line):
for char in line:
parse_mask_char(char)
def parse_mask_char(char):
global CURRENT_POSITION
if char == NOT_IN_MASK_CHAR:
if IN_MASK_INTERVAL:
exit_mask_interval_and_print()
else:
assert char == IN_MASK_CHAR
if not IN_MASK_INTERVAL:
enter_mask_interval()
CURRENT_POSITION += 1
def enter_mask_interval():
global IN_MASK_INTERVAL
global CURRENT_INTERVAL
assert not IN_MASK_INTERVAL
IN_MASK_INTERVAL = True
CURRENT_INTERVAL = CURRENT_CHROMOSOME + "\t" + str(CURRENT_POSITION)
def exit_mask_interval_and_print():
global IN_MASK_INTERVAL
global CURRENT_INTERVAL
assert IN_MASK_INTERVAL
IN_MASK_INTERVAL = False
CURRENT_INTERVAL += ("\t" + str(CURRENT_POSITION))
print(CURRENT_INTERVAL)
if __name__ == "__main__":
main()