| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 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 |
|
| 60 |
|
| 61 |
|
| 62 |
private NumberFormat format; |
| 63 |
private LogUpon condition = null; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public interface LogUpon { |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 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); |
| 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); |
| 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 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
this.nexusFormat = nexusFormat; |
| 106 |
|
| 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 |
|
| 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 |
|
| 202 |
|
| 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 |
|
| 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 |
} |