| 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.oldevomodel.substmodel.SVSComplexSubstitutionModel; |
| 32 |
import dr.oldevomodel.substmodel.SVSGeneralSubstitutionModel; |
| 33 |
import dr.evoxml.util.DataTypeUtils; |
| 34 |
import dr.inference.model.BayesianStochasticSearchVariableSelection; |
| 35 |
import dr.inference.model.Parameter; |
| 36 |
import dr.xml.*; |
| 37 |
|
| 38 |
import java.util.logging.Logger; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class GeneralSubstitutionModelParser extends AbstractXMLObjectParser { |
| 44 |
|
| 45 |
public static final String GENERAL_SUBSTITUTION_MODEL = "generalSubstitutionModel"; |
| 46 |
public static final String DATA_TYPE = "dataType"; |
| 47 |
public static final String RATES = "rates"; |
| 48 |
public static final String RELATIVE_TO = "relativeTo"; |
| 49 |
public static final String FREQUENCIES = "frequencies"; |
| 50 |
public static final String INDICATOR = "rateIndicator"; |
| 51 |
|
| 52 |
public static final String SVS_GENERAL_SUBSTITUTION_MODEL = "svsGeneralSubstitutionModel"; |
| 53 |
public static final String SVS_COMPLEX_SUBSTITUTION_MODEL = "svsComplexSubstitutionModel"; |
| 54 |
|
| 55 |
public String getParserName() { |
| 56 |
return GENERAL_SUBSTITUTION_MODEL; |
| 57 |
} |
| 58 |
|
| 59 |
public String[] getParserNames() { |
| 60 |
return new String[]{getParserName(), SVS_GENERAL_SUBSTITUTION_MODEL, SVS_COMPLEX_SUBSTITUTION_MODEL}; |
| 61 |
} |
| 62 |
|
| 63 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 64 |
|
| 65 |
Parameter ratesParameter = null; |
| 66 |
FrequencyModel freqModel = null; |
| 67 |
|
| 68 |
if (xo.hasChildNamed(FREQUENCIES)) { |
| 69 |
XMLObject cxo = xo.getChild(FREQUENCIES); |
| 70 |
freqModel = (FrequencyModel) cxo.getChild(FrequencyModel.class); |
| 71 |
} |
| 72 |
|
| 73 |
DataType dataType = DataTypeUtils.getDataType(xo); |
| 74 |
|
| 75 |
if (dataType == null) dataType = (DataType) xo.getChild(DataType.class); |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
if (dataType == null) dataType = freqModel.getDataType(); |
| 91 |
|
| 92 |
if (dataType != freqModel.getDataType()) { |
| 93 |
throw new XMLParseException("Data type of " + getParserName() + " element does not match that of its frequencyModel."); |
| 94 |
} |
| 95 |
|
| 96 |
XMLObject cxo = xo.getChild(RATES); |
| 97 |
ratesParameter = (Parameter) cxo.getChild(Parameter.class); |
| 98 |
|
| 99 |
int states = dataType.getStateCount(); |
| 100 |
Logger.getLogger("dr.evomodel").info(" General Substitution Model (stateCount=" + states + ")"); |
| 101 |
|
| 102 |
boolean hasRelativeRates = cxo.hasChildNamed(RELATIVE_TO) || (cxo.hasAttribute(RELATIVE_TO) && cxo.getIntegerAttribute(RELATIVE_TO) > 0); |
| 103 |
|
| 104 |
int nonReversibleRateCount = ((dataType.getStateCount() - 1) * dataType.getStateCount()); |
| 105 |
int reversibleRateCount = (nonReversibleRateCount / 2); |
| 106 |
|
| 107 |
boolean isNonReversible = ratesParameter.getDimension() == nonReversibleRateCount; |
| 108 |
boolean hasIndicator = xo.hasChildNamed(INDICATOR); |
| 109 |
|
| 110 |
if (!hasRelativeRates) { |
| 111 |
Parameter indicatorParameter = null; |
| 112 |
|
| 113 |
if (ratesParameter.getDimension() != reversibleRateCount && ratesParameter.getDimension() != nonReversibleRateCount) { |
| 114 |
throw new XMLParseException("Rates parameter in " + getParserName() + " element should have " + (reversibleRateCount) |
| 115 |
+ " dimensions for reversible model or " + nonReversibleRateCount + " dimensions for non-reversible. " + |
| 116 |
"However parameter dimension is " + ratesParameter.getDimension()); |
| 117 |
} |
| 118 |
|
| 119 |
if (hasIndicator) { |
| 120 |
cxo = xo.getChild(INDICATOR); |
| 121 |
indicatorParameter = (Parameter) cxo.getChild(Parameter.class); |
| 122 |
|
| 123 |
if (indicatorParameter.getDimension() != ratesParameter.getDimension()) { |
| 124 |
throw new XMLParseException("Rates and indicator parameters in " + getParserName() + " element must be the same dimension."); |
| 125 |
} |
| 126 |
|
| 127 |
boolean randomize = xo.getAttribute(ComplexSubstitutionModelParser.RANDOMIZE, false); |
| 128 |
if (randomize) { |
| 129 |
BayesianStochasticSearchVariableSelection.Utils.randomize(indicatorParameter, |
| 130 |
dataType.getStateCount(), !isNonReversible); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
if (isNonReversible) { |
| 135 |
Logger.getLogger("dr.evomodel").info(" Using BSSVS Complex Substitution Model"); |
| 136 |
return new SVSComplexSubstitutionModel(getParserName(), dataType, freqModel, ratesParameter, indicatorParameter); |
| 137 |
} else { |
| 138 |
Logger.getLogger("dr.evomodel").info(" Using BSSVS General Substitution Model"); |
| 139 |
return new SVSGeneralSubstitutionModel(dataType, freqModel, ratesParameter, indicatorParameter); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
} else { |
| 144 |
|
| 145 |
|
| 146 |
if (ratesParameter.getDimension() != reversibleRateCount - 1) { |
| 147 |
throw new XMLParseException("Rates parameter in " + getParserName() + " element should have " + (reversibleRateCount - 1) |
| 148 |
+ " dimensions. However parameter dimension is " + ratesParameter.getDimension()); |
| 149 |
} |
| 150 |
|
| 151 |
int relativeTo = 0; |
| 152 |
if (hasRelativeRates) { |
| 153 |
relativeTo = cxo.getIntegerAttribute(RELATIVE_TO) - 1; |
| 154 |
} |
| 155 |
|
| 156 |
if (relativeTo < 0 || relativeTo >= reversibleRateCount) { |
| 157 |
throw new XMLParseException(RELATIVE_TO + " must be 1 or greater"); |
| 158 |
} else { |
| 159 |
int t = relativeTo; |
| 160 |
int s = states - 1; |
| 161 |
int row = 0; |
| 162 |
while (t >= s) { |
| 163 |
t -= s; |
| 164 |
s -= 1; |
| 165 |
row += 1; |
| 166 |
} |
| 167 |
int col = t + row + 1; |
| 168 |
|
| 169 |
Logger.getLogger("dr.evomodel").info(" Rates relative to " |
| 170 |
+ dataType.getCode(row) + "<->" + dataType.getCode(col)); |
| 171 |
} |
| 172 |
|
| 173 |
if (ratesParameter == null) { |
| 174 |
if (reversibleRateCount == 1) { |
| 175 |
|
| 176 |
} else { |
| 177 |
throw new XMLParseException("No rates parameter found in " + getParserName()); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
return new GeneralSubstitutionModel(dataType, freqModel, ratesParameter, relativeTo); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public String getParserDescription() { |
| 190 |
return "A general reversible model of sequence substitution for any data type."; |
| 191 |
} |
| 192 |
|
| 193 |
public Class getReturnType() { |
| 194 |
return GeneralSubstitutionModelParser.class; |
| 195 |
} |
| 196 |
|
| 197 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 198 |
return rules; |
| 199 |
} |
| 200 |
|
| 201 |
private final XMLSyntaxRule[] rules = { |
| 202 |
new XORRule( |
| 203 |
new StringAttributeRule(DataType.DATA_TYPE, "The type of sequence data", |
| 204 |
DataType.getRegisteredDataTypeNames(), false), |
| 205 |
new ElementRule(DataType.class) |
| 206 |
, true), |
| 207 |
new ElementRule(FREQUENCIES, FrequencyModel.class), |
| 208 |
new ElementRule(RATES, |
| 209 |
new XMLSyntaxRule[]{ |
| 210 |
new ElementRule(Parameter.class)} |
| 211 |
), |
| 212 |
new ElementRule(INDICATOR, |
| 213 |
new XMLSyntaxRule[]{ |
| 214 |
new ElementRule(Parameter.class), |
| 215 |
}, true), |
| 216 |
AttributeRule.newBooleanRule(ComplexSubstitutionModelParser.RANDOMIZE, true), |
| 217 |
}; |
| 218 |
} |