1 | #include "muscle.h" |
---|
2 | #include "msa.h" |
---|
3 | #include "tree.h" |
---|
4 | #include "profile.h" |
---|
5 | #include "pwpath.h" |
---|
6 | |
---|
7 | #define TRACE 0 |
---|
8 | |
---|
9 | // Progressive alignment according to a diffs tree. |
---|
10 | |
---|
11 | static void MakeNode(const MSA &msaIn, const Tree &Diffs, unsigned uDiffsNodeIndex, |
---|
12 | const unsigned IdToDiffsTreeNodeIndex[], ProgNode &Node) |
---|
13 | { |
---|
14 | const unsigned uSeqCount = msaIn.GetSeqCount(); |
---|
15 | |
---|
16 | unsigned *Ids = new unsigned[uSeqCount]; |
---|
17 | |
---|
18 | unsigned uSeqsInDiffCount = 0; |
---|
19 | for (unsigned uId = 0; uId < uSeqCount; ++uId) |
---|
20 | { |
---|
21 | if (IdToDiffsTreeNodeIndex[uId] == uDiffsNodeIndex) |
---|
22 | { |
---|
23 | Ids[uSeqsInDiffCount] = uId; |
---|
24 | ++uSeqsInDiffCount; |
---|
25 | } |
---|
26 | } |
---|
27 | if (0 == uSeqsInDiffCount) |
---|
28 | Quit("MakeNode: no seqs in diff"); |
---|
29 | |
---|
30 | MSASubsetByIds(msaIn, Ids, uSeqsInDiffCount, Node.m_MSA); |
---|
31 | |
---|
32 | #if DEBUG |
---|
33 | ValidateMuscleIds(Node.m_MSA); |
---|
34 | #endif |
---|
35 | |
---|
36 | DeleteGappedCols(Node.m_MSA); |
---|
37 | delete[] Ids; |
---|
38 | } |
---|
39 | |
---|
40 | void RealignDiffs(const MSA &msaIn, const Tree &Diffs, |
---|
41 | const unsigned IdToDiffsTreeNodeIndex[], MSA &msaOut) |
---|
42 | { |
---|
43 | assert(Diffs.IsRooted()); |
---|
44 | |
---|
45 | #if TRACE |
---|
46 | Log("RealignDiffs\n"); |
---|
47 | Log("Diff tree:\n"); |
---|
48 | Diffs.LogMe(); |
---|
49 | #endif |
---|
50 | |
---|
51 | const unsigned uNodeCount = Diffs.GetNodeCount(); |
---|
52 | if (uNodeCount%2 == 0) |
---|
53 | Quit("RealignDiffs: Expected odd number of nodes"); |
---|
54 | |
---|
55 | const unsigned uMergeCount = (uNodeCount - 1)/2; |
---|
56 | |
---|
57 | ProgNode *ProgNodes = new ProgNode[uNodeCount]; |
---|
58 | |
---|
59 | unsigned uJoin = 0; |
---|
60 | SetProgressDesc("Refine tree"); |
---|
61 | for (unsigned uDiffsNodeIndex = Diffs.FirstDepthFirstNode(); |
---|
62 | NULL_NEIGHBOR != uDiffsNodeIndex; |
---|
63 | uDiffsNodeIndex = Diffs.NextDepthFirstNode(uDiffsNodeIndex)) |
---|
64 | { |
---|
65 | if (Diffs.IsLeaf(uDiffsNodeIndex)) |
---|
66 | { |
---|
67 | assert(uDiffsNodeIndex < uNodeCount); |
---|
68 | if (uDiffsNodeIndex >= uNodeCount) |
---|
69 | Quit("TreeNodeIndex=%u NodeCount=%u\n", uDiffsNodeIndex, uNodeCount); |
---|
70 | |
---|
71 | ProgNode &Node = ProgNodes[uDiffsNodeIndex]; |
---|
72 | MakeNode(msaIn, Diffs, uDiffsNodeIndex, IdToDiffsTreeNodeIndex, Node); |
---|
73 | |
---|
74 | Node.m_uLength = Node.m_MSA.GetColCount(); |
---|
75 | } |
---|
76 | else |
---|
77 | { |
---|
78 | Progress(uJoin, uMergeCount); |
---|
79 | ++uJoin; |
---|
80 | const unsigned uMergeNodeIndex = uDiffsNodeIndex; |
---|
81 | ProgNode &Parent = ProgNodes[uMergeNodeIndex]; |
---|
82 | |
---|
83 | const unsigned uLeft = Diffs.GetLeft(uDiffsNodeIndex); |
---|
84 | const unsigned uRight = Diffs.GetRight(uDiffsNodeIndex); |
---|
85 | |
---|
86 | ProgNode &Node1 = ProgNodes[uLeft]; |
---|
87 | ProgNode &Node2 = ProgNodes[uRight]; |
---|
88 | |
---|
89 | PWPath Path; |
---|
90 | AlignTwoMSAs(Node1.m_MSA, Node2.m_MSA, Parent.m_MSA, Path); |
---|
91 | |
---|
92 | #if TRACE |
---|
93 | { |
---|
94 | Log("Combined:\n"); |
---|
95 | Parent.m_MSA.LogMe(); |
---|
96 | } |
---|
97 | #endif |
---|
98 | |
---|
99 | Node1.m_MSA.Clear(); |
---|
100 | Node2.m_MSA.Clear(); |
---|
101 | } |
---|
102 | } |
---|
103 | ProgressStepsDone(); |
---|
104 | |
---|
105 | unsigned uRootNodeIndex = Diffs.GetRootNodeIndex(); |
---|
106 | const ProgNode &RootProgNode = ProgNodes[uRootNodeIndex]; |
---|
107 | msaOut.Copy(RootProgNode.m_MSA); |
---|
108 | |
---|
109 | #if DEBUG |
---|
110 | AssertMSAEqIgnoreCaseAndGaps(msaIn, msaOut); |
---|
111 | #endif |
---|
112 | |
---|
113 | delete[] ProgNodes; |
---|
114 | ProgNodes = 0; |
---|
115 | } |
---|