-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContigous_alignment.py
More file actions
executable file
·279 lines (241 loc) · 8.54 KB
/
Contigous_alignment.py
File metadata and controls
executable file
·279 lines (241 loc) · 8.54 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/python3
import argparse
import os
import time
from typing import Optional, DefaultDict, TextIO
import numpy as np
read_dict_type = dict[Optional[list[Optional[tuple[int, int]]]]]
def parse_cigar(cig: str) -> list[tuple[int,str]]:
'''
Return a list of cigar elements as tuples
'''
#print(f"Parse_Cigar : {cigar}")
cigar_components: list[Optional[tuple[int,str]]] = []
current_num: str = ""
ch: str
for ch in cig:
#split, add, reset on identified alpha char (I, D, M, H, S)
if ch.isalpha():
cigar_components.append((int(current_num), ch))
current_num = ""
else:
current_num += ch
return cigar_components
def parse_cigar_for_length(cigar: str) -> int:
#Determine the aligned length with a cigar string
count:str = ""
code: str = ""
total_dist: int = 0
c: str
for c in cigar:
if c.isalpha():
if c == 'M' or c == 'D':
total_dist += int(count)
count = ""
else:
count = count + c
return total_dist
def is_alignment_contigous(line: str,
max_ins: int,
max_del: int,
max_soft: Optional[int] = None,
max_hard: Optional[int] = None
) -> tuple[bool, str]:
#Check if an alignment is contigous by parsing cigar string elements
#print(f"Is alignment_contigous\n{line}")
for element in parse_cigar(line):
if element[1] == 'I' and element[0] > max_ins:
return (False, 'I')
if element[1] == 'D' and element[0] > max_del:
return (False, 'D')
if element[1] == 'S' and max_soft and element[0] > max_soft:
return (False, 'S')
if element[1] == 'H' and max_hard and element[0] > max_hard:
return (False, 'H')
return (True, 'A')
def coverage_depth(alignment_list_dict: read_dict_type,
min_coverage_depth: int
) -> read_dict_type:
# Sort list of alignments by start position
print("Calculating coverage depth")
key : str
covered_dict: read_dict_type = {}
for key in list(alignment_list_dict.keys()):
covered_dict[key] = list()
print(f"{key=}")
alignment_list_dict[key] = sorted(alignment_list_dict[key],
key=lambda x: x[0])
pair: tuple[int, int]
max_pos: int = 0
for pair in alignment_list_dict[key]:
if pair[1] > max_pos:
max_pos = pair[1]
coverage_list: np.array = np.zeros(max_pos+1)
for pair in alignment_list_dict[key]:
#trying numpy native addition instead of iteration
coverage_list[pair[0]:pair[1] + 1] = \
coverage_list[pair[0]:pair[1] + 1] + 1
i: int
start: int = -1
for i in range(len(coverage_list)):
if i % 10000000 == 0:
print(i)
if start == -1 and coverage_list[i] >= min_coverage_depth:
start = i
elif start != -1 and coverage_list[i] < min_coverage_depth:
covered_dict[key].append((start, i-1))
start = -1
if start != -1:
covered_dict[key].append((start, i-1))
return covered_dict
def write_to_bed(alignment_dict: read_dict_type,
output: str
)-> None:
out_fh: TextIO = open(output, 'w')
chrom: str
pair: tuple[int,int]
for chrom in alignment_dict:
for pair in alignment_dict[chrom]:
out_fh.write(f"{chrom}\t{pair[0]}\t{pair[1]}\n")
def main(output: str,
input_list: list[str],
supplementary_output: Optional[str] = None,
max_ins: Optional[int] = None,
max_del: Optional[int] = None,
max_soft: Optional[int] = None,
max_hard: Optional[int] = None,
min_coverage_depth: Optional[int] = None
):
contigous_alignments: dict[Optional[list[Optional[tuple[str, int, int]]]]]\
= {}
file_path: str
file_handle: TextIO
for file_path in input_list:
print(f"Reading in {file_path}")
file_handle = open(file_path, 'r')
start: int
stop: int
line: str
i: int = 0
contiguity_count: dict[str, int] = {'A':0,
'H':0,
'S':0,
'I':0,
'D':0}
for line in file_handle:
if i % 1000000 == 0:
print(f"{i} reads processed")
i += 1
if line[0] == '@':
continue
contiguity: tuple[bool, str]
contiguity = is_alignment_contigous(line.split()[5],
max_ins,
max_del,
max_soft,
max_hard
)
contiguity_count[contiguity[1]] += 1
if contiguity[0]:
chrom: str = line.split()[2]
#Exclude unaligned reads
if chrom == "*":
continue
start = int(line.split()[3])
stop = (int(parse_cigar_for_length(line.split()[5])) +
int(line.split()[3]))
if chrom not in contigous_alignments:
contigous_alignments[chrom] = list()
contigous_alignments[chrom].append((start,stop))
key: str
for key in contiguity_count:
print(f"{contiguity_count[key]} reads were {key}")
coverage_filtered_dict: read_dict_type = \
coverage_depth(contigous_alignments,
min_coverage_depth)
write_to_bed(coverage_filtered_dict, output)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
"--output",
"-o",
type=str,
required=True,
help="Path to desitnation output bed file"
)
parser.add_argument(
"--supplementary_output",
"-so",
type=str,
help="Optional output for regions deemed not covered",
default=None
)
parser.add_argument(
"--input",
"-i",
type=str,
nargs='+',
required=True,
help="Any number of input sam paths, must be for same reference"
)
parser.add_argument(
"--min_coverage",
"-m",
type=int,
help="The minimum number of contigous reads to count as observed."
)
parser.add_argument(
"--max_insertion",
type=int,
help="The maximum size of a single insertion event in a given SAM " +
"alignment."
)
parser.add_argument(
"--max_deletion",
type=int,
help="The maximum size of a single deletion event in a given SAM " +
"alignment.",
default=None
)
parser.add_argument(
"--max_softclip",
type=int,
help="Maximum allowable size for softclip",
default=None
)
parser.add_argument(
"--max_hardclip",
type=int,
help="Maximum allowable size for hardclip",
default=None
)
Flags, unparsed = parser.parse_known_args()
t1:time
t2:time
#Check input types
assert type(Flags.output) == str
assert (not Flags.supplementary_output or
type(Flags.supplementary_output) == str)
assert (type(Flags.input) == str or
type(Flags.input) == list or
not Flags.input)
assert (not Flags.max_insertion or
type(Flags.max_insertion) == int)
assert (not Flags.max_deletion or
type(Flags.max_deletion) == int)
assert (not Flags.max_softclip or
type(Flags.max_softclip) == int)
assert (not Flags.max_hardclip or
type(Flags.max_hardclip) == int)
t1 = time.time()
main(Flags.output,
Flags.input,
Flags.supplementary_output,
Flags.max_insertion,
Flags.max_deletion,
Flags.max_softclip,
Flags.max_hardclip,
Flags.min_coverage
)
t2 = time.time()
print(f"{round((t2-t1) / 60, 4)} minutes elapsed")