Debian Codegraph @

src/dr/evoxml/MicrosatellitePatternParser.java

raw symbols SCIP
1 /*
2 * MicrosatellitePatternParser.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evoxml;
27
28 import dr.xml.*;
29 import dr.evolution.util.Taxa;
30 import dr.evolution.datatype.Microsatellite;
31 import dr.evolution.alignment.Patterns;
32 import java.util.logging.Logger;
33
34 /**
35 * @author Chieh-Hsi Wu
36 *
37 * Parser that returns a list of microsatellite patterns
38 *
39 */
40 public class MicrosatellitePatternParser extends AbstractXMLObjectParser {
41
42 public static final String MICROSATPATTERN = "microsatellitePattern";
43 public static final String MICROSAT_SEQ = "microsatSeq";
44 public static final String PRINT_DETAILS = "printDetails";
45 public static final String PRINT_MSAT_PATTERN_CONTENT = "printMsatPatContent";
46 public static final String ID ="id";
47
48
49 public static final int COUNT_INCREMENT = 100;
50
51 public String getParserName() {
52 return MICROSATPATTERN;
53 }
54
55 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
56
57 Taxa taxonList = (Taxa)xo.getChild(Taxa.class);
58
59 Microsatellite microsatellite = (Microsatellite)xo.getChild(Microsatellite.class);
60
61 String[] strLengths = ((String) xo.getElementFirstChild(MICROSAT_SEQ)).split(",");
62 int[] pattern = new int[strLengths.length];
63 try {
64 for (int i = 0; i < strLengths.length; i++) {
65 pattern[i] = microsatellite.getState(strLengths[i]);
66 }
67 } catch (NumberFormatException nfe) {
68 throw new XMLParseException("Unable to parse microsatellite data: " + nfe.getMessage());
69 } catch (IllegalArgumentException iae) {
70 throw new XMLParseException("Unable to parse microsatellite data: " + iae.getMessage());
71 }
72
73 Patterns microsatPat = new Patterns(microsatellite, taxonList);
74 microsatPat.addPattern(pattern);
75 microsatPat.setId((String)xo.getAttribute(ID));
76
77 if(xo.getAttribute(PRINT_DETAILS,true)){
78 printDetails(microsatPat);
79 }
80
81 if(xo.getAttribute(PRINT_MSAT_PATTERN_CONTENT,true)){
82 printMicrosatContent(microsatPat);
83 }
84
85 return microsatPat;
86
87 }
88
89 public static void printDetails(Patterns microsatPat){
90 Logger.getLogger("dr.evoxml").info(
91 " Locus name: "+microsatPat.getId()+
92 "\n Number of Taxa: "+microsatPat.getPattern(0).length+
93 "\n min: "+((Microsatellite)microsatPat.getDataType()).getMin()+" "+
94 "max: "+((Microsatellite)microsatPat.getDataType()).getMax()+
95 "\n state count: "+microsatPat.getDataType().getStateCount()+"\n");
96 }
97
98 public static void printMicrosatContent(Patterns microsatPat){
99 Logger.getLogger("dr.evoxml").info(
100 " Locus name: "+ microsatPat.getId());
101 int[] pat = microsatPat.getPattern(0);
102 for(int i = 0; i < pat.length; i++){
103 Logger.getLogger("dr.evoxml").info(" Taxon: "+microsatPat.getTaxon(i)+" "+"state: "+pat[i]);
104 }
105 Logger.getLogger("dr.evoxml").info("\n");
106 }
107
108
109 public XMLSyntaxRule[] getSyntaxRules() {
110 return rules;
111 }
112
113 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
114 new ElementRule(Taxa.class),
115 new ElementRule(Microsatellite.class),
116 new ElementRule(MICROSAT_SEQ,new XMLSyntaxRule[]{
117 new ElementRule(String.class,
118 "A string of numbers representing the microsatellite lengths for a locus",
119 "1,2,3,4,5,67,100")},false),
120 new StringAttributeRule(ID, "the name of the locus"),
121 AttributeRule.newBooleanRule(PRINT_DETAILS, true),
122 AttributeRule.newBooleanRule(PRINT_MSAT_PATTERN_CONTENT, true)
123
124 };
125
126 public String getParserDescription() {
127 return "This element represents a microsatellite pattern.";
128 }
129
130 public Class getReturnType() {
131 return Patterns.class;
132 }
133
134
135 }