Debian Codegraph @

src/dr/app/pathogen/TemporalStress.java

raw symbols SCIP
1 /*
2 * TemporalStress.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.app.pathogen;
27
28 import java.util.*;
29
30 import dr.evolution.tree.TreeUtils;
31 import dr.stats.DiscreteStatistics;
32 import dr.math.UnivariateFunction;
33 import dr.math.UnivariateMinimum;
34 import dr.evolution.tree.*;
35 import dr.evolution.util.Taxon;
36
37 /**
38 * @author Andrew Rambaut
39 * @version $Id$
40 */
41 public class TemporalStress {
42
43 public static Set<Taxon> annotateStress(MutableTree tree, NodeRef node) {
44 Set<Taxon> taxa = new HashSet<Taxon>();
45
46 if (!tree.isExternal(node)) {
47 for (int i = 0; i < tree.getChildCount(node); i++) {
48 NodeRef child = tree.getChild(node, i);
49 taxa.addAll(annotateStress(tree, child));
50 }
51
52 if (taxa.size() > 2) {
53 Tree subtree = null;
54
55 double stress = findGlobalRoot(subtree);
56 tree.setNodeAttribute(node, "stress", stress);
57
58 }
59 } else {
60 taxa.add(tree.getNodeTaxon(node));
61 }
62
63 return taxa;
64 }
65
66
67 private static double findGlobalRoot(Tree source) {
68
69 FlexibleTree bestTree = new FlexibleTree(source);
70 double minF = findLocalRoot(bestTree);
71
72 for (int i = 0; i < source.getNodeCount(); i++) {
73 FlexibleTree tmpTree = new FlexibleTree(source);
74 NodeRef node = tmpTree.getNode(i);
75 if (!tmpTree.isRoot(node)) {
76 double length = tmpTree.getBranchLength(node);
77 tmpTree.changeRoot(node, length * 0.5, length * 0.5);
78
79 double f = findLocalRoot(tmpTree);
80 if (f < minF) {
81 minF = f;
82 bestTree = tmpTree;
83 }
84 }
85 }
86 return minF;
87 }
88
89 private static double findLocalRoot(final FlexibleTree tree) {
90
91 NodeRef node1 = tree.getChild(tree.getRoot(), 0);
92 NodeRef node2 = tree.getChild(tree.getRoot(), 1);
93
94 final double length1 = tree.getBranchLength(node1);
95 final double length2 = tree.getBranchLength(node2);
96
97 final double sumLength = length1 + length2;
98
99 final Set<NodeRef> tipSet1 = TreeUtils.getExternalNodes(tree, node1);
100 final Set<NodeRef> tipSet2 = TreeUtils.getExternalNodes(tree, node2);
101
102 final double[] y = new double[tree.getExternalNodeCount()];
103
104 UnivariateFunction f = new UnivariateFunction() {
105 public double evaluate(double argument) {
106 double l1 = argument * sumLength;
107
108 for (NodeRef tip : tipSet1) {
109 y[tip.getNumber()] = getRootToTipDistance(tree, tip) - length1 + l1;
110 }
111
112 double l2 = (1.0 - argument) * sumLength;
113
114 for (NodeRef tip : tipSet2) {
115 y[tip.getNumber()] = getRootToTipDistance(tree, tip) - length2 + l2;
116 }
117
118 return DiscreteStatistics.variance(y);
119 }
120
121 public double getLowerBound() { return 0; }
122 public double getUpperBound() { return 1.0; }
123 };
124
125 UnivariateMinimum minimum = new UnivariateMinimum();
126
127 double x = minimum.findMinimum(f);
128
129 double fminx = minimum.fminx;
130
131 double l1 = x * sumLength;
132 double l2 = (1.0 - x) * sumLength;
133
134 tree.setBranchLength(node1, l1);
135 tree.setBranchLength(node2, l2);
136
137 return fminx;
138 }
139
140 private static double getRootToTipDistance(Tree tree, NodeRef node) {
141 double distance = 0;
142 while (node != null) {
143 distance += tree.getBranchLength(node);
144 node = tree.getParent(node);
145 }
146 return distance;
147 }
148
149
150 }