| 1 | #include "muscle.h" |
|---|
| 2 | #include "msa.h" |
|---|
| 3 | #include "tree.h" |
|---|
| 4 | #include "clust.h" |
|---|
| 5 | #include "profile.h" |
|---|
| 6 | #include "pwpath.h" |
|---|
| 7 | |
|---|
| 8 | #define TRACE 0 |
|---|
| 9 | |
|---|
| 10 | static void ProgressiveAlignSubfams(const Tree &tree, const unsigned Subfams[], |
|---|
| 11 | unsigned uSubfamCount, const MSA SubfamMSAs[], MSA &msa); |
|---|
| 12 | |
|---|
| 13 | // Identify subfamilies in a tree. |
|---|
| 14 | // Returns array of internal node indexes, one for each subfamily. |
|---|
| 15 | // First try is to select groups by height (which should approximate |
|---|
| 16 | // minimum percent identity), if this gives too many subfamilies then |
|---|
| 17 | // we cut at a point that gives the maximum allowed number of subfams. |
|---|
| 18 | static void GetSubfams(const Tree &tree, double dMaxHeight, |
|---|
| 19 | unsigned uMaxSubfamCount, unsigned **ptrptrSubfams, unsigned *ptruSubfamCount) |
|---|
| 20 | { |
|---|
| 21 | const unsigned uNodeCount = tree.GetNodeCount(); |
|---|
| 22 | |
|---|
| 23 | unsigned *Subfams = new unsigned[uNodeCount]; |
|---|
| 24 | |
|---|
| 25 | unsigned uSubfamCount; |
|---|
| 26 | ClusterByHeight(tree, dMaxHeight, Subfams, &uSubfamCount); |
|---|
| 27 | |
|---|
| 28 | if (uSubfamCount > uMaxSubfamCount) |
|---|
| 29 | ClusterBySubfamCount(tree, uMaxSubfamCount, Subfams, &uSubfamCount); |
|---|
| 30 | |
|---|
| 31 | *ptrptrSubfams = Subfams; |
|---|
| 32 | *ptruSubfamCount = uSubfamCount; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | static void LogSubfams(const Tree &tree, const unsigned Subfams[], |
|---|
| 36 | unsigned uSubfamCount) |
|---|
| 37 | { |
|---|
| 38 | const unsigned uNodeCount = tree.GetNodeCount(); |
|---|
| 39 | Log("%u subfamilies found\n", uSubfamCount); |
|---|
| 40 | Log("Subfam Sequence\n"); |
|---|
| 41 | Log("------ --------\n"); |
|---|
| 42 | unsigned *Leaves = new unsigned[uNodeCount]; |
|---|
| 43 | for (unsigned uSubfamIndex = 0; uSubfamIndex < uSubfamCount; ++uSubfamIndex) |
|---|
| 44 | { |
|---|
| 45 | unsigned uSubfamNodeIndex = Subfams[uSubfamIndex]; |
|---|
| 46 | unsigned uLeafCount; |
|---|
| 47 | GetLeaves(tree, uSubfamNodeIndex, Leaves, &uLeafCount); |
|---|
| 48 | for (unsigned uLeafIndex = 0; uLeafIndex < uLeafCount; ++uLeafIndex) |
|---|
| 49 | Log("%6u %s\n", uSubfamIndex + 1, tree.GetLeafName(Leaves[uLeafIndex])); |
|---|
| 50 | Log("\n"); |
|---|
| 51 | } |
|---|
| 52 | delete[] Leaves; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | bool RefineSubfams(MSA &msa, const Tree &tree, unsigned uIters) |
|---|
| 56 | { |
|---|
| 57 | const unsigned uSeqCount = msa.GetSeqCount(); |
|---|
| 58 | if (uSeqCount < 3) |
|---|
| 59 | return false; |
|---|
| 60 | |
|---|
| 61 | const double dMaxHeight = 0.6; |
|---|
| 62 | const unsigned uMaxSubfamCount = 16; |
|---|
| 63 | const unsigned uNodeCount = tree.GetNodeCount(); |
|---|
| 64 | |
|---|
| 65 | unsigned *Subfams; |
|---|
| 66 | unsigned uSubfamCount; |
|---|
| 67 | GetSubfams(tree, dMaxHeight, uMaxSubfamCount, &Subfams, &uSubfamCount); |
|---|
| 68 | assert(uSubfamCount <= uSeqCount); |
|---|
| 69 | |
|---|
| 70 | if (g_bVerbose) |
|---|
| 71 | LogSubfams(tree, Subfams, uSubfamCount); |
|---|
| 72 | |
|---|
| 73 | MSA *SubfamMSAs = new MSA[uSubfamCount]; |
|---|
| 74 | unsigned *Leaves = new unsigned[uSeqCount]; |
|---|
| 75 | unsigned *Ids = new unsigned[uSeqCount]; |
|---|
| 76 | |
|---|
| 77 | bool bAnyChanges = false; |
|---|
| 78 | for (unsigned uSubfamIndex = 0; uSubfamIndex < uSubfamCount; ++uSubfamIndex) |
|---|
| 79 | { |
|---|
| 80 | unsigned uSubfam = Subfams[uSubfamIndex]; |
|---|
| 81 | unsigned uLeafCount; |
|---|
| 82 | GetLeaves(tree, uSubfam, Leaves, &uLeafCount); |
|---|
| 83 | assert(uLeafCount <= uSeqCount); |
|---|
| 84 | |
|---|
| 85 | LeafIndexesToIds(tree, Leaves, uLeafCount, Ids); |
|---|
| 86 | |
|---|
| 87 | MSA &msaSubfam = SubfamMSAs[uSubfamIndex]; |
|---|
| 88 | MSASubsetByIds(msa, Ids, uLeafCount, msaSubfam); |
|---|
| 89 | DeleteGappedCols(msaSubfam); |
|---|
| 90 | |
|---|
| 91 | #if TRACE |
|---|
| 92 | Log("Subfam %u MSA=\n", uSubfamIndex); |
|---|
| 93 | msaSubfam.LogMe(); |
|---|
| 94 | #endif |
|---|
| 95 | |
|---|
| 96 | if (msaSubfam.GetSeqCount() <= 2) |
|---|
| 97 | continue; |
|---|
| 98 | |
|---|
| 99 | // TODO ///////////////////////////////////////// |
|---|
| 100 | // Try using existing tree, may actually hurt to |
|---|
| 101 | // re-estimate, may also be a waste of CPU & mem. |
|---|
| 102 | ///////////////////////////////////////////////// |
|---|
| 103 | Tree SubfamTree; |
|---|
| 104 | TreeFromMSA(msaSubfam, SubfamTree, g_Cluster2, g_Distance2, g_Root2); |
|---|
| 105 | |
|---|
| 106 | bool bAnyChangesThisSubfam; |
|---|
| 107 | if (g_bAnchors) |
|---|
| 108 | bAnyChangesThisSubfam = RefineVert(msaSubfam, SubfamTree, uIters); |
|---|
| 109 | else |
|---|
| 110 | bAnyChangesThisSubfam = RefineHoriz(msaSubfam, SubfamTree, uIters, false, false); |
|---|
| 111 | #if TRACE |
|---|
| 112 | Log("Subfam %u Changed %d\n", uSubfamIndex, bAnyChangesThisSubfam); |
|---|
| 113 | #endif |
|---|
| 114 | if (bAnyChangesThisSubfam) |
|---|
| 115 | bAnyChanges = true; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | if (bAnyChanges) |
|---|
| 119 | ProgressiveAlignSubfams(tree, Subfams, uSubfamCount, SubfamMSAs, msa); |
|---|
| 120 | |
|---|
| 121 | delete[] Leaves; |
|---|
| 122 | delete[] Subfams; |
|---|
| 123 | delete[] SubfamMSAs; |
|---|
| 124 | |
|---|
| 125 | return bAnyChanges; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | static void ProgressiveAlignSubfams(const Tree &tree, const unsigned Subfams[], |
|---|
| 129 | unsigned uSubfamCount, const MSA SubfamMSAs[], MSA &msa) |
|---|
| 130 | { |
|---|
| 131 | const unsigned uNodeCount = tree.GetNodeCount(); |
|---|
| 132 | |
|---|
| 133 | bool *Ready = new bool[uNodeCount]; |
|---|
| 134 | MSA **MSAs = new MSA *[uNodeCount]; |
|---|
| 135 | for (unsigned uNodeIndex = 0; uNodeIndex < uNodeCount; ++uNodeIndex) |
|---|
| 136 | { |
|---|
| 137 | Ready[uNodeIndex] = false; |
|---|
| 138 | MSAs[uNodeIndex] = 0; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | for (unsigned uSubfamIndex = 0; uSubfamIndex < uSubfamCount; ++uSubfamIndex) |
|---|
| 142 | { |
|---|
| 143 | unsigned uNodeIndex = Subfams[uSubfamIndex]; |
|---|
| 144 | Ready[uNodeIndex] = true; |
|---|
| 145 | MSA *ptrMSA = new MSA; |
|---|
| 146 | // TODO: Wasteful copy, needs re-design |
|---|
| 147 | ptrMSA->Copy(SubfamMSAs[uSubfamIndex]); |
|---|
| 148 | MSAs[uNodeIndex] = ptrMSA; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | for (unsigned uNodeIndex = tree.FirstDepthFirstNode(); |
|---|
| 152 | NULL_NEIGHBOR != uNodeIndex; |
|---|
| 153 | uNodeIndex = tree.NextDepthFirstNode(uNodeIndex)) |
|---|
| 154 | { |
|---|
| 155 | if (tree.IsLeaf(uNodeIndex)) |
|---|
| 156 | continue; |
|---|
| 157 | |
|---|
| 158 | unsigned uRight = tree.GetRight(uNodeIndex); |
|---|
| 159 | unsigned uLeft = tree.GetLeft(uNodeIndex); |
|---|
| 160 | if (!Ready[uRight] || !Ready[uLeft]) |
|---|
| 161 | continue; |
|---|
| 162 | |
|---|
| 163 | MSA *ptrLeft = MSAs[uLeft]; |
|---|
| 164 | MSA *ptrRight = MSAs[uRight]; |
|---|
| 165 | assert(ptrLeft != 0 && ptrRight != 0); |
|---|
| 166 | |
|---|
| 167 | MSA *ptrParent = new MSA; |
|---|
| 168 | |
|---|
| 169 | PWPath Path; |
|---|
| 170 | AlignTwoMSAs(*ptrLeft, *ptrRight, *ptrParent, Path); |
|---|
| 171 | |
|---|
| 172 | MSAs[uNodeIndex] = ptrParent; |
|---|
| 173 | Ready[uNodeIndex] = true; |
|---|
| 174 | Ready[uLeft] = false; |
|---|
| 175 | Ready[uRight] = false; |
|---|
| 176 | |
|---|
| 177 | delete MSAs[uLeft]; |
|---|
| 178 | delete MSAs[uRight]; |
|---|
| 179 | MSAs[uLeft] = 0; |
|---|
| 180 | MSAs[uRight] = 0; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | #if DEBUG |
|---|
| 184 | { |
|---|
| 185 | unsigned uReadyCount = 0; |
|---|
| 186 | for (unsigned uNodeIndex = 0; uNodeIndex < uNodeCount; ++uNodeIndex) |
|---|
| 187 | { |
|---|
| 188 | if (Ready[uNodeIndex]) |
|---|
| 189 | { |
|---|
| 190 | assert(tree.IsRoot(uNodeIndex)); |
|---|
| 191 | ++uReadyCount; |
|---|
| 192 | assert(0 != MSAs[uNodeIndex]); |
|---|
| 193 | } |
|---|
| 194 | else |
|---|
| 195 | assert(0 == MSAs[uNodeIndex]); |
|---|
| 196 | } |
|---|
| 197 | assert(1 == uReadyCount); |
|---|
| 198 | } |
|---|
| 199 | #endif |
|---|
| 200 | |
|---|
| 201 | const unsigned uRoot = tree.GetRootNodeIndex(); |
|---|
| 202 | MSA *ptrRootAlignment = MSAs[uRoot]; |
|---|
| 203 | |
|---|
| 204 | msa.Copy(*ptrRootAlignment); |
|---|
| 205 | |
|---|
| 206 | delete ptrRootAlignment; |
|---|
| 207 | |
|---|
| 208 | #if TRACE |
|---|
| 209 | Log("After refine subfamilies, root alignment=\n"); |
|---|
| 210 | msa.LogMe(); |
|---|
| 211 | #endif |
|---|
| 212 | } |
|---|