aboutsummaryrefslogtreecommitdiff
path: root/sys
diff options
context:
space:
mode:
authorNicholas Noll <nbnoll@eml.cc>2020-04-28 14:41:33 -0700
committerNicholas Noll <nbnoll@eml.cc>2020-04-28 14:41:33 -0700
commit9fd30c9e0ec9c5716b8cb7b8896318178d4b08bd (patch)
tree0815cd9deb346cf729834566275446c3d2d8b40b /sys
parent4a03c700d5391e3fa18f9d9b219936ef5c9eb722 (diff)
fix: ladderize now sorts by number of nodes contained below
Diffstat (limited to 'sys')
-rw-r--r--sys/libbio/phylo.c41
-rw-r--r--sys/libbio/test.c1
2 files changed, 35 insertions, 7 deletions
diff --git a/sys/libbio/phylo.c b/sys/libbio/phylo.c
index 2bff92d..71901ba 100644
--- a/sys/libbio/phylo.c
+++ b/sys/libbio/phylo.c
@@ -54,14 +54,15 @@ phylo·countnodes(bio·Node *node, int *n)
error err;
bio·Node *child;
- *n += 1;
+ node->nnode = 0;
for (child = node->child; child != nil; child = child->sibling) {
- if (err = phylo·countnodes(child, n), err) {
+ if (err = phylo·countnodes(child, &node->nnode), err) {
errorf("node count: failure at '%s'", child->name);
return 1;
}
}
+ *n += node->nnode + 1;
return 0;
}
@@ -88,24 +89,50 @@ phylo·countleafs(bio·Node *node, int *n)
// -----------------------------------------------------------------------
// tree editing
+static
+void
+sortnodelist(bio·Node **head, bio·Node *next)
+{
+ bio·Node tmp, *it;
+
+ it = &tmp;
+ tmp.sibling = *head;
+
+ while (it->sibling != nil && it->sibling->nnode < next->nnode) {
+ it = it->sibling;
+ }
+
+ next->sibling = it->sibling;
+ it->sibling = next;
+ *head = tmp.sibling;
+}
+
error
phylo·ladderize(bio·Node *root)
{
int i;
error err;
- bio·Node *child;
- double dists[50];
+ bio·Node *child, *sorted, *sibling;
if (!root->nchild) return 0;
- Assert(root->nchild < arrlen(dists));
- for (i = 0, child = root->child; child != nil; child = child->sibling, i++) {
+ // ladderize below
+ for (child = root->child; child != nil; child = child->sibling) {
if (err = phylo·ladderize(child), err) {
errorf("ladderize: failure at '%s'", child->name);
return 1;
}
- dists[i] = child->dist;
}
+ // ladderize yourself
+ sorted = nil;
+ child = root->child;
+ while (child != nil) {
+ sibling = child->sibling;
+ sortnodelist(&sorted, child);
+ child = sibling;
+ }
+ root->child = sorted;
+
return 0;
}
diff --git a/sys/libbio/test.c b/sys/libbio/test.c
index dddecd4..2efcdb6 100644
--- a/sys/libbio/test.c
+++ b/sys/libbio/test.c
@@ -109,6 +109,7 @@ test·newick()
fd[1] = io·open("/home/nolln/root/data/test/zika.proc.nwk", "w");
err = bio·readnewick(rdr, fd[0], al, mem, &t);
+ phylo·ladderize(t.root);
printf("Loaded tree with %d leafs and %d nodes\n", t.nleaf, t.nnode);
err = bio·writenewick(t, wtr, fd[1]);