Debian Codegraph @

src/dr/app/beauti/generator/TreeModelGenerator.java

raw symbols SCIP
1 /*
2 * TreeModelGenerator.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.app.beauti.generator;
27
28 import dr.app.beauti.components.ComponentFactory;
29 import dr.app.beauti.options.AbstractPartitionData;
30 import dr.app.beauti.options.BeautiOptions;
31 import dr.app.beauti.options.PartitionTreeModel;
32 import dr.app.beauti.util.XMLWriter;
33 import dr.evolution.datatype.DataType;
34 import dr.evomodel.tree.TMRCAStatistic;
35 import dr.evomodel.tree.TreeModel;
36 import dr.evomodelxml.coalescent.OldCoalescentSimulatorParser;
37 import dr.evomodelxml.tree.MicrosatelliteSamplerTreeModelParser;
38 import dr.evomodelxml.tree.TMRCAStatisticParser;
39 import dr.evomodelxml.tree.TreeLengthStatisticParser;
40 import dr.evomodelxml.tree.TreeModelParser;
41 import dr.evoxml.MicrosatellitePatternParser;
42 import dr.evoxml.UPGMATreeParser;
43 import dr.inference.model.ParameterParser;
44 import dr.util.Attribute;
45 import dr.xml.XMLParser;
46
47 /**
48 * @author Alexei Drummond
49 * @author Andrew Rambaut
50 * @author Walter Xie
51 */
52 public class TreeModelGenerator extends Generator {
53
54 public TreeModelGenerator(BeautiOptions options, ComponentFactory[] components) {
55 super(options, components);
56 }
57
58 /**
59 * Write tree model XML block.
60 *
61 * @param model
62 * @param writer the writer
63 */
64 void writeTreeModel(PartitionTreeModel model, XMLWriter writer) {
65
66 String prefix = model.getPrefix();
67
68 final String treeModelName = prefix + TreeModel.TREE_MODEL; // treemodel.treeModel or treeModel
69
70 writer.writeComment("Generate a tree model");
71 writer.writeTag(TreeModel.TREE_MODEL, new Attribute.Default<String>(XMLParser.ID, treeModelName), false);
72
73 final String STARTING_TREE = InitialTreeGenerator.STARTING_TREE;
74
75 switch (model.getStartingTreeType()) {
76 case USER:
77 writer.writeIDref("tree", prefix + STARTING_TREE);
78 break;
79 case UPGMA:
80 writer.writeIDref(UPGMATreeParser.UPGMA_TREE, prefix + STARTING_TREE);
81 break;
82 case RANDOM:
83 writer.writeIDref(OldCoalescentSimulatorParser.COALESCENT_TREE, prefix + STARTING_TREE);
84 break;
85 default:
86 throw new IllegalArgumentException("Unknown StartingTreeType");
87 }
88
89 writer.writeOpenTag(TreeModelParser.ROOT_HEIGHT);
90 writer.writeTag(ParameterParser.PARAMETER,
91 new Attribute.Default<String>(XMLParser.ID, treeModelName + "." + TreeModelParser.ROOT_HEIGHT), true);
92 writer.writeCloseTag(TreeModelParser.ROOT_HEIGHT);
93
94
95 writer.writeOpenTag(TreeModelParser.NODE_HEIGHTS, new Attribute.Default<String>(TreeModelParser.INTERNAL_NODES, "true"));
96 writer.writeTag(ParameterParser.PARAMETER,
97 new Attribute.Default<String>(XMLParser.ID, treeModelName + "." + "internalNodeHeights"), true);
98 writer.writeCloseTag(TreeModelParser.NODE_HEIGHTS);
99
100 writer.writeOpenTag(TreeModelParser.NODE_HEIGHTS,
101 new Attribute[]{
102 new Attribute.Default<String>(TreeModelParser.INTERNAL_NODES, "true"),
103 new Attribute.Default<String>(TreeModelParser.ROOT_NODE, "true")
104 });
105 writer.writeTag(ParameterParser.PARAMETER,
106 new Attribute.Default<String>(XMLParser.ID, treeModelName + "." + "allInternalNodeHeights"), true);
107 writer.writeCloseTag(TreeModelParser.NODE_HEIGHTS);
108
109 // int randomLocalClockCount = 0;
110 // int autocorrelatedClockCount = 0;
111 // for (PartitionData pd : model.getDataPartitions()) { // only the PDs linked to this tree model
112 // PartitionClockModel clockModel = pd.getPartitionClockModel();
113 // switch (clockModel.getClockType()) {
114 // case AUTOCORRELATED_LOGNORMAL: autocorrelatedClockCount += 1; break;
115 // case RANDOM_LOCAL_CLOCK: randomLocalClockCount += 1; break;
116 // }
117 // }
118 //
119 // if (autocorrelatedClockCount > 1 || randomLocalClockCount > 1 || autocorrelatedClockCount + randomLocalClockCount > 1) {
120 // //FAIL
121 // throw new IllegalArgumentException("clock model/tree model combination not implemented by BEAST yet!");
122 // }
123 // move to validateClockTreeModelCombination(PartitionTreeModel model)
124
125 // if (autocorrelatedClockCount == 1) {
126 // if (count[0] == 1) {
127 // writer.writeOpenTag(TreeModelParser.NODE_RATES,
128 // new Attribute[]{
129 // new Attribute.Default<String>(TreeModelParser.ROOT_NODE, "false"),
130 // new Attribute.Default<String>(TreeModelParser.INTERNAL_NODES, "true"),
131 // new Attribute.Default<String>(TreeModelParser.LEAF_NODES, "true")
132 // });
133 // writer.writeTag(ParameterParser.PARAMETER,
134 // new Attribute.Default<String>(XMLParser.ID, treeModelName + "." + TreeModelParser.NODE_RATES), true);
135 // writer.writeCloseTag(TreeModelParser.NODE_RATES);
136 //
137 // writer.writeOpenTag(TreeModelParser.NODE_RATES,
138 // new Attribute[]{
139 // new Attribute.Default<String>(TreeModelParser.ROOT_NODE, "true"),
140 // new Attribute.Default<String>(TreeModelParser.INTERNAL_NODES, "false"),
141 // new Attribute.Default<String>(TreeModelParser.LEAF_NODES, "false")
142 // });
143 // writer.writeTag(ParameterParser.PARAMETER,
144 // new Attribute.Default<String>(XMLParser.ID,
145 // treeModelName + "." + RateEvolutionLikelihood.ROOTRATE), true);
146 // writer.writeCloseTag(TreeModelParser.NODE_RATES);
147 //// } else if (randomLocalClockCount == 1 ) {
148 // } else
149
150 //+++++++++++++ removed because random local clock XML is changed ++++++++++++++++
151 // int[] count = validateClockTreeModelCombination(model);
152 // if (count[1] == 1) {
153 // writer.writeOpenTag(TreeModelParser.NODE_RATES,
154 // new Attribute[]{
155 // new Attribute.Default<String>(TreeModelParser.ROOT_NODE, "false"),
156 // new Attribute.Default<String>(TreeModelParser.INTERNAL_NODES, "true"),
157 // new Attribute.Default<String>(TreeModelParser.LEAF_NODES, "true")
158 // });
159 // writer.writeTag(ParameterParser.PARAMETER,
160 // new Attribute.Default<String>(XMLParser.ID, modelPrefix + ClockType.LOCAL_CLOCK + ".relativeRates"), true);
161 // writer.writeCloseTag(TreeModelParser.NODE_RATES);
162 //
163 // writer.writeOpenTag(TreeModelParser.NODE_TRAITS,
164 // new Attribute[]{
165 // new Attribute.Default<String>(TreeModelParser.ROOT_NODE, "false"),
166 // new Attribute.Default<String>(TreeModelParser.INTERNAL_NODES, "true"),
167 // new Attribute.Default<String>(TreeModelParser.LEAF_NODES, "true")
168 // });
169 // writer.writeTag(ParameterParser.PARAMETER,
170 // new Attribute.Default<String>(XMLParser.ID, modelPrefix + ClockType.LOCAL_CLOCK + ".changes"), true);
171 // writer.writeCloseTag(TreeModelParser.NODE_TRAITS);
172 // }
173
174 generateInsertionPoint(ComponentGenerator.InsertionPoint.IN_TREE_MODEL, model, writer);
175
176 writer.writeCloseTag(TreeModel.TREE_MODEL);
177
178 // if (autocorrelatedClockCount == 1) {
179 // if (count[0] == 1) {
180 // writer.writeText("");
181 // writer.writeOpenTag(CompoundParameter.COMPOUND_PARAMETER,
182 // new Attribute[]{new Attribute.Default<String>(XMLParser.ID, treeModelName + "." + "allRates")});
183 // writer.writeIDref(ParameterParser.PARAMETER, treeModelName + "." + TreeModelParser.NODE_RATES);
184 // writer.writeIDref(ParameterParser.PARAMETER, treeModelName + "." + RateEvolutionLikelihood.ROOTRATE);
185 // writer.writeCloseTag(CompoundParameter.COMPOUND_PARAMETER);
186 // }
187
188 writer.writeComment("Statistic for sum of the branch lengths of the tree (tree length)");
189 writer.writeTag(TreeLengthStatisticParser.TREE_LENGTH_STATISTIC,
190 new Attribute[]{
191 new Attribute.Default<String>(XMLParser.ID, prefix + "treeLength"),
192 }, false);
193 writer.writeIDref(TreeModel.TREE_MODEL, treeModelName);
194 writer.writeCloseTag(TreeLengthStatisticParser.TREE_LENGTH_STATISTIC);
195
196 if (model.hasTipCalibrations()) {
197 writer.writeComment("Statistic for time of most recent common ancestor of tree");
198 writer.writeTag(TMRCAStatisticParser.TMRCA_STATISTIC,
199 new Attribute[]{
200 new Attribute.Default<String>(XMLParser.ID, prefix + "age(root)"),
201 new Attribute.Default<String>(TMRCAStatisticParser.ABSOLUTE, "true")
202 }, false);
203 writer.writeIDref(TreeModel.TREE_MODEL, treeModelName);
204 writer.writeCloseTag(TMRCAStatisticParser.TMRCA_STATISTIC);
205 }
206
207 if (model.getDataType().getType() == DataType.MICRO_SAT) {
208 for (AbstractPartitionData partitionData : options.getDataPartitions(model)) {
209 writer.writeComment("Generate a microsatellite tree model");
210 writer.writeTag(MicrosatelliteSamplerTreeModelParser.TREE_MICROSATELLITE_SAMPLER_MODEL,
211 new Attribute.Default<String>(XMLParser.ID, partitionData.getName() + "." +
212 MicrosatelliteSamplerTreeModelParser.TREE_MICROSATELLITE_SAMPLER_MODEL), false);
213
214 writer.writeOpenTag(MicrosatelliteSamplerTreeModelParser.TREE);
215 writer.writeIDref(TreeModel.TREE_MODEL, treeModelName);
216 writer.writeCloseTag(MicrosatelliteSamplerTreeModelParser.TREE);
217
218 writer.writeOpenTag(MicrosatelliteSamplerTreeModelParser.INTERNAL_VALUES);
219 writer.writeTag(ParameterParser.PARAMETER, new Attribute[]{
220 new Attribute.Default<String>(XMLParser.ID, partitionData.getName() + "." +
221 MicrosatelliteSamplerTreeModelParser.TREE_MICROSATELLITE_SAMPLER_MODEL + ".internalNodesParameter"),
222 new Attribute.Default<Integer>(ParameterParser.DIMENSION, model.getDimension())}, true);
223 writer.writeCloseTag(MicrosatelliteSamplerTreeModelParser.INTERNAL_VALUES);
224
225 writer.writeOpenTag(MicrosatelliteSamplerTreeModelParser.EXTERNAL_VALUES);
226
227 writer.writeIDref(MicrosatellitePatternParser.MICROSATPATTERN, partitionData.getName());
228
229 writer.writeCloseTag(MicrosatelliteSamplerTreeModelParser.EXTERNAL_VALUES);
230
231 writer.writeCloseTag(MicrosatelliteSamplerTreeModelParser.TREE_MICROSATELLITE_SAMPLER_MODEL);
232 }
233 }
234 }
235 }