| 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.app.beagle.tools.parsers; |
| 27 |
|
| 28 |
import dr.evomodel.branchmodel.BranchModel; |
| 29 |
import dr.evomodel.branchmodel.HomogeneousBranchModel; |
| 30 |
import dr.evomodel.siteratemodel.GammaSiteRateModel; |
| 31 |
import dr.evomodel.substmodel.FrequencyModel; |
| 32 |
import dr.evomodel.substmodel.SubstitutionModel; |
| 33 |
import dr.app.beagle.tools.Partition; |
| 34 |
import dr.evolution.sequence.Sequence; |
| 35 |
import dr.evolution.datatype.DataType; |
| 36 |
import dr.evomodel.branchratemodel.BranchRateModel; |
| 37 |
import dr.evomodel.branchratemodel.DefaultBranchRateModel; |
| 38 |
import dr.evomodel.tree.TreeModel; |
| 39 |
import dr.evoxml.util.DataTypeUtils; |
| 40 |
import dr.xml.AbstractXMLObjectParser; |
| 41 |
import dr.xml.AttributeRule; |
| 42 |
import dr.xml.ElementRule; |
| 43 |
import dr.xml.XMLObject; |
| 44 |
import dr.xml.XMLParseException; |
| 45 |
import dr.xml.XMLSyntaxRule; |
| 46 |
import dr.xml.XORRule; |
| 47 |
|
| 48 |
import java.util.ArrayList; |
| 49 |
import java.util.List; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public class PartitionParser extends AbstractXMLObjectParser { |
| 56 |
|
| 57 |
public static final String PARTITION = "partition"; |
| 58 |
public static final String FROM = "from"; |
| 59 |
public static final String TO = "to"; |
| 60 |
public static final String EVERY = "every"; |
| 61 |
public static final String DATA_TYPE = "dataType"; |
| 62 |
|
| 63 |
@Override |
| 64 |
public String getParserName() { |
| 65 |
return PARTITION; |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public String getParserDescription() { |
| 70 |
return "Partition element"; |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public Class<Partition> getReturnType() { |
| 75 |
return Partition.class; |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 80 |
|
| 81 |
int from = 0; |
| 82 |
int to = -1; |
| 83 |
int every = xo.getAttribute(EVERY, 1); |
| 84 |
DataType dataType = null; |
| 85 |
|
| 86 |
if (xo.hasAttribute(FROM)) { |
| 87 |
from = xo.getIntegerAttribute(FROM) - 1; |
| 88 |
|
| 89 |
if (from < 0) { |
| 90 |
throw new XMLParseException( |
| 91 |
"Illegal 'from' attribute in patterns element"); |
| 92 |
} |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
if (xo.hasAttribute(TO)) { |
| 97 |
|
| 98 |
to = xo.getIntegerAttribute(TO) - 1; |
| 99 |
if (to < 0 || to < from) { |
| 100 |
throw new XMLParseException( |
| 101 |
"Illegal 'to' attribute in patterns element"); |
| 102 |
} |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
if (every <= 0) { |
| 107 |
throw new XMLParseException( |
| 108 |
"Illegal 'every' attribute in patterns element"); |
| 109 |
} |
| 110 |
|
| 111 |
if (xo.hasAttribute(DATA_TYPE)){ |
| 112 |
dataType = DataTypeUtils.getDataType(xo); |
| 113 |
} |
| 114 |
|
| 115 |
TreeModel tree = (TreeModel) xo.getChild(TreeModel.class); |
| 116 |
|
| 117 |
GammaSiteRateModel siteModel = (GammaSiteRateModel) xo.getChild(GammaSiteRateModel.class); |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
FrequencyModel freqModel; |
| 122 |
|
| 123 |
List<FrequencyModel> freqModels = new ArrayList<FrequencyModel>(); |
| 124 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 125 |
Object cxo = xo.getChild(i); |
| 126 |
if (cxo instanceof FrequencyModel) { |
| 127 |
freqModels.add((FrequencyModel) cxo); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
if(freqModels.size()==1){ |
| 132 |
freqModel = freqModels.get(0); |
| 133 |
}else{ |
| 134 |
double [] freqParameter = new double[freqModels.size()*freqModels.get(0).getFrequencyCount()]; |
| 135 |
int index = 0; |
| 136 |
for(int i = 0; i < freqModels.size(); i++){ |
| 137 |
for(int j = 0; j < freqModels.get(i).getFrequencyCount(); j++){ |
| 138 |
freqParameter[index] = (freqModels.get(i).getFrequency(j))/freqModels.size(); |
| 139 |
index++; |
| 140 |
} |
| 141 |
} |
| 142 |
freqModel = new FrequencyModel(dataType, freqParameter); |
| 143 |
} |
| 144 |
|
| 145 |
Sequence rootSequence = (Sequence) xo.getChild(Sequence.class); |
| 146 |
|
| 147 |
BranchRateModel rateModel = (BranchRateModel) xo.getChild(BranchRateModel.class); |
| 148 |
if (rateModel == null) { |
| 149 |
rateModel = new DefaultBranchRateModel(); |
| 150 |
} |
| 151 |
|
| 152 |
BranchModel branchModel = (BranchModel) xo.getChild(BranchModel.class); |
| 153 |
if (branchModel == null) { |
| 154 |
|
| 155 |
SubstitutionModel substitutionModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class); |
| 156 |
branchModel = new HomogeneousBranchModel(substitutionModel); |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
Partition partition = new Partition(tree, branchModel, siteModel, rateModel, freqModel, from, to, every, dataType); |
| 161 |
|
| 162 |
if (rootSequence != null) { |
| 163 |
partition.setRootSequence(rootSequence); |
| 164 |
} |
| 165 |
|
| 166 |
return partition; |
| 167 |
} |
| 168 |
|
| 169 |
@Override |
| 170 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 171 |
|
| 172 |
return new XMLSyntaxRule[] { |
| 173 |
AttributeRule.newIntegerRule(FROM, true, |
| 174 |
"The site position to start at, default is 1 (the first position)"), |
| 175 |
AttributeRule.newIntegerRule(TO, true, |
| 176 |
"The site position to finish at, must be greater than <b>" |
| 177 |
+ FROM |
| 178 |
+ "</b>, default is number of sites"), |
| 179 |
AttributeRule.newIntegerRule( |
| 180 |
EVERY, |
| 181 |
true, |
| 182 |
"Determines how many sites are selected. A value of 3 will select every third site starting from <b>" |
| 183 |
+ FROM |
| 184 |
+ "</b>, default is 1 (every site)"), |
| 185 |
AttributeRule.newStringRule(DataType.DATA_TYPE, true), |
| 186 |
new ElementRule(TreeModel.class), |
| 187 |
new XORRule(new ElementRule(BranchModel.class), |
| 188 |
new ElementRule(SubstitutionModel.class), false), |
| 189 |
new ElementRule(GammaSiteRateModel.class), |
| 190 |
new ElementRule(BranchRateModel.class, true), |
| 191 |
new ElementRule(FrequencyModel.class, 1, Integer.MAX_VALUE), |
| 192 |
new ElementRule(Sequence.class, true) |
| 193 |
}; |
| 194 |
|
| 195 |
} |
| 196 |
|
| 197 |
} |