1 | #include "muscle.h" |
---|
2 | #include "profile.h" |
---|
3 | #include "diaglist.h" |
---|
4 | |
---|
5 | #define TRACE 0 |
---|
6 | |
---|
7 | const unsigned KTUP = 5; |
---|
8 | const unsigned KTUPS = 6*6*6*6*6; |
---|
9 | static unsigned TuplePos[KTUPS]; |
---|
10 | |
---|
11 | static char *TupleToStr(int t) |
---|
12 | { |
---|
13 | static char s[7]; |
---|
14 | int t1, t2, t3, t4, t5; |
---|
15 | |
---|
16 | t1 = t%6; |
---|
17 | t2 = (t/6)%6; |
---|
18 | t3 = (t/(6*6))%6; |
---|
19 | t4 = (t/(6*6*6))%6; |
---|
20 | t5 = (t/(6*6*6*6))%6; |
---|
21 | |
---|
22 | s[4] = '0' + t1; |
---|
23 | s[3] = '0' + t2; |
---|
24 | s[2] = '0' + t3; |
---|
25 | s[1] = '0' + t4; |
---|
26 | s[0] = '0' + t5; |
---|
27 | return s; |
---|
28 | } |
---|
29 | |
---|
30 | static unsigned GetTuple(const ProfPos *PP, unsigned uPos) |
---|
31 | { |
---|
32 | const unsigned t0 = PP[uPos].m_uResidueGroup; |
---|
33 | if (RESIDUE_GROUP_MULTIPLE == t0) |
---|
34 | return EMPTY; |
---|
35 | |
---|
36 | const unsigned t1 = PP[uPos+1].m_uResidueGroup; |
---|
37 | if (RESIDUE_GROUP_MULTIPLE == t1) |
---|
38 | return EMPTY; |
---|
39 | |
---|
40 | const unsigned t2 = PP[uPos+2].m_uResidueGroup; |
---|
41 | if (RESIDUE_GROUP_MULTIPLE == t2) |
---|
42 | return EMPTY; |
---|
43 | |
---|
44 | const unsigned t3 = PP[uPos+3].m_uResidueGroup; |
---|
45 | if (RESIDUE_GROUP_MULTIPLE == t3) |
---|
46 | return EMPTY; |
---|
47 | |
---|
48 | const unsigned t4 = PP[uPos+4].m_uResidueGroup; |
---|
49 | if (RESIDUE_GROUP_MULTIPLE == t4) |
---|
50 | return EMPTY; |
---|
51 | |
---|
52 | return t0 + t1*6 + t2*6*6 + t3*6*6*6 + t4*6*6*6*6; |
---|
53 | } |
---|
54 | |
---|
55 | void FindDiags(const ProfPos *PX, unsigned uLengthX, const ProfPos *PY, |
---|
56 | unsigned uLengthY, DiagList &DL) |
---|
57 | { |
---|
58 | if (ALPHA_Amino != g_Alpha) |
---|
59 | Quit("FindDiags: requires amino acid alphabet"); |
---|
60 | |
---|
61 | DL.Clear(); |
---|
62 | |
---|
63 | if (uLengthX < 12 || uLengthY < 12) |
---|
64 | return; |
---|
65 | |
---|
66 | // Set A to shorter profile, B to longer |
---|
67 | const ProfPos *PA; |
---|
68 | const ProfPos *PB; |
---|
69 | unsigned uLengthA; |
---|
70 | unsigned uLengthB; |
---|
71 | bool bSwap; |
---|
72 | if (uLengthX < uLengthY) |
---|
73 | { |
---|
74 | bSwap = false; |
---|
75 | PA = PX; |
---|
76 | PB = PY; |
---|
77 | uLengthA = uLengthX; |
---|
78 | uLengthB = uLengthY; |
---|
79 | } |
---|
80 | else |
---|
81 | { |
---|
82 | bSwap = true; |
---|
83 | PA = PY; |
---|
84 | PB = PX; |
---|
85 | uLengthA = uLengthY; |
---|
86 | uLengthB = uLengthX; |
---|
87 | } |
---|
88 | |
---|
89 | // Build tuple map for the longer profile, B |
---|
90 | if (uLengthB < KTUP) |
---|
91 | Quit("FindDiags: profile too short"); |
---|
92 | |
---|
93 | memset(TuplePos, EMPTY, sizeof(TuplePos)); |
---|
94 | |
---|
95 | for (unsigned uPos = 0; uPos < uLengthB - KTUP; ++uPos) |
---|
96 | { |
---|
97 | const unsigned uTuple = GetTuple(PB, uPos); |
---|
98 | if (EMPTY == uTuple) |
---|
99 | continue; |
---|
100 | TuplePos[uTuple] = uPos; |
---|
101 | } |
---|
102 | |
---|
103 | // Find matches |
---|
104 | for (unsigned uPosA = 0; uPosA < uLengthA - KTUP; ++uPosA) |
---|
105 | { |
---|
106 | const unsigned uTuple = GetTuple(PA, uPosA); |
---|
107 | if (EMPTY == uTuple) |
---|
108 | continue; |
---|
109 | const unsigned uPosB = TuplePos[uTuple]; |
---|
110 | if (EMPTY == uPosB) |
---|
111 | continue; |
---|
112 | |
---|
113 | // This tuple is found in both profiles |
---|
114 | unsigned uStartPosA = uPosA; |
---|
115 | unsigned uStartPosB = uPosB; |
---|
116 | |
---|
117 | // Try to extend the match forwards |
---|
118 | unsigned uEndPosA = uPosA + KTUP - 1; |
---|
119 | unsigned uEndPosB = uPosB + KTUP - 1; |
---|
120 | for (;;) |
---|
121 | { |
---|
122 | if (uLengthA - 1 == uEndPosA || uLengthB - 1 == uEndPosB) |
---|
123 | break; |
---|
124 | const unsigned uAAGroupA = PA[uEndPosA+1].m_uResidueGroup; |
---|
125 | if (RESIDUE_GROUP_MULTIPLE == uAAGroupA) |
---|
126 | break; |
---|
127 | const unsigned uAAGroupB = PB[uEndPosB+1].m_uResidueGroup; |
---|
128 | if (RESIDUE_GROUP_MULTIPLE == uAAGroupB) |
---|
129 | break; |
---|
130 | if (uAAGroupA != uAAGroupB) |
---|
131 | break; |
---|
132 | ++uEndPosA; |
---|
133 | ++uEndPosB; |
---|
134 | } |
---|
135 | uPosA = uEndPosA; |
---|
136 | |
---|
137 | #if TRACE |
---|
138 | { |
---|
139 | Log("Match: A %4u-%4u ", uStartPosA, uEndPosA); |
---|
140 | for (unsigned n = uStartPosA; n <= uEndPosA; ++n) |
---|
141 | Log("%c", 'A' + PA[n].m_uResidueGroup); |
---|
142 | Log("\n"); |
---|
143 | Log(" B %4u-%4u ", uStartPosB, uEndPosB); |
---|
144 | for (unsigned n = uStartPosB; n <= uEndPosB; ++n) |
---|
145 | Log("%c", 'A' + PB[n].m_uResidueGroup); |
---|
146 | Log("\n"); |
---|
147 | } |
---|
148 | #endif |
---|
149 | |
---|
150 | const unsigned uLength = uEndPosA - uStartPosA + 1; |
---|
151 | assert(uEndPosB - uStartPosB + 1 == uLength); |
---|
152 | |
---|
153 | if (uLength >= g_uMinDiagLength) |
---|
154 | { |
---|
155 | if (bSwap) |
---|
156 | DL.Add(uStartPosB, uStartPosA, uLength); |
---|
157 | else |
---|
158 | DL.Add(uStartPosA, uStartPosB, uLength); |
---|
159 | } |
---|
160 | } |
---|
161 | } |
---|