1 | #include "muscle.h" |
---|
2 | #include "msa.h" |
---|
3 | #include "objscore.h" |
---|
4 | |
---|
5 | #define TRACE 0 |
---|
6 | |
---|
7 | struct GAPINFO |
---|
8 | { |
---|
9 | GAPINFO *Next; |
---|
10 | unsigned Start; |
---|
11 | unsigned End; |
---|
12 | }; |
---|
13 | |
---|
14 | static GAPINFO **g_Gaps; |
---|
15 | static GAPINFO *g_FreeList; |
---|
16 | static unsigned g_MaxSeqCount; |
---|
17 | static unsigned g_MaxColCount; |
---|
18 | static unsigned g_ColCount; |
---|
19 | static bool *g_ColDiff; |
---|
20 | |
---|
21 | static GAPINFO *NewGapInfo() |
---|
22 | { |
---|
23 | if (0 == g_FreeList) |
---|
24 | { |
---|
25 | const int NEWCOUNT = 256; |
---|
26 | GAPINFO *NewList = new GAPINFO[NEWCOUNT]; |
---|
27 | g_FreeList = &NewList[0]; |
---|
28 | for (int i = 0; i < NEWCOUNT-1; ++i) |
---|
29 | NewList[i].Next = &NewList[i+1]; |
---|
30 | NewList[NEWCOUNT-1].Next = 0; |
---|
31 | } |
---|
32 | GAPINFO *GI = g_FreeList; |
---|
33 | g_FreeList = g_FreeList->Next; |
---|
34 | return GI; |
---|
35 | } |
---|
36 | |
---|
37 | static void FreeGapInfo(GAPINFO *GI) |
---|
38 | { |
---|
39 | GI->Next = g_FreeList; |
---|
40 | g_FreeList = GI; |
---|
41 | } |
---|
42 | |
---|
43 | // TODO: This could be much faster, no need to look |
---|
44 | // at all columns. |
---|
45 | static void FindIntersectingGaps(const MSA &msa, unsigned SeqIndex) |
---|
46 | { |
---|
47 | const unsigned ColCount = msa.GetColCount(); |
---|
48 | bool InGap = false; |
---|
49 | bool Intersects = false; |
---|
50 | unsigned Start = uInsane; |
---|
51 | for (unsigned Col = 0; Col <= ColCount; ++Col) |
---|
52 | { |
---|
53 | bool Gap = ((Col != ColCount) && msa.IsGap(SeqIndex, Col)); |
---|
54 | if (Gap) |
---|
55 | { |
---|
56 | if (!InGap) |
---|
57 | { |
---|
58 | InGap = true; |
---|
59 | Start = Col; |
---|
60 | } |
---|
61 | if (g_ColDiff[Col]) |
---|
62 | Intersects = true; |
---|
63 | } |
---|
64 | else if (InGap) |
---|
65 | { |
---|
66 | InGap = false; |
---|
67 | if (Intersects) |
---|
68 | { |
---|
69 | GAPINFO *GI = NewGapInfo(); |
---|
70 | GI->Start = Start; |
---|
71 | GI->End = Col - 1; |
---|
72 | GI->Next = g_Gaps[SeqIndex]; |
---|
73 | g_Gaps[SeqIndex] = GI; |
---|
74 | } |
---|
75 | Intersects = false; |
---|
76 | } |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | static SCORE Penalty(unsigned Length, bool Term) |
---|
81 | { |
---|
82 | if (0 == Length) |
---|
83 | return 0; |
---|
84 | SCORE s1 = g_scoreGapOpen + g_scoreGapExtend*(Length - 1); |
---|
85 | #if DOUBLE_AFFINE |
---|
86 | SCORE s2 = g_scoreGapOpen2 + g_scoreGapExtend2*(Length - 1); |
---|
87 | if (s1 > s2) |
---|
88 | return s1; |
---|
89 | return s2; |
---|
90 | #else |
---|
91 | return s1; |
---|
92 | #endif |
---|
93 | } |
---|
94 | |
---|
95 | //static SCORE ScorePair(unsigned Seq1, unsigned Seq2) |
---|
96 | // { |
---|
97 | //#if TRACE |
---|
98 | // { |
---|
99 | // Log("ScorePair(%d,%d)\n", Seq1, Seq2); |
---|
100 | // Log("Gaps seq 1: "); |
---|
101 | // for (GAPINFO *GI = g_Gaps[Seq1]; GI; GI = GI->Next) |
---|
102 | // Log(" %d-%d", GI->Start, GI->End); |
---|
103 | // Log("\n"); |
---|
104 | // Log("Gaps seq 2: "); |
---|
105 | // for (GAPINFO *GI = g_Gaps[Seq2]; GI; GI = GI->Next) |
---|
106 | // Log(" %d-%d", GI->Start, GI->End); |
---|
107 | // Log("\n"); |
---|
108 | // } |
---|
109 | //#endif |
---|
110 | // return 0; |
---|
111 | // } |
---|
112 | |
---|
113 | SCORE ScoreGaps(const MSA &msa, const unsigned DiffCols[], unsigned DiffColCount) |
---|
114 | { |
---|
115 | #if TRACE |
---|
116 | { |
---|
117 | Log("ScoreGaps\n"); |
---|
118 | Log("DiffCols "); |
---|
119 | for (unsigned i = 0; i < DiffColCount; ++i) |
---|
120 | Log(" %u", DiffCols[i]); |
---|
121 | Log("\n"); |
---|
122 | Log("msa=\n"); |
---|
123 | msa.LogMe(); |
---|
124 | Log("\n"); |
---|
125 | } |
---|
126 | #endif |
---|
127 | const unsigned SeqCount = msa.GetSeqCount(); |
---|
128 | const unsigned ColCount = msa.GetColCount(); |
---|
129 | g_ColCount = ColCount; |
---|
130 | |
---|
131 | if (SeqCount > g_MaxSeqCount) |
---|
132 | { |
---|
133 | delete[] g_Gaps; |
---|
134 | g_MaxSeqCount = SeqCount + 256; |
---|
135 | g_Gaps = new GAPINFO *[g_MaxSeqCount]; |
---|
136 | } |
---|
137 | memset(g_Gaps, 0, SeqCount*sizeof(GAPINFO *)); |
---|
138 | |
---|
139 | if (ColCount > g_MaxColCount) |
---|
140 | { |
---|
141 | delete[] g_ColDiff; |
---|
142 | g_MaxColCount = ColCount + 256; |
---|
143 | g_ColDiff = new bool[g_MaxColCount]; |
---|
144 | } |
---|
145 | |
---|
146 | memset(g_ColDiff, 0, g_ColCount*sizeof(bool)); |
---|
147 | for (unsigned i = 0; i < DiffColCount; ++i) |
---|
148 | { |
---|
149 | unsigned Col = DiffCols[i]; |
---|
150 | assert(Col < ColCount); |
---|
151 | g_ColDiff[Col] = true; |
---|
152 | } |
---|
153 | |
---|
154 | for (unsigned SeqIndex = 0; SeqIndex < SeqCount; ++SeqIndex) |
---|
155 | FindIntersectingGaps(msa, SeqIndex); |
---|
156 | |
---|
157 | #if TRACE |
---|
158 | { |
---|
159 | Log("\n"); |
---|
160 | Log("Intersecting gaps:\n"); |
---|
161 | Log(" "); |
---|
162 | for (unsigned Col = 0; Col < ColCount; ++Col) |
---|
163 | Log("%c", g_ColDiff[Col] ? '*' : ' '); |
---|
164 | Log("\n"); |
---|
165 | Log(" "); |
---|
166 | for (unsigned Col = 0; Col < ColCount; ++Col) |
---|
167 | Log("%d", Col%10); |
---|
168 | Log("\n"); |
---|
169 | for (unsigned Seq = 0; Seq < SeqCount; ++Seq) |
---|
170 | { |
---|
171 | Log("%3d: ", Seq); |
---|
172 | for (unsigned Col = 0; Col < ColCount; ++Col) |
---|
173 | Log("%c", msa.GetChar(Seq, Col)); |
---|
174 | Log(" :: "); |
---|
175 | for (GAPINFO *GI = g_Gaps[Seq]; GI; GI = GI->Next) |
---|
176 | Log(" (%d,%d)", GI->Start, GI->End); |
---|
177 | Log(" >%s\n", msa.GetSeqName(Seq)); |
---|
178 | } |
---|
179 | Log("\n"); |
---|
180 | } |
---|
181 | #endif |
---|
182 | |
---|
183 | SCORE Score = 0; |
---|
184 | for (unsigned Seq1 = 0; Seq1 < SeqCount; ++Seq1) |
---|
185 | { |
---|
186 | const WEIGHT w1 = msa.GetSeqWeight(Seq1); |
---|
187 | for (unsigned Seq2 = Seq1 + 1; Seq2 < SeqCount; ++Seq2) |
---|
188 | { |
---|
189 | const WEIGHT w2 = msa.GetSeqWeight(Seq2); |
---|
190 | // const SCORE Pair = ScorePair(Seq1, Seq2); |
---|
191 | const SCORE Pair = ScoreSeqPairGaps(msa, Seq1, msa, Seq2); |
---|
192 | Score += w1*w2*Pair; |
---|
193 | #if TRACE |
---|
194 | Log("Seq1=%u Seq2=%u ScorePair=%.4g w1=%.4g w2=%.4g Sum=%.4g\n", |
---|
195 | Seq1, Seq2, Pair, w1, w2, Score); |
---|
196 | #endif |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | return Score; |
---|
201 | } |
---|