1 #include "muscle.h"
2 #include "msa.h"
3 #include "tree.h"
4 #include "profile.h"
5 #include <stdio.h>
6 #include "muscle_context.h"
7 
RefineTree(MSA & msa,Tree & tree)8 void RefineTree(MSA &msa, Tree &tree)
9 	{
10     MuscleContext *ctx = getMuscleContext();
11     unsigned &g_uMaxTreeRefineIters = ctx->params.g_uMaxTreeRefineIters;
12     CLUSTER &g_Cluster2 = ctx->params.g_Cluster2;
13     DISTANCE &g_Distance2 =  ctx->params.g_Distance2;
14     ROOT &g_Root2 = ctx->params.g_Root2;
15     const char* &g_pstrDistMxFileName2 = ctx->params.g_pstrDistMxFileName2;
16 	const unsigned uSeqCount = msa.GetSeqCount();
17 	if (tree.GetLeafCount() != uSeqCount)
18 		Quit("Refine tree, tree has different number of nodes");
19 
20 	if (uSeqCount < 3)
21 		return;
22 
23 #if	DEBUG
24 	ValidateMuscleIds(msa);
25 	ValidateMuscleIds(tree);
26 #endif
27 
28 	unsigned *IdToDiffsLeafNodeIndex = new unsigned[uSeqCount];
29 	unsigned uDiffsCount = uSeqCount;
30 	Tree Tree2;
31 	for (unsigned uIter = 0; uIter < g_uMaxTreeRefineIters; ++uIter)
32 		{
33 		TreeFromMSA(msa, Tree2, g_Cluster2, g_Distance2, g_Root2, g_pstrDistMxFileName2);
34 
35 #if	DEBUG
36 		ValidateMuscleIds(Tree2);
37 #endif
38 
39 		Tree Diffs;
40 		DiffTrees(Tree2, tree, Diffs, IdToDiffsLeafNodeIndex);
41 
42 		tree.Copy(Tree2);
43 
44 		const unsigned uNewDiffsNodeCount = Diffs.GetNodeCount();
45 		const unsigned uNewDiffsCount = (uNewDiffsNodeCount - 1)/2;
46 
47 		if (0 == uNewDiffsCount || uNewDiffsCount >= uDiffsCount)
48 			{
49 			ProgressStepsDone();
50 			break;
51 			}
52 		uDiffsCount = uNewDiffsCount;
53 
54 		MSA msa2;
55 		RealignDiffs(msa, Diffs, IdToDiffsLeafNodeIndex, msa2);
56 
57 #if	DEBUG
58 		ValidateMuscleIds(msa2);
59 #endif
60 
61 		msa.Copy(msa2);
62 		SetCurrentAlignment(msa);
63 		}
64 
65 	delete[] IdToDiffsLeafNodeIndex;
66 	}
67