| 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.evomodelxml.substmodel; |
| 27 |
|
| 28 |
import java.util.logging.Logger; |
| 29 |
|
| 30 |
import dr.evomodel.substmodel.PCARateMatrixMammalia; |
| 31 |
import dr.evomodel.substmodel.codon.PCACodonModel; |
| 32 |
import dr.evolution.datatype.Codons; |
| 33 |
import dr.evolution.datatype.DataType; |
| 34 |
import dr.evolution.datatype.GeneticCode; |
| 35 |
import dr.evomodel.substmodel.AbstractPCARateMatrix; |
| 36 |
import dr.evomodel.substmodel.FrequencyModel; |
| 37 |
import dr.inference.model.Parameter; |
| 38 |
import dr.xml.*; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class PCACodonModelParser extends AbstractXMLObjectParser { |
| 46 |
public static final String PCA_CODON_MODEL = "pcaCodonModel"; |
| 47 |
public static final String PCATYPE = "pcaType"; |
| 48 |
public static final String PCANUMBER = "pcaNumber"; |
| 49 |
public static final String PCA_DIMENSION = "pcaDimension"; |
| 50 |
public static final String PCA_DATA_DIR = "pcaDataDir"; |
| 51 |
|
| 52 |
public String getParserName() { return PCA_CODON_MODEL; } |
| 53 |
|
| 54 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 55 |
|
| 56 |
Codons codons = Codons.UNIVERSAL; |
| 57 |
if (xo.hasAttribute(GeneticCode.GENETIC_CODE)) { |
| 58 |
String codeStr = xo.getStringAttribute(GeneticCode.GENETIC_CODE); |
| 59 |
if (codeStr.equals(GeneticCode.UNIVERSAL.getName())) { |
| 60 |
codons = Codons.UNIVERSAL; |
| 61 |
} else if (codeStr.equals(GeneticCode.VERTEBRATE_MT.getName())) { |
| 62 |
codons = Codons.VERTEBRATE_MT; |
| 63 |
} else if (codeStr.equals(GeneticCode.YEAST.getName())) { |
| 64 |
codons = Codons.YEAST; |
| 65 |
} else if (codeStr.equals(GeneticCode.MOLD_PROTOZOAN_MT.getName())) { |
| 66 |
codons = Codons.MOLD_PROTOZOAN_MT; |
| 67 |
} else if (codeStr.equals(GeneticCode.INVERTEBRATE_MT.getName())) { |
| 68 |
codons = Codons.INVERTEBRATE_MT; |
| 69 |
} else if (codeStr.equals(GeneticCode.CILIATE.getName())) { |
| 70 |
codons = Codons.CILIATE; |
| 71 |
} else if (codeStr.equals(GeneticCode.ECHINODERM_MT.getName())) { |
| 72 |
codons = Codons.ECHINODERM_MT; |
| 73 |
} else if (codeStr.equals(GeneticCode.EUPLOTID_NUC.getName())) { |
| 74 |
codons = Codons.EUPLOTID_NUC; |
| 75 |
} else if (codeStr.equals(GeneticCode.BACTERIAL.getName())) { |
| 76 |
codons = Codons.BACTERIAL; |
| 77 |
} else if (codeStr.equals(GeneticCode.ALT_YEAST.getName())) { |
| 78 |
codons = Codons.ALT_YEAST; |
| 79 |
} else if (codeStr.equals(GeneticCode.ASCIDIAN_MT.getName())) { |
| 80 |
codons = Codons.ASCIDIAN_MT; |
| 81 |
} else if (codeStr.equals(GeneticCode.FLATWORM_MT.getName())) { |
| 82 |
codons = Codons.FLATWORM_MT; |
| 83 |
} else if (codeStr.equals(GeneticCode.BLEPHARISMA_NUC.getName())) { |
| 84 |
codons = Codons.BLEPHARISMA_NUC; |
| 85 |
} else if (codeStr.equals(GeneticCode.NO_STOPS.getName())) { |
| 86 |
codons = Codons.NO_STOPS; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
Parameter pcaDimensionParameter = (Parameter)xo.getElementFirstChild(PCA_DIMENSION); |
| 92 |
|
| 93 |
|
| 94 |
String dirString = "pcadata"; |
| 95 |
if(xo.hasAttribute(PCA_DATA_DIR)) { |
| 96 |
dirString = xo.getStringAttribute(PCA_DATA_DIR); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
AbstractPCARateMatrix pcaType = new PCARateMatrixMammalia(pcaDimensionParameter.getDimension(), dirString); |
| 101 |
|
| 102 |
if(xo.hasAttribute(PCATYPE)) { |
| 103 |
String pcaTypeString = xo.getStringAttribute(PCATYPE); |
| 104 |
if(pcaTypeString.equals(PCARateMatrixMammalia.getName())) { |
| 105 |
pcaType = new PCARateMatrixMammalia(pcaDimensionParameter.getDimension(), dirString); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
FrequencyModel freqModel = null; |
| 111 |
if (xo.getChild(FrequencyModel.class) != null) { |
| 112 |
freqModel = (FrequencyModel)xo.getChild(FrequencyModel.class); |
| 113 |
} else { |
| 114 |
freqModel = createNewFreqModel(codons, pcaType); |
| 115 |
} |
| 116 |
|
| 117 |
return new PCACodonModel(codons, pcaType, pcaDimensionParameter, freqModel); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
private FrequencyModel createNewFreqModel(DataType codons, AbstractPCARateMatrix type) throws XMLParseException { |
| 122 |
double[] freqs = type.getFrequencies(); |
| 123 |
double sum = 0; |
| 124 |
for (int j = 0; j < freqs.length; j++) { |
| 125 |
sum += freqs[j]; |
| 126 |
} |
| 127 |
|
| 128 |
if (Math.abs(sum - 1.0) > 1e-8) { |
| 129 |
throw new XMLParseException("Frequencies do not sum to 1 (they sum to " + sum + ")"); |
| 130 |
} |
| 131 |
|
| 132 |
FrequencyModel fm = new FrequencyModel(codons, freqs); |
| 133 |
|
| 134 |
Logger.getLogger("dr.evomodel").info("Using frequencies from data file"); |
| 135 |
|
| 136 |
return fm; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public String getParserDescription() { |
| 144 |
return "This element represents the PCA model of codon evolution."; |
| 145 |
} |
| 146 |
|
| 147 |
public Class getReturnType() { return PCACodonModel.class; } |
| 148 |
|
| 149 |
public XMLSyntaxRule[] getSyntaxRules() { return rules; } |
| 150 |
|
| 151 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[] { |
| 152 |
new StringAttributeRule(GeneticCode.GENETIC_CODE, |
| 153 |
"The genetic code to use", |
| 154 |
new String[] { |
| 155 |
GeneticCode.UNIVERSAL.getName(), |
| 156 |
GeneticCode.VERTEBRATE_MT.getName(), |
| 157 |
GeneticCode.YEAST.getName(), |
| 158 |
GeneticCode.MOLD_PROTOZOAN_MT.getName(), |
| 159 |
GeneticCode.INVERTEBRATE_MT.getName(), |
| 160 |
GeneticCode.CILIATE.getName(), |
| 161 |
GeneticCode.ECHINODERM_MT.getName(), |
| 162 |
GeneticCode.EUPLOTID_NUC.getName(), |
| 163 |
GeneticCode.BACTERIAL.getName(), |
| 164 |
GeneticCode.ALT_YEAST.getName(), |
| 165 |
GeneticCode.ASCIDIAN_MT.getName(), |
| 166 |
GeneticCode.FLATWORM_MT.getName(), |
| 167 |
GeneticCode.BLEPHARISMA_NUC.getName(), |
| 168 |
GeneticCode.NO_STOPS.getName()}, true), |
| 169 |
new StringAttributeRule(PCATYPE, |
| 170 |
"The PCA type to use", |
| 171 |
new String[] { |
| 172 |
PCARateMatrixMammalia.getName(), |
| 173 |
}, true), |
| 174 |
new StringAttributeRule(PCA_DATA_DIR, |
| 175 |
"The directory with the PCA data files", |
| 176 |
"pcadata", true), |
| 177 |
new ElementRule(PCA_DIMENSION, |
| 178 |
new XMLSyntaxRule[] { new ElementRule(Parameter.class) }), |
| 179 |
new ElementRule(FrequencyModel.class, true) |
| 180 |
}; |
| 181 |
} |
| 182 |
|