| 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 java.util.ArrayList; |
| 29 |
import java.util.logging.Logger; |
| 30 |
|
| 31 |
import dr.evolution.io.NewickImporter; |
| 32 |
import dr.evolution.tree.NodeRef; |
| 33 |
import dr.evolution.tree.Tree; |
| 34 |
import dr.inference.model.MatrixParameter; |
| 35 |
import dr.inference.model.Parameter; |
| 36 |
import dr.math.Binomial; |
| 37 |
|
| 38 |
public class GMRFFixedGridLikelihood extends GMRFSkyrideLikelihood{ |
| 39 |
|
| 40 |
private Parameter covariateData; |
| 41 |
private Parameter covariateTimes; |
| 42 |
|
| 43 |
private ArrayList<CoalescentIntervalWithData> intervals; |
| 44 |
private ArrayList<CoalescentIntervalWithData> storedIntervals; |
| 45 |
|
| 46 |
public static void main(String[] args){ |
| 47 |
|
| 48 |
try{ |
| 49 |
run(); |
| 50 |
}catch(Exception e){ |
| 51 |
System.err.println(e.getMessage()); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
public static void run() throws Exception{ |
| 56 |
NewickImporter importer = new NewickImporter("((((5:0.5,1:0.2):0.5,0:1):0.2,2:0.8):0.2,3:1.4)"); |
| 57 |
Tree tree = importer.importNextTree(); |
| 58 |
|
| 59 |
double[] data = new double[15]; |
| 60 |
double[] times = new double[15]; |
| 61 |
|
| 62 |
data[0] = 1.0; |
| 63 |
times[0] = 0.05; |
| 64 |
|
| 65 |
for(int i = 1; i < data.length; i++){ |
| 66 |
data[i] = data[i-1] + 0.5; |
| 67 |
times[i] = times[i-1] + 0.1; |
| 68 |
} |
| 69 |
|
| 70 |
GMRFFixedGridLikelihood like = new GMRFFixedGridLikelihood(tree, |
| 71 |
new Parameter.Default(data),new Parameter.Default(times),4); |
| 72 |
|
| 73 |
System.out.println(like.getLogLikelihood()); |
| 74 |
} |
| 75 |
|
| 76 |
public GMRFFixedGridLikelihood(Tree tree, Parameter data, Parameter times, int tips){ |
| 77 |
super(tree, new Parameter.Default(tips), null, new Parameter.Default(5.0), |
| 78 |
new Parameter.Default(1.0), null, null,false, true); |
| 79 |
|
| 80 |
covariateData = data; |
| 81 |
covariateTimes = times; |
| 82 |
|
| 83 |
fieldLength += covariateData.getDimension(); |
| 84 |
|
| 85 |
intervals = new ArrayList<CoalescentIntervalWithData>(fieldLength); |
| 86 |
storedIntervals = new ArrayList<CoalescentIntervalWithData>(fieldLength); |
| 87 |
|
| 88 |
sSetupIntervals(); |
| 89 |
} |
| 90 |
|
| 91 |
public void initializationReport() { |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
public GMRFFixedGridLikelihood(Tree tree, Parameter popParameter, Parameter precParameter, |
| 96 |
Parameter lambda, Parameter beta, MatrixParameter dMatrix, |
| 97 |
Parameter data, Parameter times) { |
| 98 |
super(tree, popParameter, null, precParameter, lambda, beta, dMatrix, false, true); |
| 99 |
|
| 100 |
covariateData = data; |
| 101 |
covariateTimes = times; |
| 102 |
|
| 103 |
fieldLength += covariateData.getDimension(); |
| 104 |
|
| 105 |
addVariable(covariateData); |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
public void sSetupIntervals() { |
| 111 |
|
| 112 |
intervals.clear(); |
| 113 |
intervals.ensureCapacity(fieldLength); |
| 114 |
|
| 115 |
|
| 116 |
NodeRef x; |
| 117 |
for (int i = 0; i < tree.getInternalNodeCount(); i++) { |
| 118 |
x = tree.getInternalNode(i); |
| 119 |
intervals.add(new CoalescentIntervalWithData(tree.getNodeHeight(x), Double.NaN, 0, CoalescentEventType.COALESCENT)); |
| 120 |
} |
| 121 |
for (int i = 0; i < tree.getExternalNodeCount(); i++) { |
| 122 |
x = tree.getExternalNode(i); |
| 123 |
if (tree.getNodeHeight(x) > 1E-5){ |
| 124 |
intervals.add(new CoalescentIntervalWithData(tree.getNodeHeight(x), Double.NaN, 0, CoalescentEventType.NEW_SAMPLE)); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
dr.util.HeapSort.sort(intervals); |
| 129 |
for(int i = 0; i < intervals.size(); i++){ |
| 130 |
intervals.get(i).lineage = getLineageCount(i); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
for (int i = 0; i < covariateTimes.getDimension(); i++) { |
| 136 |
intervals.add(new CoalescentIntervalWithData(covariateTimes.getParameterValue(i), |
| 137 |
covariateData.getParameterValue(i), 0, CoalescentEventType.NOTHING)); |
| 138 |
} |
| 139 |
dr.util.HeapSort.sort(intervals); |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
double a = 0, b = 0; |
| 144 |
for (int i = 0; i < intervals.size(); i++) { |
| 145 |
b = intervals.get(i).length; |
| 146 |
intervals.get(i).length = intervals.get(i).length - a; |
| 147 |
a = b; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
for(int i = 0; i < intervals.size(); i++){ |
| 152 |
if(intervals.get(i).type.equals(CoalescentEventType.NOTHING)){ |
| 153 |
int j = i - 1; |
| 154 |
double temp = intervals.get(i).datum; |
| 155 |
while(j > -1 && |
| 156 |
!intervals.get(j).type.equals(CoalescentEventType.NOTHING)){ |
| 157 |
intervals.get(j).datum = temp; |
| 158 |
j--; |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
for(int i = 0; i < intervals.size(); i++){ |
| 164 |
if(!intervals.get(i).type.equals(CoalescentEventType.NOTHING)){ |
| 165 |
int lcount = intervals.get(i).lineage; |
| 166 |
int j = i - 1; |
| 167 |
while(j > -1 && intervals.get(j).type.equals(CoalescentEventType.NOTHING)){ |
| 168 |
intervals.get(j).lineage = lcount; |
| 169 |
j--; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
for(int i = 0; i < intervals.size(); i++){ |
| 175 |
if(intervals.get(i).lineage == 0){ |
| 176 |
intervals.get(i).lineage = 1; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
intervalsKnown = true; |
| 181 |
} |
| 182 |
|
| 183 |
public double calculateLogLikelihood(){ |
| 184 |
|
| 185 |
double logLike = 0; |
| 186 |
|
| 187 |
for(CoalescentIntervalWithData interval : intervals){ |
| 188 |
|
| 189 |
if(interval.lineage > 1){ |
| 190 |
|
| 191 |
double lineageChoose2 = Binomial.choose2(interval.lineage); |
| 192 |
|
| 193 |
logLike += -lineageChoose2*Math.exp(-interval.datum)*interval.length; |
| 194 |
|
| 195 |
if(interval.type.equals(CoalescentEventType.COALESCENT)){ |
| 196 |
logLike += -interval.datum; |
| 197 |
} |
| 198 |
|
| 199 |
}else{ |
| 200 |
break; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
if(Double.isNaN(logLike)){ |
| 205 |
System.out.println(logLike); |
| 206 |
System.out.println(intervals); |
| 207 |
System.out.println(tree.getNodeHeight(tree.getRoot())); |
| 208 |
System.exit(-1); |
| 209 |
} |
| 210 |
|
| 211 |
return logLike; |
| 212 |
} |
| 213 |
|
| 214 |
public void setupGMRFWeights() { |
| 215 |
super.setupGMRFWeights(); |
| 216 |
} |
| 217 |
|
| 218 |
public void storeState() { |
| 219 |
storedIntervals = new ArrayList<CoalescentIntervalWithData>(intervals.size()); |
| 220 |
for (CoalescentIntervalWithData interval : intervals) { |
| 221 |
storedIntervals.add(interval.clone()); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
public void restoreState() { |
| 226 |
intervals = storedIntervals; |
| 227 |
storedIntervals.clear(); |
| 228 |
} |
| 229 |
|
| 230 |
public String toString(){ |
| 231 |
return intervals.toString(); |
| 232 |
} |
| 233 |
|
| 234 |
public int getNumberOfIntervals(){ |
| 235 |
return intervals.size(); |
| 236 |
} |
| 237 |
|
| 238 |
public CoalescentIntervalWithData getDataInterval(int interval){ |
| 239 |
return intervals.get(interval); |
| 240 |
} |
| 241 |
|
| 242 |
public class CoalescentIntervalWithData implements Comparable<CoalescentIntervalWithData>, Cloneable { |
| 243 |
public CoalescentEventType type; |
| 244 |
public double length; |
| 245 |
public int lineage; |
| 246 |
public double datum; |
| 247 |
|
| 248 |
public CoalescentIntervalWithData(double length, double datum, int lineage, CoalescentEventType type) { |
| 249 |
this.length = length; |
| 250 |
this.type = type; |
| 251 |
this.datum = datum; |
| 252 |
this.lineage = lineage; |
| 253 |
} |
| 254 |
|
| 255 |
public int compareTo(CoalescentIntervalWithData a) { |
| 256 |
|
| 257 |
if (a.length < this.length) { |
| 258 |
return 1; |
| 259 |
} else if (a.length == this.length) { |
| 260 |
Logger.getLogger("dr.evomodel.coalescent").severe("The current model " + |
| 261 |
"has 2 internal nodes or 1 node and 1 covariate at the same height\n" + |
| 262 |
a.toString() + "\n" + this.toString()); |
| 263 |
return 0; |
| 264 |
} |
| 265 |
return -1; |
| 266 |
} |
| 267 |
|
| 268 |
public String toString() { |
| 269 |
return "(" + length + "," + type + "," + datum + "," + lineage + ")"; |
| 270 |
} |
| 271 |
|
| 272 |
public CoalescentIntervalWithData clone() { |
| 273 |
return new CoalescentIntervalWithData(length, datum, lineage, type); |
| 274 |
} |
| 275 |
|
| 276 |
} |
| 277 |
|
| 278 |
} |