-
Notifications
You must be signed in to change notification settings - Fork 4
/
reciprologs
executable file
·1186 lines (1026 loc) · 36.5 KB
/
reciprologs
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# the above sources Python from $PATH
##!/usr/local/bin/python3
##!/usr/bin/python3
# the above uses specific Python version; allows script name in top
# authorship information
__author__ = 'Graham E. Larue'
__maintainer__ = "Graham E. Larue"
__email__ = 'egrahamlarue@gmail.com'
__license__ = 'GPL'
"""
usage: reciprologs [-h] [-p PARALLEL_PROCESSES] [-q PERCENTAGE] [--chain]
[--subset subset_1 [subset_2 ...]] [--ignore_same_id]
[--ignore_same_prefix <prefix_delimiter>] [-o [OUTPUT]]
[-d path [path ...]] [-b BLAST_FILE] [--overwrite]
[--one_to_one] [--logging] [--no_hash_tag]
file_1 file_2 ... [file_1 file_2 ... ...]
{diamondp,diamondx,blastn,blastp,blastx,tblastn,tblastx}
Find reciprocal best hits between two or more files. Any unrecognized
arguments will be passed along to the chosen alignment program.
positional arguments:
file_1 file_2 ... files to use to build reciprolog sets (space
separated)
{diamondp,diamondx,blastn,blastp,blastx,tblastn,tblastx}
type of alignment program to run
optional arguments:
-h, --help show this help message and exit
-p PARALLEL_PROCESSES, --parallel_processes PARALLEL_PROCESSES
run the alignment step using multiple parallel
processes (default: 1)
-q PERCENTAGE, --query_percentage_threshold PERCENTAGE
require a specified fraction of the query length to
match in order for a hit to qualify (lowest
allowable percentage (default: None)
--chain cluster reciprologs without requiring all-by-all
pairwise relationships, e.g. A-B, A-C, A-D --> A-B-
C-D (default: False)
--subset subset_1 [subset_2 ...]
Files containing subsets of headers to be used as
queries for each input file. Supplied in the same
order as the input files; one header per line. To
omit a subset file for a given input file, provide
"." as the argument, e.g. for three input files
with only 1 & 3 with subsets: --subsets subset_1 .
subset_2 (default: None)
--ignore_same_id ignore hits where both query and subject have
identical IDs (default: False)
--ignore_same_prefix <prefix_delimiter>
ignore hits where both query and subject have
identical prefixes, where the prefix for each ID is
delimited by the specified <prefix_delimiter>
(default: None)
-o [OUTPUT], --output [OUTPUT]
output filename (use flag without argument for
auto-naming) (default: stdout)
-d path [path ...], --alignment_source_directory path [path ...]
check for existing alignment files to use in this
directory first (default: None)
-b BLAST_FILE, --blast_file BLAST_FILE
aggregated BLAST output to use (both directions)
(default: None)
--overwrite overwrite existing output files (instead of using
them to bypass alignment step) (default: False)
--one_to_one remove any many-to-one reciprolog relationships in
each pairwise set, such that each member of each
pairwise comparison is only present exactly one
time in output (default: False)
--logging output a log of best-hit choice criteria (default:
False)
--no_hash_tag do not auto-tag output files with MD5 hashes of
source files (default: False)
NOTE: Depends on palign
"""
import sys
import subprocess
import os
import time
import argparse
import re
import shutil
from operator import itemgetter
from multiprocessing import cpu_count
from collections import defaultdict
from itertools import combinations, permutations
from biogl import fasta_parse, get_runtime
from hashlib import md5
# use networkx library for fast ortholog clustering if available
try:
import networkx as nx
USE_GRAPH = True
except ModuleNotFoundError:
USE_GRAPH = False
def parse_blast_line(bl, *args):
"""
Returns info from certain columns in a tab-separated BLAST
output file. $args may be: query, subject, length, e, bitscore
"""
columns = bl.strip().split("\t")
(
query, subject, length,
e_value, bitscore
) = itemgetter(0, 1, 3, 10, 11)(columns)
arg_map = {
"query": query,
"subject": subject,
"length": int(length) - 1, # seem to be off by 1 in BLAST output
"e": float(e_value),
"bitscore": float(bitscore)
}
results = []
for a in args:
results.append(arg_map[a])
if len(results) == 1:
results = results[0]
return results
def is_better(challenger, defender, seq_lengths=None):
"""
Compares attributes of two dictionaries of BLAST
hits for a given query to determine which is better.
Returns the winning dictionary and reason if it's
better, otherwise False.
"""
cbs = challenger['score']
dbs = defender['score']
# criteria: bitscore
if cbs < dbs:
return False
elif cbs > dbs:
return challenger, 'bitscore'
elif cbs == dbs:
# criteria --> e-value
cev = challenger['evalue']
dev = defender['evalue']
if cev < dev: # lower is better
return challenger, 'e-value'
elif seq_lengths is not None:
# criteria --> length
# if scores are equal, check if sequence lengths
# have been provided as an additional tiebreaking
# criteria and look up the subject length to
# see if there's a difference
dn = defender['name']
cn = challenger['name']
try:
if seq_lengths[cn] > seq_lengths[dn]:
return challenger, 'length'
except KeyError:
return False
else:
return False
else:
return False
def get_prefix(seq_id, delimiter):
split_list = re.split(delimiter, seq_id, maxsplit=1)
split_list = [s for s in split_list if s]
return split_list[0]
def get_top_hits(
blast,
paralogs=False,
query_match=None,
seq_lengths=None,
ignore_same_id=False,
ignore_same_prefix=False,
query_list=None):
results = {}
# dictionary to store tie-broken matches
win_ledger = defaultdict(lambda: defaultdict(set))
with open(blast) as blst:
for l in blst:
new_best_hit = False
if l.startswith("#"):
continue
(q, s, score, length, evalue) = parse_blast_line(
l, "query", "subject", "bitscore", "length", "e")
challenger = {
'name': s,
'score': score,
'evalue': evalue,
'length': length
}
if query_list and q not in query_list:
continue
# do not consider hits to self if BLASTing against self,
# but allow query/subject names to be the same
if q == s and (paralogs is True or ignore_same_id is True):
continue
if ignore_same_prefix is not None:
prefix = ignore_same_prefix
if get_prefix(q, prefix) == get_prefix(s, prefix):
continue
if query_match:
# use query_match dictionary to compare query lengths to
# match lengths to exclude matches where query percentage
# is below query_match_threshold key
fraction = (length / query_match[q]) * 100
if fraction < query_match['query_match_threshold']:
continue
if q in results:
defender = results[q]
challenger_wins = is_better(
challenger, defender, seq_lengths)
if challenger_wins: # new hit is better
new_best_hit = True
defender_name = results[q]['name']
reason = challenger_wins[1]
loser_info = (defender_name, reason)
win_ledger[q]['losers'].add(loser_info)
win_ledger[q]['best'] = s
else:
new_best_hit = True
if new_best_hit is True:
results[q] = {
"name": s,
"score": score,
"evalue": evalue,
"length": length
}
return results, win_ledger
def get_reciprocals(d1, d2, tag1, tag2):
"""
Takes two dictionaries of top BLAST hits,
returns a list of tuples of all pairs that were
reciprocal best hits, along with their bitscore
values.
"""
reciprologs = set()
blast_dicts = [(d1, tag1), (d2, tag2)]
blast_permuts = permutations(blast_dicts, 2)
for (first, first_tag), (second, second_tag) in blast_permuts:
for query, hit_info in first.items():
best_hit = hit_info["name"]
score = hit_info["score"]
if best_hit in second:
reciprocal_hit = second[best_hit]["name"]
if query == reciprocal_hit: # best hit refers back to query
r_score = second[best_hit]["score"]
query = (first_tag, query)
best_hit = (second_tag, best_hit)
hit_pair = sorted([query, best_hit])
score_tuple = tuple(sorted([score, r_score]))
hit_pair.append(score_tuple)
reciprologs.add(tuple(hit_pair))
return sorted(reciprologs)
def clean_reciprologs(reciprologs, subset_index=None):
cleaned = []
for group in reciprologs:
if subset_index and not any(m[1] in subset_index[m[0]] for m in group):
continue
# remove file tags from tuples
clean_group = [m[1] for m in group]
cleaned.append(clean_group)
return cleaned
def file_md5(fn, buffer_size=65536):
hash = md5()
with open(fn, 'rb') as f:
while True:
data = f.read(buffer_size)
if not data:
break
hash.update(data)
return hash.hexdigest()
def abbreviate(name, delimiter=".", use_hash=True, keep_path=False):
local_name = os.path.basename(name) # in case of non-local file path
abbreviation = local_name.split(delimiter)[0]
if use_hash is True: # use shortened md5 hash to uniqueify name
hash = file_md5(name)[:5]
abbreviation = abbreviation + delimiter + hash
if keep_path is True:
file_path = os.path.dirname(os.path.abspath(name))
abbreviation = os.path.join(file_path, abbreviation)
return abbreviation
def unique_filenames(*file_list, skip=None, use_hash=True, keep_path=False):
if skip is not None:
abbreviated = [
os.path.basename(f) if f in skip
else abbreviate(f, use_hash=use_hash)
for f in file_list
]
else:
abbreviated = [os.path.basename(f) for f in file_list]
if len(set(abbreviated)) < len(abbreviated): # not all are unique
abbreviated = [os.path.basename(f) for f in file_list]
if keep_path is True: # add parent directory paths
dirpaths = [os.path.dirname(f) for f in file_list]
abbreviated = [
os.path.join(p, f) for p, f in zip(dirpaths, abbreviated)
]
return abbreviated
def concatenate(outname, file_list, clean=True):
with open(outname, 'w') as outfile:
for fn in file_list:
with open(fn) as f:
for l in f:
outfile.write(l)
if clean:
[os.remove(fn) for fn in file_list]
def parse_run_type(align_type_arg):
type_map = {
'diamondp': ('diamond', 'blastp'),
'diamondx': ('diamond', 'blastx'),
'blastn': ('blast', 'blastn'),
'blastp': ('blast', 'blastp'),
'blastx': ('blast', 'blastx'),
'tblastn': ('blast', 'tblastn'),
'tblastx': ('blast', 'tblastx'),
}
return type_map[align_type_arg]
def aggregate_dict_chained(ortho_dict):
"""
IN:
defaultdict(set,
{'a': {'b', 'c', 'd'},
'b': {'a', 'c', 'e', 'f'},
'c': {'a', 'b', 'e', 'f', 'g'},
'd': {'a'},
'e': {'b', 'c'},
'f': {'b', 'c'},
'g': {'c'},
'w': {'z'},
'x': {'z'},
'y': {'z'},
'z': {'w', 'x', 'y'}})
OUT:
defaultdict(set, {'a': {'b', 'c', 'd', 'e', 'f', 'g'}, 'z': {'w', 'x', 'y'}})
"""
changed = False
processed = []
master = defaultdict(set)
for k, v in ortho_dict.items():
if k in processed:
continue
processed.append(k)
for v2 in v:
if v2 == k:
continue
master[k].add(v2)
processed.append(v2)
if v2 not in ortho_dict:
continue
changed = True
master[k].update(ortho_dict[v2])
if changed is True:
master = aggregate_dict_chained(master)
return master
def aggregate_orthos_chained(orthos, use_graph=False):
"""
IN:
[
[('a', 'b'), ('a', 'c'), ('a', 'd')],
[('b', 'c'), ('b', 'e'), ('b', 'f')],
[('c', 'e'), ('c', 'f'), ('c', 'g')],
[('z', 'x'), ('z', 'y'), ('z', 'w')]
]
OUT:
[['a', 'b', 'c', 'd', 'e', 'f', 'g'], ['w', 'x', 'y', 'z']]
"""
if use_graph:
ortho_groups = graph_cluster(orthos, chain=True)
else:
o_dict = make_ortho_dict(*orthos)
aggregated = aggregate_dict_chained(o_dict)
ortho_groups = []
for k, v in aggregated.items():
combined = tuple(v) + (k,)
ortho_groups.append(sorted(combined))
return sorted(ortho_groups)
def aggregate_orthos_strict(orthos, use_graph=False):
"""
IN:
[
[('a', 'b'), ('a', 'c'), ('a', 'd')],
[('b', 'c'), ('b', 'e'), ('b', 'f')],
[('c', 'e'), ('c', 'f'), ('c', 'g')],
[('z', 'x'), ('z', 'y'), ('z', 'w')]
]
OUT:
[
['x', 'z'],
['y', 'z'],
['w', 'z'],
['a', 'd'],
['c', 'g'],
['b', 'c', 'f'],
['b', 'c', 'e'],
['a', 'b', 'c']
]
"""
if use_graph is True:
aggregated = graph_cluster(orthos)
else:
o_dict = make_ortho_dict(*orthos)
aggregated = all_by_all_orthos(o_dict)
return aggregated
def all_by_all_orthos(ortho_dict):
full_groups = []
for k, v in ortho_dict.items():
groups = []
max_n = len(v)
# go backward in size and cull subsets as we go
for i in range(max_n, 0, -1):
for g in combinations(v, i):
g = set(list(g) + [k])
if g in full_groups or len(g) == 1:
continue
if every_member_match(g, ortho_dict):
if any(og.issuperset(g) for og in full_groups):
continue
full_groups.append(g)
return sorted([sorted(g) for g in full_groups])
def every_member_match(members, m_dict):
all_match = True
for m in members:
others = [e for e in members if e != m]
if not others:
return True
if any(m not in m_dict[o] for o in others):
return False
return all_match
def make_ortho_dict(*orthos):
"""
IN:
[
[('a', 'b'), ('a', 'c'), ('a', 'd')],
[('b', 'c'), ('b', 'e'), ('b', 'f')],
[('c', 'e'), ('c', 'f'), ('c', 'g')],
[('z', 'x'), ('z', 'y'), ('z', 'w')]
]
OUT:
defaultdict(set,
{'a': {'b', 'c', 'd'},
'b': {'a', 'c', 'e', 'f'},
'c': {'a', 'b', 'e', 'f', 'g'},
'd': {'a'},
'e': {'b', 'c'},
'f': {'b', 'c'},
'g': {'c'},
'w': {'z'},
'x': {'z'},
'y': {'z'},
'z': {'w', 'x', 'y'}})
"""
collector = defaultdict(set)
for o_list in orthos:
for pair in o_list:
for a, b in permutations(pair, 2):
collector[a].add(b)
return collector
def names_from_blastfile(blast_fn):
file_pattern = r'(.+)-vs-(.+)\.t?blast[npx]'
query_fn, subject_fn = re.findall(file_pattern, blast_fn)[0]
return query_fn, subject_fn
def make_subset(fasta, output_fn, keep_file=None, keep_list=None):
if not (keep_file or keep_list):
print(
'[!] Cannot make subset for {} - aborting'.format(fasta),
file=sys.stderr
)
sys.exit(1)
keep_list_set = set()
keep_file_set = set()
if keep_list is not None:
for e in keep_list:
keep_list_set.add(e)
if keep_file is not None:
with open(keep_file) as f:
for l in f:
keep_file_set.add(l.strip())
# get the union of the two sets of headers to include any new additions
keep_set = keep_list_set | keep_file_set
# write combined set to new <keep_file> if there are entries in
# <keep_list_set> that were not in the original <keep_file>
new_keeps = len(keep_set) - len(keep_file_set)
if new_keeps > 0 and keep_file is not None:
print(
'[#] Updating {} with {} new entries'.format(keep_file, new_keeps)
, file=sys.stderr)
with open(keep_file, 'w') as new_keep_file:
for h in keep_set:
new_keep_file.write(h + '\n')
kept = 0
with open(output_fn, 'w') as out:
for h, s in fasta_parse(fasta, trim_header=False):
trunc_header = h.split()[0]
if trunc_header in keep_set:
record = '>{}\n{}\n'.format(h, s)
out.write(record)
kept += 1
return output_fn, kept
def list_hash(string_list, length=3, sort_first=True):
hash = md5()
if sort_first is True:
string_list = sorted(string_list)
for s in string_list:
hash.update(s.encode())
return hash.hexdigest()[:length]
def subset_name(fn, file_tag='subset', use_hash=True, keep_path=False):
if type(file_tag) is not str or not file_tag:
file_tag = ''
else:
file_tag = '_{}'.format(file_tag)
out_fn = '{}{}.fa'.format(
abbreviate(fn, use_hash=use_hash, keep_path=keep_path), file_tag
)
return out_fn
def align(aligner, query, subject, run_type, output_name, extra_args=None):
if extra_args is None:
extra_args = []
aligner_args = [
aligner,
query,
subject,
run_type,
'--output_name',
output_name
]
result = subprocess.run(aligner_args + extra_args)
return result
def alignment_filenames(query, subject, run_type, use_hash=True):
run_files = {}
# when subsetting is used, this hack will produce more obvious filenames
# that reflect which alignments were of subsets and which weren't
special_case_tags = ['_residual', '_subset']
no_abbrev = []
for f in [query, subject]:
if any(t in f for t in special_case_tags):
no_abbrev.append(f)
fw_names = unique_filenames(query, subject, skip=no_abbrev, use_hash=use_hash)
rv_names = unique_filenames(subject, query, skip=no_abbrev, use_hash=use_hash)
run_files['forward'] = '{}-vs-{}.{}'.format(*fw_names, run_type)
run_files['reverse'] = '{}-vs-{}.{}'.format(*rv_names, run_type)
return run_files
def seq_lengths(fasta):
l = {}
for h, s in fasta_parse(fasta):
l[h] = len(s)
return l
def remove_many_to_one(pairs):
"""
Each element of >pairs< is a tuple: (hitA, hitB, (scoreX, scoreY))
Takes a list of paired reciprocal hits (plus scores) and filters it
such that each member of each pair only occur once, i.e. it removes
any many-to-one hits, using the bitscores in the last element of
each tuple.
"""
uniques = {}
to_remove = []
for index, (a, b, scores) in enumerate(pairs):
avg_score = sum(scores) / 2
for e in [a, b]:
if e not in uniques:
uniques[e] = {'score': avg_score, 'index': index}
elif uniques[e]['score'] >= avg_score:
to_remove.append(index)
continue
else:
to_remove.append(uniques[e]['index'])
uniques[e] = {'score': avg_score, 'index': index}
filtered = []
for i, p in enumerate(pairs):
if i in to_remove:
names = p[0:2]
print('Removed: {}'.format('\t'.join(names)), file=sys.stderr)
else:
filtered.append(p)
return filtered
def graph_cluster(pairwise_sets, chain=False):
graph = nx.Graph()
for p_set in pairwise_sets:
for pair in p_set:
graph.add_edge(*pair)
if chain:
clusters = nx.connected_components(graph)
else:
clusters = nx.find_cliques(graph)
clusters = [sorted(c) for c in clusters]
return sorted(clusters, key=len)
def subset_size_check(input_file, subset, kept_n):
if kept_n == 0:
print('[!] Subset size of {} = 0; aborting.'.format(subset))
sys.exit(1)
else:
print(
'[#] Subset size for {}: {}'.format(input_file, kept_n),
file=sys.stderr
)
def log_ledger(q, s, run_type, win_ledger, use_hash=True):
ledger_file = '{}-{}.{}.log'.format(
abbreviate(q, use_hash=use_hash), abbreviate(s, use_hash=use_hash), run_type)
with open(ledger_file, 'w') as lf:
for query, info in sorted(win_ledger.items()):
winner = info['best']
loser_tuples = info['losers']
lf.write('>{}\t[{}]\n'.format(winner, query))
for loser in sorted(loser_tuples):
lf.write('\t'.join(loser) + '\n')
def get_alignments(
pairs_index,
aligner,
run_type,
extra,
overwrite=False,
file_dirs=None,
blast_file=None,
use_hash=True
):
"""
<pairs_index> is a dictionary of the form {label: (q, s), ...}
"""
# get reciprologs for each pairwise permutation of files [(1, 2), (2, 1), ...]
alignment_index = {}
for alignment_key, pair in pairs_index.items():
q, s = pair
# this block might want to move to above the preceding if block...?
if blast_file:
alignment_index[alignment_key] = blast_file
continue
align_fn = alignment_filenames(q, s, run_type, use_hash=use_hash)['forward']
if file_dirs:
for d in file_dirs:
file_list = os.listdir(d)
if align_fn in file_list:
align_fn = os.path.join(
os.path.abspath(d), align_fn)
break
if not os.path.isfile(align_fn) or overwrite is True:
alignment = align(
aligner, q, s, run_type, align_fn, extra_args=extra
)
if alignment.returncode != 0:
sys.exit(
'[!] ERROR: alignment failed: {} (return code: {})'
.format(align_fn, alignment.returncode)
)
else:
print(
'[#] Using existing output \'{}\''.format(align_fn),
file=sys.stderr
)
alignment_index[alignment_key] = align_fn
return alignment_index
def align_residuals(alignments, aligner, residual_index, args, overwrite=False, use_hash=True):
alignment_index = {}
run_type = args.run_type
extra = args.extra
for aln_key, aln in sorted(alignments.items(), key=lambda x: sorted(x[0])):
q, s = aln_key
if q not in residual_index:
continue
r_sub_set = residual_index[q]
r_sub_fn = subset_name(q, file_tag='residual', use_hash=use_hash)
r_sub, kept_n = make_subset(q, r_sub_fn, keep_list=r_sub_set)
subset_size_check(q, r_sub, kept_n)
align_fn = alignment_filenames(q, s, run_type, use_hash=use_hash)['forward']
if not os.path.isfile(align_fn) or overwrite is True:
residual_aln = align(
aligner, r_sub, s, run_type, align_fn, extra_args=extra)
if residual_aln.returncode != 0:
sys.exit(
'[!] ERROR: alignment failed: {} (return code: {})'
.format(align_fn, residual_aln.returncode)
)
else:
print(
'[#] Using existing output \'{}\''.format(align_fn),
file=sys.stderr
)
alignment_index[aln_key] = align_fn
return alignment_index
def build_pair_index(
input_files,
subset_index,
subset_tag=None,
subset_only=False,
use_hash=True
):
pairs = {}
made = set()
for q, s in permutations(sorted(input_files), 2):
alignment_key = (q, s)
paralogs = q == s
if subset_index and subset_index.get(q) and not paralogs:
# subset files are getting tagged twice—probably need to move
# tagging to a different function or something
sub_set = subset_index[q]
subset_hash = list_hash(sub_set)
hash_tag = f'{subset_tag}_h{subset_hash}'
sub_fn = subset_name(
q, file_tag=hash_tag, use_hash=use_hash
)
# check if subset file has already been made
# during this loop to avoid remaking recent files
# unnecessarily
if q not in made:
made.add(q)
sub_q, kept_n = make_subset(q, sub_fn, keep_list=sub_set)
subset_size_check(q, sub_q, kept_n)
else:
sub_q = sub_fn
q = sub_q
elif subset_only is True:
continue
p = (q, s)
pairs[alignment_key] = p
return pairs
def get_residual_hits(hit_index, subset_index):
residual_index = defaultdict(set)
for pair, fwd_hits in hit_index.items():
q, s = pair
if subset_index and subset_index[s] is not None:
reverse_pair = (s, q)
rv_query_names = set(hit_index[reverse_pair].keys())
s_hits = set(h['name'] for h in fwd_hits.values())
residual = s_hits - rv_query_names
residual_index[s] |= residual
print(f'residual length for {s}: {len(residual_index[s])}', file=sys.stderr) ###!!!
return residual_index
def build_hit_indices(alignments, args):
query_percentage = args.query_percentage_threshold
blast_file = args.blast_file
length_index = {}
hit_index = {}
residual_index = defaultdict(set)
for (q, s), aln in sorted(alignments.items(), key=lambda x: sorted(x[0])):
if blast_file:
aln = blast_file
# set flag if both files are the same
paralogs = q == s
if paralogs and (q, s) in hit_index:
continue
# add the lengths of all sequences to an index for later
# tie-breaking of best hits
for e in (q, s):
if e not in length_index:
length_index[e] = seq_lengths(e)
# this is needed for the weird get_top_hits() API - might
# be better as a separate arg in the future...
length_index[e]['query_match_threshold'] = query_percentage
# get sets of query IDs to filter alignment lines to
# relevant hits (matters in case of aggregate alignment file)
q_list = set(length_index[q].keys())
if query_percentage is not None:
q_lengths = length_index[q]
else:
q_lengths = {}
s_lengths = length_index[s]
top_hits, win_ledger = get_top_hits(
aln,
paralogs,
query_match=q_lengths,
seq_lengths=s_lengths,
ignore_same_id=args.ignore_same_id,
ignore_same_prefix=args.ignore_same_prefix,
query_list=q_list
)
hit_index[(q, s)] = top_hits
return hit_index
def get_subset_set(subset_list_file):
subset = set()
with open(subset_list_file) as f:
for l in f:
subset.add(l.strip())
return subset
parser = argparse.ArgumentParser(
description=(
'Find reciprocal best hits between two or more files. '
'Any unrecognized arguments will be passed along to the chosen '
'alignment program.'),
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'input_files',
metavar='file_1 file_2 ...',
help='files to use to build reciprolog sets (space separated)',
nargs='+'
)
parser.add_argument(
'run_type',
choices=[
'diamondp',
'diamondx',
'blastn',
'blastp',
'blastx',
'tblastn',
'tblastx'
],
help='type of alignment program to run'
)
parser.add_argument(
'-p',
'--parallel_processes',
help=(
'run the alignment step using multiple parallel processes'),
type=int,
default=1
)
parser.add_argument(
'-q',
'--query_percentage_threshold',
metavar='PERCENTAGE',
help=(
'require a specified fraction of the query length to match in '
'order for a hit to qualify (lowest allowable percentage'),
type=float,
default=None
)
parser.add_argument(
'--chain',
action='store_true',
help=(
'cluster reciprologs without requiring all-by-all pairwise '
'relationships, e.g. A-B, A-C, A-D --> A-B-C-D')
)
parser.add_argument(
'--subset',
metavar=('subset_1', 'subset_2'),
nargs='+',
help=(
'Files containing subsets of headers to be used as queries for each '
'input file. Supplied in the same order as the input files; one header '
'per line. To omit a subset file for a given input file, '
'provide "." as the argument, e.g. for three input files with only 1 & '
'3 with subsets: --subsets subset_1 . subset_2'
)
)
parser.add_argument(
'--ignore_same_id',
action='store_true',
help='ignore hits where both query and subject have identical IDs'
)
parser.add_argument(
'--ignore_same_prefix',
metavar='<prefix_delimiter>',
help=(
'ignore hits where both query and subject have identical prefixes, '
'where the prefix for each ID is delimited by the specified '
'<prefix_delimiter>')
)
parser.add_argument(
'-o',
'--output',
help=(
'output filename (use flag without argument for auto-naming)'
),
nargs='?',
default='stdout'
)
parser.add_argument(
'-d',
'--alignment_source_directory',
help='check for existing alignment files to use in this directory first',
nargs='+',
metavar='path'
)
parser.add_argument(
'-b',
'--blast_file',
help='aggregated BLAST output to use (both directions)'
)
parser.add_argument(
'--overwrite',
help=(
'overwrite existing output files '
'(instead of using them to bypass alignment step)'
),
action='store_true'
)
parser.add_argument(
'--one_to_one',
help=(