| 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; |
| 27 |
|
| 28 |
import dr.evolution.coalescent.TreeIntervals; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evomodel.tree.TreeModel; |
| 31 |
import dr.evomodelxml.coalescent.VariableDemographicModelParser; |
| 32 |
import dr.inference.model.Model; |
| 33 |
import dr.inference.model.Parameter; |
| 34 |
import dr.inference.model.Variable; |
| 35 |
import dr.util.Author; |
| 36 |
import dr.util.Citable; |
| 37 |
import dr.util.Citation; |
| 38 |
|
| 39 |
import java.util.Arrays; |
| 40 |
import java.util.List; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class VariableDemographicModel extends DemographicModel implements MultiLociTreeSet, Citable { |
| 47 |
|
| 48 |
private final Parameter popSizeParameter; |
| 49 |
private final Parameter indicatorParameter; |
| 50 |
|
| 51 |
public Type getType() { |
| 52 |
return type; |
| 53 |
} |
| 54 |
|
| 55 |
private final Type type; |
| 56 |
private final boolean logSpace; |
| 57 |
private final boolean mid; |
| 58 |
private final TreeModel[] trees; |
| 59 |
private VDdemographicFunction demoFunction = null; |
| 60 |
private VDdemographicFunction savedDemoFunction = null; |
| 61 |
private final double[] populationFactors; |
| 62 |
|
| 63 |
public Parameter getPopulationValues() { |
| 64 |
return popSizeParameter; |
| 65 |
} |
| 66 |
|
| 67 |
public enum Type { |
| 68 |
LINEAR("linear"), |
| 69 |
EXPONENTIAL("exponential"), |
| 70 |
STEPWISE("stepwise"); |
| 71 |
|
| 72 |
Type(String name) { |
| 73 |
this.name = name; |
| 74 |
} |
| 75 |
|
| 76 |
public String toString() { |
| 77 |
return name; |
| 78 |
} |
| 79 |
|
| 80 |
String name; |
| 81 |
} |
| 82 |
|
| 83 |
public VariableDemographicModel(TreeModel[] trees, double[] popFactors, |
| 84 |
Parameter popSizeParameter, Parameter indicatorParameter, |
| 85 |
Type type, boolean logSpace, boolean mid) { |
| 86 |
super(VariableDemographicModelParser.MODEL_NAME); |
| 87 |
|
| 88 |
this.popSizeParameter = popSizeParameter; |
| 89 |
this.indicatorParameter = indicatorParameter; |
| 90 |
|
| 91 |
this.populationFactors = popFactors; |
| 92 |
|
| 93 |
int events = 0; |
| 94 |
for (Tree t : trees) { |
| 95 |
|
| 96 |
events += t.getExternalNodeCount() - 1; |
| 97 |
|
| 98 |
assert t.getUnits() == trees[0].getUnits(); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
events += type == Type.STEPWISE ? 0 : 1; |
| 103 |
|
| 104 |
final int popSizes = popSizeParameter.getDimension(); |
| 105 |
final int nIndicators = indicatorParameter.getDimension(); |
| 106 |
this.type = type; |
| 107 |
this.logSpace = logSpace; |
| 108 |
this.mid = mid; |
| 109 |
|
| 110 |
if (popSizes != events) { |
| 111 |
|
| 112 |
System.err.println("INFO: resetting length of parameter " + popSizeParameter.getParameterName() + |
| 113 |
"(size " + popSizeParameter.getSize() + ") in variable demographic model to " + events); |
| 114 |
popSizeParameter.setDimension(events); |
| 115 |
popSizeParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, -Double.MAX_VALUE, popSizeParameter.getDimension())); |
| 116 |
} |
| 117 |
|
| 118 |
if (nIndicators != events - 1) { |
| 119 |
System.err.println("INFO: resetting length of parameter " + indicatorParameter.getParameterName() + |
| 120 |
" in variable demographic model to " + (events - 1)); |
| 121 |
|
| 122 |
indicatorParameter.setDimension(events - 1); |
| 123 |
indicatorParameter.addBounds(new Parameter.DefaultBounds(1, 0, indicatorParameter.getDimension())); |
| 124 |
} |
| 125 |
|
| 126 |
this.trees = trees; |
| 127 |
|
| 128 |
for (TreeModel t : trees) { |
| 129 |
addModel(t); |
| 130 |
} |
| 131 |
|
| 132 |
addVariable(indicatorParameter); |
| 133 |
addVariable(popSizeParameter); |
| 134 |
} |
| 135 |
|
| 136 |
public int nLoci() { |
| 137 |
return trees.length; |
| 138 |
} |
| 139 |
|
| 140 |
public Tree getTree(int k) { |
| 141 |
return trees[k]; |
| 142 |
} |
| 143 |
|
| 144 |
public TreeIntervals getTreeIntervals(int nt) { |
| 145 |
return getDemographicFunction().getTreeIntervals(nt); |
| 146 |
} |
| 147 |
|
| 148 |
public double getPopulationFactor(int nt) { |
| 149 |
return populationFactors[nt]; |
| 150 |
} |
| 151 |
|
| 152 |
public void storeTheState() { |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
public void restoreTheState() { |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
public VDdemographicFunction getDemographicFunction() { |
| 161 |
if (demoFunction == null) { |
| 162 |
demoFunction = new VDdemographicFunction(trees, type, |
| 163 |
indicatorParameter.getParameterValues(), popSizeParameter.getParameterValues(), logSpace, mid); |
| 164 |
} else { |
| 165 |
demoFunction.setup(trees, indicatorParameter.getParameterValues(), popSizeParameter.getParameterValues(), |
| 166 |
logSpace, mid); |
| 167 |
} |
| 168 |
return demoFunction; |
| 169 |
} |
| 170 |
|
| 171 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 172 |
|
| 173 |
|
| 174 |
if (demoFunction != null) { |
| 175 |
if (demoFunction == savedDemoFunction) { |
| 176 |
demoFunction = new VDdemographicFunction(demoFunction); |
| 177 |
} |
| 178 |
for (int k = 0; k < trees.length; ++k) { |
| 179 |
if (model == trees[k]) { |
| 180 |
demoFunction.treeChanged(k); |
| 181 |
|
| 182 |
|
| 183 |
break; |
| 184 |
} |
| 185 |
assert k + 1 < trees.length; |
| 186 |
} |
| 187 |
} |
| 188 |
super.handleModelChangedEvent(model, object, index); |
| 189 |
fireModelChanged(this); |
| 190 |
} |
| 191 |
|
| 192 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 193 |
|
| 194 |
super.handleVariableChangedEvent(variable, index, type); |
| 195 |
if (demoFunction != null) { |
| 196 |
if (demoFunction == savedDemoFunction) { |
| 197 |
demoFunction = new VDdemographicFunction(demoFunction); |
| 198 |
} |
| 199 |
demoFunction.setDirty(); |
| 200 |
} |
| 201 |
fireModelChanged(this); |
| 202 |
} |
| 203 |
|
| 204 |
protected void storeState() { |
| 205 |
savedDemoFunction = demoFunction; |
| 206 |
} |
| 207 |
|
| 208 |
protected void restoreState() { |
| 209 |
|
| 210 |
demoFunction = savedDemoFunction; |
| 211 |
savedDemoFunction = null; |
| 212 |
} |
| 213 |
|
| 214 |
@Override |
| 215 |
public Citation.Category getCategory() { |
| 216 |
return Citation.Category.TREE_PRIORS; |
| 217 |
} |
| 218 |
|
| 219 |
@Override |
| 220 |
public String getDescription() { |
| 221 |
return "Extended Bayesian Skyline multi-locus coalescent model"; |
| 222 |
} |
| 223 |
|
| 224 |
@Override |
| 225 |
public List<Citation> getCitations() { |
| 226 |
return Arrays.asList(new Citation( |
| 227 |
new Author[]{ |
| 228 |
new Author("J", "Heled"), |
| 229 |
new Author("AJ", "Drummond"), |
| 230 |
}, |
| 231 |
"Bayesian inference of population size history from multiple loci", |
| 232 |
2008, |
| 233 |
"BMC Evolutionary Biology", |
| 234 |
8, |
| 235 |
"289", |
| 236 |
"10.1186/1471-2148-8-289" |
| 237 |
)); |
| 238 |
} |
| 239 |
|
| 240 |
} |