Debian Codegraph @

src/dr/evomodelxml/substmodel/LewisMkSubstitutionModelParser.java

raw symbols SCIP
1 /*
2 * LewisMkSubstitutionModelParser.java
3 *
4 * Copyright (c) 2002-2016 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.evomodelxml.substmodel;
27
28 import dr.evomodel.substmodel.FrequencyModel;
29 import dr.evomodel.substmodel.GeneralSubstitutionModel;
30 import dr.evolution.datatype.DataType;
31 import dr.inference.model.Parameter;
32 import dr.xml.*;
33
34 import java.util.Stack;
35
36 /**
37 * Package: LewisMkSubstitutionModelParser
38 * Description:
39 * <p/>
40 * <p/>
41 * Created by
42 *
43 * @author Alexander V. Alekseyenko ([email protected])
44 * Date: Oct 13, 2009
45 * Time: 4:01:38 PM
46 */
47 public class LewisMkSubstitutionModelParser extends AbstractXMLObjectParser {
48 public static final String LEWIS_MK_MODEL = "lewisMk";
49 public static final String TOTAL_ORDER = "totalOrder";
50 public static final String FREQUENCIES = "frequencies";
51 public static final String ORDER = "order";
52 public static final String STATE = "state";
53 public static final String ADJACENT = "adjacentTo";
54
55 //public static XMLObjectParser PARSER=new LewisMkSubstitutionModelParser();
56
57 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
58 XMLObject cxo = xo.getChild(FREQUENCIES);
59 FrequencyModel freqModel = (FrequencyModel) cxo.getChild(FrequencyModel.class);
60 DataType dataType = freqModel.getDataType();
61 int k = dataType.getStateCount();
62 System.err.println("Number of states " + k);
63 Parameter ratesParameter;
64 if (xo.hasAttribute(TOTAL_ORDER) && xo.getBooleanAttribute(TOTAL_ORDER)) { //TOTAL ORDERING OF THE STATES BASED ON DATATYPE
65 ratesParameter = new Parameter.Default(k * (k - 1) / 2, 0);
66 int j = k - 1;
67 for (int i = 0; i < (k - 1) * k / 2; i = i + j + 1) {
68 ratesParameter.setParameterValue(i, 1);
69 j -= 1;
70 }
71 } else if (xo.hasChildNamed(ORDER)) { // USER-SPECIFIED ORDERING OF THE STATES
72 ratesParameter = new Parameter.Default(k * (k - 1) / 2, 0);
73 for (int i = 0; i < xo.getChildCount(); ++i) {
74 if (xo.getChildName(i).equals(ORDER)) {
75 cxo = (XMLObject) xo.getChild(i);
76 if (cxo.getName().equals(ORDER)) {
77 int from = dataType.getState(cxo.getStringAttribute(STATE).charAt(0));
78 int to = dataType.getState(cxo.getStringAttribute(ADJACENT).charAt(0));
79 if (from > to) {//SWAP: from should have the smaller state number
80 to += from;
81 from = to - from;
82 to -= from;
83 }
84 int ratesIndex = (from * (2 * k - 3) - from * from) / 2 + to - 1;
85 ratesParameter.setParameterValue(ratesIndex, 1);
86 }
87 }
88 }
89 } else {
90 ratesParameter = new Parameter.Default(k * (k - 1) / 2, 1);
91 }
92 System.err.println(ratesParameter.toString());
93 System.err.println("Infinitesimal matrix:");
94 for (int i = 0; i < k; ++i) {
95 for (int j = 0; j < k; ++j) {
96 int from, to;
97 if (i < j) {
98 from = i;
99 to = j;
100 } else {
101 from = j;
102 to = i;
103 }
104
105
106 int ratesIndex = (from * (2 * k - 3) - from * from) / 2 + to - 1; //This is right now!!! Thanks, Marc!
107
108 if (i != j)
109 System.err.print(Double.toString(ratesParameter.getValue(ratesIndex)) + "\t(" + ratesIndex + ")\t");
110 else System.err.print("-\t\t");
111
112 }
113 System.err.println("");//newline
114 }
115 System.err.println("");
116
117 if (!checkConnected(ratesParameter.getValues(), k)) {
118 throw (new XMLParseException("The state transitions form a disconnected graph! This model is not suited for this case."));
119 }
120
121 return new GeneralSubstitutionModel(LEWIS_MK_MODEL, dataType, freqModel, ratesParameter, -1);
122 }
123
124 public XMLSyntaxRule[] getSyntaxRules() {
125 return rules;
126 }
127
128 private boolean checkConnected(Double rates[], int states) {
129 boolean[] visited = new boolean[states];
130 Stack<Integer> open = new Stack<Integer>();
131
132 open.push(0);
133
134 for (int i = 1; i < states; ++i) {
135 visited[i] = false;
136 }
137 visited[0] = true;
138
139 while (!open.empty()) {
140 int current = open.pop();
141 for (int j = 0; j < states; ++j) {
142 int rateIndex;
143 if (current < j) rateIndex = (current * (2 * states - 3) - current * current) / 2 + j - 1;
144 else rateIndex = (j * (2 * states - 3) - j * j) / 2 + current - 1;
145 if (current != j && !visited[j] && rates[rateIndex] != 0.0) {
146 visited[j] = true;
147 open.push(j);
148 }
149 }
150 }
151 for (int i = 0; i < states; ++i) {
152 if (!visited[i]) return false;
153 }
154 return true;
155 }
156
157 private final XMLSyntaxRule[] rules = {
158 new ElementRule(FREQUENCIES, FrequencyModel.class),
159 AttributeRule.newBooleanRule(TOTAL_ORDER, true),
160 new ElementRule(ORDER,
161 new XMLSyntaxRule[]{AttributeRule.newStringRule(STATE, false),
162 AttributeRule.newStringRule(ADJACENT, false)}, 0, Integer.MAX_VALUE)
163 };
164
165 public String getParserDescription() {
166 return "A parser for Lewis's Mk model";
167 }
168
169 public Class getReturnType() {
170 return GeneralSubstitutionModel.class;
171 }
172
173 public String getParserName() {
174 return LEWIS_MK_MODEL;
175 }
176 }