1 | #include "muscle.h" |
---|
2 | #include "textfile.h" |
---|
3 | #include "seqvect.h" |
---|
4 | #include "distfunc.h" |
---|
5 | #include "msa.h" |
---|
6 | #include "tree.h" |
---|
7 | #include "profile.h" |
---|
8 | #include "timing.h" |
---|
9 | |
---|
10 | static char g_strUseTreeWarning[] = |
---|
11 | "\n******** WARNING ****************\n" |
---|
12 | "\nYou specified the -usetree option.\n" |
---|
13 | "Note that a good evolutionary tree may NOT be a good\n" |
---|
14 | "guide tree for multiple alignment. For more details,\n" |
---|
15 | "please refer to the user guide. To disable this\n" |
---|
16 | "warning, use -usetree_nowarn <treefilename>.\n\n"; |
---|
17 | |
---|
18 | void DoMuscle() |
---|
19 | { |
---|
20 | SetOutputFileName(g_pstrOutFileName); |
---|
21 | SetInputFileName(g_pstrInFileName); |
---|
22 | |
---|
23 | SetMaxIters(g_uMaxIters); |
---|
24 | SetSeqWeightMethod(g_SeqWeight1); |
---|
25 | |
---|
26 | TextFile fileIn(g_pstrInFileName); |
---|
27 | SeqVect v; |
---|
28 | v.FromFASTAFile(fileIn); |
---|
29 | const unsigned uSeqCount = v.Length(); |
---|
30 | |
---|
31 | if (0 == uSeqCount) |
---|
32 | Quit("No sequences in input file"); |
---|
33 | |
---|
34 | ALPHA Alpha = ALPHA_Undefined; |
---|
35 | switch (g_SeqType) |
---|
36 | { |
---|
37 | case SEQTYPE_Auto: |
---|
38 | Alpha = v.GuessAlpha(); |
---|
39 | break; |
---|
40 | |
---|
41 | case SEQTYPE_Protein: |
---|
42 | Alpha = ALPHA_Amino; |
---|
43 | break; |
---|
44 | |
---|
45 | case SEQTYPE_DNA: |
---|
46 | Alpha = ALPHA_DNA; |
---|
47 | break; |
---|
48 | |
---|
49 | case SEQTYPE_RNA: |
---|
50 | Alpha = ALPHA_RNA; |
---|
51 | break; |
---|
52 | |
---|
53 | default: |
---|
54 | Quit("Invalid seq type"); |
---|
55 | } |
---|
56 | SetAlpha(Alpha); |
---|
57 | v.FixAlpha(); |
---|
58 | |
---|
59 | PTR_SCOREMATRIX UserMatrix = 0; |
---|
60 | if (0 != g_pstrMatrixFileName) |
---|
61 | { |
---|
62 | const char *FileName = g_pstrMatrixFileName; |
---|
63 | const char *Path = getenv("MUSCLE_MXPATH"); |
---|
64 | if (Path != 0) |
---|
65 | { |
---|
66 | size_t n = strlen(Path) + 1 + strlen(FileName) + 1; |
---|
67 | char *NewFileName = new char[n]; |
---|
68 | sprintf(NewFileName, "%s/%s", Path, FileName); |
---|
69 | FileName = NewFileName; |
---|
70 | } |
---|
71 | TextFile File(FileName); |
---|
72 | UserMatrix = ReadMx(File); |
---|
73 | g_Alpha = ALPHA_Amino; |
---|
74 | g_PPScore = PPSCORE_SP; |
---|
75 | } |
---|
76 | |
---|
77 | SetPPScore(); |
---|
78 | |
---|
79 | if (0 != UserMatrix) |
---|
80 | g_ptrScoreMatrix = UserMatrix; |
---|
81 | |
---|
82 | unsigned uMaxL = 0; |
---|
83 | unsigned uTotL = 0; |
---|
84 | for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex) |
---|
85 | { |
---|
86 | unsigned L = v.GetSeq(uSeqIndex).Length(); |
---|
87 | uTotL += L; |
---|
88 | if (L > uMaxL) |
---|
89 | uMaxL = L; |
---|
90 | } |
---|
91 | |
---|
92 | SetIter(1); |
---|
93 | g_bDiags = g_bDiags1; |
---|
94 | SetSeqStats(uSeqCount, uMaxL, uTotL/uSeqCount); |
---|
95 | |
---|
96 | SetMuscleSeqVect(v); |
---|
97 | |
---|
98 | MSA::SetIdCount(uSeqCount); |
---|
99 | |
---|
100 | // Initialize sequence ids. |
---|
101 | // From this point on, ids must somehow propogate from here. |
---|
102 | for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex) |
---|
103 | v.SetSeqId(uSeqIndex, uSeqIndex); |
---|
104 | |
---|
105 | if (0 == uSeqCount) |
---|
106 | Quit("Input file '%s' has no sequences", g_pstrInFileName); |
---|
107 | if (1 == uSeqCount) |
---|
108 | { |
---|
109 | TextFile fileOut(g_pstrOutFileName, true); |
---|
110 | v.ToFile(fileOut); |
---|
111 | return; |
---|
112 | } |
---|
113 | |
---|
114 | if (uSeqCount > 1) |
---|
115 | MHackStart(v); |
---|
116 | |
---|
117 | // First iteration |
---|
118 | Tree GuideTree; |
---|
119 | if (0 != g_pstrUseTreeFileName) |
---|
120 | { |
---|
121 | // Discourage users... |
---|
122 | if (!g_bUseTreeNoWarn) |
---|
123 | fprintf(stderr, "%s", g_strUseTreeWarning); |
---|
124 | |
---|
125 | // Read tree from file |
---|
126 | TextFile TreeFile(g_pstrUseTreeFileName); |
---|
127 | GuideTree.FromFile(TreeFile); |
---|
128 | |
---|
129 | // Make sure tree is rooted |
---|
130 | if (!GuideTree.IsRooted()) |
---|
131 | Quit("User tree must be rooted"); |
---|
132 | |
---|
133 | if (GuideTree.GetLeafCount() != uSeqCount) |
---|
134 | Quit("User tree does not match input sequences"); |
---|
135 | |
---|
136 | const unsigned uNodeCount = GuideTree.GetNodeCount(); |
---|
137 | for (unsigned uNodeIndex = 0; uNodeIndex < uNodeCount; ++uNodeIndex) |
---|
138 | { |
---|
139 | if (!GuideTree.IsLeaf(uNodeIndex)) |
---|
140 | continue; |
---|
141 | const char *LeafName = GuideTree.GetLeafName(uNodeIndex); |
---|
142 | unsigned uSeqIndex; |
---|
143 | bool SeqFound = v.FindName(LeafName, &uSeqIndex); |
---|
144 | if (!SeqFound) |
---|
145 | Quit("Label %s in tree does not match sequences", LeafName); |
---|
146 | unsigned uId = v.GetSeqIdFromName(LeafName); |
---|
147 | GuideTree.SetLeafId(uNodeIndex, uId); |
---|
148 | } |
---|
149 | } |
---|
150 | else |
---|
151 | TreeFromSeqVect(v, GuideTree, g_Cluster1, g_Distance1, g_Root1, |
---|
152 | g_pstrDistMxFileName1); |
---|
153 | |
---|
154 | const char *Tree1 = ValueOpt("Tree1"); |
---|
155 | if (0 != Tree1) |
---|
156 | { |
---|
157 | TextFile f(Tree1, true); |
---|
158 | GuideTree.ToFile(f); |
---|
159 | if (g_bClusterOnly) |
---|
160 | return; |
---|
161 | } |
---|
162 | |
---|
163 | SetMuscleTree(GuideTree); |
---|
164 | ValidateMuscleIds(GuideTree); |
---|
165 | |
---|
166 | MSA msa; |
---|
167 | ProgNode *ProgNodes = 0; |
---|
168 | if (g_bLow) |
---|
169 | ProgNodes = ProgressiveAlignE(v, GuideTree, msa); |
---|
170 | else |
---|
171 | ProgressiveAlign(v, GuideTree, msa); |
---|
172 | SetCurrentAlignment(msa); |
---|
173 | |
---|
174 | if (0 != g_pstrComputeWeightsFileName) |
---|
175 | { |
---|
176 | extern void OutWeights(const char *FileName, const MSA &msa); |
---|
177 | SetMSAWeightsMuscle(msa); |
---|
178 | OutWeights(g_pstrComputeWeightsFileName, msa); |
---|
179 | return; |
---|
180 | } |
---|
181 | |
---|
182 | ValidateMuscleIds(msa); |
---|
183 | |
---|
184 | if (1 == g_uMaxIters || 2 == uSeqCount) |
---|
185 | { |
---|
186 | //TextFile fileOut(g_pstrOutFileName, true); |
---|
187 | //MHackEnd(msa); |
---|
188 | //msa.ToFile(fileOut); |
---|
189 | MuscleOutput(msa); |
---|
190 | return; |
---|
191 | } |
---|
192 | |
---|
193 | if (0 == g_pstrUseTreeFileName) |
---|
194 | { |
---|
195 | g_bDiags = g_bDiags2; |
---|
196 | SetIter(2); |
---|
197 | |
---|
198 | if (g_bLow) |
---|
199 | { |
---|
200 | if (0 != g_uMaxTreeRefineIters) |
---|
201 | RefineTreeE(msa, v, GuideTree, ProgNodes); |
---|
202 | } |
---|
203 | else |
---|
204 | RefineTree(msa, GuideTree); |
---|
205 | |
---|
206 | const char *Tree2 = ValueOpt("Tree2"); |
---|
207 | if (0 != Tree2) |
---|
208 | { |
---|
209 | TextFile f(Tree2, true); |
---|
210 | GuideTree.ToFile(f); |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | SetSeqWeightMethod(g_SeqWeight2); |
---|
215 | SetMuscleTree(GuideTree); |
---|
216 | |
---|
217 | if (g_bAnchors) |
---|
218 | RefineVert(msa, GuideTree, g_uMaxIters - 2); |
---|
219 | else |
---|
220 | RefineHoriz(msa, GuideTree, g_uMaxIters - 2, false, false); |
---|
221 | |
---|
222 | #if 0 |
---|
223 | // Refining by subfamilies is disabled as it didn't give better |
---|
224 | // results. I tried doing this before and after RefineHoriz. |
---|
225 | // Should get back to this as it seems like this should work. |
---|
226 | RefineSubfams(msa, GuideTree, g_uMaxIters - 2); |
---|
227 | #endif |
---|
228 | |
---|
229 | ValidateMuscleIds(msa); |
---|
230 | ValidateMuscleIds(GuideTree); |
---|
231 | |
---|
232 | //TextFile fileOut(g_pstrOutFileName, true); |
---|
233 | //MHackEnd(msa); |
---|
234 | //msa.ToFile(fileOut); |
---|
235 | MuscleOutput(msa); |
---|
236 | } |
---|
237 | |
---|
238 | void Run() |
---|
239 | { |
---|
240 | SetStartTime(); |
---|
241 | Log("Started %s\n", GetTimeAsStr()); |
---|
242 | for (int i = 0; i < g_argc; ++i) |
---|
243 | Log("%s ", g_argv[i]); |
---|
244 | Log("\n"); |
---|
245 | |
---|
246 | #if TIMING |
---|
247 | TICKS t1 = GetClockTicks(); |
---|
248 | #endif |
---|
249 | if (g_bRefine) |
---|
250 | Refine(); |
---|
251 | else if (g_bRefineW) |
---|
252 | { |
---|
253 | extern void DoRefineW(); |
---|
254 | DoRefineW(); |
---|
255 | } |
---|
256 | else if (g_bProfDB) |
---|
257 | ProfDB(); |
---|
258 | else if (g_bSW) |
---|
259 | Local(); |
---|
260 | else if (0 != g_pstrSPFileName) |
---|
261 | DoSP(); |
---|
262 | else if (g_bProfile) |
---|
263 | Profile(); |
---|
264 | else if (g_bPPScore) |
---|
265 | PPScore(); |
---|
266 | else if (g_bPAS) |
---|
267 | ProgAlignSubFams(); |
---|
268 | else if (g_bMakeTree) |
---|
269 | { |
---|
270 | extern void DoMakeTree(); |
---|
271 | DoMakeTree(); |
---|
272 | } |
---|
273 | else |
---|
274 | DoMuscle(); |
---|
275 | |
---|
276 | #if TIMING |
---|
277 | extern TICKS g_ticksDP; |
---|
278 | extern TICKS g_ticksObjScore; |
---|
279 | TICKS t2 = GetClockTicks(); |
---|
280 | TICKS TotalTicks = t2 - t1; |
---|
281 | TICKS ticksOther = TotalTicks - g_ticksDP - g_ticksObjScore; |
---|
282 | double dSecs = TicksToSecs(TotalTicks); |
---|
283 | double PctDP = (double) g_ticksDP*100.0/(double) TotalTicks; |
---|
284 | double PctOS = (double) g_ticksObjScore*100.0/(double) TotalTicks; |
---|
285 | double PctOther = (double) ticksOther*100.0/(double) TotalTicks; |
---|
286 | Log(" Ticks Secs Pct\n"); |
---|
287 | Log(" ============ ======= =====\n"); |
---|
288 | Log("DP %12ld %7.2f %5.1f%%\n", |
---|
289 | (long) g_ticksDP, TicksToSecs(g_ticksDP), PctDP); |
---|
290 | Log("OS %12ld %7.2f %5.1f%%\n", |
---|
291 | (long) g_ticksObjScore, TicksToSecs(g_ticksObjScore), PctOS); |
---|
292 | Log("Other %12ld %7.2f %5.1f%%\n", |
---|
293 | (long) ticksOther, TicksToSecs(ticksOther), PctOther); |
---|
294 | Log("Total %12ld %7.2f 100.0%%\n", (long) TotalTicks, dSecs); |
---|
295 | #endif |
---|
296 | |
---|
297 | ListDiagSavings(); |
---|
298 | Log("Finished %s\n", GetTimeAsStr()); |
---|
299 | } |
---|