Debian Codegraph @

src/dr/evomodel/treelikelihood/SplitBySiteTraitLogger.java

raw symbols SCIP
1 /*
2 * SplitBySiteTraitLogger.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.treelikelihood;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evolution.tree.TreeTrait;
31 import dr.evolution.tree.TreeTraitProvider;
32 import dr.evomodel.tree.TreeModel;
33 import dr.oldevomodel.treelikelihood.AncestralStateTreeLikelihood;
34 import dr.util.Citable;
35 import dr.util.Citation;
36 import dr.util.CommonCitations;
37 import dr.xml.*;
38
39 import java.util.*;
40 import java.util.logging.Logger;
41
42 /**
43 * Takes a TreeTraitProvider that returns multiple-site traits and provides a new Trait for logging by site
44 *
45 * @author Marc A. Suchard
46 */
47 public class SplitBySiteTraitLogger extends TreeTraitProvider.Helper implements Citable {
48
49 public static final String TRAIT_LOGGER = "splitTraitBySite";
50 public static final String TRAIT_NAME = "traitName";
51 public static final String SCALE = "scaleByBranchLength";
52
53 public SplitBySiteTraitLogger(final AncestralStateTraitProvider treeLikelihood, String traitName, boolean scale) throws XMLParseException {
54 TreeTrait trait = treeLikelihood.getTreeTrait(traitName);
55 if (trait == null) {
56 throw new XMLParseException("TreeTraitProvider does not provide trait named '" + traitName + ".");
57 }
58
59 TreeModel tree = treeLikelihood.getTreeModel();
60 int length;
61 Object obj = trait.getTrait(tree, tree.getNode(0));
62
63 boolean isDoubleArray = false;
64 boolean isIntegerArray = false;
65
66 if (obj instanceof double[]) {
67 length = ((double[]) obj).length;
68 isDoubleArray = true;
69 } else if (obj instanceof int[]) {
70 length = ((int[]) obj).length;
71 isIntegerArray = true;
72 } else {
73 throw new XMLParseException("Unknown trait type to split");
74 }
75
76 TreeTrait[] partitionedTraits = new TreeTrait[length];
77 if (isDoubleArray) {
78 for (int i = 0; i < length; i++) {
79 if (scale) {
80 partitionedTraits[i] = new TreeTrait.PickEntryDAndScale(trait, i);
81 } else {
82 partitionedTraits[i] = new TreeTrait.PickEntryD(trait, i);
83 }
84 }
85 } else if (isIntegerArray) {
86 for (int i = 0; i < length; ++i) {
87 if (traitName.compareTo(AncestralStateTreeLikelihood.STATES_KEY) == 0) {
88 partitionedTraits[i] = new TreeTrait.PickEntryI(trait, i) {
89 public String getTraitString(Tree tree, NodeRef node) {
90 int[] state = new int[1];
91 state[0] = getTrait(tree, node);
92 return treeLikelihood.formattedState(state);
93 }
94 };
95 } else {
96 partitionedTraits[i] = new TreeTrait.PickEntryI(trait, i);
97 }
98 }
99 }
100 addTraits(partitionedTraits);
101
102 Logger.getLogger("dr.app.beagle").info("\tConstructing a split logger with " + length + " partitions; please cite:\n"
103 + Citable.Utils.getCitationString(this));
104
105 }
106
107 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
108
109 public String getParserName() {
110 return TRAIT_LOGGER;
111 }
112
113 /**
114 * @return an object based on the XML element it was passed.
115 */
116 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
117
118 String traitName = xo.getStringAttribute(TRAIT_NAME);
119 AncestralStateTraitProvider tree = (AncestralStateTraitProvider) xo.getChild(AncestralStateTraitProvider.class);
120 boolean scale = xo.getAttribute(SCALE, false);
121
122 return new SplitBySiteTraitLogger(tree, traitName, scale);
123 }
124
125 //************************************************************************
126 // AbstractXMLObjectParser implementation
127 //************************************************************************
128
129 public XMLSyntaxRule[] getSyntaxRules() {
130 return rules;
131 }
132
133 private final XMLSyntaxRule[] rules = {
134 new ElementRule(AncestralStateTraitProvider.class, "The tree which is to be logged"),
135 AttributeRule.newStringRule(TRAIT_NAME),
136 AttributeRule.newBooleanRule(SCALE, true),
137 };
138
139 public String getParserDescription() {
140 return null;
141 }
142
143 public String getExample() {
144 return null;
145 }
146
147 public Class getReturnType() {
148 return TreeTraitProvider.class;
149 }
150 };
151
152 @Override
153 public Citation.Category getCategory() {
154 return Citation.Category.COUNTING_PROCESSES;
155 }
156
157 @Override
158 public String getDescription() {
159 return "Split by site trait logger";
160 }
161
162 public List<Citation> getCitations() {
163 return Collections.singletonList(CommonCitations.LEMEY_2012_RENAISSANCE);
164 }
165 }