Debian Codegraph @

src/dr/evomodel/tree/Convergence.java

raw symbols SCIP
1 /*
2 * Convergence.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 /**
27 *
28 */
29 package dr.evomodel.tree;
30
31 import dr.evolution.tree.NodeRef;
32 import dr.evolution.tree.Tree;
33 import dr.evolution.util.Taxon;
34
35 import java.io.BufferedReader;
36 import java.io.FileNotFoundException;
37 import java.io.FileReader;
38 import java.io.IOException;
39 import java.util.*;
40
41 /**
42 * @author shhn001
43 */
44 public class Convergence {
45
46
47 double distance = Double.MAX_VALUE;
48
49 public final static String LOG_CONVERGENCE = "LogConvergence";
50
51 public final static String TREE_FILE_NAME = "treeFilename";
52
53 public final static String REFERENCE_FILE_NAME = "referenceFilename";
54
55 public final static String CHECK_EVERY = "checkEvery";
56
57 private Tree tree = null;
58
59 private long trees = 0;
60
61 private HashMap<BitSet, Double> referenceCladeFrequencies = null;
62
63 private HashMap<BitSet, Double> cladeOccurences = null;
64
65 private HashMap<String, Integer> taxonMap;
66
67 private String[] taxonArray;
68
69 private String referenceTreeFileName;
70
71 private double logEvery;
72
73 /**
74 *
75 */
76 public Convergence(Tree tree, int logEvery, String referenceTreeFileName) {
77
78 this.logEvery = logEvery;
79 this.referenceTreeFileName = referenceTreeFileName;
80 this.tree = tree;
81
82 taxonMap = new HashMap<String, Integer>();
83 cladeOccurences = new HashMap<BitSet, Double>();
84
85 startLogging();
86 }
87
88 private void startLogging() {
89
90 parseReferenceFile(referenceTreeFileName);
91
92 taxonArray = new String[taxonMap.size()];
93 Set<String> taxon = taxonMap.keySet();
94 for (String s : taxon) {
95 taxonArray[taxonMap.get(s)] = s;
96 }
97 }
98
99 private void parseReferenceFile(String log) {
100 FileReader fr;
101 BufferedReader br;
102 String line;
103 referenceCladeFrequencies = new HashMap<BitSet, Double>();
104
105 try {
106 fr = new FileReader(log);
107 br = new BufferedReader(fr);
108
109 line = br.readLine();
110 String[] tokens = line.split("\\s++");
111 long total = Long.parseLong(tokens[0]);
112
113 while ((line = br.readLine()) != null) {
114 String[] taxa = getTaxa(line);
115 BitSet split = new BitSet();
116 for (String t : taxa) {
117 if (!taxonMap.containsKey(t)) {
118 taxonMap.put(t, taxonMap.size());
119 }
120 split.set(taxonMap.get(t));
121 }
122 tokens = line.split("\\s++");
123 double frequency = (100.0 * Long.parseLong(tokens[0])) / (double) total;
124 referenceCladeFrequencies.put(split,
125 frequency);
126 }
127
128 br.close();
129 fr.close();
130 } catch (FileNotFoundException e) {
131 // TODO Auto-generated catch block
132 e.printStackTrace();
133 } catch (IOException e) {
134 // TODO Auto-generated catch block
135 e.printStackTrace();
136 }
137 }
138
139 private String[] getTaxa(String line) {
140 line = line.substring(line.indexOf('(') + 1, line.indexOf(')'));
141 String[] taxa = line.split(",");
142 return taxa;
143 }
144
145 public double log(long state) {
146
147 if (logEvery <= 0 || ((state % logEvery) == 0)) {
148
149 addTree(tree);
150
151 distance = getMaxCladeDistance();
152 }
153
154 return distance;
155 }
156
157 private void addTree(Tree tree) {
158 trees++;
159 List<BitSet> splits = getSplits(tree);
160
161 for (BitSet s : splits) {
162 if (!cladeOccurences.containsKey(s)) {
163 cladeOccurences.put(s, 1.0);
164 } else {
165 cladeOccurences.put(s, cladeOccurences.get(s) + 1);
166 }
167 }
168 }
169
170 private List<BitSet> getSplits(Tree tree) {
171 List<BitSet> splits = new ArrayList<BitSet>();
172
173 NodeRef root = tree.getRoot();
174 fillSplits(splits, root, tree);
175
176 return splits;
177 }
178
179 private BitSet fillSplits(List<BitSet> splits, NodeRef root, Tree tree) {
180 BitSet split = new BitSet();
181
182 if (!tree.isExternal(root)) {
183 split.or(fillSplits(splits, tree.getChild(root, 0), tree));
184 split.or(fillSplits(splits, tree.getChild(root, 1), tree));
185 splits.add(split);
186 } else {
187 Taxon taxon = tree.getNodeTaxon(root);
188 String name = taxon.getId();
189 split.set(taxonMap.get(name));
190 }
191
192 return split;
193
194 }
195
196 private double getMaxCladeDistance() {
197
198 return getMaxDeviation();
199 }
200
201 private double getMaxDeviation() {
202 double[] deviation = getDeviations();
203
204 double max = 0;
205 for (double m : deviation) {
206 if (m > max) {
207 max = m;
208 }
209 }
210 return max;
211 }
212
213 private double[] getDeviations() {
214 double[] deviations = new double[Math.max(referenceCladeFrequencies
215 .size(), cladeOccurences.size())];
216
217 Set<BitSet> keys = null;
218 if (referenceCladeFrequencies.size() < cladeOccurences.size()) {
219 keys = cladeOccurences.keySet();
220 } else {
221 keys = referenceCladeFrequencies.keySet();
222 }
223
224 Iterator<BitSet> it = keys.iterator();
225 for (int i = 0; i < deviations.length; i++) {
226 BitSet k = it.next();
227 double r = 0.0;
228 if (referenceCladeFrequencies.containsKey(k)) {
229 r = referenceCladeFrequencies.get(k);
230 }
231 double o = 0.0;
232 if (cladeOccurences.containsKey(k)) {
233 o = cladeOccurences.get(k);
234 }
235 deviations[i] = Math.abs(r - ((o * 100.0) / (double) trees));
236 }
237
238 return deviations;
239 }
240
241 }