| 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.alignment.PatternList; |
| 29 |
import dr.evolution.alignment.Patterns; |
| 30 |
import dr.evolution.alignment.SimpleSiteList; |
| 31 |
import dr.evolution.alignment.SitePatterns; |
| 32 |
import dr.evolution.datatype.DataType; |
| 33 |
import dr.evolution.util.Taxon; |
| 34 |
import dr.evolution.util.TaxonList; |
| 35 |
import dr.evoxml.util.DataTypeUtils; |
| 36 |
import dr.xml.*; |
| 37 |
|
| 38 |
import java.util.logging.Logger; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class AttributePatternsParser extends AbstractXMLObjectParser { |
| 47 |
|
| 48 |
public static final String ATTRIBUTE = "attribute"; |
| 49 |
public static final String SECONDARY_ATTRIBUTE = "secondary"; |
| 50 |
|
| 51 |
public static final String PATTERNS = "Patterns"; |
| 52 |
public static final String ATTRIBUTE_PATTERNS = ATTRIBUTE + PATTERNS; |
| 53 |
|
| 54 |
public String getParserName() { return ATTRIBUTE_PATTERNS; } |
| 55 |
|
| 56 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 57 |
|
| 58 |
String attributeName = xo.getStringAttribute(ATTRIBUTE); |
| 59 |
String secondaryAttributeName = xo.getAttribute(SECONDARY_ATTRIBUTE, (String)null); |
| 60 |
TaxonList taxa = (TaxonList)xo.getChild(TaxonList.class); |
| 61 |
DataType dataType = DataTypeUtils.getDataType(xo); |
| 62 |
|
| 63 |
if (dataType == null) { |
| 64 |
throw new XMLParseException("dataType expected for attributePatterns element"); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
SimpleSiteList patterns = new SimpleSiteList(dataType, taxa); |
| 69 |
|
| 70 |
int[] pattern = new int[taxa.getTaxonCount()]; |
| 71 |
|
| 72 |
boolean attributeFound = false; |
| 73 |
|
| 74 |
for (int i = 0; i < taxa.getTaxonCount(); i++) { |
| 75 |
Taxon taxon = taxa.getTaxon(i); |
| 76 |
|
| 77 |
if (secondaryAttributeName == null || secondaryAttributeName.isEmpty()) { |
| 78 |
Object value = taxon.getAttribute(attributeName); |
| 79 |
|
| 80 |
if (value != null) { |
| 81 |
int state = dataType.getState(value.toString()); |
| 82 |
if (state < 0) { |
| 83 |
throw new XMLParseException("State for attribute, " + attributeName + ", in taxon, " + taxon.getId() + ", is unknown: " + value.toString()); |
| 84 |
} |
| 85 |
pattern[i] = state; |
| 86 |
attributeFound = true; |
| 87 |
} else { |
| 88 |
pattern[i] = dataType.getUnknownState(); |
| 89 |
} |
| 90 |
} else { |
| 91 |
Object value1 = taxon.getAttribute(attributeName); |
| 92 |
Object value2 = taxon.getAttribute(secondaryAttributeName); |
| 93 |
|
| 94 |
if (value1 != null && value2 != null) { |
| 95 |
String code = value1.toString() + CompositeDataTypeParser.COMPOSITE_STATE_SEPARATOR + value2.toString(); |
| 96 |
int state = dataType.getState(code); |
| 97 |
if (state < 0) { |
| 98 |
throw new XMLParseException("State for attributes, " + attributeName + " & " + secondaryAttributeName + ", in taxon, " + taxon.getId() + ", is unknown: " + code); |
| 99 |
} |
| 100 |
pattern[i] = state; |
| 101 |
attributeFound = true; |
| 102 |
} else { |
| 103 |
pattern[i] = dataType.getUnknownState(); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
if (!attributeFound) { |
| 109 |
throw new XMLParseException("The attribute, " + attributeName + " was missing in all taxa. Check the name of the attribute."); |
| 110 |
} |
| 111 |
|
| 112 |
patterns.addPattern(pattern); |
| 113 |
|
| 114 |
if (xo.hasAttribute(XMLParser.ID)) { |
| 115 |
Logger.getLogger("dr.evoxml").info("Read attribute patterns, '" + xo.getId() + "' for attribute, "+ attributeName); |
| 116 |
} else { |
| 117 |
Logger.getLogger("dr.evoxml").info("Read attribute patterns for attribute, "+ attributeName); |
| 118 |
} |
| 119 |
|
| 120 |
return patterns; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public XMLSyntaxRule[] getSyntaxRules() { return rules; } |
| 128 |
|
| 129 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[] { |
| 130 |
new XORRule( |
| 131 |
new StringAttributeRule( |
| 132 |
DataType.DATA_TYPE, |
| 133 |
"The data type", |
| 134 |
DataType.getRegisteredDataTypeNames(), false), |
| 135 |
new ElementRule(DataType.class) |
| 136 |
), |
| 137 |
AttributeRule.newStringRule(ATTRIBUTE), |
| 138 |
AttributeRule.newStringRule(SECONDARY_ATTRIBUTE, true), |
| 139 |
new ElementRule(TaxonList.class, "The taxon set") |
| 140 |
}; |
| 141 |
|
| 142 |
public String getParserDescription() { |
| 143 |
return "A site pattern defined by an attribute in a set of taxa."; |
| 144 |
} |
| 145 |
|
| 146 |
public Class getReturnType() { return PatternList.class; } |
| 147 |
|
| 148 |
} |