| 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.Alignment; |
| 29 |
import dr.evolution.alignment.SimpleAlignment; |
| 30 |
import dr.evolution.datatype.DataType; |
| 31 |
import dr.evolution.datatype.Nucleotides; |
| 32 |
import dr.evolution.sequence.Sequence; |
| 33 |
import dr.evolution.sequence.UncertainSequence; |
| 34 |
import dr.evoxml.util.DataTypeUtils; |
| 35 |
import dr.xml.*; |
| 36 |
|
| 37 |
import java.util.logging.Logger; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class AlignmentParser extends AbstractXMLObjectParser { |
| 45 |
|
| 46 |
public static final String ALIGNMENT = "alignment"; |
| 47 |
|
| 48 |
public String getParserName() { |
| 49 |
return ALIGNMENT; |
| 50 |
} |
| 51 |
|
| 52 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 53 |
|
| 54 |
final SimpleAlignment alignment = new SimpleAlignment(); |
| 55 |
|
| 56 |
final DataType dataType = DataTypeUtils.getDataType(xo); |
| 57 |
|
| 58 |
if (dataType == null) { |
| 59 |
throw new XMLParseException("dataType attribute expected for alignment element"); |
| 60 |
} |
| 61 |
|
| 62 |
alignment.setDataType(dataType); |
| 63 |
|
| 64 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 65 |
|
| 66 |
final Object child = xo.getChild(i); |
| 67 |
if (child instanceof UncertainSequence) { |
| 68 |
alignment.addSequence((UncertainSequence) child); |
| 69 |
} else if (child instanceof Sequence) { |
| 70 |
alignment.addSequence((Sequence) child); |
| 71 |
} else if (child instanceof DataType) { |
| 72 |
|
| 73 |
} else { |
| 74 |
throw new XMLParseException("Unknown child element found in alignment"); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
final Logger logger = Logger.getLogger("dr.evoxml"); |
| 79 |
logger.info("\nRead alignment" + (xo.hasAttribute(XMLParser.ID) ? ": " + xo.getId() : "") + |
| 80 |
"\n Sequences = " + alignment.getSequenceCount() + |
| 81 |
"\n Sites = " + alignment.getSiteCount() + |
| 82 |
"\n Datatype = " + alignment.getDataType().getDescription()); |
| 83 |
|
| 84 |
return alignment; |
| 85 |
} |
| 86 |
|
| 87 |
public String getParserDescription() { |
| 88 |
return "This element represents an alignment of molecular sequences."; |
| 89 |
} |
| 90 |
|
| 91 |
public Class getReturnType() { |
| 92 |
return Alignment.class; |
| 93 |
} |
| 94 |
|
| 95 |
public String getExample() { |
| 96 |
|
| 97 |
return |
| 98 |
"<!-- An alignment of three short DNA sequences -->\n" + |
| 99 |
"<alignment missing=\"-?\" dataType=\"" + Nucleotides.DESCRIPTION + "\">\n" + |
| 100 |
" <sequence>\n" + |
| 101 |
" <taxon idref=\"taxon1\"/>\n" + |
| 102 |
" ACGACTAGCATCGAGCTTCG--GATAGCAGGC\n" + |
| 103 |
" </sequence>\n" + |
| 104 |
" <sequence>\n" + |
| 105 |
" <taxon idref=\"taxon2\"/>\n" + |
| 106 |
" ACGACTAGCATCGAGCTTCGG-GATAGCATGC\n" + |
| 107 |
" </sequence>\n" + |
| 108 |
" <sequence>\n" + |
| 109 |
" <taxon idref=\"taxon3\"/>\n" + |
| 110 |
" ACG?CTAGAATCGAGCTTCGAGGATAGCATGC\n" + |
| 111 |
" </sequence>\n" + |
| 112 |
"</alignment>\n"; |
| 113 |
} |
| 114 |
|
| 115 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 116 |
return rules; |
| 117 |
} |
| 118 |
|
| 119 |
private final XMLSyntaxRule[] rules = { |
| 120 |
new XORRule( |
| 121 |
new StringAttributeRule( |
| 122 |
DataType.DATA_TYPE, |
| 123 |
"The data type", |
| 124 |
DataType.getRegisteredDataTypeNames(), false), |
| 125 |
new ElementRule(DataType.class) |
| 126 |
), |
| 127 |
new ElementRule(Sequence.class, 1, Integer.MAX_VALUE) |
| 128 |
}; |
| 129 |
} |