| 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.oldevomodel.lineage; |
| 27 |
|
| 28 |
import dr.evolution.alignment.Alignment; |
| 29 |
import dr.evolution.alignment.PatternList; |
| 30 |
import dr.evolution.util.TaxonList; |
| 31 |
import dr.xml.*; |
| 32 |
|
| 33 |
import java.util.logging.Logger; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class LineageSitePatternsParser extends AbstractXMLObjectParser { |
| 40 |
|
| 41 |
public static final String PATTERNS = "lineageSNPS"; |
| 42 |
public static final String FROM = "from"; |
| 43 |
public static final String TO = "to"; |
| 44 |
public static final String EVERY = "every"; |
| 45 |
public static final String TAXON_LIST = "taxonList"; |
| 46 |
public static final String STRIP = "strip"; |
| 47 |
public static final String UNIQUE = "unique"; |
| 48 |
|
| 49 |
|
| 50 |
public String getParserName() { |
| 51 |
return PATTERNS; |
| 52 |
} |
| 53 |
|
| 54 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 55 |
|
| 56 |
Alignment alignment = (Alignment) xo.getChild(Alignment.class); |
| 57 |
TaxonList taxa = null; |
| 58 |
|
| 59 |
int from = 0; |
| 60 |
int to = -1; |
| 61 |
int every = xo.getAttribute(EVERY, 1); |
| 62 |
|
| 63 |
boolean strip = xo.getAttribute(STRIP, true); |
| 64 |
|
| 65 |
boolean unique = xo.getAttribute(UNIQUE, true); |
| 66 |
|
| 67 |
if (xo.hasAttribute(FROM)) { |
| 68 |
from = xo.getIntegerAttribute(FROM) - 1; |
| 69 |
if (from < 0) |
| 70 |
throw new XMLParseException("illegal 'from' attribute in patterns element"); |
| 71 |
} |
| 72 |
|
| 73 |
if (xo.hasAttribute(TO)) { |
| 74 |
to = xo.getIntegerAttribute(TO) - 1; |
| 75 |
if (to < 0 || to < from) |
| 76 |
throw new XMLParseException("illegal 'to' attribute in patterns element"); |
| 77 |
} |
| 78 |
|
| 79 |
if (every <= 0) throw new XMLParseException("illegal 'every' attribute in patterns element"); |
| 80 |
|
| 81 |
if (xo.hasChildNamed(TAXON_LIST)) { |
| 82 |
taxa = (TaxonList) xo.getElementFirstChild(TAXON_LIST); |
| 83 |
} |
| 84 |
|
| 85 |
if (from > alignment.getSiteCount()) |
| 86 |
throw new XMLParseException("illegal 'from' attribute in patterns element"); |
| 87 |
|
| 88 |
if (to > alignment.getSiteCount()) |
| 89 |
throw new XMLParseException("illegal 'to' attribute in patterns element"); |
| 90 |
|
| 91 |
LineageSitePatterns patterns = new LineageSitePatterns(alignment, taxa, from, to, every, strip, unique); |
| 92 |
|
| 93 |
int f = from + 1; |
| 94 |
int t = to + 1; |
| 95 |
if (to == -1) t = alignment.getSiteCount(); |
| 96 |
|
| 97 |
if (xo.hasAttribute(XMLParser.ID)) { |
| 98 |
final Logger logger = Logger.getLogger("dr.evoxml"); |
| 99 |
logger.info("Site patterns '" + xo.getId() + "' created from positions " + |
| 100 |
Integer.toString(f) + "-" + Integer.toString(t) + |
| 101 |
" of alignment '" + alignment.getId() + "'"); |
| 102 |
|
| 103 |
if (every > 1) { |
| 104 |
logger.info(" only using every " + every + " site"); |
| 105 |
} |
| 106 |
logger.info(" pattern count = " + patterns.getPatternCount()); |
| 107 |
} |
| 108 |
|
| 109 |
return patterns; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 117 |
return rules; |
| 118 |
} |
| 119 |
|
| 120 |
private final XMLSyntaxRule[] rules = { |
| 121 |
AttributeRule.newIntegerRule(FROM, true, "The site position to start at, default is 1 (the first position)"), |
| 122 |
AttributeRule.newIntegerRule(TO, true, "The site position to finish at, must be greater than <b>" + FROM + "</b>, default is length of given alignment"), |
| 123 |
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)"), |
| 124 |
new ElementRule(TAXON_LIST, |
| 125 |
new XMLSyntaxRule[]{new ElementRule(TaxonList.class)}, true), |
| 126 |
new ElementRule(Alignment.class), |
| 127 |
AttributeRule.newBooleanRule(STRIP, true, "Strip out completely ambiguous sites"), |
| 128 |
AttributeRule.newBooleanRule(UNIQUE, true, "Return a weight list of unique patterns"), |
| 129 |
}; |
| 130 |
|
| 131 |
public String getParserDescription() { |
| 132 |
return "A weighted list of the unique site patterns (unique columns) in an alignment."; |
| 133 |
} |
| 134 |
|
| 135 |
public Class getReturnType() { |
| 136 |
return PatternList.class; |
| 137 |
} |
| 138 |
|
| 139 |
} |