| 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.evoxml; |
| 27 |
|
| 28 |
import dr.evolution.datatype.DataType; |
| 29 |
import dr.evolution.datatype.MutationDeathType; |
| 30 |
import dr.evoxml.util.DataTypeUtils; |
| 31 |
import dr.xml.*; |
| 32 |
|
| 33 |
import java.util.logging.Logger; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class MutationDeathTypeParser extends AbstractXMLObjectParser { |
| 39 |
|
| 40 |
public static final String MODEL_NAME = "extendedDataType"; |
| 41 |
public static final String STATE = "deathState"; |
| 42 |
public static final String CODE = "code"; |
| 43 |
public static final String STATES = "states"; |
| 44 |
public static final String AMBIGUITY = "ambiguity"; |
| 45 |
public static final String EXTANT = "extantState"; |
| 46 |
|
| 47 |
public String getParserName() { |
| 48 |
return MODEL_NAME; |
| 49 |
} |
| 50 |
|
| 51 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 52 |
Logger.getLogger("dr.evolution").info("\nCreating an extended data type."); |
| 53 |
Logger.getLogger("dr.evolution").info("\tIf you publish results using this model, please reference Alekseyenko, Lee and Suchard (in submision).\n"); |
| 54 |
|
| 55 |
DataType dataType = DataTypeUtils.getDataType(xo); |
| 56 |
|
| 57 |
char extantChar = '\0'; |
| 58 |
XMLObject cxo = xo.getChild(EXTANT); |
| 59 |
if (cxo != null) { |
| 60 |
extantChar = cxo.getStringAttribute(CODE).charAt(0); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
cxo = xo.getChild(STATE); |
| 65 |
char stateChar; |
| 66 |
if (cxo != null) { |
| 67 |
stateChar = cxo.getStringAttribute(CODE).charAt(0); |
| 68 |
} else { |
| 69 |
stateChar = dataType.getChar(dataType.getGapState()); |
| 70 |
} |
| 71 |
|
| 72 |
Logger.getLogger("dr.evolution").info("\tNon-existent code: " + stateChar); |
| 73 |
|
| 74 |
if (dataType == null && extantChar == '\0') |
| 75 |
throw new XMLParseException("In " + xo.getName() + " you must either provide a data type or a code for extant state"); |
| 76 |
|
| 77 |
MutationDeathType mdt; |
| 78 |
if (dataType != null) { |
| 79 |
Logger.getLogger("dr.evolution").info("\tBase type: " + dataType.toString()); |
| 80 |
mdt = new MutationDeathType(dataType, stateChar); |
| 81 |
} else { |
| 82 |
Logger.getLogger("dr.evolution").info("\tExtant code: " + extantChar); |
| 83 |
mdt = new MutationDeathType(stateChar, extantChar); |
| 84 |
} |
| 85 |
|
| 86 |
char ambiguityChar = '\0'; |
| 87 |
String states; |
| 88 |
Object dxo; |
| 89 |
for (int i = 0; i < xo.getChildCount(); ++i) { |
| 90 |
dxo = xo.getChild(i); |
| 91 |
if (dxo instanceof XMLObject) { |
| 92 |
cxo = (XMLObject) dxo; |
| 93 |
if (cxo.getName().equals(AMBIGUITY)) { |
| 94 |
ambiguityChar = cxo.getStringAttribute(CODE).charAt(0); |
| 95 |
if (cxo.hasAttribute(STATES)) { |
| 96 |
states = cxo.getStringAttribute(STATES); |
| 97 |
} else { |
| 98 |
states = ""; |
| 99 |
} |
| 100 |
mdt.addAmbiguity(ambiguityChar, states); |
| 101 |
Logger.getLogger("dr.evolution").info("\tAmbiguity code: " + ambiguityChar); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
return mdt; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public String getParserDescription() { |
| 113 |
return "This element represents an instance of the MutationDeathType which extends a base datatype with an additional \"death\" state."; |
| 114 |
} |
| 115 |
|
| 116 |
public Class getReturnType() { |
| 117 |
return MutationDeathType.class; |
| 118 |
} |
| 119 |
|
| 120 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 121 |
return rules; |
| 122 |
} |
| 123 |
|
| 124 |
private final XMLSyntaxRule[] rules = { |
| 125 |
new StringAttributeRule(DataType.DATA_TYPE, "Base datatype name", DataType.getRegisteredDataTypeNames(), true), |
| 126 |
new ElementRule(DataType.class, true), |
| 127 |
new ElementRule(STATE, new XMLSyntaxRule[]{AttributeRule.newStringRule(CODE)}, true), |
| 128 |
new ElementRule(EXTANT, new XMLSyntaxRule[]{AttributeRule.newStringRule(CODE)}, true), |
| 129 |
new ContentRule("<ambiguity code=\"Z\" states=\"XY\"/>") |
| 130 |
}; |
| 131 |
} |