| 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 |
|
| 27 |
|
| 28 |
|
| 29 |
package dr.evomodel.tree; |
| 30 |
|
| 31 |
import dr.evolution.tree.Tree; |
| 32 |
import dr.evolution.tree.TreeUtils; |
| 33 |
import dr.inference.loggers.LogFormatter; |
| 34 |
import dr.inference.loggers.MCLogger; |
| 35 |
import dr.inference.loggers.MLLogger; |
| 36 |
import dr.inference.loggers.TabDelimitedFormatter; |
| 37 |
import dr.inferencexml.loggers.LoggerParser; |
| 38 |
import dr.xml.*; |
| 39 |
|
| 40 |
import java.io.*; |
| 41 |
import java.util.*; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class TreeSummary extends MCLogger { |
| 47 |
|
| 48 |
public final static String LOG_TREE = "TreeSummary"; |
| 49 |
|
| 50 |
public final static String OUTPUT_FILE_NAME = "file"; |
| 51 |
|
| 52 |
public final static String CHECK_EVERY = "checkEvery"; |
| 53 |
|
| 54 |
private Tree tree = null; |
| 55 |
|
| 56 |
private HashMap<String, Integer> taxa = null; |
| 57 |
|
| 58 |
private HashMap<String, Integer> treeOccurences = null; |
| 59 |
|
| 60 |
private String outputFilename; |
| 61 |
|
| 62 |
public TreeSummary(Tree tree, LogFormatter formatter, int logEvery, |
| 63 |
String outputFilename) { |
| 64 |
|
| 65 |
super(formatter, logEvery, false); |
| 66 |
this.tree = tree; |
| 67 |
this.outputFilename = outputFilename; |
| 68 |
|
| 69 |
treeOccurences = new HashMap<String, Integer>(); |
| 70 |
} |
| 71 |
|
| 72 |
public void startLogging() { |
| 73 |
|
| 74 |
File f = new File(outputFilename); |
| 75 |
if (f.exists()) { |
| 76 |
f.delete(); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
private HashMap<String, Integer> getTaxa() { |
| 81 |
int n = tree.getTaxonCount(); |
| 82 |
|
| 83 |
List<String> l = new ArrayList<String>(); |
| 84 |
for (int i = 0; i < n; i++) { |
| 85 |
l.add(tree.getTaxonId(i)); |
| 86 |
} |
| 87 |
Collections.sort(l); |
| 88 |
|
| 89 |
HashMap<String, Integer> map = new HashMap<String, Integer>(); |
| 90 |
for (int i = 1; i <= n; i++) { |
| 91 |
map.put(l.get(i - 1), i); |
| 92 |
} |
| 93 |
|
| 94 |
return map; |
| 95 |
} |
| 96 |
|
| 97 |
public void log(long state) { |
| 98 |
|
| 99 |
if (logEvery <= 0 || ((state % logEvery) == 0)) { |
| 100 |
if (state == 0) { |
| 101 |
taxa = getTaxa(); |
| 102 |
} |
| 103 |
|
| 104 |
addTree(tree); |
| 105 |
|
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
private void addTree(Tree tree) { |
| 110 |
String newick = TreeUtils.uniqueNewick(tree, tree.getRoot()); |
| 111 |
newick = replaceTaxa(newick); |
| 112 |
|
| 113 |
if (treeOccurences.containsKey(newick)) { |
| 114 |
treeOccurences.put(newick, treeOccurences.get(newick) + 1); |
| 115 |
} else { |
| 116 |
treeOccurences.put(newick, 1); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
private String replaceTaxa(String newick) { |
| 121 |
String s = newick; |
| 122 |
int start = 0; |
| 123 |
int end; |
| 124 |
|
| 125 |
for (int i = 0; i < s.length(); i++) { |
| 126 |
char c = s.charAt(i); |
| 127 |
|
| 128 |
if (c == '(') { |
| 129 |
start = i + 1; |
| 130 |
} else if (c == ')') { |
| 131 |
end = i; |
| 132 |
if (end - start > 0) { |
| 133 |
String taxon = s.substring(start, end); |
| 134 |
int index = taxa.get(taxon); |
| 135 |
s = s.substring(0, start) + index + s.substring(end); |
| 136 |
i = start + (int) Math.log10(index); |
| 137 |
} |
| 138 |
start = i + 1; |
| 139 |
} else if (c == ',') { |
| 140 |
end = i; |
| 141 |
if (end - start > 0) { |
| 142 |
String taxon = s.substring(start, end); |
| 143 |
int index = taxa.get(taxon); |
| 144 |
s = s.substring(0, start) + index + s.substring(end); |
| 145 |
i = start + (int) Math.log10(index); |
| 146 |
} |
| 147 |
|
| 148 |
start = i + 1; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return s; |
| 153 |
} |
| 154 |
|
| 155 |
public void stopLogging() { |
| 156 |
printTrees(); |
| 157 |
|
| 158 |
logLine("End;"); |
| 159 |
super.stopLogging(); |
| 160 |
} |
| 161 |
|
| 162 |
private void printTrees() { |
| 163 |
Set<String> keys = treeOccurences.keySet(); |
| 164 |
|
| 165 |
List<Sample> samples = new ArrayList<Sample>(); |
| 166 |
for (String t : keys) { |
| 167 |
samples.add(new Sample(t, treeOccurences.get(t))); |
| 168 |
} |
| 169 |
Collections.sort(samples); |
| 170 |
|
| 171 |
try { |
| 172 |
FileWriter fw = new FileWriter(outputFilename); |
| 173 |
BufferedWriter writer = new BufferedWriter(fw); |
| 174 |
|
| 175 |
writer.write("Taxa"); |
| 176 |
writer.newLine(); |
| 177 |
writer.newLine(); |
| 178 |
|
| 179 |
Set<String> taxon = taxa.keySet(); |
| 180 |
for (String t : taxon) { |
| 181 |
int i = taxa.get(t); |
| 182 |
writer.write(i + "\t=\t" + t); |
| 183 |
writer.newLine(); |
| 184 |
writer.flush(); |
| 185 |
} |
| 186 |
|
| 187 |
writer.newLine(); |
| 188 |
writer.newLine(); |
| 189 |
writer.newLine(); |
| 190 |
|
| 191 |
for (Sample s : samples) { |
| 192 |
writer.write(s.samples + "\t" + s.tree); |
| 193 |
writer.newLine(); |
| 194 |
writer.flush(); |
| 195 |
} |
| 196 |
|
| 197 |
writer.close(); |
| 198 |
fw.close(); |
| 199 |
} catch (IOException e) { |
| 200 |
|
| 201 |
e.printStackTrace(); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
public static XMLObjectParser PARSER = new LoggerParser() { |
| 206 |
|
| 207 |
public String getParserName() { |
| 208 |
return LOG_TREE; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 215 |
|
| 216 |
String outputFile = xo.getStringAttribute(OUTPUT_FILE_NAME); |
| 217 |
|
| 218 |
int checkEvery = 1; |
| 219 |
if (xo.hasAttribute(CHECK_EVERY)) { |
| 220 |
checkEvery = xo.getIntegerAttribute(CHECK_EVERY); |
| 221 |
} |
| 222 |
|
| 223 |
Tree tree = (Tree) xo.getChild(Tree.class); |
| 224 |
|
| 225 |
final PrintWriter pw = getLogFile(xo, getParserName()); |
| 226 |
|
| 227 |
LogFormatter formatter = new TabDelimitedFormatter(pw); |
| 228 |
|
| 229 |
return new TreeSummary(tree, formatter, checkEvery, outputFile); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 236 |
return rules; |
| 237 |
} |
| 238 |
|
| 239 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 240 |
new StringAttributeRule(OUTPUT_FILE_NAME, |
| 241 |
"name of a tree log file", "ds.trees"), |
| 242 |
AttributeRule.newIntegerRule(CHECK_EVERY, true),}; |
| 243 |
|
| 244 |
public String getParserDescription() { |
| 245 |
return "Calculates the tree probabilities on the flow."; |
| 246 |
} |
| 247 |
|
| 248 |
public String getExample() { |
| 249 |
return "<!-- The " + getParserName() |
| 250 |
+ " element takes a treeModel to be logged -->\n" + "<" |
| 251 |
+ getParserName() + " " + LOG_EVERY + "=\"100\" " |
| 252 |
+ OUTPUT_FILE_NAME + "=\"log.trees\" " |
| 253 |
+ " <treeModel idref=\"treeModel1\"/>\n" + "</" |
| 254 |
+ getParserName() + ">\n"; |
| 255 |
} |
| 256 |
|
| 257 |
public Class getReturnType() { |
| 258 |
return MLLogger.class; |
| 259 |
} |
| 260 |
}; |
| 261 |
|
| 262 |
class Sample implements Comparable<Sample> { |
| 263 |
|
| 264 |
String tree; |
| 265 |
int samples; |
| 266 |
|
| 267 |
public Sample(String tree, int samples) { |
| 268 |
super(); |
| 269 |
this.tree = tree; |
| 270 |
this.samples = samples; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
public int compareTo(Sample o) { |
| 277 |
return o.samples - samples; |
| 278 |
} |
| 279 |
|
| 280 |
} |
| 281 |
|
| 282 |
} |