1 | #include "muscle.h" |
---|
2 | #include "msa.h" |
---|
3 | #include "seqvect.h" |
---|
4 | #include "textfile.h" |
---|
5 | |
---|
6 | #define MEMDEBUG 0 |
---|
7 | |
---|
8 | #if MEMDEBUG |
---|
9 | #include <crtdbg.h> |
---|
10 | #endif |
---|
11 | |
---|
12 | void MUSCLE(SeqVect &v, MSA &msaOut); |
---|
13 | |
---|
14 | // Append msa2 at the end of msa1 |
---|
15 | void AppendMSA(MSA &msa1, const MSA &msa2) |
---|
16 | { |
---|
17 | const unsigned uSeqCount = msa1.GetSeqCount(); |
---|
18 | |
---|
19 | const unsigned uColCount1 = msa1.GetColCount(); |
---|
20 | const unsigned uColCount2 = msa2.GetColCount(); |
---|
21 | |
---|
22 | const unsigned uColCountCat = uColCount1 + uColCount2; |
---|
23 | |
---|
24 | for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex) |
---|
25 | { |
---|
26 | unsigned uId = msa1.GetSeqId(uSeqIndex); |
---|
27 | unsigned uSeqIndex2; |
---|
28 | bool bFound = msa2.GetSeqIndex(uId, &uSeqIndex2); |
---|
29 | if (bFound) |
---|
30 | { |
---|
31 | for (unsigned uColIndex = 0; uColIndex < uColCount2; ++uColIndex) |
---|
32 | { |
---|
33 | const char c = msa2.GetChar(uSeqIndex2, uColIndex); |
---|
34 | msa1.SetChar(uSeqIndex, uColCount1 + uColIndex, c); |
---|
35 | } |
---|
36 | } |
---|
37 | else |
---|
38 | { |
---|
39 | for (unsigned uColIndex = 0; uColIndex < uColCount2; ++uColIndex) |
---|
40 | msa1.SetChar(uSeqIndex, uColCount1 + uColIndex, '-'); |
---|
41 | } |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | static void SeqFromMSACols(const MSA &msa, unsigned uSeqIndex, unsigned uColFrom, |
---|
46 | unsigned uColTo, Seq &s) |
---|
47 | { |
---|
48 | s.Clear(); |
---|
49 | s.SetName(msa.GetSeqName(uSeqIndex)); |
---|
50 | s.SetId(msa.GetSeqId(uSeqIndex)); |
---|
51 | for (unsigned uColIndex = uColFrom; uColIndex <= uColTo; ++uColIndex) |
---|
52 | { |
---|
53 | char c = msa.GetChar(uSeqIndex, uColIndex); |
---|
54 | if (!IsGapChar(c)) |
---|
55 | s.AppendChar(c); |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | static void SeqVectFromMSACols(const MSA &msa, unsigned uColFrom, unsigned uColTo, |
---|
60 | SeqVect &v) |
---|
61 | { |
---|
62 | v.Clear(); |
---|
63 | const unsigned uSeqCount = msa.GetSeqCount(); |
---|
64 | for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex) |
---|
65 | { |
---|
66 | Seq s; |
---|
67 | SeqFromMSACols(msa, uSeqIndex, uColFrom, uColTo, s); |
---|
68 | v.AppendSeq(s); |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | void RefineW(const MSA &msaIn, MSA &msaOut) |
---|
73 | { |
---|
74 | const unsigned uSeqCount = msaIn.GetSeqCount(); |
---|
75 | const unsigned uColCount = msaIn.GetColCount(); |
---|
76 | |
---|
77 | // Reserve same nr seqs, 20% more cols |
---|
78 | const unsigned uReserveColCount = (uColCount*120)/100; |
---|
79 | msaOut.SetSize(uSeqCount, uReserveColCount); |
---|
80 | |
---|
81 | for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex) |
---|
82 | { |
---|
83 | msaOut.SetSeqName(uSeqIndex, msaIn.GetSeqName(uSeqIndex)); |
---|
84 | msaOut.SetSeqId(uSeqIndex, msaIn.GetSeqId(uSeqIndex)); |
---|
85 | } |
---|
86 | |
---|
87 | const unsigned uWindowCount = (uColCount + g_uRefineWindow - 1)/g_uRefineWindow; |
---|
88 | if (0 == g_uWindowTo) |
---|
89 | g_uWindowTo = uWindowCount - 1; |
---|
90 | |
---|
91 | #if MEMDEBUG |
---|
92 | _CrtSetBreakAlloc(1560); |
---|
93 | #endif |
---|
94 | |
---|
95 | if (g_uWindowOffset > 0) |
---|
96 | { |
---|
97 | MSA msaTmp; |
---|
98 | MSAFromColRange(msaIn, 0, g_uWindowOffset, msaOut); |
---|
99 | } |
---|
100 | |
---|
101 | fprintf(stderr, "\n"); |
---|
102 | for (unsigned uWindowIndex = g_uWindowFrom; uWindowIndex <= g_uWindowTo; ++uWindowIndex) |
---|
103 | { |
---|
104 | fprintf(stderr, "Window %d of %d \r", uWindowIndex, uWindowCount); |
---|
105 | const unsigned uColFrom = g_uWindowOffset + uWindowIndex*g_uRefineWindow; |
---|
106 | unsigned uColTo = uColFrom + g_uRefineWindow - 1; |
---|
107 | if (uColTo >= uColCount) |
---|
108 | uColTo = uColCount - 1; |
---|
109 | assert(uColTo >= uColFrom); |
---|
110 | |
---|
111 | SeqVect v; |
---|
112 | SeqVectFromMSACols(msaIn, uColFrom, uColTo, v); |
---|
113 | |
---|
114 | #if MEMDEBUG |
---|
115 | _CrtMemState s1; |
---|
116 | _CrtMemCheckpoint(&s1); |
---|
117 | #endif |
---|
118 | |
---|
119 | MSA msaTmp; |
---|
120 | MUSCLE(v, msaTmp); |
---|
121 | AppendMSA(msaOut, msaTmp); |
---|
122 | if (uWindowIndex == g_uSaveWindow) |
---|
123 | { |
---|
124 | MSA msaInTmp; |
---|
125 | unsigned uOutCols = msaOut.GetColCount(); |
---|
126 | unsigned un = uColTo - uColFrom + 1; |
---|
127 | MSAFromColRange(msaIn, uColFrom, un, msaInTmp); |
---|
128 | |
---|
129 | char fn[256]; |
---|
130 | sprintf(fn, "win%d_inaln.tmp", uWindowIndex); |
---|
131 | TextFile fIn(fn, true); |
---|
132 | msaInTmp.ToFile(fIn); |
---|
133 | |
---|
134 | sprintf(fn, "win%d_inseqs.tmp", uWindowIndex); |
---|
135 | TextFile fv(fn, true); |
---|
136 | v.ToFile(fv); |
---|
137 | |
---|
138 | sprintf(fn, "win%d_outaln.tmp", uWindowIndex); |
---|
139 | TextFile fOut(fn, true); |
---|
140 | msaTmp.ToFile(fOut); |
---|
141 | } |
---|
142 | |
---|
143 | #if MEMDEBUG |
---|
144 | void FreeDPMemSPN(); |
---|
145 | FreeDPMemSPN(); |
---|
146 | |
---|
147 | _CrtMemState s2; |
---|
148 | _CrtMemCheckpoint(&s2); |
---|
149 | |
---|
150 | _CrtMemState s; |
---|
151 | _CrtMemDifference(&s, &s1, &s2); |
---|
152 | |
---|
153 | _CrtMemDumpStatistics(&s); |
---|
154 | _CrtMemDumpAllObjectsSince(&s1); |
---|
155 | exit(1); |
---|
156 | #endif |
---|
157 | //#if DEBUG |
---|
158 | // AssertMSAEqIgnoreCaseAndGaps(msaInTmp, msaTmp); |
---|
159 | //#endif |
---|
160 | } |
---|
161 | fprintf(stderr, "\n"); |
---|
162 | |
---|
163 | // AssertMSAEqIgnoreCaseAndGaps(msaIn, msaOut);//@@uncomment! |
---|
164 | } |
---|
165 | |
---|
166 | void DoRefineW() |
---|
167 | { |
---|
168 | SetOutputFileName(g_pstrOutFileName); |
---|
169 | SetInputFileName(g_pstrInFileName); |
---|
170 | SetStartTime(); |
---|
171 | |
---|
172 | SetMaxIters(g_uMaxIters); |
---|
173 | SetSeqWeightMethod(g_SeqWeight1); |
---|
174 | |
---|
175 | TextFile fileIn(g_pstrInFileName); |
---|
176 | MSA msa; |
---|
177 | msa.FromFile(fileIn); |
---|
178 | |
---|
179 | const unsigned uSeqCount = msa.GetSeqCount(); |
---|
180 | if (0 == uSeqCount) |
---|
181 | Quit("No sequences in input file"); |
---|
182 | |
---|
183 | MSA::SetIdCount(uSeqCount); |
---|
184 | |
---|
185 | // Initialize sequence ids. |
---|
186 | // From this point on, ids must somehow propogate from here. |
---|
187 | for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex) |
---|
188 | msa.SetSeqId(uSeqIndex, uSeqIndex); |
---|
189 | SetMuscleInputMSA(msa); |
---|
190 | |
---|
191 | ALPHA Alpha = ALPHA_Undefined; |
---|
192 | switch (g_SeqType) |
---|
193 | { |
---|
194 | case SEQTYPE_Auto: |
---|
195 | Alpha = msa.GuessAlpha(); |
---|
196 | break; |
---|
197 | |
---|
198 | case SEQTYPE_Protein: |
---|
199 | Alpha = ALPHA_Amino; |
---|
200 | break; |
---|
201 | |
---|
202 | case SEQTYPE_DNA: |
---|
203 | Alpha = ALPHA_DNA; |
---|
204 | break; |
---|
205 | |
---|
206 | case SEQTYPE_RNA: |
---|
207 | Alpha = ALPHA_RNA; |
---|
208 | break; |
---|
209 | |
---|
210 | default: |
---|
211 | Quit("Invalid SeqType"); |
---|
212 | } |
---|
213 | SetAlpha(Alpha); |
---|
214 | msa.FixAlpha(); |
---|
215 | |
---|
216 | if (ALPHA_DNA == Alpha || ALPHA_RNA == Alpha) |
---|
217 | SetPPScore(PPSCORE_SPN); |
---|
218 | |
---|
219 | MSA msaOut; |
---|
220 | RefineW(msa, msaOut); |
---|
221 | |
---|
222 | // ValidateMuscleIds(msa); |
---|
223 | |
---|
224 | // TextFile fileOut(g_pstrOutFileName, true); |
---|
225 | // msaOut.ToFile(fileOut); |
---|
226 | MuscleOutput(msaOut); |
---|
227 | } |
---|