| 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.evolution.tree.NodeRef; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evolution.tree.TreeTraitProvider; |
| 31 |
import dr.inference.model.Parameter; |
| 32 |
import dr.xml.*; |
| 33 |
|
| 34 |
import java.util.ArrayList; |
| 35 |
import java.util.List; |
| 36 |
import java.util.Map; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class NodeTraitLogger extends TreeTraitProvider.Helper { |
| 42 |
|
| 43 |
public static final String TRAIT_LOGGER = "logAllTraits"; |
| 44 |
|
| 45 |
private final TreeModel treeModel; |
| 46 |
|
| 47 |
public NodeTraitLogger(TreeModel treeModel) { |
| 48 |
this.treeModel = treeModel; |
| 49 |
|
| 50 |
throw new RuntimeException("NodeTraitLogger has not been fully converted to using TreeTraitProvider"); |
| 51 |
|
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
public static String[] getAllNodeTraitLabels(TreeModel tree) { |
| 56 |
|
| 57 |
Map<String, Parameter> traits = tree.getTraitMap(tree.getRoot()); |
| 58 |
List<String> labels = new ArrayList<String>(); |
| 59 |
for (Map.Entry<String, Parameter> stringParameterEntry : traits.entrySet()) { |
| 60 |
|
| 61 |
Parameter traitParameter = stringParameterEntry.getValue(); |
| 62 |
if (traitParameter.getDimension() == 1) |
| 63 |
labels.add(stringParameterEntry.getKey()); |
| 64 |
else { |
| 65 |
for (int i = 1; i <= traitParameter.getDimension(); i++) |
| 66 |
labels.add(stringParameterEntry.getKey() + i); |
| 67 |
} |
| 68 |
} |
| 69 |
return labels.toArray(new String[labels.size()]); |
| 70 |
} |
| 71 |
|
| 72 |
public static String[] getAllNodeTraitValues(TreeModel tree, NodeRef node) { |
| 73 |
|
| 74 |
Map<String, Parameter> traits = tree.getTraitMap(node); |
| 75 |
List<String> values = new ArrayList<String>(); |
| 76 |
for( Map.Entry<String, Parameter> stringParameterEntry : traits.entrySet() ) { |
| 77 |
|
| 78 |
Parameter traitParameter = stringParameterEntry.getValue(); |
| 79 |
|
| 80 |
for (int i = 0; i < traitParameter.getDimension(); i++) |
| 81 |
values.add(Double.toString(traitParameter.getParameterValue(i))); |
| 82 |
} |
| 83 |
return values.toArray(new String[values.size()]); |
| 84 |
} |
| 85 |
|
| 86 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 87 |
|
| 88 |
public String getParserName() { |
| 89 |
return TRAIT_LOGGER; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 96 |
|
| 97 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 98 |
|
| 99 |
try { |
| 100 |
|
| 101 |
|
| 102 |
return new NodeTraitLogger(treeModel); |
| 103 |
|
| 104 |
} catch (IllegalArgumentException e) { |
| 105 |
|
| 106 |
throw new XMLParseException("Tree " + treeModel.getId() + " contains no traits to log"); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 116 |
return rules; |
| 117 |
} |
| 118 |
|
| 119 |
private final XMLSyntaxRule[] rules = { |
| 120 |
new ElementRule(TreeModel.class, "The tree which is to be logged") |
| 121 |
}; |
| 122 |
|
| 123 |
public String getParserDescription() { |
| 124 |
return null; |
| 125 |
} |
| 126 |
|
| 127 |
public String getExample() { |
| 128 |
return null; |
| 129 |
} |
| 130 |
|
| 131 |
public Class getReturnType() { |
| 132 |
return TreeTraitProvider.class; |
| 133 |
} |
| 134 |
}; |
| 135 |
} |