| 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.oldevomodelxml.substmodel; |
| 27 |
|
| 28 |
import dr.evolution.datatype.DataType; |
| 29 |
import dr.oldevomodel.substmodel.FrequencyModel; |
| 30 |
import dr.oldevomodel.substmodel.GeneralSubstitutionModel; |
| 31 |
import dr.inference.model.Parameter; |
| 32 |
import dr.xml.*; |
| 33 |
|
| 34 |
import java.util.Stack; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 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 |
|
| 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)) { |
| 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)) { |
| 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) { |
| 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; |
| 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(""); |
| 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(dataType, freqModel, ratesParameter, 0); |
| 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' 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 |
} |