| 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.AscertainedSitePatterns; |
| 30 |
import dr.evolution.alignment.PatternList; |
| 31 |
import dr.evolution.util.TaxonList; |
| 32 |
import dr.xml.*; |
| 33 |
import dr.util.Citable; |
| 34 |
|
| 35 |
import java.util.logging.Logger; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public class AscertainedSitePatternsParser extends AbstractXMLObjectParser { |
| 48 |
|
| 49 |
public static final String APATTERNS = "ascertainedPatterns"; |
| 50 |
public static final String FROM = "from"; |
| 51 |
public static final String TO = "to"; |
| 52 |
public static final String EVERY = "every"; |
| 53 |
public static final String TAXON_LIST = "taxonList"; |
| 54 |
public static final String INCLUDE = "includePatterns"; |
| 55 |
public static final String EXCLUDE = "excludePatterns"; |
| 56 |
|
| 57 |
public String getParserName() { |
| 58 |
return APATTERNS; |
| 59 |
} |
| 60 |
|
| 61 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 62 |
Alignment alignment = (Alignment) xo.getChild(Alignment.class); |
| 63 |
XMLObject xoc; |
| 64 |
TaxonList taxa = null; |
| 65 |
|
| 66 |
int from = -1; |
| 67 |
int to = -1; |
| 68 |
int every = xo.getAttribute(EVERY, 1); |
| 69 |
if (every <= 0) throw new XMLParseException("illegal 'every' attribute in patterns element"); |
| 70 |
|
| 71 |
int startInclude = -1; |
| 72 |
int stopInclude = -1; |
| 73 |
int startExclude = -1; |
| 74 |
int stopExclude = -1; |
| 75 |
|
| 76 |
if (xo.hasAttribute(FROM)) { |
| 77 |
from = xo.getIntegerAttribute(FROM) - 1; |
| 78 |
if (from < 0) |
| 79 |
throw new XMLParseException("illegal 'from' attribute in patterns element"); |
| 80 |
} |
| 81 |
|
| 82 |
if (xo.hasAttribute(TO)) { |
| 83 |
to = xo.getIntegerAttribute(TO) - 1; |
| 84 |
if (to < 0 || to < from) |
| 85 |
throw new XMLParseException("illegal 'to' attribute in patterns element"); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
if (xo.hasChildNamed(TAXON_LIST)) { |
| 90 |
taxa = (TaxonList) xo.getElementFirstChild(TAXON_LIST); |
| 91 |
} |
| 92 |
|
| 93 |
if (from > alignment.getSiteCount()) |
| 94 |
throw new XMLParseException("illegal 'from' attribute in patterns element"); |
| 95 |
|
| 96 |
if (to > alignment.getSiteCount()) |
| 97 |
throw new XMLParseException("illegal 'to' attribute in patterns element"); |
| 98 |
|
| 99 |
if (from < 0) from = 0; |
| 100 |
if (to < 0) to = alignment.getSiteCount() - 1; |
| 101 |
|
| 102 |
|
| 103 |
Logger.getLogger("dr.evoxml").info("Creating ascertained site patterns '" + xo.getId() + "' from positions " + |
| 104 |
Integer.toString(from + 1) + "-" + Integer.toString(to + 1) + |
| 105 |
" of alignment '" + alignment.getId() + "'"); |
| 106 |
if (every > 1) { |
| 107 |
Logger.getLogger("dr.evoxml").info(" only using every " + every + " site"); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
if (xo.hasChildNamed(INCLUDE)) { |
| 112 |
xoc = xo.getChild(INCLUDE); |
| 113 |
if (xoc.hasAttribute(FROM) && xoc.hasAttribute(TO)) { |
| 114 |
startInclude = xoc.getIntegerAttribute(FROM) - 1; |
| 115 |
stopInclude = xoc.getIntegerAttribute(TO); |
| 116 |
} else { |
| 117 |
throw new XMLParseException("both from and to attributes are required for includePatterns"); |
| 118 |
} |
| 119 |
|
| 120 |
if (startInclude < 0 || stopInclude < startInclude) { |
| 121 |
throw new XMLParseException("invalid 'from' and 'to' attributes in includePatterns"); |
| 122 |
} |
| 123 |
Logger.getLogger("dr.evoxml").info("\tAscertainment: Patterns in columns " + (startInclude + 1) + " to " + (stopInclude) + " are only possible. "); |
| 124 |
} |
| 125 |
|
| 126 |
if (xo.hasChildNamed(EXCLUDE)) { |
| 127 |
xoc = xo.getChild(EXCLUDE); |
| 128 |
if (xoc.hasAttribute(FROM) && xoc.hasAttribute(TO)) { |
| 129 |
startExclude = xoc.getIntegerAttribute(FROM) - 1; |
| 130 |
stopExclude = xoc.getIntegerAttribute(TO); |
| 131 |
} else { |
| 132 |
throw new XMLParseException("both from and to attributes are required for excludePatterns"); |
| 133 |
} |
| 134 |
|
| 135 |
if (startExclude < 0 || stopExclude < startExclude) { |
| 136 |
throw new XMLParseException("invalid 'from' and 'to' attributes in includePatterns"); |
| 137 |
} |
| 138 |
Logger.getLogger("dr.evoxml").info("\tAscertainment: Patterns in columns " + (startExclude + 1) + " to " + (stopExclude) + " are not possible. "); |
| 139 |
} |
| 140 |
|
| 141 |
AscertainedSitePatterns patterns = new AscertainedSitePatterns(alignment, taxa, |
| 142 |
from, to, every, |
| 143 |
startInclude, stopInclude, |
| 144 |
startExclude, stopExclude); |
| 145 |
|
| 146 |
Logger.getLogger("dr.evoxml").info("\tThere are " + patterns.getPatternCount() + " patterns in total."); |
| 147 |
|
| 148 |
Logger.getLogger("dr.evoxml").info("\tPlease cite:\n" + Citable.Utils.getCitationString(patterns)); |
| 149 |
|
| 150 |
return patterns; |
| 151 |
} |
| 152 |
|
| 153 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 154 |
return rules; |
| 155 |
} |
| 156 |
|
| 157 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 158 |
AttributeRule.newIntegerRule(FROM, true, "The site position to start at, default is 1 (the first position)"), |
| 159 |
AttributeRule.newIntegerRule(TO, true, "The site position to finish at, must be greater than <b>" + FROM + "</b>, default is length of given alignment"), |
| 160 |
AttributeRule.newIntegerRule(EVERY, true, "Determines how many sites are selected. A value of 3 will select every third site starting from <b>" + FROM + "</b>, default is 1 (every site)"), |
| 161 |
new ElementRule(TAXON_LIST, |
| 162 |
new XMLSyntaxRule[]{new ElementRule(TaxonList.class)}, true), |
| 163 |
new ElementRule(Alignment.class), |
| 164 |
new ContentRule("<includePatterns from=\"Z\" to=\"X\"/>"), |
| 165 |
new ContentRule("<excludePatterns from=\"Z\" to=\"X\"/>") |
| 166 |
}; |
| 167 |
|
| 168 |
public String getParserDescription() { |
| 169 |
return "A weighted list of the unique site patterns (unique columns) in an alignment."; |
| 170 |
} |
| 171 |
|
| 172 |
public Class getReturnType() { |
| 173 |
return PatternList.class; |
| 174 |
} |
| 175 |
|
| 176 |
} |