-
Notifications
You must be signed in to change notification settings - Fork 0
/
most-freq-subseq.py
executable file
·342 lines (272 loc) · 9.64 KB
/
most-freq-subseq.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
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
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
import sys
if sys.version_info.major < 3:
print( "\nYour python interpreter version is " + "%d.%d" % (sys.version_info.major,
sys.version_info.minor) )
print(" Please, use Python 3!\a")
# In python 2 'raw_input' does the same thing as 'input' in python 3.
# Neither does 'input' in python2.
raw_input("Press ENTER to exit:")
sys.exit(1)
# end if
__version__ = "1.2.a"
__last_update_date__ = "2021-08-10"
def platf_depend_exit(exit_code=0):
"""
Function asks to press ENTER press on Windows
and exits after that.
:type exit_code: int;
"""
if sys.platform.startswith("win"):
input("Press ENTER to exit:")
# end if
sys.exit(exit_code)
# end def platf_depend_exit
def print_help():
print("""\n |=== most-freq-subseq.py ===|\n
This script finds N most frequently occuring subsequences
of given length for each sequence in FASTA file.""")
print("Format of input: `*.fasta(.gz)` or `*.fa(.gz)` file and length of query subsequence.")
print("Version: {}; {} edition.\n".format(__version__, __last_update_date__))
print("./most-freq-subseq.py [-s <FASTA_FILE>] [-l <LENGTH>] [-h|--help]\n")
print("Options:")
print(" -h (--help) --- print help message;")
print(" -v (--version) --- print versson;")
print(" -l (--query-length) --- length of subsequence to search;")
print(""" -s (--subject) --- FASTA file that contains sequence(s) to count
the most frequently occuring subsequence in;""")
print(""" -N (--num-top-occur) -- number of top the highest frequencies to show in output.
Default value is 1 (i.e. show only the most frequently occuring subsequences);""")
print(""" --both-strands -- count subsequences on both strands
(default behaviour is to count only subsequences on positive strand).""")
print("Example:")
print(" ./most-freq-subseq.py -l 16 -s my_favourite_genome.fasta.gz -N 3")
# end def print_help
# Firstly check for information-providing flags
if "-h" in sys.argv[1:] or "--help" in sys.argv[1:]:
print_help()
platf_depend_exit()
# end if
if "-v" in sys.argv[1:] or "--version" in sys.argv[1:]:
print(__version__)
platf_depend_exit()
# end if
import os
import re
from getopt import gnu_getopt, GetoptError
try:
opts, args = gnu_getopt(
sys.argv[1:],
"hl:s:N:",
[
"help",
"query-length",
"subject",
"num-top-occur",
"both-strands"
]
)
except GetoptError as gerr:
print( str(gerr) )
platf_depend_exit(2)
# end try
is_fasta = lambda file: not re.search(r"\.fa(sta)?(\.gz)?$", file) is None
query_len = None
sbjct_lst = list()
num_occs_print = 1
both_strands = False
for opt, arg in opts:
if opt in ("-l", "--query-length"):
try:
query_len = int(arg)
if query_len <= 0:
raise ValueError
# end if
except ValueError:
print("\aLength of query sequence must be positive integer number!")
platf_depend_exit(1)
# end try
elif opt in ("-s", "--subject"):
if not os.path.exists(arg):
print("\aFile `{}` does not exist!".format(os.path.abspath(arg)))
platf_depend_exit(1)
# end if
if not is_fasta(arg):
print("\aFile `{}` is not a FASTA file!".format(os.path.abspath(arg)))
platf_depend_exit(1)
# end if
sbjct_lst.append( os.path.abspath(arg) )
elif opt in ("-N", "--num-top-occur"):
try:
num_occs_print = int(arg)
if num_occs_print <= 0:
raise ValueError
# end if
except ValueError:
print("""\a\nNumber of top-most-frequently-occuring subsequences
must be positive integer number.""")
print("Your value: '{}'".format(arg))
platf_depend_exit(1)
# end try
elif opt == "--both-strands":
both_strands = True
# end if
# end for
if len(sbjct_lst) == 0:
sbjct_lst = list(filter(is_fasta, os.listdir('.')))
# end if
if len(sbjct_lst) == 0:
print("No input file found!")
print("Please, run this script with -h flag to see help.")
platf_depend_exit(1)
else:
print("\nFollowing files will be processed:")
for i, fpath in enumerate(sbjct_lst):
print(" {}. `{}`".format(i+1, fpath))
print('-' * 15 + '\n')
# end if
if query_len is None:
error = True
while error:
query_len = input("Please, enter length of query sequence:>>")
try:
query_len = int(query_len)
if query_len <= 0:
raise ValueError
# end if
except ValueError:
print("\aLength of query sequence must be positive integer number!\n")
else:
error = False
# end try
# end while
print()
# end if
import gzip
from functools import partial
is_gzipped = lambda f: f.endswith(".gz")
OPEN_FUNCS = (
partial(open, mode='rt'),
partial(gzip.open, mode='rt')
)
# Define the samt function differently
# depending on flag `--both-strands`.
# Let's do it in order not to check this flag at every fricking iteration
if both_strands:
def count_occurences(sequence, query_len):
# Function counts occurences only on both strands
# In accordance with IUPAC:
# https://www.bioinformatics.org/sms/iupac.html
complement_dict = {
'A': 'T', 'T': 'A', 'G': 'C', 'C': 'G',
'R': 'Y', 'Y': 'R', 'S': 'S', 'W': 'W',
'K': 'M', 'M': 'K', 'B': 'V', 'D': 'H',
'H': 'D', 'V': 'B', 'U': 'A', 'N': 'N'
}
subseq_dict = dict()
rc = lambda seq: "".join((complement_dict[nucl] for nucl in reversed(seq)))
stop = len(sequence) - query_len
for left in range(stop+1):
curr_seq = sequence[left: left + query_len]
for subseq in (curr_seq, rc(curr_seq)):
try:
subseq_dict[subseq] += 1
except KeyError:
subseq_dict[subseq] = 1
# end if
# end for
# end while
return subseq_dict
# end def count_occurences
else:
def count_occurences(sequence, query_len):
# Function counts occurences only on positive strand
subseq_dict = dict()
stop = len(sequence) - query_len
for left in range(stop+1):
curr_seq = sequence[left: left + query_len]
try:
subseq_dict[curr_seq] += 1
except KeyError:
subseq_dict[curr_seq] = 1
# end if
# end while
return subseq_dict
# end def count_occurences
# end if
def report_N_most_freq_occs(sequence, query_len, num_occs_print, outfile):
sequence = sequence.upper()
# `subseq_dict` (dictionary of subsequences) has the following format:
# {<SUBSEQUENCE>: <OCCURRENCE>},
# where: <SUBSEQUENCE> if of str type,
# <OCCURRENCE> is of int type
subseq_dict = count_occurences(sequence, query_len)
# Select top-'num_occs_print' entries
subseq_list = sorted(subseq_dict.items(), key=lambda x: -x[1])
del subseq_dict
prev_score = subseq_list[0][1]
N = 0
for subseq, score in subseq_list:
if score != prev_score:
prev_score = score
N += 1
# end if
if N == num_occs_print:
break
# end if
printl("{} - {} occurences.".format(subseq, score))
# end for
# end def report_N_most_freq_occs
print(" - Length of subsequence to search: {};".format(query_len))
print(" - Number of top-the-highest frequencies to show in output: {};".format(num_occs_print))
if both_strands:
print(" - The script will count subsequences on both strands of input sequences.")
else:
print(" - The script will count subsequences only on positive strand of input sequences.")
# end if
print()
with open("most_freq_subseq_result.txt", 'w') as outfile:
def printl(text=""):
print(text)
outfile.write(text + '\n')
# end def printl
for fpath in sbjct_lst:
printl("File `{}`:".format(fpath))
how_to_open = OPEN_FUNCS[ is_gzipped(fpath) ]
with how_to_open(fpath) as infile:
sequence = ""
line = infile.readline().strip()
seq_id = line
if not line.startswith('>'):
printl("Input file is not a valid FASTA file!")
printl("File: '{}'".format(fpath))
platf_depend_exit(1)
else:
printl("Sequence {}:".format(seq_id[1:]))
# end if
line = infile.readline().strip()
while line != "":
sequence += line
line = infile.readline().strip()
if line.startswith('>') or line == "":
if query_len > len(sequence):
printl(
"Query length ({} bp) is greater then length of the subject sequence ({} bp)!" \
.format(query_len, len(sequence))
)
else:
report_N_most_freq_occs(sequence, query_len, num_occs_print, outfile)
# end if
if line.startswith('>'):
sequence = ""
seq_id = line
line = infile.readline().strip()
printl("Sequence {}:".format(seq_id[1:]))
# end if
# end if
# end while
# end with
# end for
# end with
platf_depend_exit(0)