1 | #include "muscle.h" |
---|
2 | #include "msa.h" |
---|
3 | #include "objscore.h" |
---|
4 | #include "profile.h" |
---|
5 | |
---|
6 | #define TRACE 0 |
---|
7 | #define COMPARE_3_52 0 |
---|
8 | #define BRUTE_LETTERS 0 |
---|
9 | |
---|
10 | static SCORE ScoreColLetters(const MSA &msa, unsigned uColIndex) |
---|
11 | { |
---|
12 | SCOREMATRIX &Mx = *g_ptrScoreMatrix; |
---|
13 | const unsigned uSeqCount = msa.GetSeqCount(); |
---|
14 | |
---|
15 | #if BRUTE_LETTERS |
---|
16 | SCORE BruteScore = 0; |
---|
17 | for (unsigned uSeqIndex1 = 0; uSeqIndex1 < uSeqCount; ++uSeqIndex1) |
---|
18 | { |
---|
19 | unsigned uLetter1 = msa.GetLetterEx(uSeqIndex1, uColIndex); |
---|
20 | if (uLetter1 >= g_AlphaSize) |
---|
21 | continue; |
---|
22 | WEIGHT w1 = msa.GetSeqWeight(uSeqIndex1); |
---|
23 | for (unsigned uSeqIndex2 = uSeqIndex1 + 1; uSeqIndex2 < uSeqCount; ++uSeqIndex2) |
---|
24 | { |
---|
25 | unsigned uLetter2 = msa.GetLetterEx(uSeqIndex2, uColIndex); |
---|
26 | if (uLetter2 >= g_AlphaSize) |
---|
27 | continue; |
---|
28 | WEIGHT w2 = msa.GetSeqWeight(uSeqIndex2); |
---|
29 | BruteScore += w1*w2*Mx[uLetter1][uLetter2]; |
---|
30 | } |
---|
31 | } |
---|
32 | #endif |
---|
33 | |
---|
34 | double N = 0; |
---|
35 | for (unsigned uSeqIndex1 = 0; uSeqIndex1 < uSeqCount; ++uSeqIndex1) |
---|
36 | { |
---|
37 | WEIGHT w = msa.GetSeqWeight(uSeqIndex1); |
---|
38 | N += w; |
---|
39 | } |
---|
40 | if (N <= 0) |
---|
41 | return 0; |
---|
42 | |
---|
43 | FCOUNT Freqs[20]; |
---|
44 | memset(Freqs, 0, sizeof(Freqs)); |
---|
45 | SCORE Score = 0; |
---|
46 | for (unsigned uSeqIndex1 = 0; uSeqIndex1 < uSeqCount; ++uSeqIndex1) |
---|
47 | { |
---|
48 | unsigned uLetter = msa.GetLetterEx(uSeqIndex1, uColIndex); |
---|
49 | if (uLetter >= g_AlphaSize) |
---|
50 | continue; |
---|
51 | WEIGHT w = msa.GetSeqWeight(uSeqIndex1); |
---|
52 | Freqs[uLetter] += w; |
---|
53 | Score -= w*w*Mx[uLetter][uLetter]; |
---|
54 | } |
---|
55 | |
---|
56 | for (unsigned uLetter1 = 0; uLetter1 < g_AlphaSize; ++uLetter1) |
---|
57 | { |
---|
58 | const FCOUNT f1 = Freqs[uLetter1]; |
---|
59 | Score += f1*f1*Mx[uLetter1][uLetter1]; |
---|
60 | for (unsigned uLetter2 = uLetter1 + 1; uLetter2 < g_AlphaSize; ++uLetter2) |
---|
61 | { |
---|
62 | const FCOUNT f2 = Freqs[uLetter2]; |
---|
63 | Score += 2*f1*f2*Mx[uLetter1][uLetter2]; |
---|
64 | } |
---|
65 | } |
---|
66 | Score /= 2; |
---|
67 | #if BRUTE_LETTERS |
---|
68 | assert(BTEq(BruteScore, Score)); |
---|
69 | #endif |
---|
70 | return Score; |
---|
71 | } |
---|
72 | |
---|
73 | static SCORE ScoreLetters(const MSA &msa, const unsigned Edges[], |
---|
74 | unsigned uEdgeCount) |
---|
75 | { |
---|
76 | const unsigned uSeqCount = msa.GetSeqCount(); |
---|
77 | const unsigned uColCount = msa.GetColCount(); |
---|
78 | |
---|
79 | // Letters |
---|
80 | SCORE Score = 0; |
---|
81 | for (unsigned uEdgeIndex = 0; uEdgeIndex < uEdgeCount; ++uEdgeIndex) |
---|
82 | { |
---|
83 | const unsigned uColIndex = Edges[uEdgeIndex]; |
---|
84 | assert(uColIndex < uColCount); |
---|
85 | Score += ScoreColLetters(msa, uColIndex); |
---|
86 | } |
---|
87 | return Score; |
---|
88 | } |
---|
89 | |
---|
90 | void GetLetterScores(const MSA &msa, SCORE Scores[]) |
---|
91 | { |
---|
92 | const unsigned uColCount = msa.GetColCount(); |
---|
93 | for (unsigned uColIndex = 0; uColIndex < uColCount; ++uColIndex) |
---|
94 | Scores[uColIndex] = ScoreColLetters(msa, uColIndex); |
---|
95 | } |
---|
96 | |
---|
97 | SCORE DiffObjScore( |
---|
98 | const MSA &msa1, const PWPath &Path1, const unsigned Edges1[], unsigned uEdgeCount1, |
---|
99 | const MSA &msa2, const PWPath &Path2, const unsigned Edges2[], unsigned uEdgeCount2) |
---|
100 | { |
---|
101 | #if TRACE |
---|
102 | { |
---|
103 | Log("============DiffObjScore===========\n"); |
---|
104 | Log("msa1:\n"); |
---|
105 | msa1.LogMe(); |
---|
106 | Log("\n"); |
---|
107 | Log("Cols1: "); |
---|
108 | for (unsigned i = 0; i < uEdgeCount1; ++i) |
---|
109 | Log(" %u", Edges1[i]); |
---|
110 | Log("\n\n"); |
---|
111 | Log("msa2:\n"); |
---|
112 | msa2.LogMe(); |
---|
113 | Log("Cols2: "); |
---|
114 | for (unsigned i = 0; i < uEdgeCount2; ++i) |
---|
115 | Log(" %u", Edges2[i]); |
---|
116 | Log("\n\n"); |
---|
117 | } |
---|
118 | #endif |
---|
119 | |
---|
120 | #if COMPARE_3_52 |
---|
121 | extern SCORE g_SPScoreLetters; |
---|
122 | extern SCORE g_SPScoreGaps; |
---|
123 | SCORE SP1 = ObjScoreSP(msa1); |
---|
124 | SCORE SPLetters1 = g_SPScoreLetters; |
---|
125 | SCORE SPGaps1 = g_SPScoreGaps; |
---|
126 | |
---|
127 | SCORE SP2 = ObjScoreSP(msa2); |
---|
128 | SCORE SPLetters2 = g_SPScoreLetters; |
---|
129 | SCORE SPGaps2 = g_SPScoreGaps; |
---|
130 | SCORE SPDiffLetters = SPLetters2 - SPLetters1; |
---|
131 | SCORE SPDiffGaps = SPGaps2 - SPGaps1; |
---|
132 | SCORE SPDiff = SPDiffLetters + SPDiffGaps; |
---|
133 | #endif |
---|
134 | |
---|
135 | SCORE Letters1 = ScoreLetters(msa1, Edges1, uEdgeCount1); |
---|
136 | SCORE Letters2 = ScoreLetters(msa2, Edges2, uEdgeCount2); |
---|
137 | |
---|
138 | SCORE Gaps1 = ScoreGaps(msa1, Edges1, uEdgeCount1); |
---|
139 | SCORE Gaps2 = ScoreGaps(msa2, Edges2, uEdgeCount2); |
---|
140 | |
---|
141 | SCORE DiffLetters = Letters2 - Letters1; |
---|
142 | SCORE DiffGaps = Gaps2 - Gaps1; |
---|
143 | SCORE Diff = DiffLetters + DiffGaps; |
---|
144 | |
---|
145 | #if COMPARE_3_52 |
---|
146 | Log("ObjScoreSP Letters1=%.4g Letters2=%.4g DiffLetters=%.4g\n", |
---|
147 | SPLetters1, SPLetters2, SPDiffLetters); |
---|
148 | |
---|
149 | Log("DiffObjScore Letters1=%.4g Letters2=%.4g DiffLetters=%.4g\n", |
---|
150 | Letters1, Letters2, DiffLetters); |
---|
151 | |
---|
152 | Log("ObjScoreSP Gaps1=%.4g Gaps2=%.4g DiffGaps=%.4g\n", |
---|
153 | SPGaps1, SPGaps2, SPDiffGaps); |
---|
154 | |
---|
155 | Log("DiffObjScore Gaps1=%.4g Gaps2=%.4g DiffGaps=%.4g\n", |
---|
156 | Gaps1, Gaps2, DiffGaps); |
---|
157 | |
---|
158 | Log("SP diff=%.4g DiffObjScore Diff=%.4g\n", SPDiff, Diff); |
---|
159 | #endif |
---|
160 | |
---|
161 | return Diff; |
---|
162 | } |
---|