Repository navigation
Expand file tree
/
Copy pathpairwise.py
More file actions
1413 lines (1313 loc) · 57.2 KB
/
Copy pathpairwise.py
File metadata and controls
1413 lines (1313 loc) · 57.2 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
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
from matrix import StateMatrix, DirectionalMatrixWrapper
from buffer import status_message
import concurrent.futures
import traceback
import sys
from sequence import NeuriteSequence
from itertools import repeat
import os
# from random import shuffle
os.system("taskset -p 0xff %d" % os.getpid())
def create_ta_dictionary(seq,node_types,submatrix=None,gap_cost=-1):
'''
Creates a dictionary for a given tree sequence linking each T node to
an associated A node.
'''
if submatrix is None:
submatrix = {}
for nodetype in node_types:
if not (nodetype,'-') in submatrix.keys():
submatrix[(nodetype,'-')] = gap_cost
# Dictionary with (key,value) pairs of (T-node index, A-node index) as well as (str(T-node index), total gap cost)
taDict = {}
AStack = []
AStack.append(-1) # Begin the AStack with a sentinal value
CostStack = []
CostStack.append(0)
# Visit each character in the sequence
for index in range(0,len(seq)):
# Add the cost of the current node to the current gap cost register
CostStack[-1] += submatrix[seq[index],'-']
# If the current node is an A-node
if seq[index] in node_types['A']:
# push it onto the top of the stack
AStack.append(index)
# push on a new gap cost register
CostStack.append(0)
# The cost of an A-node at the front of a gap is added on in
# NeedlemanWunsch.calculate_gap when determining whether the A is gapped or is
# matched to a C-node
# If the current node is a T-node
elif seq[index] in node_types['T']:
# Set the dictionary to contain the (T-node index, associated A-node pair)
taDict[index] = AStack.pop()
# Get the cost of the gap between the T and A nodes
currCost = CostStack.pop()
# Add that cost to the dictionary
taDict[str(index)] = currCost
# Add the cost to the enclosing AT pair
if (len(CostStack) > 0):
CostStack[-1] += currCost
return taDict
class PairwiseDriver():
'''
Executes the local alignment application
'''
def __init__(self, targets, queries, input_state=None, store_pairwise=False, score_type='alignment_score', **kwargs):
self.targets = targets # core operates given targets and queries
self.queries = queries
self.query_map = {query.name:query for query in queries}
self.num_complete = 0 # len(self.priorCompletions) # for how many sequences have been aligned
self.store_pairwise = store_pairwise
if store_pairwise:
self.score_dict = {}
self.score_type = score_type # Can also be 'num_gaps'
acceptable_score_types = ['alignment_score','num_gaps','per_character_score']
if not score_type in acceptable_score_types:
raise IOError('Unknown score type "'+score_type+'". Acceptable values: '+ acceptable_score_types )
if input_state is None:
input_state = InputStateWrapper(kwargs)
self.costs = input_state.get_penalties() # set costs to core
self.submat = input_state.get_submatrix() # set submatrix to core
scoremat_outfile = input_state.get_args()['output']
# Set openMode to append if some targets have already been run and completed
if self.num_complete > 0:
openMode = 'a'
else:
openMode = 'w'
self.scorehandle = None
if not scoremat_outfile is None:
self.scorehandle = open(scoremat_outfile, openMode) # output file
self.alignhandle = None
if input_state.get_args()['alignment_file'] is not None:
self.alignhandle = open(input_state.get_args()['alignment_file'], openMode) # alignments file
self.num_workers = input_state.get_args()['num_processes']
# Get node type lists
self.node_types = input_state.get_node_types()
self.debug = 0
def set_debug(val):
self.debug = val
# Initialize the core given query sequences and input arguments
def start(self):
status_message('Pairwise alignment running', 'please wait')
executor = concurrent.futures.ProcessPoolExecutor(self.num_workers)
try:
with concurrent.futures.ProcessPoolExecutor(self.num_workers) as executor:
for result in executor.map(_aligner, self.targets, repeat(self.queries), repeat(self.costs),
repeat(self.submat), repeat(self.node_types)):
self._process_result(result)
# for target in self.targets: # per fasta, create a concurrent job, f.
# print(target.name)
# f = executor.submit(_aligner, target,
# self.queries, self.costs,
# self.submat, self.node_types)
# f.add_done_callback(self._callback)
executor.shutdown()
self.close_output_buffers()
status_message('Analysis complete', 'OK')
except KeyboardInterrupt:
executor.shutdown()
# Close all I/O buffers such as file handles
def close_output_buffers(self):
if self.alignhandle is not None:
self.alignhandle.close()
if self.scorehandle is not None:
self.scorehandle.close()
# Get the headers, i.e. top-most row for the score matrix
def _create_header(self, results):
h = '\t' +'\t'.join([h[-1] for h in results])
self.scorehandle.write(h + '\n')
self.scorehandle.flush()
def _process_result(self, result):
target, results = result
#results = sorted(results, key=lambda x: x[-1]) # sort by query (last item)
# determine scores
if self.score_type == 'alignment_score':
scores = [result[0] for result in results]
elif self.score_type == 'num_gaps':
scores = [result[1][0].count('-')+result[1][1].count('-') for result in results]
elif self.score_type == 'per_character_score':
scores = [(result[0] + abs(self.costs['gapopen']) +
abs(self.costs['gap'] * (len(target.seq)-len(self.query_map[result[2]].seq)))) /
min(len(target.seq),len(self.query_map[result[2]].seq))
for result in results]
# save scores
if self.store_pairwise:
self.score_dict[target] = scores
# write scores to output file
if self.scorehandle is not None:
if self.num_complete == 0: # for the first result, write headers
self._create_header(results)
scores = '\t'.join([str(s) for s in scores])
self.scorehandle.write(target + '\t' + scores + '\n')
self.scorehandle.flush()
# also save actual alignment string
if self.alignhandle is not None:
for r in results:
if r[1] is not None:
align_target, align_query = r[1]
out_str = target + '\t' + r[-1] +'\t'+ align_target +'\t'+ align_query
self.alignhandle.write(out_str + '\n')
self.alignhandle.flush()
self.num_complete += 1
if self.debug >= 1:
print(' --> ' + target + ' [OK] '+str(self.num_complete) +
' of '+ str(len(self.targets))) # print progress
# Callback function once a thread is complete
def _callback(self, return_val):
cbexcept = return_val.exception()
if cbexcept is not None:
print("Err1: "+str(cbexcept))
#print("Targets: "+str(list([x.seq for x in self.targets])))
#print("Queries: "+str(list([x.seq for x in self.queries])))
res = return_val.result() # get result once thread is complete
self._process_result(res)
# target, results = res
# #results = sorted(results, key=lambda x: x[-1]) # sort by query (last item)
#
# # determine scores
# if self.score_type == 'alignment_score':
# scores = [result[0] for result in results]
# elif self.score_type == 'num_gaps':
# scores = [result[1][0].count('-')+result[1][1].count('-') for result in results]
# elif self.score_type == 'per_character_score':
# scores = [(result[0] + abs(self.costs['gapopen']) +
# abs(self.costs['gap'] * (len(target.seq)-len(self.query_map[result[2]].seq)))) /
# min(len(target.seq),len(self.query_map[result[2]].seq))
# for result in results]
#
# # save scores
# if self.store_pairwise:
# self.score_dict[target] = scores
#
# # write scores to output file
# if self.scorehandle is not None:
# if self.num_complete == 0: # for the first result, write headers
# self._create_header(results)
# scores = '\t'.join([str(s) for s in scores])
# self.scorehandle.write(target + '\t' + scores + '\n')
# self.scorehandle.flush()
#
# # also save actual alignment string
# if self.alignhandle is not None:
# for r in results:
# if r[1] is not None:
# align_target, align_query = r[1]
# out_str = target + '\t' + r[-1] +'\t'+ align_target +'\t'+ align_query
# self.alignhandle.write(out_str + '\n')
# self.alignhandle.flush()
# self.num_complete += 1
# if self.debug >= 1:
# print(' --> ' + target + ' [OK] '+str(self.num_complete) +
# ' of '+ str(len(self.targets))) # print progress
def get_score_matrix(self):
score_mat = []
for target in self.targets:
if target.name in self.score_dict:
score_mat.append(self.score_dict[target.name])
return score_mat
# Maps each query sequence against a set of targets (itself)
def _aligner(target, queries, costs, submat, node_types):
results = [] # K => target, V => aligned queries
# get the gap and substitution matrix
for query in queries:
NW = NeedlemanWunsch(target, query, costs, submat, node_types)
output = NW.prettify()
results.append(output)
return target.name, results
# Implementation of global alignment - based on Needleman-Wunsch
class NeedlemanWunsch():
def __init__(self, s1, s2, costs, submat, node_types, composite=0):
self.seq1 = s1 # sequence 1
self.seq2 = s2 # sequence 2
self.costs = costs # dictionary of all costs (i.e. penalties)
self.submat = submat # substitution matrix
self.create_node_types(node_types)
self.create_residue_specific_gapcost()
self.TADict1 = create_ta_dictionary(s1.seq, node_types, submat, costs['gap'])
self.TADict2 = create_ta_dictionary(s2.seq, node_types, submat, costs['gap'])
self.scoreMat = None # references score matrix
self.directionMat = None # references diag(0),left(1),up(2) matrix
self.leftMat = None # references diag(0),left(1),up(2) matrix
self.upMat = None # references diag(0),left(1),up(2) matrix
self.backPos = {} # the backtrace position from one position to its prior (contains integer pairs)
self.align1 = '' # alignment string for sequence 1
self.align2 = '' # alignment string for sequence 2
self.composite = composite
self._aligner()
def create_node_types(self,node_types):
self.node_types = {}
for node_type in node_types.keys():
for residue in node_types[node_type]:
self.node_types[residue] = node_type
# Fills in all gap-residue pairs either with flipped order entry if it exists, else with the default gap cost
# Also fills in flipped residue-residue scores
def create_residue_specific_gapcost(self):
newToSubmat = {}
for pair in self.submat.keys():
if pair[0] == '-' and pair[1] == '-':
# do nothing, this is useless and shouldn't happen
pass
elif pair[0] == '-' or pair[1] == '-':
# Note that the given residue has an associated gap cost
if pair[0] == '-' and (pair[1],'-') not in self.submat.keys():
newToSubmat[pair[1],'-'] = self.submat[pair]
elif pair[1] == '-' and (pair[1],'-') not in self.submat.keys():
newToSubmat[pair[1],'-'] = self.submat[pair]
else:
# Fill the residueDict so none are missed
if (pair[0],'-') not in self.submat.keys() and ('-',pair[0]) not in self.submat.keys():
newToSubmat[pair[0],'-'] = self.costs['gap']
newToSubmat['-',pair[0]] = self.costs['gap']
if (pair[1],'-') not in self.submat.keys() and ('-',pair[1]) not in self.submat.keys():
newToSubmat[pair[1],'-'] = self.costs['gap']
newToSubmat['-',pair[1]] = self.costs['gap']
if (pair[1],pair[0]) not in self.submat.keys():
newToSubmat[pair[1],pair[0]] = self.submat[pair]
for pair in newToSubmat.keys():
self.submat[pair] = newToSubmat[pair]
# return top (highest) alignment score given sequence 1 and 2
def get_top_score(self):
return self.scoreMat.get_data(-1,-1)
# alignment string resultant from sequence 1 and 2
def get_alignment(self):
return (self.align1, self.align2)
# Create parser-friendly output given a NW alignment
def prettify(self):
return [ self.get_top_score(), self.get_alignment(), self.seq2.name ]
def determine_open_extend(self,i,j,m,directionM,dirScoreM,currentGapCost,gap_direction):
if m.get_data(i,j) is None:
return None,False
gapScore = m.get_data(i,j) + currentGapCost
# Add on the gap open cost if the prior position is not gapped in the same direction (gapping the same sequence)
if dirScoreM.score.get_data(i,j) is None:#[0]): # Previous position can't gap
gapScore += self.costs['gapopen']
scoreExtendPair = gapScore,False
elif directionM.get_data(i,j) is not gap_direction: # Previous position didn't choose gap
extendGapScore = dirScoreM.score.get_data(i,j) + currentGapCost
openGapScore = gapScore + self.costs['gapopen']
if openGapScore >= extendGapScore: # new gap is best, go with previous position's choice
scoreExtendPair = openGapScore,False
else:
# continuation is best, override previous position choice; if current position is on path, previous position will gap
scoreExtendPair = extendGapScore,True
else: # Previous position did choose gap, so this is automatically a continuation
scoreExtendPair = gapScore,True
return scoreExtendPair
# Calculates the gap cost in a given direction from a given position, which depends on the node type
def calculate_gap(self,i,j,seq1,seq2,m,directionM,dirScoreM,TADict,gap_direction):
isExtend = False
a_c_match = False
# CType: gap one
if self.node_types[seq1[i-1]] == 'C':
# The prior position assuming a gap (index based on m)
gapPosi = i - 1
gapPosj = j
# Determine the appropriate gap score depending on whether this opens or extends a gap
gapScore,isExtend = self.determine_open_extend(gapPosi,
gapPosj,
m,
directionM,
dirScoreM,
get_gapcost(seq1[i-1],self.submat),
gap_direction)
# TType: gap until paired A
elif self.node_types[seq1[i-1]] == 'T':
# Special Case: Final position of sequence
if i == len(seq1):
# Check all possible previous T positions for the case in which the other sequence (seq2) is a subtree of this sequence (seq1)
gapScore = None
gapPosi = None
gapPosj = None
isExtend = False
for k in range(1,len(seq1)-1):
# Previous position must be a T and must match to be a candidate
if self.node_types[seq1[k-1]] == 'T' and directionM.get_data(k,j) == 0:
# print(str(k)+' '+str(j)+' '+str(m.score.get_data(k,j)))
# print('\n'.join([''.join(["{:5s}".format(str(cell)) for cell in row]) for row in dirScoreM.score.data]))
cur_score = m.get_data(k,j) + self.costs['gapopen'] - (len(seq1)-k)
if gapScore is None or cur_score > gapScore:
gapScore = cur_score
gapPosi = k
if gapScore is not None:
gapPosj = j
else: # Normal case (gap until paired A)
# The prior position assuming a gap (index based on m)
gapPosi = TADict[i-1]
# Case where this is the last T; handle sentinal and get cost of front-gap
if TADict[i-1] is -1:
gapPosi = 0
gapCostStart = 0
else:
gapCostStart = get_gapcost(seq1[gapPosi],self.submat) # Cost of the A-node that starts the gap
gapCostMajor = TADict[str(i-1)] # Cost of the gap from the T-node up to the A-node
# Calculate the total gap cost assuming the associated A is also gapped
gapScoreGapFinish,isExtend = self.determine_open_extend(gapPosi,
j,
m,
directionM,
dirScoreM,
gapCostMajor+gapCostStart,
gap_direction)
# If seq2 character is C-type, determine whether to match T-paired A and C, or to just gap the A
# This will not happen if this is the last T (TADict[i-1] is -1), as the whole sequence must be gapped
if self.node_types[seq2[j-1]] == 'C' and TADict[i-1] is not -1:
# Calculate the total gap cost assuming the associated A-node matches a C-node
ACScore = get_score(seq1[gapPosi],seq2[j-1],self.submat)
if m.get_data(gapPosi,j-1) is None:
gapScoreACFinish = None
else:
gapScoreACFinish = m.get_data(gapPosi,j-1) + gapCostMajor + ACScore + self.costs['gapopen']
# Determine which gap produces a higher overall score, and use that for this position's gap score
if gapScoreACFinish is not None and (gapScoreGapFinish is None or gapScoreACFinish >= gapScoreGapFinish):
# Best score achieved if gap finishes with A-C match
gapScore = gapScoreACFinish
gapPosj = j-1
isExtend = False
a_c_match = True
elif gapScoreGapFinish is not None:
# Best score achieved if gap includes the associated A
gapScore = gapScoreGapFinish
gapPosj = j
else:
# Gap cannot be completed
gapScore = None
gapPosj = j
isExtend = False
# If seq2 character is not a C, then use the total gap score assuming the A is also gapped
else:
gapScore = gapScoreGapFinish
gapPosj = j
else: # AType, no gapping allowed
gapScore = None # original was set to None
gapPosi = None
gapPosj = None
dirScoreM.score.set_data(i,j, gapScore)
dirScoreM.extend_flag.set_data(i,j, isExtend)
dirScoreM.a_c_match.set_data(i,j, a_c_match)
return [gapScore, gapPosi, gapPosj]
# Execute alignment
def _aligner(self):
l1, l2 = len(self.seq1.seq), len(self.seq2.seq)
self.scoreMat = StateMatrix(l1+1, l2+1) # create matrix for storing counts
self.directionMat = StateMatrix(l1+1, l2+1)
# Each position contains a 2-tuple of the respective gap score and True if the gap is a continuation, False if it is new
self.leftMat = DirectionalMatrixWrapper(nrows=l1+1, ncols=l2+1) #numpy.zeros((l1+1, l2+1), dtype=('f16,b1'))
self.upMat = DirectionalMatrixWrapper(nrows=l1+1, ncols=l2+1) #numpy.zeros((l1+1, l2+1), dtype=('f16,b1'))
self.scoreMat.set_data(0,0, 0)
a_count = 0
for i in range(1, l1 + 1): # set each row by the desired gap
if self.composite != 2:
# Update A(subtree) count; REMOVING - allowing gaps within a subtree for case of smaller tree matching part of larger tree
#a_count += 1 if self.seq1.seq[i-1] == 'A' else -1 if self.seq1.seq[i-1] == 'T' else 0
if a_count > 0: # Gap cannot start within a subtree
self.scoreMat.set_data(i,0, None)
else:
self.scoreMat.set_data(i,0, self.costs['gap'] * i + self.costs['gapopen'])
else:
self.scoreMat.set_data(i,0, None)
self.leftMat.score.set_data(i,0, None)
self.upMat.score.set_data(i,0, None)
a_count = 0
for j in range(1, l2 + 1): # set each column by the desired gap
if self.composite != 1:
# Update A(subtree) count; REMOVING - allowing gaps within a subtree for case of smaller tree matching part of larger tree
#a_count += 1 if self.seq2.seq[j-1] == 'A' else -1 if self.seq2.seq[j-1] == 'T' else 0
if a_count > 0: # Gap cannot start within a subtree
self.scoreMat.set_data(0,j, None)
else:
self.scoreMat.set_data(0,j, self.costs['gap'] * j + self.costs['gapopen'])
else:
self.scoreMat.set_data(0,j, None)
self.leftMat.score.set_data(0,j, None)
self.upMat.score.set_data(0,j, None)
for i in range(1, l1+1): # per base-pair in sequence 1 ...
for j in range(1, l2+1): # per base-pair in sequence 2, align them
if (self.node_types[self.seq1.seq[i-1]] == 'C' and self.node_types[self.seq2.seq[j-1]] == 'A') or (self.node_types[self.seq1.seq[i-1]] == 'A' and self.node_types[self.seq2.seq[j-1]] == 'C'):
score = None # no match if one is a C type and the other is an A type
elif (self.node_types[self.seq1.seq[i-1]] == 'T') ^ (self.node_types[self.seq2.seq[j-1]] == 'T'):
score = None # no match if one is a T type and the other is not
elif self.scoreMat.get_data(i-1,j-1) is None:
score = None # Diagonal is an unreachable position due to composite
else:
score = self.scoreMat.get_data(i-1,j-1) + get_score(self.seq1.seq[i-1], self.seq2.seq[j-1], self.submat)
left, up = None, None
if self.composite != 2: # If seq2 is composite, can't put gap characters in seq1
# Cost for gapping left (over sequence 1)
left, lefti, leftj = self.calculate_gap(i,
j,
self.seq1.seq,
self.seq2.seq,
self.scoreMat,
self.directionMat,
self.leftMat,
self.TADict1,
1)
if self.composite != 1: # If seq1 is composite, can't put gap characters in seq2
# Cost for gapping up (over sequence 2)
up, upj, upi = self.calculate_gap(j,
i,
self.seq2.seq,
self.seq1.seq,
self.scoreMat.T,
self.directionMat.T,
self.upMat.T,
self.TADict2,
2)
#stdout(str(i)+' '+str(j)+' match='+str(score)+' left='+str(left)+' up='+str(up))
if score is not None and (left is None or score >= left) and (up is None or score >= up):
# Node match is allowed and produces the best score
self.scoreMat.set_data(i,j, score)
self.directionMat.set_data(i,j, 0)
self.backPos[i,j] = i-1,j-1
#stdout('MATCH')
elif left is not None and (up is None or left >= up):
# Gapping left is allowed and produces the best score
self.scoreMat.set_data(i,j, left)
self.directionMat.set_data(i,j, 1)
self.backPos[i,j] = lefti,leftj
#stdout('LEFT')
elif up is not None:
# Gapping up is allowed and produces the best score
self.scoreMat.set_data(i,j, up)
self.directionMat.set_data(i,j, 2)
self.backPos[i,j] = upi,upj
#stdout('UP')
else:
# This location is unreachable due to presence of a composite sequence
self.scoreMat.set_data(i,j,None)
#### BACKTRACE ####
i, j = l1, l2
keepGapping = 0
while i > 0 and j > 0: # walk-back to the index [0][0] of the m
# Use next_i and next_j to keep track of backtrace position while holding on to current round's starting point
next_i, next_j = i, j
# if score is a gap in sequence 2 (direction is 1), only walk back on i
if keepGapping == 1 or keepGapping == 0 and self.directionMat.get_data(i,j) == 1:
# If the node being gapped is a T-node, appropriately gap the entire subtree
if self.node_types[self.seq1.seq[i-1]] == 'T':
# Get i_target and j_target depending on whether "forced" gapping or not
if keepGapping == 1:
# Determine whether T-gapping has includes the A node or matches A-C
i_target = self.TADict1[i-1]
if self.leftMat.a_c_match.get_data(i,j) == True:
j_target = j-1
else:
j_target = j
else:
i_target = self.backPos[i,j][0]
j_target = self.backPos[i,j][1]
while next_i > i_target+1: # Walk back with gaps until the one position greater than the final position
self.align1 += self.seq1.seq[next_i-1]
self.align2 += '-'
next_i -= 1
# If j_target < next_j, then the gap is preceeded by an A-C match, so add that to the alignment
if j_target < next_j:
self.align1 += self.seq1.seq[next_i-1]
self.align2 += self.seq2.seq[next_j-1]
next_i -= 1
next_j -= 1
else: # otherwise the A is also gapped
self.align1 += self.seq1.seq[next_i-1]
self.align2 += '-'
next_i -= 1
# otherwise just gap the current node (it will be a C-node)
else:
self.align1 += self.seq1.seq[next_i-1]
self.align2 += '-'
next_i -= 1
# Check whether the choice of gapping left requires the leftward position to also gap left
if self.leftMat.extend_flag.get_data(i,j) == True:#[1]:
keepGapping = 1
else:
keepGapping = 0
# if score is a gap in sequence 1 (direction is 2), only walk back on j
elif keepGapping == 2 or keepGapping == 0 and self.directionMat.get_data(i,j) == 2:
if self.node_types[self.seq2.seq[j-1]] == 'T':
# Get i_target and j_target depending on whether "forced" gapping or not
if keepGapping == 2:
# Determine whether T-gapping has includes the A node or matches A-C
j_target = self.TADict2[j-1]
if self.upMat.a_c_match.get_data(i,j) == True:
i_target = i-1
else:
i_target = i
else:
i_target = self.backPos[i,j][0]
j_target = self.backPos[i,j][1]
while next_j > j_target+1:
self.align1 += '-'
self.align2 += self.seq2.seq[next_j-1]
next_j -= 1
if i_target < next_i:
self.align1 += self.seq1.seq[next_i-1]
self.align2 += self.seq2.seq[next_j-1]
next_i -= 1
next_j -= 1
else:
self.align1 += '-'
self.align2 += self.seq2.seq[next_j-1]
next_j -= 1
else:
self.align1 += '-'
self.align2 += self.seq2.seq[next_j-1]
next_j -= 1
# Check whether the choice of gapping up requires the leftward position to also gap up
if self.upMat.extend_flag.get_data(i,j) == True:
keepGapping = 2
else:
keepGapping = 0
# if the score is a match, walk-back one index in both i and j
elif self.directionMat.get_data(i,j) == 0:
keepGapping = 0
self.align1 += self.seq1.seq[i-1]
self.align2 += self.seq2.seq[j-1]
next_i -= 1
next_j -= 1
i, j = next_i, next_j
# walk-back to index 0 for both i and j; either could be reached first
while i > 0:
self.align1 += self.seq1.seq[i-1]
self.align2 += '-'
i -= 1
while j > 0:
self.align1 += '-'
self.align2 += self.seq2.seq[j-1]
j -= 1
# Reverse the alignment strings as they are assembled backwards
self.align1 = self.align1[::-1]
self.align2 = self.align2[::-1]
# Global alignment of sequence to position weighted matrix, assuming pwm contains sequence
# This implementation only accepts A,C,T encoding and could be generalized
class PositionWeightedMatcher():
def __init__(self, sequence, pwm, costs, node_types, allow_pwm_gaps=False, use_total=True):
self.seq1 = sequence
#self.seq2 = NeuriteSequence('PWM',pwm.node_type_sequence)
self.seq2 = pwm.node_type_sequence
self.costs = costs # dictionary of all costs (i.e. penalties)
self.pwm = pwm.pwm # position weighted matrix / position specific score matrix: array of {char:weight} dictionary
self.use_total = use_total
self.create_node_types(node_types)
self.TADict1 = None
self.allow_pwm_gaps = allow_pwm_gaps
if allow_pwm_gaps:
self.TADict1 = create_ta_dictionary(self.seq1.seq, node_types, gap_cost=costs['gap'])
self.TADict2 = create_ta_dictionary(self.seq2.seq, node_types, gap_cost=costs['gap'])
if use_total:
self.get_score = self.get_weight
else:
self.get_score = self.get_char_weight
self.scoreMat = None # references score matrix
self.directionMat = None # references diag(0),left(1),up(2) matrix
self.upMat = None # references diag(0),left(1),up(2) matrix
self.leftMat = None # references diag(0),left(1),up(2) matrix
self.backPos = {} # the backtrace position from one position to its prior (contains integer pairs)
self.align = '' # alignment string
self.pwm_align = '' # pwm alignment string will be the composite with gaps if any
self._aligner()
def create_node_types(self,node_types):
self.node_types = {}
for node_type in node_types.keys():
for residue in node_types[node_type]:
self.node_types[residue] = node_type
def get_char_weight(self,position, char1):
return self.pwm[position][char1]
# char1 is ignored and only used so that it takes the same parameters as get_char_weight
def get_weight(self,position, char1='total'):
return self.pwm[position]['total']
# return top (highest) alignment score given sequence 1 and 2
def get_top_score(self):
return self.scoreMat.get_data(-1,-1)
# alignment string resultant from sequence 1 and 2
def get_alignment(self):
return (self.align, self.pwm_align)
# Create parser-friendly output given a NW alignment
def prettify(self):
return [ self.get_top_score(), self.get_alignment(), self.seq2.name ]
def determine_open_extend(self,i,j,m,directionM,dirScoreM,currentGapCost,gap_direction):
if m.get_data(i,j) is None:
return None,False
gapScore = m.get_data(i,j) + currentGapCost
# Add on the gap open cost if the prior position is not gapped in the same direction (gapping the same sequence)
if dirScoreM.score.get_data(i,j) is None:#[0]): # Previous position can't gap
gapScore += self.costs['gapopen']
scoreExtendPair = gapScore,False
elif directionM.get_data(i,j) is not gap_direction: # Previous position didn't choose gap in the same direction
# Determine what the score would be if the previous position did gap in the same direction
extendGapScore = dirScoreM.score.get_data(i,j) + currentGapCost
openGapScore = gapScore + self.costs['gapopen']
if openGapScore >= extendGapScore: # new gap is best, go with previous position's choice
scoreExtendPair = openGapScore,False
else:
# continuation is best, override previous position choice; if current position is on path, previous position will gap
scoreExtendPair = extendGapScore,True
else: # Previous position did choose gap, so this is automatically a continuation
scoreExtendPair = gapScore,True
return scoreExtendPair
# Calculates the gap cost in a given direction from a given position, which depends on the node type
def calculate_gap(self,i,j,seq1,seq2,m,directionM,dirScoreM,TADict,gap_direction):
isExtend = False
a_c_match = False
# CType: gap one
if self.node_types[seq1[i-1]] == 'C':
# The prior position assuming a gap (index based on m)
gapPosi = i - 1
gapPosj = j
# Determine the appropriate gap score depending on whether this opens or extends a gap
gapScore,isExtend = self.determine_open_extend(gapPosi,
gapPosj,
m,
directionM,
dirScoreM,
self.costs['gap'],
gap_direction)
# T-type: gap until paired A
elif self.node_types[seq1[i-1]] == 'T':
# The prior position assuming a gap (index based on m)
gapPosi = TADict[i-1]
# Case where this is the last T; handle sentinal and get cost of front-gap
if TADict[i-1] is -1:
gapPosi = 0
gapCostMajor = TADict[str(i-1)] # Cost of the gap from the T-node up to the A-node
gapCostStart = self.costs['gap'] # Cost of the A-node that starts the gap
# Calculate the total gap cost assuming the associated A is also gapped
gapScoreGapFinish,isExtend = self.determine_open_extend(gapPosi,
j,
m,
directionM,
dirScoreM,
gapCostMajor+gapCostStart,
gap_direction)
# If seq2 character is C-type, determine whether to match T-paired A and C, or to just gap the A
# This will not happen if this is the last T (TADict[i-1] is -1), as the whole sequence must be gapped
if self.node_types[seq2[j-1]] == 'C' and TADict[i-1] is not -1:
# Calculate the total gap cost assuming the associated A-node matches a C-node
if gap_direction == 1:
ACScore = self.get_score(j-1,seq1[gapPosi])
else:
ACScore = self.get_score(gapPosi,seq2[j-1])
if m.get_data(gapPosi,j-1) is None:
gapScoreACFinish = None
else:
gapScoreACFinish = m.get_data(gapPosi,j-1) + gapCostMajor + ACScore + self.costs['gapopen']
# Determine which gap produces a higher overall score, and use that for this position's gap score
if gapScoreACFinish is not None and (gapScoreGapFinish is None or gapScoreACFinish >= gapScoreGapFinish):
gapScore = gapScoreACFinish
gapPosj = j-1
isExtend = False
a_c_match = True
elif gapScoreGapFinish is not None:
gapScore = gapScoreGapFinish
gapPosj = j
# isExtend depends on result of determine_open_extend
else:
gapScore = None
gapPosj = j
isExtend = False
# If seq2 character is not a C, then use the total gap score assuming the A is also gapped
else:
gapScore = gapScoreGapFinish
gapPosj = j
# isExtend depends on result of determine_open_extend
else: # A-type, no gapping allowed
gapScore = None # original was set to None
gapPosi = None
gapPosj = None
dirScoreM.score.set_data(i,j, gapScore)
dirScoreM.extend_flag.set_data(i,j, isExtend)
dirScoreM.a_c_match.set_data(i,j, a_c_match)
return [gapScore, gapPosi, gapPosj]
# Execute alignment
def _aligner(self):
l1, l2 = len(self.seq1.seq), len(self.seq2.seq)
self.scoreMat = StateMatrix(l1+1, l2+1) # create matrix for storing counts
self.directionMat = StateMatrix(l1+1, l2+1)
# Each position contains a 2-tuple of the respective gap score and True if the gap is a continuation, False if it is new
self.leftMat = DirectionalMatrixWrapper(nrows=l1+1, ncols=l2+1)
self.upMat = DirectionalMatrixWrapper(nrows=l1+1, ncols=l2+1)
self.scoreMat.set_data(0,0, 0)
a_count = 0
for i in range(1, l1 + 1): # set each row by the desired gap
if self.allow_pwm_gaps:
# Update A(subtree) count
a_count += 1 if self.seq1.seq[i-1] == 'A' else -1 if self.seq1.seq[i-1] == 'T' else 0
if a_count > 0: # Gap cannot start within a subtree
self.scoreMat.set_data(i,0, None)
else:
self.scoreMat.set_data(i,0, self.costs['gap'] * i + self.costs['gapopen'])
else:
self.scoreMat.set_data(i,0, None)
self.leftMat.score.set_data(i,0, None)
self.upMat.score.set_data(i,0, None)
a_count = 0
for j in range(1, l2 + 1): # set each column by the desired gap
# Update A(subtree) count
a_count += 1 if self.seq2.seq[j-1] == 'A' else -1 if self.seq2.seq[j-1] == 'T' else 0
if a_count > 0: # Gap cannot start within a subtree
self.scoreMat.set_data(0,j, None)
else:
self.scoreMat.set_data(0,j, self.costs['gap'] * j + self.costs['gapopen'])
#self.scoreMat.set_data(0,j, self.costs['gap'] * j + self.costs['gapopen'])
self.leftMat.score.set_data(0,j, None)
self.upMat.score.set_data(0,j, None)
for i in range(1, l1+1): # per base-pair in sequence 1 ...
for j in range(1, l2+1): # per base-pair in sequence 2, align them
#print(str(i)+" "+str(j)+ " of "+str(l1)+" " +str(l2))
if (self.node_types[self.seq1.seq[i-1]] == 'C' and self.node_types[self.seq2.seq[j-1]] == 'A') or (self.node_types[self.seq1.seq[i-1]] == 'A' and self.node_types[self.seq2.seq[j-1]] == 'C'):
score = None # no match if one is a C type and the other is an A type
elif (self.node_types[self.seq1.seq[i-1]] == 'T') ^ (self.node_types[self.seq2.seq[j-1]] == 'T'):
score = None # no match if one is a T type and the other is not
elif self.scoreMat.get_data(i-1,j-1) is None:
score = None # Diagonal is an unreachable position due to composite
else:
score = self.scoreMat.get_data(i-1,j-1) + self.get_score(j-1,self.seq1.seq[i-1])
left, up = None, None
if self.allow_pwm_gaps: # Only calculate gap costs in this direction if gaps in pwm are allowed
# Cost for gapping left (over sequence 1)
left, lefti, leftj = self.calculate_gap(i,
j,
self.seq1.seq,
self.seq2.seq,
self.scoreMat,
self.directionMat,
self.leftMat,
self.TADict1,
1)
# Cost for gapping up (over sequence 2)
up, upj, upi = self.calculate_gap(j,
i,
self.seq2.seq,
self.seq1.seq,
self.scoreMat.T,
self.directionMat.T,
self.upMat.T,
self.TADict2,
2)
#stdout(str(i)+' '+str(j)+' match='+str(score)+' left='+str(left)+' up='+str(up))
if score is not None and (left is None or score >= left) and (up is None or score >= up):
# Node match is allowed and produces the best score
self.scoreMat.set_data(i,j, score)
self.directionMat.set_data(i,j, 0)
self.backPos[i,j] = i-1,j-1
#stdout('MATCH')
elif left is not None and (up is None or left >= up):
# Gapping left is allowed and produces the best score
self.scoreMat.set_data(i,j, left)
self.directionMat.set_data(i,j, 1)
self.backPos[i,j] = lefti,leftj
#stdout('LEFT')
elif up is not None:
# Gapping up is allowed and produces the best score
self.scoreMat.set_data(i,j, up)
self.directionMat.set_data(i,j, 2)
self.backPos[i,j] = upi,upj
#stdout('UP')
else:
# This location is unreachable due to presence of a pre-consensus sequence
self.scoreMat.set_data(i,j,None)
i, j = l1, l2 # for trace-back process
keepGapping = 0
while i > 0 and j > 0: # walk-back to the index [0][0] of the m
# Use next_i and next_j to keep track of backtrace position while holding on to current round's starting point
next_i, next_j = i, j
#if j == 1106:
# print(self.seq1.seq[i-1]+'vs'+self.seq2.seq[j-1]+'; keepGapping: '+str(keepGapping)+'; acMatch: '+str(self.upMat.a_c_match.get_data(i,j)))
# if score is a gap in sequence 2 (direction is 1), only walk back on i
if keepGapping == 1 or keepGapping == 0 and self.directionMat.get_data(i,j) == 1:
# If the node being gapped is a T-node, appropriately gap the entire subtree
if self.node_types[self.seq1.seq[i-1]] == 'T':
# Get i_target and j_target depending on whether "forced" gapping or not
if keepGapping == 1:
i_target = self.TADict1[i-1]
if self.leftMat.a_c_match.get_data(i,j) == True:
j_target = j-1
else:
j_target = j
else:
i_target = self.backPos[i,j][0]
j_target = self.backPos[i,j][1]
while next_i > i_target+1: # Walk back with gaps until the one position greater than the final position
self.align += self.seq1.seq[next_i-1]
self.pwm_align += '-'
next_i -= 1
if j_target < j: # If j_target < j, then the gap is preceeded by an A-C match, so add that to the alignment
self.align += self.seq1.seq[next_i-1]
self.pwm_align += self.seq2.seq[next_j-1]
next_i -= 1
next_j -= 1
else: # otherwise the A is also gapped
self.align += self.seq1.seq[next_i-1]
self.pwm_align += '-'
next_i -= 1
# otherwise just gap the current node (it will be a C-node)
else:
self.align += self.seq1.seq[next_i-1]
self.pwm_align += '-'
next_i -= 1
# Check whether the choice of gapping left requires the leftward position to also gap left
if self.leftMat.extend_flag.get_data(i,j) == True:#[1]:
keepGapping = 1
else:
keepGapping = 0
# if score is a gap in sequence 1 (direction is 2), only walk back on j
elif keepGapping == 2 or keepGapping == 0 and self.directionMat.get_data(i,j) == 2:
if self.node_types[self.seq2.seq[j-1]] == 'T':
# Get i_target and j_target depending on whether "forced" gapping or not
if keepGapping == 2:
if self.upMat.a_c_match.get_data(i,j) == True:
i_target = i-1
else:
i_target = i
j_target = self.TADict2[j-1]
else:
i_target = self.backPos[i,j][0]
j_target = self.backPos[i,j][1]
while next_j > j_target+1:
self.align += '-'
self.pwm_align += self.seq2.seq[next_j-1]
next_j -= 1
if i_target < next_i:
self.align += self.seq1.seq[next_i-1]
self.pwm_align += self.seq2.seq[next_j-1]
next_i -= 1
next_j -= 1
else:
self.align += '-'
self.pwm_align += self.seq2.seq[next_j-1]
next_j -= 1
else:
self.align += '-'
self.pwm_align += self.seq2.seq[next_j-1]
next_j -= 1
# Check whether the choice of gapping up requires the leftward position to also gap up
if self.upMat.extend_flag.get_data(i,j) == True:
keepGapping = 2
else:
keepGapping = 0
# if the score is a match, walk-back one index in both i and j
elif self.directionMat.get_data(i,j) == 0:
keepGapping = 0
self.align += self.seq1.seq[i-1]
self.pwm_align += self.seq2.seq[j-1]
next_i -= 1
next_j -= 1
i, j = next_i, next_j
# walk-back to index 0 for both i and j; either could be reached first
while i > 0:
self.align += self.seq1.seq[i-1]
self.pwm_align += '-'
i -= 1
while j > 0:
self.align += '-'
self.pwm_align += self.seq2.seq[j-1]
j -= 1
# Reverse the alignment strings as they are assembled backwards
self.align = self.align[::-1]
self.pwm_align = self.pwm_align[::-1]