| 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.GeneralDataType; |
| 30 |
import dr.xml.*; |
| 31 |
import dr.util.Attribute; |
| 32 |
import dr.util.Identifiable; |
| 33 |
|
| 34 |
import java.util.ArrayList; |
| 35 |
import java.util.List; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public class GeneralDataTypeParser extends AbstractXMLObjectParser { |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public static final String GENERAL_DATA_TYPE = "generalDataType"; |
| 62 |
public static final String STATE = "state"; |
| 63 |
public static final String STATES = "states"; |
| 64 |
public static final String ALIAS = "alias"; |
| 65 |
public static final String AMBIGUITY = "ambiguity"; |
| 66 |
public static final String CODE = "code"; |
| 67 |
|
| 68 |
public String getParserName() { return GENERAL_DATA_TYPE; } |
| 69 |
|
| 70 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 71 |
|
| 72 |
List<String> states = new ArrayList<String>(); |
| 73 |
|
| 74 |
for (int i =0; i < xo.getChildCount(); i++) { |
| 75 |
if (xo.getChild(i) instanceof XMLObject) { |
| 76 |
XMLObject cxo = (XMLObject)xo.getChild(i); |
| 77 |
|
| 78 |
if (cxo.getName().equals(STATE)) { |
| 79 |
states.add(cxo.getStringAttribute(CODE)); |
| 80 |
} else if (cxo.getName().equals(ALIAS)) { |
| 81 |
|
| 82 |
} else if (cxo.getName().equals(AMBIGUITY)) { |
| 83 |
|
| 84 |
} else { |
| 85 |
throw new XMLParseException("illegal element, " + cxo.getName() + ", in " + getParserName() + " element"); |
| 86 |
} |
| 87 |
} else if (xo.getChild(i) instanceof Identifiable) { |
| 88 |
states.add(((Identifiable)xo.getChild(i)).getId()); |
| 89 |
} else { |
| 90 |
throw new XMLParseException("illegal element in " + getParserName() + " element"); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if (states.size() == 0) { |
| 95 |
throw new XMLParseException("No state elements defined in " + getParserName() + " element"); |
| 96 |
} else if (states.size() < 2 ) { |
| 97 |
throw new XMLParseException("Less than two state elements defined in " + getParserName() + " element"); |
| 98 |
} |
| 99 |
|
| 100 |
GeneralDataType dataType = new GeneralDataType(states); |
| 101 |
|
| 102 |
for (int i =0; i < xo.getChildCount(); i++) { |
| 103 |
if (xo.getChild(i) instanceof XMLObject) { |
| 104 |
XMLObject cxo = (XMLObject)xo.getChild(i); |
| 105 |
if (cxo.getName().equals(ALIAS)) { |
| 106 |
|
| 107 |
String alias = cxo.getStringAttribute(CODE); |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
String state = cxo.getStringAttribute(STATE); |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
try { |
| 118 |
dataType.addAlias(alias, state); |
| 119 |
} catch (IllegalArgumentException iae) { |
| 120 |
throw new XMLParseException(iae.getMessage() + "in " + getParserName() + " element"); |
| 121 |
} |
| 122 |
|
| 123 |
} else if (cxo.getName().equals(AMBIGUITY)) { |
| 124 |
|
| 125 |
String code = cxo.getStringAttribute(CODE); |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
String[] ambiguities = cxo.getStringArrayAttribute(STATES); |
| 131 |
if (ambiguities.length == 1) { |
| 132 |
String codes = ambiguities[0]; |
| 133 |
if (codes.length() < 2) { |
| 134 |
throw new XMLParseException("States for ambiguity code in " + getParserName() + " element are not ambiguous"); |
| 135 |
} |
| 136 |
ambiguities = new String[codes.length()]; |
| 137 |
for (int j = 0; j < codes.length(); j++) { |
| 138 |
ambiguities[j] = String.valueOf(codes.charAt(j)); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
try { |
| 143 |
dataType.addAmbiguity(code, ambiguities); |
| 144 |
} catch (IllegalArgumentException iae) { |
| 145 |
throw new XMLParseException(iae.getMessage() + "in " + getParserName() + " element"); |
| 146 |
} |
| 147 |
|
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return dataType; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public String getParserDescription() { |
| 160 |
return "Defines a general DataType for any number of states"; |
| 161 |
} |
| 162 |
|
| 163 |
public String getExample() { |
| 164 |
return "<!-- The XML for a nucleotide data type under this scheme would be -->\n"+ |
| 165 |
"<generalDataType id=\"nucleotides\">\n"+ |
| 166 |
" <state code=\"A\"/>\n"+ |
| 167 |
" <state code=\"C\"/>\n"+ |
| 168 |
" <state code=\"G\"/>\n"+ |
| 169 |
" <state code=\"T\"/>\n"+ |
| 170 |
" <alias code=\"U\" state=\"T\"/>\n"+ |
| 171 |
" <ambiguity code=\"R\" states=\"AG\"/>\n"+ |
| 172 |
" <ambiguity code=\"Y\" states=\"CT\"/>\n"+ |
| 173 |
" <ambiguity code=\"?\" states=\"ACGT\"/>\n"+ |
| 174 |
" <ambiguity code=\"-\" states=\"ACGT\"/>\n"+ |
| 175 |
"</generalDataType>\n"; |
| 176 |
} |
| 177 |
|
| 178 |
public Class getReturnType() { return DataType.class; } |
| 179 |
|
| 180 |
public XMLSyntaxRule[] getSyntaxRules() { return rules; } |
| 181 |
|
| 182 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[] { |
| 183 |
new ElementRule(Identifiable.class, 0, Integer.MAX_VALUE), |
| 184 |
new ContentRule("<state code=\"X\"/>"), |
| 185 |
new ContentRule("<alias code=\"Y\" state=\"X\"/>"), |
| 186 |
new ContentRule("<ambiguity code=\"Z\" states=\"XY\"/>") |
| 187 |
}; |
| 188 |
} |