| 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.coalescent.DemographicFunction; |
| 29 |
import dr.evolution.coalescent.ScaledDemographic; |
| 30 |
import dr.evolution.tree.NodeRef; |
| 31 |
import dr.evolution.tree.TreeUtils; |
| 32 |
import dr.evolution.util.Units; |
| 33 |
import dr.inference.model.Likelihood; |
| 34 |
import dr.inference.model.Model; |
| 35 |
import jebl.util.FixedBitSet; |
| 36 |
|
| 37 |
import java.util.Arrays; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class MultiSpeciesCoalescent extends Likelihood.Abstract implements Units { |
| 46 |
private final SpeciesTreeModel spTree; |
| 47 |
private final SpeciesBindings species; |
| 48 |
private boolean checkCompatibility; |
| 49 |
private final boolean[] compatibleCheckRequited; |
| 50 |
|
| 51 |
public MultiSpeciesCoalescent(SpeciesBindings species, SpeciesTreeModel tree) { |
| 52 |
super(tree); |
| 53 |
spTree = tree; |
| 54 |
this.species = species; |
| 55 |
|
| 56 |
spTree.addModelRestoreListener(this); |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
final SpeciesBindings.GeneTreeInfo[] trees = species.getGeneTrees(); |
| 61 |
for(SpeciesBindings.GeneTreeInfo geneTree : trees) { |
| 62 |
geneTree.tree.addModelListener(this); |
| 63 |
} |
| 64 |
|
| 65 |
compatibleCheckRequited = new boolean[trees.length]; |
| 66 |
Arrays.fill(compatibleCheckRequited, false); |
| 67 |
checkCompatibility = false; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
public void modelRestored(Model model) { |
| 72 |
super.makeDirty(); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public void makeDirty() { |
| 78 |
super.makeDirty(); |
| 79 |
checkCompatibility = true; |
| 80 |
for(int i = 0; i < species.getGeneTrees().length; i++) { |
| 81 |
compatibleCheckRequited[i] = true; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
protected double calculateLogLikelihood() { |
| 86 |
if( checkCompatibility ) { |
| 87 |
boolean compatibility = true; |
| 88 |
|
| 89 |
for(int i = 0; i < compatibleCheckRequited.length; ++i) { |
| 90 |
if( compatibleCheckRequited[i] ) { |
| 91 |
|
| 92 |
if( !spTree.isCompatible(species.getGeneTrees()[i]) ) { |
| 93 |
compatibility = false; |
| 94 |
} |
| 95 |
compatibleCheckRequited[i] = false; |
| 96 |
|
| 97 |
} |
| 98 |
} |
| 99 |
if( !compatibility ) { |
| 100 |
return Double.NEGATIVE_INFINITY; |
| 101 |
} |
| 102 |
checkCompatibility = false; |
| 103 |
} |
| 104 |
|
| 105 |
double logl = 0; |
| 106 |
int[] info = {0, 0}; |
| 107 |
for( SpeciesBindings.GeneTreeInfo geneTree : species.getGeneTrees() ) { |
| 108 |
final double v = treeLogLikelihood(geneTree, spTree.getRoot(), info, geneTree.popFactor()); |
| 109 |
assert ! Double.isNaN(v); |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
logl += v; |
| 114 |
} |
| 115 |
ccc += 1; |
| 116 |
return logl; |
| 117 |
} |
| 118 |
int ccc = 0; |
| 119 |
private final boolean verbose = false; |
| 120 |
|
| 121 |
private double treeLogLikelihood(SpeciesBindings.GeneTreeInfo geneTree, NodeRef node, int[] info, double popFactor) { |
| 122 |
|
| 123 |
int nLineages; |
| 124 |
|
| 125 |
int indexInClist = 0; |
| 126 |
|
| 127 |
double like = 0; |
| 128 |
|
| 129 |
final double t0 = spTree.getNodeHeight(node); |
| 130 |
|
| 131 |
final SpeciesBindings.CoalInfo[] cList = geneTree.getCoalInfo(); |
| 132 |
|
| 133 |
if( verbose ) { |
| 134 |
if( spTree.isRoot(node) ) { |
| 135 |
System.err.println("gtree:" + geneTree.tree.getId()); |
| 136 |
System.err.println("t0 " + t0); |
| 137 |
for(int k = 0; k < cList.length; ++k) { |
| 138 |
System.err.println(k + " " + cList[k].ctime + " " + cList[k].sinfo[0] + " " + cList[k].sinfo[1]); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
if( spTree.isExternal(node) ) { |
| 144 |
nLineages = geneTree.nLineages(spTree.speciesIndex(node)); |
| 145 |
indexInClist = 0; |
| 146 |
} else { |
| 147 |
|
| 148 |
|
| 149 |
nLineages = 0; |
| 150 |
for(int nc = 0; nc < 2; ++nc) { |
| 151 |
final NodeRef child = spTree.getChild(node, nc); |
| 152 |
like += treeLogLikelihood(geneTree, child, info, popFactor); |
| 153 |
nLineages += info[0]; |
| 154 |
indexInClist = Math.max(indexInClist, info[1]); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
if( indexInClist >= cList.length) { |
| 160 |
int k = 1; |
| 161 |
} |
| 162 |
assert indexInClist < cList.length; |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
while( cList[indexInClist].ctime < t0 ) { |
| 168 |
++indexInClist; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
final boolean isRoot = spTree.isRoot(node); |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
final double stopTime = isRoot ? Double.MAX_VALUE : spTree.getNodeHeight(spTree.getParent(node)); |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
double lastTime = 0.0; |
| 182 |
|
| 183 |
|
| 184 |
DemographicFunction demog = spTree.getNodeDemographic(node); |
| 185 |
if( popFactor > 0 ) { |
| 186 |
demog = new ScaledDemographic(demog, popFactor); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
FixedBitSet subspeciesSet = spTree.spSet(node); |
| 199 |
|
| 200 |
if( verbose ) { |
| 201 |
System.err.println(TreeUtils.uniqueNewick(spTree, node) + " nl " + nLineages |
| 202 |
+ " " + subspeciesSet + " t0 - st " + t0 + " - " + stopTime); |
| 203 |
} |
| 204 |
|
| 205 |
while( nLineages > 1 ) { |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
assert ( indexInClist < cList.length ); |
| 210 |
|
| 211 |
final double nextT = cList[indexInClist].ctime; |
| 212 |
|
| 213 |
|
| 214 |
if( nextT >= stopTime ) { |
| 215 |
break; |
| 216 |
} |
| 217 |
|
| 218 |
if( nonEmptyIntersection(cList[indexInClist].sinfo, subspeciesSet) ) { |
| 219 |
final double time = nextT - t0; |
| 220 |
if( time > 0 ) { |
| 221 |
final double interval = demog.getIntegral(lastTime, time); |
| 222 |
lastTime = time; |
| 223 |
|
| 224 |
final int nLineageOver2 = (nLineages * (nLineages - 1)) / 2; |
| 225 |
like -= nLineageOver2 * interval; |
| 226 |
|
| 227 |
final double pop = demog.getDemographic(time); assert( pop > 0 ); |
| 228 |
like -= Math.log(pop); |
| 229 |
} |
| 230 |
--nLineages; |
| 231 |
} |
| 232 |
++indexInClist; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
if( nLineages > 1 ) { |
| 237 |
|
| 238 |
final double interval = demog.getIntegral(lastTime, stopTime - t0); |
| 239 |
|
| 240 |
final int nLineageOver2 = (nLineages * (nLineages - 1)) / 2; |
| 241 |
|
| 242 |
like -= nLineageOver2 * interval; |
| 243 |
} |
| 244 |
|
| 245 |
info[0] = nLineages; |
| 246 |
info[1] = indexInClist; |
| 247 |
if( verbose ) { |
| 248 |
System.err.println(TreeUtils.uniqueNewick(spTree, node) + " stopTime " + stopTime + |
| 249 |
" nl " + nLineages + " icl " + indexInClist); |
| 250 |
} |
| 251 |
return like; |
| 252 |
} |
| 253 |
|
| 254 |
public void modelChangedEvent(Model model, Object object, int index) { |
| 255 |
|
| 256 |
|
| 257 |
super.makeDirty(); |
| 258 |
|
| 259 |
if( model == spTree ) { |
| 260 |
if( object == spTree && index != -1 ) { |
| 261 |
|
| 262 |
checkCompatibility = true; |
| 263 |
Arrays.fill(compatibleCheckRequited, true); |
| 264 |
} |
| 265 |
} else { |
| 266 |
|
| 267 |
final SpeciesBindings.GeneTreeInfo[] trees = species.getGeneTrees(); |
| 268 |
for(int i = 0; i < species.getGeneTrees().length; i++) { |
| 269 |
if( trees[i].tree == model ) { |
| 270 |
checkCompatibility = true; |
| 271 |
compatibleCheckRequited[i] = true; |
| 272 |
break; |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
private boolean nonEmptyIntersection(FixedBitSet[] sinfo, FixedBitSet subspeciesSet) { |
| 279 |
for( FixedBitSet nodeSpSet : sinfo ) { |
| 280 |
if( nodeSpSet.intersectCardinality(subspeciesSet) == 0 ) { |
| 281 |
return false; |
| 282 |
} |
| 283 |
} |
| 284 |
return true; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
public Type getUnits() { |
| 289 |
return spTree.getUnits(); |
| 290 |
} |
| 291 |
|
| 292 |
public void setUnits(Type units) { |
| 293 |
assert false; |
| 294 |
} |
| 295 |
|
| 296 |
} |