| 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.speciation; |
| 27 |
|
| 28 |
import dr.evolution.tree.Tree; |
| 29 |
import dr.evolution.util.Taxon; |
| 30 |
import dr.evolution.util.Units; |
| 31 |
import dr.evomodelxml.speciation.SpeciationLikelihoodParser; |
| 32 |
import dr.inference.model.AbstractModelLikelihood; |
| 33 |
import dr.inference.model.Model; |
| 34 |
import dr.inference.model.Parameter; |
| 35 |
import dr.inference.model.Variable; |
| 36 |
|
| 37 |
import java.util.Set; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class SpeciationLikelihood extends AbstractModelLikelihood implements Units { |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public SpeciationLikelihood(Tree tree, SpeciationModel speciationModel, Set<Taxon> exclude, String id) { |
| 58 |
this(SpeciationLikelihoodParser.SPECIATION_LIKELIHOOD, tree, speciationModel, exclude); |
| 59 |
setId(id); |
| 60 |
} |
| 61 |
|
| 62 |
public SpeciationLikelihood(Tree tree, SpeciationModel speciationModel, String id) { |
| 63 |
this(tree, speciationModel, null, id); |
| 64 |
} |
| 65 |
|
| 66 |
public SpeciationLikelihood(String name, Tree tree, SpeciationModel speciationModel, Set<Taxon> exclude) { |
| 67 |
|
| 68 |
super(name); |
| 69 |
|
| 70 |
this.tree = tree; |
| 71 |
this.speciationModel = speciationModel; |
| 72 |
this.exclude = exclude; |
| 73 |
|
| 74 |
if (tree instanceof Model) { |
| 75 |
addModel((Model) tree); |
| 76 |
} |
| 77 |
if (speciationModel != null) { |
| 78 |
addModel(speciationModel); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
public SpeciationLikelihood(Tree tree, SpeciationModel specModel, String id, CalibrationPoints calib) { |
| 83 |
this(tree, specModel, id); |
| 84 |
this.calibration = calib; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
protected final void handleModelChangedEvent(Model model, Object object, int index) { |
| 92 |
likelihoodKnown = false; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
protected final void storeState() { |
| 110 |
storedLikelihoodKnown = likelihoodKnown; |
| 111 |
storedLogLikelihood = logLikelihood; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
protected final void restoreState() { |
| 118 |
likelihoodKnown = storedLikelihoodKnown; |
| 119 |
logLikelihood = storedLogLikelihood; |
| 120 |
} |
| 121 |
|
| 122 |
protected final void acceptState() { |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public final Model getModel() { |
| 130 |
return this; |
| 131 |
} |
| 132 |
|
| 133 |
public final double getLogLikelihood() { |
| 134 |
if (!likelihoodKnown) { |
| 135 |
logLikelihood = calculateLogLikelihood(); |
| 136 |
likelihoodKnown = true; |
| 137 |
} |
| 138 |
return logLikelihood; |
| 139 |
} |
| 140 |
|
| 141 |
public final void makeDirty() { |
| 142 |
likelihoodKnown = false; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
private double calculateLogLikelihood() { |
| 152 |
if (exclude != null) { |
| 153 |
return speciationModel.calculateTreeLogLikelihood(tree, exclude); |
| 154 |
} |
| 155 |
|
| 156 |
if ( calibration != null ) { |
| 157 |
return speciationModel.calculateTreeLogLikelihood(tree, calibration); |
| 158 |
} |
| 159 |
|
| 160 |
return speciationModel.calculateTreeLogLikelihood(tree); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public final dr.inference.loggers.LogColumn[] getColumns() { |
| 171 |
|
| 172 |
String columnName = getId(); |
| 173 |
if (columnName == null) columnName = getModelName() + ".likelihood"; |
| 174 |
|
| 175 |
return new dr.inference.loggers.LogColumn[]{ |
| 176 |
new LikelihoodColumn(columnName) |
| 177 |
}; |
| 178 |
} |
| 179 |
|
| 180 |
private final class LikelihoodColumn extends dr.inference.loggers.NumberColumn { |
| 181 |
public LikelihoodColumn(String label) { |
| 182 |
super(label); |
| 183 |
} |
| 184 |
|
| 185 |
public double getDoubleValue() { |
| 186 |
return getLogLikelihood(); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public final void setUnits(Type u) { |
| 199 |
speciationModel.setUnits(u); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public final Type getUnits() { |
| 207 |
return speciationModel.getUnits(); |
| 208 |
} |
| 209 |
|
| 210 |
@Override |
| 211 |
public String prettyName() { |
| 212 |
String s = speciationModel.getClass().getName(); |
| 213 |
String[] parts = s.split("\\."); |
| 214 |
s = parts[parts.length - 1]; |
| 215 |
if( speciationModel.getId() != null ) { |
| 216 |
s = s + '/' + speciationModel.getId(); |
| 217 |
} |
| 218 |
s = s + '(' + tree.getId() + ')'; |
| 219 |
return s; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
SpeciationModel speciationModel = null; |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
Tree tree = null; |
| 234 |
private final Set<Taxon> exclude; |
| 235 |
|
| 236 |
private CalibrationPoints calibration; |
| 237 |
|
| 238 |
private double logLikelihood; |
| 239 |
private double storedLogLikelihood; |
| 240 |
private boolean likelihoodKnown = false; |
| 241 |
private boolean storedLikelihoodKnown = false; |
| 242 |
} |