| 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.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 |
|
| 36 |
|
| 37 |
|
| 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 |
} |