Debian Codegraph @

src/dr/app/beagle/tools/parsers/CompleteHistorySimulatorParser.java

raw symbols SCIP
1 /*
2 * CompleteHistorySimulatorParser.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.beagle.tools.parsers;
27
28 import dr.evomodelxml.treelikelihood.MarkovJumpsTreeLikelihoodParser;
29 import dr.evomodel.siteratemodel.GammaSiteRateModel;
30 import dr.evomodel.substmodel.CodonLabeling;
31 import dr.app.beagle.tools.CompleteHistorySimulator;
32 import dr.evolution.alignment.Alignment;
33 import dr.evolution.datatype.Codons;
34 import dr.evolution.datatype.DataType;
35 import dr.evolution.tree.Tree;
36 import dr.evomodel.branchratemodel.BranchRateModel;
37 import dr.evomodel.branchratemodel.DefaultBranchRateModel;
38 import dr.inference.markovjumps.MarkovJumpsType;
39 import dr.inference.model.Parameter;
40 import dr.xml.*;
41
42 /**
43 * @author Marc A. Suchard
44 */
45
46 public class CompleteHistorySimulatorParser extends AbstractXMLObjectParser {
47
48 /* standard xml parser stuff follows */
49 public static final String HISTORY_SIMULATOR = "completeHistorySimulator";
50 public static final String TREE = "tree";
51 public static final String REPLICATIONS = "replications";
52 public static final String COUNTS = MarkovJumpsTreeLikelihoodParser.COUNTS;
53 public static final String REWARDS = MarkovJumpsTreeLikelihoodParser.REWARDS;
54 public static final String JUMP_TAG_NAME = MarkovJumpsTreeLikelihoodParser.JUMP_TAG_NAME;
55 public static final String JUMP_TAG = MarkovJumpsTreeLikelihoodParser.JUMP_TAG;
56
57 public static final String SYN_JUMPS = "reportSynonymousMutations";
58 public static final String NON_SYN_JUMPS = "reportNonSynonymousMutations";
59 public static final String SUM_SITES = "sumAcrossSites";
60
61 public static final String BRANCH_SPECIFIC_SPECIFICATION = "branchSpecificSpecification";
62 public static final String BRANCH_VARIABLE_PARAMETER = "variableParameter";
63 public static final String VARIABLE_VALUE_PARAMETER = "valuesParameter";
64
65 public static final String ANNOTATE_WITH_ALIGNMENT = "annotateWithAlignment";
66
67 public static final String ALIGNMENT_ONLY = "alignmentOnly";
68
69 public String getParserName() {
70 return HISTORY_SIMULATOR;
71 }
72
73 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
74
75 int nReplications = xo.getIntegerAttribute(REPLICATIONS);
76
77 Tree tree = (Tree) xo.getChild(Tree.class);
78 GammaSiteRateModel siteModel = (GammaSiteRateModel) xo.getChild(GammaSiteRateModel.class);
79 BranchRateModel rateModel = (BranchRateModel) xo.getChild(BranchRateModel.class);
80
81 if (rateModel == null)
82 rateModel = new DefaultBranchRateModel();
83
84 DataType dataType = siteModel.getSubstitutionModel().getDataType();
85
86 String jumpTag = xo.getAttribute(JUMP_TAG_NAME, JUMP_TAG);
87
88 boolean sumAcrossSites = xo.getAttribute(SUM_SITES, false);
89
90 Parameter branchSpecificParameter = null;
91 Parameter variableValueParameter = null;
92
93 if (xo.hasChildNamed(BRANCH_SPECIFIC_SPECIFICATION)) {
94 XMLObject cxo = xo.getChild(BRANCH_SPECIFIC_SPECIFICATION);
95 branchSpecificParameter = (Parameter) cxo.getChild(BRANCH_VARIABLE_PARAMETER).getChild(Parameter.class);
96 variableValueParameter = (Parameter) cxo.getChild(VARIABLE_VALUE_PARAMETER).getChild(Parameter.class);
97 }
98
99 CompleteHistorySimulator history = new CompleteHistorySimulator(tree, siteModel, rateModel, nReplications,
100 sumAcrossSites, branchSpecificParameter, variableValueParameter);
101
102 XMLObject cxo = xo.getChild(COUNTS);
103 if (cxo != null) {
104 MarkovJumpsTreeLikelihoodParser.parseAllChildren(cxo, history, dataType.getStateCount(),
105 jumpTag, MarkovJumpsType.COUNTS, false);
106 }
107
108 cxo = xo.getChild(REWARDS);
109 if (cxo != null) {
110 MarkovJumpsTreeLikelihoodParser.parseAllChildren(cxo, history, dataType.getStateCount(),
111 jumpTag, MarkovJumpsType.REWARDS, false);
112 }
113
114 if (dataType instanceof Codons) {
115 Codons codons = (Codons) dataType;
116 if (xo.getAttribute(SYN_JUMPS, false)) {
117 double[] synRegMatrix = CodonLabeling.getRegisterMatrix(CodonLabeling.SYN, codons, false); // use base 61
118 Parameter registerParameter = new Parameter.Default(synRegMatrix);
119 registerParameter.setId("S");
120 history.addRegister(registerParameter, MarkovJumpsType.COUNTS, false);
121 }
122 if (xo.getAttribute(NON_SYN_JUMPS, false)) {
123 double[] nonSynRegMatrix = CodonLabeling.getRegisterMatrix(CodonLabeling.NON_SYN, codons, false); // use base 61
124 Parameter registerParameter = new Parameter.Default(nonSynRegMatrix);
125 registerParameter.setId("N");
126 history.addRegister(registerParameter, MarkovJumpsType.COUNTS, false);
127 }
128 }
129
130 if (xo.getAttribute(ANNOTATE_WITH_ALIGNMENT,false)) {
131 history.addAlignmentTrait();
132 }
133
134 boolean alignmentOnly = xo.getAttribute(ALIGNMENT_ONLY, false);
135 if (dataType instanceof Codons && !alignmentOnly) {
136 System.out.println("Codon models give exception when count statistics are done on them. "
137 + "You can supress this by setting alignmentOnly to true.");
138 }
139
140 if (alignmentOnly) {
141 history.setAlignmentOnly();
142 }
143
144 history.simulate();
145 return history;
146 }
147
148 //************************************************************************
149 // AbstractXMLObjectParser implementation
150 //************************************************************************
151
152 public String getParserDescription() {
153 return "A SequenceSimulator that generates random sequences for a given tree, siteratemodel and branch rate model";
154 }
155
156 public Class getReturnType() {
157 return Alignment.class;
158 }
159
160 public XMLSyntaxRule[] getSyntaxRules() {
161 return rules;
162 }
163
164 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
165 new ElementRule(Tree.class),
166 new ElementRule(GammaSiteRateModel.class),
167 new ElementRule(BranchRateModel.class, true),
168 AttributeRule.newIntegerRule(REPLICATIONS),
169 AttributeRule.newBooleanRule(SYN_JUMPS, true),
170 AttributeRule.newBooleanRule(NON_SYN_JUMPS, true),
171 AttributeRule.newBooleanRule(SUM_SITES, true),
172 AttributeRule.newBooleanRule(ANNOTATE_WITH_ALIGNMENT, true),
173 AttributeRule.newBooleanRule(ALIGNMENT_ONLY, true),
174 new ElementRule(BRANCH_SPECIFIC_SPECIFICATION, new XMLSyntaxRule[] {
175 new ElementRule(VARIABLE_VALUE_PARAMETER, Parameter.class),
176 new ElementRule(BRANCH_VARIABLE_PARAMETER, Parameter.class),
177 }, true),
178 };
179 }