Debian Codegraph @

src/dr/evolution/tree/TreeMetrics.java

raw symbols SCIP
1 /*
2 * TreeMetrics.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.evolution.tree;
27
28 import dr.evolution.io.NewickImporter;
29 import dr.evolution.util.TaxonList;
30
31 import java.io.FileReader;
32
33 /**
34 * various utility functions on trees.
35 *
36 * @version $Id: TreeMetrics.java,v 1.7 2005/05/24 20:25:57 rambaut Exp $
37 *
38 * @author Alexei Drummond
39 * @author Korbinian Strimmer
40 */
41 @Deprecated
42 public class TreeMetrics {
43
44 /**
45 * computes Robinson-Foulds (1981) distance between two trees
46 *
47 * @param t1 tree 1
48 * @param t2 tree 2
49 *
50 * Definition: Assuming that t1 is the reference tree, let fn be the
51 * false negatives, i.e. the number of edges in t1 missing in t2,
52 * and fp the number of false positives, i.e. the number of edges
53 * in t2 missing in t1. The RF distance is then (fn + fp)/2
54 */
55 public static double getRobinsonFouldsDistance(Tree t1, Tree t2)
56 {
57 SplitSystem s1 = SplitUtils.getSplits(t1);
58
59 return getRobinsonFouldsDistance(s1, t2);
60 }
61
62
63 /**
64 * computes Robinson-Foulds (1981) distance between two trees
65 *
66 * @param s1 tree 1 (as represented by a SplitSystem)
67 * @param t2 tree 2
68 */
69 public static double getRobinsonFouldsDistance(SplitSystem s1, Tree t2)
70 {
71 TaxonList taxonList = s1.getTaxonList();
72 SplitSystem s2 = SplitUtils.getSplits(taxonList, t2);
73
74 if (s1.getLabelCount() != s2.getLabelCount())
75 throw new IllegalArgumentException("Number of labels must be the same!");
76
77 int ns1 = s1.getSplitCount();
78 int ns2 = s2.getSplitCount();
79
80 // number of splits in t1 missing in t2
81 int fn = 0;
82 for (int i = 0; i < ns1; i++)
83 {
84 if (!s2.hasSplit(s1.getSplit(i))) fn++;
85 }
86
87 // number of splits in t2 missing in t1
88 int fp = 0;
89 for (int i = 0; i < ns2; i++)
90 {
91 if (!s1.hasSplit(s2.getSplit(i))) fp++;
92 }
93
94
95 return 0.5*((double) fp + (double) fn);
96 }
97
98 /**
99 * computes Robinson-Foulds (1981) distance between two trees
100 * rescaled to a number between 0 and 1
101 *
102 * @param t1 tree 1
103 * @param t2 tree 2
104 */
105 public static double getRobinsonFouldsRescaledDistance(Tree t1, Tree t2)
106 {
107 SplitSystem s1 = SplitUtils.getSplits(t1);
108
109 return getRobinsonFouldsRescaledDistance(s1, t2);
110 }
111
112
113 /**
114 * computes Robinson-Foulds (1981) distance between two trees
115 * rescaled to a number between 0 and 1
116 *
117 * @param s1 tree 1 (as represented by a SplitSystem)
118 * @param t2 tree 2
119 */
120 public static double getRobinsonFouldsRescaledDistance(SplitSystem s1, Tree t2)
121 {
122 return getRobinsonFouldsDistance(s1, t2)/(double) s1.getSplitCount();
123 }
124
125 /**
126 * Analyze trace
127 * @param trees an array of trees
128 * @param update the step size between instances of tree
129 */
130 private static void analyze(Tree[] trees, int update) {
131
132 TaxonList masterList = trees[0];
133
134 System.out.println("Calculating splits...");
135 SplitSystem[] splits = new SplitSystem[trees.length];
136 for (int i =0; i < splits.length; i++) {
137 splits[i] = SplitUtils.getSplits(masterList, trees[i]);
138 }
139
140 int maxOffset = MAX_OFFSET;
141 int samples = trees.length;
142 if ((samples/3) < maxOffset) {
143 maxOffset = (samples/3);
144 }
145
146 double[] meanDistances = new double[maxOffset];
147
148 System.out.println("Calculating mean distance per lag...");
149 for (int i=0; i < maxOffset; i++) {
150 meanDistances[i] = 0;
151 for (int j = 0; j < samples-i; j++) {
152 double distance = getRobinsonFouldsRescaledDistance(splits[i], trees[j]);
153 meanDistances[i] += distance;
154 }
155 meanDistances[i] /= ((double) samples-i);
156 System.out.println(meanDistances[i]);
157 }
158
159 }
160
161 public static final void main(String[] args) throws Exception {
162 FileReader reader = new FileReader(args[0]);
163 NewickImporter importer = new NewickImporter(reader);
164
165 Tree[] trees = importer.importTrees(null);
166 System.out.println("Imported " + trees.length + " trees...");
167 analyze(trees, 1000);
168 }
169
170 private static int MAX_OFFSET = 1000;
171 }