Debian Codegraph @

src/dr/evolution/tree/LineageCountThroughTime.java

raw symbols SCIP
1 /*
2 * LineageCountThroughTime.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.stats.Variate;
29 import jebl.evolution.graphs.Node;
30 import jebl.evolution.io.ImportException;
31 import jebl.evolution.io.NewickImporter;
32 import jebl.evolution.io.NexusImporter;
33 import jebl.evolution.io.TreeImporter;
34 import jebl.evolution.trees.RootedTree;
35
36 import java.io.BufferedReader;
37 import java.io.FileReader;
38 import java.io.IOException;
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.List;
42
43 /**
44 * @author Alexei Drummond
45 * @author Andrew Rambaut
46 */
47 public class LineageCountThroughTime {
48
49 public static Variate[] getLTT(String treeFile,
50 double minTime,
51 double maxTime,
52 int binCount,
53 int skip // number of trees to skip
54 ) throws IOException, ImportException {
55
56 double delta = (maxTime - minTime) / (binCount - 1);
57
58 BufferedReader reader = new BufferedReader(new FileReader(treeFile));
59
60 String line = reader.readLine();
61
62 TreeImporter importer;
63 if (line.toUpperCase().startsWith("#NEXUS")) {
64 importer = new NexusImporter(reader);
65 } else {
66 importer = new NewickImporter(reader, false);
67 }
68
69 int state = 0;
70
71 while (importer.hasTree() && state < skip) {
72 importer.importNextTree();
73 state += 1;
74 }
75
76 // the age of the end of this group
77 List<double[]> branchingTimes = new ArrayList<double[]>();
78 state = 0;
79
80 while (importer.hasTree()) {
81 RootedTree tree = (RootedTree) importer.importNextTree();
82
83 double[] bt = new double[tree.getInternalNodes().size()];
84
85 int i = 0;
86 for (Node node : tree.getInternalNodes()) {
87 bt[i] = tree.getHeight(node);
88 i++;
89 }
90 Arrays.sort(bt);
91
92 branchingTimes.add(bt);
93 state += 1;
94 }
95
96 Variate[] bins = new Variate[binCount];
97 double height = 0;
98
99 double n = branchingTimes.get(0).length;
100
101 for (int k = 0; k < binCount; k++) {
102 bins[k] = new Variate.D();
103
104 if (height >= 0.0 && height <= maxTime) {
105 for (state = 0; state < branchingTimes.size(); state++) {
106 int index = 0;
107 while (index < branchingTimes.get(state).length && branchingTimes.get(state)[index] < height) {
108 index += 1;
109 }
110
111 double lineageCount = 1;
112 if (index < branchingTimes.get(state).length) {
113 lineageCount = n - index + 1;
114 }
115 bins[k].add(lineageCount);
116 }
117 }
118 height += delta;
119 }
120
121 Variate xData = new Variate.D();
122 Variate yDataMean = new Variate.D();
123 Variate yDataMedian = new Variate.D();
124 Variate yDataUpper = new Variate.D();
125 Variate yDataLower = new Variate.D();
126
127 double t = minTime;
128 for (Variate bin : bins) {
129 xData.add(t);
130 if (bin.getCount() > 0) {
131 yDataMean.add(bin.getMean());
132 yDataMedian.add(bin.getQuantile(0.5));
133 yDataLower.add(bin.getQuantile(0.025));
134 yDataUpper.add(bin.getQuantile(0.975));
135 } else {
136 yDataMean.add(Double.NaN);
137 yDataMedian.add(Double.NaN);
138 yDataLower.add(Double.NaN);
139 yDataUpper.add(Double.NaN);
140 }
141 t += delta;
142 }
143
144 return new Variate[]{xData, yDataMean};
145 }
146 }