Debian Codegraph @

src/dr/evomodel/tree/TreeLogger.java

raw symbols SCIP
1 /*
2 * TreeLogger.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.evomodel.tree;
27
28 import dr.app.tools.NexusExporter;
29 import dr.evolution.tree.*;
30 import dr.inference.loggers.LogFormatter;
31 import dr.inference.loggers.MCLogger;
32 import dr.util.Keywordable;
33
34 import java.text.NumberFormat;
35 import java.util.*;
36
37 /**
38 * A logger that logs tree and clade frequencies.
39 *
40 * @author Andrew Rambaut
41 * @author Alexei Drummond
42 * @version $Id: TreeLogger.java,v 1.25 2006/09/05 13:29:34 rambaut Exp $
43 */
44 public class TreeLogger extends MCLogger {
45
46 private Tree tree;
47 private BranchRates branchRates = null;
48
49 private TreeAttributeProvider[] treeAttributeProviders;
50 private TreeTraitProvider[] treeTraitProviders;
51
52 private boolean nexusFormat = false;
53 public boolean usingRates = false;
54 public boolean substitutions = false;
55 private final Map<String, Integer> idMap = new HashMap<String, Integer>();
56 private final List<String> taxaIds = new ArrayList<String>();
57 private boolean mapNames = true;
58
59 /*private double normaliseMeanRateTo = Double.NaN;
60 boolean normaliseMeanRate = false;*/
61
62 private NumberFormat format;
63 private LogUpon condition = null;
64
65 /**
66 * Interface to indicate when to log a tree
67 */
68 public interface LogUpon {
69 /**
70 *
71 * @param state
72 * @return True if log tree of this state.
73 */
74 boolean logNow(long state);
75 }
76
77 public TreeLogger(Tree tree, LogFormatter formatter, int logEvery, boolean nexusFormat,
78 boolean sortTranslationTable, boolean mapNames) {
79
80 this(tree, null, null, null, formatter, logEvery, nexusFormat, sortTranslationTable, mapNames, null, null/*, Double.NaN*/);
81 }
82
83 public TreeLogger(Tree tree, LogFormatter formatter, int logEvery, boolean nexusFormat,
84 boolean sortTranslationTable, boolean mapNames, NumberFormat format) {
85
86 this(tree, null, null, null, formatter, logEvery, nexusFormat, sortTranslationTable, mapNames, format, null/*, Double.NaN*/);
87 }
88
89 public TreeLogger(Tree tree, BranchRates branchRates,
90 TreeAttributeProvider[] treeAttributeProviders,
91 TreeTraitProvider[] treeTraitProviders,
92 LogFormatter formatter, int logEvery, boolean nexusFormat,
93 boolean sortTranslationTable, boolean mapNames, NumberFormat format,
94 TreeLogger.LogUpon condition) {
95
96 super(formatter, logEvery, false);
97
98 this.condition = condition;
99
100 /*this.normaliseMeanRateTo = normaliseMeanRateTo;
101 if(!Double.isNaN(normaliseMeanRateTo)) {
102 normaliseMeanRate = true;
103 }*/
104
105 this.nexusFormat = nexusFormat;
106 // if not NEXUS, can't map names
107 this.mapNames = mapNames && nexusFormat;
108
109 this.branchRates = branchRates;
110
111 this.treeAttributeProviders = treeAttributeProviders;
112 this.treeTraitProviders = treeTraitProviders;
113
114 if (this.branchRates != null) {
115 this.substitutions = true;
116 }
117 this.tree = tree;
118
119 for (int i = 0; i < tree.getTaxonCount(); i++) {
120 taxaIds.add(tree.getTaxon(i).getId());
121 }
122 if (sortTranslationTable) {
123 Collections.sort(taxaIds);
124 }
125
126 int k = 1;
127 for (String taxaId : taxaIds) {
128 idMap.put(taxaId, k);
129 k += 1;
130 }
131
132 this.format = format;
133 }
134
135 public void startLogging() {
136
137 if (nexusFormat) {
138 int taxonCount = tree.getTaxonCount();
139 logLine("#NEXUS");
140 logLine("");
141 logLine("Begin taxa;");
142 logLine("\tDimensions ntax=" + taxonCount + ";");
143 logLine("\tTaxlabels");
144
145 for (String taxaId : taxaIds) {
146 logLine("\t\t" + cleanTaxonName(taxaId));
147 }
148
149 logLine("\t\t;");
150 logLine("End;");
151 logLine("");
152 logLine("Begin trees;");
153
154 if (tree instanceof Keywordable) {
155 List<String> keywords = ((Keywordable)tree).getKeywords();
156 if (keywords.size() > 0) {
157 StringBuffer sb = new StringBuffer("[keywords:");
158 for (String keyword: keywords) {
159 sb.append(" ");
160 sb.append(keyword);
161 }
162 sb.append("]");
163 logLine(sb.toString());
164 }
165 }
166
167 if (mapNames) {
168 // This is needed if the trees use numerical taxon labels
169 logLine("\tTranslate");
170 int k = 1;
171 for (String taxaId : taxaIds) {
172 if (k < taxonCount) {
173 logLine("\t\t" + k + " " + cleanTaxonName(taxaId) + ",");
174 } else {
175 logLine("\t\t" + k + " " + cleanTaxonName(taxaId));
176 }
177 k += 1;
178 }
179 logLine("\t\t;");
180 }
181 }
182 }
183
184 private String cleanTaxonName(String taxaId) {
185 if (taxaId.matches(NexusExporter.SPECIAL_CHARACTERS_REGEX)) {
186 if (taxaId.contains("\'")) {
187 if (taxaId.contains("\"")) {
188 throw new RuntimeException("Illegal taxon name - contains both single and double quotes");
189 }
190
191 return "\"" + taxaId + "\"";
192 }
193
194 return "\'" + taxaId + "\'";
195 }
196 return taxaId;
197 }
198
199 public void log(long state) {
200
201 /*if(normaliseMeanRate) {
202 NormaliseMeanTreeRate.analyze(tree, normaliseMeanRateTo);
203 }*/
204
205 final boolean doIt = condition != null ? condition.logNow(state) :
206 (logEvery < 0 || ((state % logEvery) == 0));
207
208 if ( doIt ) {
209 StringBuffer buffer = new StringBuffer("tree STATE_");
210 buffer.append(state);
211 if (treeAttributeProviders != null) {
212 boolean hasAttribute = false;
213 for (TreeAttributeProvider tap : treeAttributeProviders) {
214 String[] attributeLabel = tap.getTreeAttributeLabel();
215 String[] attributeValue = tap.getAttributeForTree(tree);
216 for (int i = 0; i < attributeLabel.length; i++) {
217 if (!hasAttribute) {
218 buffer.append(" [&");
219 hasAttribute = true;
220 } else {
221 buffer.append(",");
222 }
223 buffer.append(attributeLabel[i]);
224 buffer.append("=");
225 buffer.append(attributeValue[i]);
226 }
227 }
228 if (hasAttribute) {
229 buffer.append("]");
230 }
231 }
232
233 buffer.append(" = [&R] ");
234
235 if (substitutions) {
236 TreeUtils.newick(tree, tree.getRoot(), false, TreeUtils.BranchLengthType.LENGTHS_AS_SUBSTITUTIONS,
237 format, branchRates, treeTraitProviders, idMap, buffer);
238 } else {
239 //System.out.println(treeTraitProviders.length);
240 TreeUtils.newick(tree, tree.getRoot(), !mapNames, TreeUtils.BranchLengthType.LENGTHS_AS_TIME,
241 format, null, treeTraitProviders, idMap, buffer);
242 }
243
244 buffer.append(";");
245 logLine(buffer.toString());
246 }
247 }
248
249 public void stopLogging() {
250 logLine("End;");
251 super.stopLogging();
252 }
253
254 public Tree getTree() {
255 return tree;
256 }
257
258 public void setTree(Tree tree) {
259 this.tree = tree;
260 }
261
262 public TreeAttributeProvider[] getTreeAttributeProviders() {
263 return this.treeAttributeProviders;
264 }
265
266 public TreeTraitProvider[] getTreeTraitProviders() {
267 return this.treeTraitProviders;
268 }
269
270 public BranchRates getBranchRates() {
271 return this.branchRates;
272 }
273
274 }