-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconvert_cycles_file.py
executable file
·164 lines (141 loc) · 7.37 KB
/
convert_cycles_file.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
import os
import argparse
# Author: Siavash Dehkordi, modified by Jens Luebeck
# Converts cycles file to be numbered by BPG segment.
def extract_seg(gpath):
segment_number = 0
seg_dict = {}
segments_line = ''
with open(gpath) as f:
for line in f:
line = line.strip()
if line.startswith('sequence'):
line = line.split()
segment_number += 1
line_s = line[1].split(':')
start_chr = line_s[0]
start_index = line_s[1][:-1]
# line[1].index('-')]
line_e = line[2].split(':')
end_chr = line_e[0]
end_index = line_e[1][:-1]
seg_dict[start_index] = {'segment_number': segment_number, 'start_chr': start_chr,
'start_index': start_index,
'end_cht': end_chr, 'end_index': end_index}
seg_dict[end_index] = {'segment_number': segment_number, 'start_chr': start_chr,
'start_index': start_index,
'end_cht': end_chr, 'end_index': end_index}
segments_line = segments_line + 'Segment\t' + str(segment_number) + '\t' + start_chr + '\t' + str(
start_index) + '\t' + str(end_index) + '\n'
return seg_dict, segments_line
def extract_fuse_seg(cpath):
fuse_seg_dict = {}
with open(cpath) as f:
for line in f:
if line.startswith('Segment'):
line = line.strip().split()
fuse_seg_dict[line[1]] = {'start_chr': line[2], 'start_index': line[3],
'end_cht': line[2], 'end_index': line[4]}
return fuse_seg_dict
def make_new_cycle(graph_path, cycle_path, output_name):
seg_dict, segments_line = extract_seg(graph_path)
fuse_seg_dict = extract_fuse_seg(cycle_path)
segment_dict = {'0': {'segment_chr': '0', 'segment_start': '0',
'segment_end': '0', 'length': '0'}}
with open(output_name, 'w') as the_file:
with open(cycle_path) as f:
for line in f:
if line.startswith('Segment'):
line = line.strip().split()
segment_number = line[1]
segment_chr = line[2]
segment_start = line[3]
segment_end = line[4]
segment_length = int(segment_end) - int(segment_start) + 1
segment_dict[segment_number] = {'segment_chr': segment_chr, 'segment_start': segment_start,
'segment_end': segment_end, 'length': segment_length}
elif line.startswith('Cycle'):
continues = 1
end_loc = 0
length = 0
fields = line.rstrip().split(';')
splitfields = [(x.rsplit('=')[0], x.rsplit('=')[1]) for x in fields]
lineDict = {k: v for k, v in splitfields}
for x in splitfields:
if x[0] != "Segments":
the_file.write(x[0] + "=" + x[1] + ";")
else:
segseq = x
a = segseq[0] + '='
circle = ';IsCyclicPath=True'
segseq = segseq[1].strip().split(',')
for l in segseq:
number = l[:-1]
length = length + int(segment_dict[number]['length'])
sign = l[-1]
if number == '0':
circle = ';IsCyclicPath=False'
# the_file.write(l+',')
a = a + l + ','
else:
fuse_seg = fuse_seg_dict[number]
start_seg = int(fuse_seg['start_index'])
end_seg = int(fuse_seg['end_index'])
start_number = int(seg_dict[str(start_seg)]['segment_number'])
end_number = int(seg_dict[str(end_seg)]['segment_number'])
if sign == '+':
s = ''
for i in range(start_number, end_number + 1):
s = s + str(i) + sign + ','
# the_file.write(s)
a = a + s
if end_loc == 0:
end_loc = int(segment_dict[number]['segment_end'])
chrom = segment_dict[number]['segment_chr']
else:
if (chrom != segment_dict[number]['segment_chr'] or abs(
int(segment_dict[number]['segment_end']) - end_loc) > 10) and number != 0:
continues +=1
end_loc = int(segment_dict[number]['segment_end'])
chrom = segment_dict[number]['segment_chr']
elif sign == '-':
s = ''
for i in range(end_number, start_number - 1, -1):
s = s + str(i) + sign + ','
# the_file.write(s)
a = a + s
if end_loc == 0:
end_loc = int(segment_dict[number]['segment_start'])
chrom = segment_dict[number]['segment_chr']
else:
if (chrom != segment_dict[number]['segment_chr'] or abs(
int(segment_dict[number]['segment_end']) - end_loc) > 10) and number != 0:
continues +=1
end_loc = int(segment_dict[number]['segment_end'])
chrom = segment_dict[number]['segment_chr']
if not 'Length' in lineDict:
the_file.write('Length=' + str(int(length)) + 'bp;')
the_file.write(a[:-1])
if not 'IsCyclicPath' in lineDict:
the_file.write(circle)
the_file.write('\n')
elif line.startswith('List of cycle'):
the_file.write(line)
the_file.write(segments_line)
else:
the_file.write(line)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-o", "--output", help="output prefix")
parser.add_argument("-c", "--cycle", help="cycle file", required=True)
parser.add_argument("-g", "--graph", help="graph file", required=True)
args = parser.parse_args()
if not args.output:
args.output = os.getcwd() + "/"
bn = os.path.basename(args.cycle).rsplit(".txt")[0].split("_cycles")
args.output += (bn[0] + "_BPG_converted_cycles.txt")
if not args.output.endswith("_cycles.txt"):
args.output += "_cycles.txt"
print(args.output)
make_new_cycle(args.graph, args.cycle, args.output)