Repository navigation
Expand file tree
/
Copy pathcommand_line_aligner.py
More file actions
360 lines (308 loc) · 12.7 KB
/
Copy pathcommand_line_aligner.py
File metadata and controls
360 lines (308 loc) · 12.7 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import os
import sys
import time
import statistics
from settings import *
class CommandLine():
class Alignment():
def __init__(self):
self.start = 0
self.length = 0
self.name = ""
self.contig_name = ""
self.is_forw_starand = True
self.mapping_quality = 0
self.secondary = False
self.supplementary = False
self.cigar = []
def get_name(self):
raise Exception("This function shall be overwritten by al children")
def create_command(self, in_file, out_file):
raise Exception("This function shall be overwritten by al children")
def __get_sam(self, queries, taskset, prefix, allow_error=False):
in_file = TEMP_FILE_FOLDER + prefix + self.get_name().replace(" ", "_") + ".fasta"
out_file = TEMP_FILE_FOLDER + prefix + self.get_name().replace(" ", "_") + ".sam"
with open(in_file, "w") as f:
for name, sequence in queries:
if len(sequence) == 0:
continue
assert not "/" in name
# write in pacbio bla format so that blasr does not cause trouble...
f.write(">" + name + "/0/0_" + str(len(sequence)) + "\n" )
while len(sequence) >= 60:
line = sequence[:60]
sequence = sequence[60:]
f.write(line + "\n")
f.write(sequence + "\n")
#assemble the shell command
cmd_str = self.create_command(in_file, out_file)
if taskset:
cmd_str = "taskset 1 " + cmd_str
start_time = time.time()
returncode = os.system(cmd_str)
elapsed_time = time.time() - start_time
if returncode != 0:
if allow_error:
os.remove(in_file)
return elapsed_time, ""
print("call command:", cmd_str)
print("subprocess returned with ERROR")
exit()
os.remove(in_file)
sam_file = []
with open(out_file, "r") as f:
sam_file = f.readlines()
os.remove(out_file)
return elapsed_time, sam_file
##
# @origin https://stackoverflow.com/questions/10321978/integer-to-bitfield-as-a-list
def __bitfield(self, num):
return list(reversed([True if digit=='1' else False for digit in bin(num)[2:]]))
def __check_flag(self, string, flag_bit):
bits = self.__bitfield(int(string))
if len(bits) <= flag_bit:
return False
return bits[flag_bit]
def secondary(self, string):
return self.__check_flag(string, 8)
def supplementary(self, string):
return self.__check_flag(string, 11)
##
# @details
# queries is a list of (name, sequence) tuples (str, str)
# returns (elapsed_time, alignments)
# where alignments is a list of Alignment objects
def align(self, queries, taskset=True, prefix=""):
elapsed_time, lines = self.__get_sam(queries, taskset, prefix)
#print(sam)
#transform sam file into list data structure
alignments = []
if self.output_type() == "SAM":
# remove the header of the sam format
while len(lines) > 0 and ( len(lines[0]) == 0 or lines[0][0] is '@' ):
lines = lines[1:]
for line in lines:
# ignore empty lines
if len(line) == 0:
continue
# ignore samples where the name starts with dummy...
if line[:6] == "dummy_":
continue
# split into columns
columns = line.split("\t")
try:
align_length = 0
##
# brief helper function to read cigars...
def read_cigar(cigar):
if cigar[0] == '*':
return
number_start = 0
while number_start < len(cigar):
symbol_start = number_start
#increase symbol_start while cigar[symbol_start] is a number...
while cigar[symbol_start] in [str(x) for x in range(10)]:
symbol_start += 1
yield int(cigar[number_start:symbol_start]), cigar[symbol_start]
number_start = symbol_start + 1
assert(len(columns) >= 5)
qLen = 0
rLen = 0
cigar_list = []
for amount, char in read_cigar(columns[5]):
cigar_list.append( (amount, char) )
if char in ['M', 'I', '=', 'X', 'S']:
qLen += amount
if char in ['M', 'D', '=', 'X', 'N']:
rLen += amount
if char in ['M', 'X', '=', 'D']:
align_length += amount
# sanity check...
elif not char in ['M', 'I', 'D', '=', 'X', 'S', 'N', 'H', 'P']:
print("Error: got wierd cigar symbol", char, "in cigar", columns[5])
exit()
alignment = self.Alignment()
alignment.start = int(columns[3])
alignment.cigar = cigar_list
alignment.contig_name = columns[2]
alignment.length = rLen
alignment.mapping_quality = int(columns[4])/255
alignment.name = columns[0].split("/")[0]
alignment.secondary = self.secondary(columns[1])
alignment.supplementary = self.supplementary(columns[1])
alignments.append(alignment)
except Exception as e:
print("Error:", e)
print(line)
exit()
else:
raise Exception(self.output_type() + " output is unsupported.")
return elapsed_time, alignments
def output_type(self):
return "SAM"
def get_start_up_time(self, num_tries=10):
start_up_times = []
for _ in range(num_tries):
elapsed_time, _ = self.__get_sam([("empty_query", "")], False, ".temp", True)
start_up_times.append(elapsed_time)
return statistics.mean(start_up_times)
###
# The list of aligners that can be analyzed:
###
class Minimap2(CommandLine):
def __init__(self, index_str, num_results=None, z_drop=None, presetting=None, silent=False, threads=None):
self.minimap2_home = WORK_SPACE_FOLDER + "minimap2/"
self.num_results = num_results
self.presetting = presetting
if presetting is None:
self.index_str = index_str + ".mmi"
else:
self.index_str = index_str + presetting + ".mmi"
if not z_drop is None:
self.z_drop = " -z " + str(z_drop)
else:
self.z_drop = ""
if silent:
self.silent = " 2> /dev/null"
else:
self.silent = ""
if threads is None:
self.threads = ""
else:
self.threads = " -t " + str(threads)
def create_command(self, in_file, out_file):
cmd_str = self.minimap2_home + "minimap2 -c -a "
cmd_str += self.threads
if not self.num_results is None:
in_file += " --secondary=yes -N " + self.num_results
if not self.presetting is None:
cmd_str += " -x " + self.presetting
return cmd_str + " " + self.index_str + " " + in_file + self.z_drop + " > " + out_file + self.silent
def get_name(self):
return "Minimap 2"
class MA(CommandLine):
def __init__(self, index_str, num_results=None, fast=True, finder_mode=False, soc_width=None, threads=None, silent=False):
self.ma_home = WORK_SPACE_FOLDER + "aligner/"
self.index_str = index_str
self.num_results = num_results
self.mode = "acc"
if threads is None:
self.threads = " -t 1 "
else:
self.threads = " -t " + str(threads)
if isinstance(fast, bool):
if fast:
self.mode = "fast"
elif isinstance(fast, str):
self.mode = fast
else:
print(fast, "is neither string nor bool; aborting!")
exit()
self.finder_mode = finder_mode
self.soc_width = soc_width
if silent:
self.silent = " 2> /dev/null"
else:
self.silent = ""
def create_command(self, in_file, out_file):
cmd_str = self.ma_home + "ma -m " + self.mode + self.threads
if self.finder_mode:
cmd_str += " -d"
if not self.soc_width is None:
cmd_str += " --SoCWidth " + self.soc_width
if not self.num_results is None:
cmd_str += " -n " + self.num_results
return cmd_str + " -x " + self.index_str + " -i " + in_file + " -o " + out_file +self.silent
def get_name(self):
return "MA " + self.mode
def output_type(self):
if self.finder_mode:
return "FINDER"
else:
return "SAM"
class BWA_MEM(CommandLine):
def __init__(self, index_str, num_results=None, z_drop=None, presetting=None, silent=True, threads=None):
self.bwa_home = WORK_SPACE_FOLDER + "bwa/"
self.index_str = index_str + "bwa"
self.num_results = num_results
self.presetting = presetting
if threads is None:
self.threads = ""
else:
self.threads = " -t " + str(threads)
if not z_drop is None:
self.z_drop = " -d " + str(z_drop)
else:
self.z_drop = ""
if silent:
self.silent = " 2> /dev/null"
else:
self.silent = ""
def create_command(self, in_file, out_file):
cmd_str = self.bwa_home + "bwa mem " + self.threads
if not self.num_results is None:
cmd_str += " -a"
if not self.presetting is None:
cmd_str += " -x " + self.presetting
return cmd_str + " " + self.index_str + " " + in_file + self.z_drop + " > " + out_file + self.silent
def get_name(self):
return "BWA-MEM"
class Ngmlr(CommandLine):
def __init__(self, genome_file, threads=None):
self.ngmlr_home = WORK_SPACE_FOLDER + "ngmlr/bin/ngmlr-0.2.8/"
self.genome_file = genome_file
if threads is None:
self.threads = ""
else:
self.threads = " -t " + str(threads)
def create_command(self, in_file, out_file):
cmd_str = self.ngmlr_home + "ngmlr -r " + self.genome_file + " -q " + in_file + self.threads
#print(cmd_str)
return cmd_str + " > " + out_file
def get_name(self):
return "Ngmlr"
class Blasr(CommandLine):
def __init__(self, index_str, genome_str, threads=None):
self.blasr_home = WORK_SPACE_FOLDER + "blasr/build/bin/"
self.index_str = index_str + "blasr"
self.genome_str = genome_str
if threads is None:
self.threads = ""
else:
self.threads = " --nproc " + str(threads)
def create_command(self, in_file, out_file):
cmd_str = self.blasr_home + "blasr " + self.threads + " " + in_file
# --nproc 32
return cmd_str + " " + self.genome_str + " --sa " + self.index_str + " --sam --out " + out_file + " --cigarUseSeqMatch"
def get_name(self):
return "Blasr"
class G_MAP(CommandLine):
def __init__(self, genome_str, threads=None):
self.g_home = WORK_SPACE_FOLDER + "graphmap/bin/Linux-x64/"
self.genome_str = genome_str
if threads is None:
self.threads = ""
else:
self.threads = " -t " + str(threads)
def create_command(self, in_file, out_file):
cmd_str = self.g_home + "graphmap align -v 0 -r " + self.genome_str + self.threads
return cmd_str + " -d " + in_file + " -o " + out_file
def get_name(self):
return "GraphMap"
class Bowtie2(CommandLine):
def __init__(self, index_str, threads=None):
self.bowtie2_home = WORK_SPACE_FOLDER + "bowtie2/bowtie2-2.3.3.1/"
self.index_str = index_str + "bowtie2"
if threads is None:
self.threads = ""
else:
self.threads = " -p " + str(threads)
def create_command(self, in_file, out_file):
cmd_str = self.bowtie2_home + "bowtie2 " + self.threads
index_str = " -x " + self.index_str
input_str = " -f -U " + in_file
return cmd_str + " " + index_str + " " + input_str + " > " + out_file
def do_checks(self):
return False
def get_name(self):
return "Bowtie2"