| 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.evomodel.coalescent.structure; |
| 27 |
|
| 28 |
import dr.evolution.coalescent.structure.MetaPopulation; |
| 29 |
import dr.evomodel.coalescent.DemographicModel; |
| 30 |
import dr.inference.model.*; |
| 31 |
import dr.xml.*; |
| 32 |
|
| 33 |
import java.util.ArrayList; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
@Deprecated |
| 43 |
public class MetaPopulationModel extends AbstractModel implements MetaPopulation { |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public static final String META_POPULATION_MODEL = "metaPopulationModel"; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public MetaPopulationModel(ArrayList<DemographicModel> demographicModels, Parameter populationProportions) { |
| 54 |
|
| 55 |
this(META_POPULATION_MODEL, demographicModels, populationProportions); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public MetaPopulationModel(String name, ArrayList<DemographicModel> demographicModelList, Parameter populationProportions) { |
| 62 |
|
| 63 |
super(name); |
| 64 |
|
| 65 |
this.populationProportions = populationProportions; |
| 66 |
|
| 67 |
if (populationProportions != null) { |
| 68 |
|
| 69 |
|
| 70 |
addVariable(populationProportions); |
| 71 |
populationProportions.addBounds(new Parameter.DefaultBounds(1.0, 0.0, populationProportions.getDimension())); |
| 72 |
populationCount = populationProportions.getDimension() + 1; |
| 73 |
|
| 74 |
for (int i = 1; i < populationCount; i++) { |
| 75 |
demographicModelList.add(demographicModelList.get(0)); |
| 76 |
} |
| 77 |
demographicModels = demographicModelList.toArray(new DemographicModel[0]); |
| 78 |
addModel(demographicModels[0]); |
| 79 |
|
| 80 |
} else { |
| 81 |
|
| 82 |
|
| 83 |
populationCount = demographicModelList.size(); |
| 84 |
demographicModels = demographicModelList.toArray(new DemographicModel[0]); |
| 85 |
for (int i = 0; i < populationCount; i++) { |
| 86 |
addModel(demographicModels[i]); |
| 87 |
} |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
addStatistic(populationSizesStatistic); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
private double getProportion(int population) { |
| 97 |
|
| 98 |
if (populationProportions == null) { |
| 99 |
return 1.0; |
| 100 |
} |
| 101 |
if (population > 0) { |
| 102 |
return populationProportions.getParameterValue(population - 1); |
| 103 |
} |
| 104 |
double proportion = 1.0; |
| 105 |
for (int i = 1; i < populationCount; i++) { |
| 106 |
proportion -= populationProportions.getParameterValue(i - 1); |
| 107 |
} |
| 108 |
return proportion; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
public int getPopulationCount() { |
| 113 |
|
| 114 |
return populationCount; |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
public double[] getPopulationSizes(double time) { |
| 119 |
|
| 120 |
|
| 121 |
double[] N = new double[populationCount]; |
| 122 |
for (int i = 0; i < populationCount; i++) { |
| 123 |
N[i] = demographicModels[i].getDemographicFunction().getDemographic(time) * getProportion(i); |
| 124 |
} |
| 125 |
|
| 126 |
return N; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public double getDemographic(double time, int population) { |
| 133 |
|
| 134 |
return demographicModels[population].getDemographicFunction().getDemographic(time) * getProportion(population); |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public double getIntegral(double start, double finish, int population) { |
| 142 |
|
| 143 |
double integral = demographicModels[population].getDemographicFunction().getIntegral(start, finish); |
| 144 |
return integral / getProportion(population); |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 153 |
fireModelChanged(); |
| 154 |
} |
| 155 |
|
| 156 |
protected void storeState() { |
| 157 |
} |
| 158 |
|
| 159 |
protected void restoreState() { |
| 160 |
fireModelChanged(); |
| 161 |
} |
| 162 |
|
| 163 |
protected void acceptState() { |
| 164 |
} |
| 165 |
|
| 166 |
public void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
private Statistic populationSizesStatistic = new Statistic.Abstract() { |
| 171 |
|
| 172 |
public String getStatisticName() { |
| 173 |
return "populationSizes"; |
| 174 |
} |
| 175 |
|
| 176 |
public int getDimension() { |
| 177 |
return populationCount; |
| 178 |
} |
| 179 |
|
| 180 |
public double getStatisticValue(int dim) { |
| 181 |
|
| 182 |
double metaN0 = demographicModels[dim].getDemographicFunction().getDemographic(0); |
| 183 |
return metaN0 * getProportion(dim); |
| 184 |
} |
| 185 |
|
| 186 |
}; |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 192 |
|
| 193 |
public String getParserName() { |
| 194 |
return MetaPopulationModel.META_POPULATION_MODEL; |
| 195 |
} |
| 196 |
|
| 197 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 198 |
|
| 199 |
ArrayList<DemographicModel> demographics = new ArrayList<DemographicModel>(0); |
| 200 |
Parameter populationProportions = null; |
| 201 |
|
| 202 |
for (int i = 0; i < xo.getChildCount(); ++i) { |
| 203 |
final Object o = xo.getChild(i); |
| 204 |
if (o instanceof DemographicModel) { |
| 205 |
demographics.add((DemographicModel) o); |
| 206 |
} else if (o instanceof Parameter) { |
| 207 |
if (populationProportions != null) { |
| 208 |
throw new Error("Allowed at most one Parameter in a MetaPopulationModel"); |
| 209 |
} |
| 210 |
populationProportions = (Parameter) o; |
| 211 |
} else { |
| 212 |
throw new Error("A MetaPopulationModel may only have children of type Parameter or DemographicModel"); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
if (populationProportions == null) { |
| 218 |
if (demographics.size() < 2) { |
| 219 |
throw new Error("A MetaPopulationModel must have at least 2 DemographicModels (or a Parameter)"); |
| 220 |
} |
| 221 |
} else { |
| 222 |
if (demographics.size() != 1) { |
| 223 |
throw new Error("A MetaPopulationModel with a Parameter must have exactly one DemographicModel"); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
return new MetaPopulationModel(demographics, populationProportions); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
public String getParserDescription() { |
| 235 |
return "A model that represents a subdivided population."; |
| 236 |
} |
| 237 |
|
| 238 |
public Class getReturnType() { |
| 239 |
return MetaPopulationModel.class; |
| 240 |
} |
| 241 |
|
| 242 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 243 |
return rules; |
| 244 |
} |
| 245 |
|
| 246 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 247 |
new ElementRule(DemographicModel.class, 1, 999), |
| 248 |
new ElementRule(Parameter.class, true) |
| 249 |
}; |
| 250 |
}; |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
private int populationCount; |
| 257 |
private Parameter populationProportions; |
| 258 |
private DemographicModel[] demographicModels; |
| 259 |
} |