| 1 |
package dr.evomodel.antigenic.phyloclustering.misc.obsolete; |
| 2 |
|
| 3 |
import dr.inference.model.*; |
| 4 |
import dr.math.MathUtils; |
| 5 |
import dr.math.LogTricks; |
| 6 |
import dr.math.distributions.NormalDistribution; |
| 7 |
import dr.util.*; |
| 8 |
import dr.xml.*; |
| 9 |
|
| 10 |
import java.io.*; |
| 11 |
import java.util.*; |
| 12 |
import java.util.logging.Logger; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class AGLikelihoodTreeCluster extends AbstractModelLikelihood implements Citable { |
| 28 |
private static final boolean CHECK_INFINITE = false; |
| 29 |
private static final boolean USE_THRESHOLDS = true; |
| 30 |
private static final boolean USE_INTERVALS = true; |
| 31 |
|
| 32 |
public final static String AG_LIKELIHOOD = "aglikelihoodtreecluster"; |
| 33 |
|
| 34 |
|
| 35 |
private static final int VIRUS_ISOLATE = 0; |
| 36 |
private static final int VIRUS_STRAIN = 1; |
| 37 |
private static final int VIRUS_DATE = 2; |
| 38 |
private static final int SERUM_ISOLATE = 3; |
| 39 |
private static final int SERUM_STRAIN = 4; |
| 40 |
private static final int SERUM_DATE = 5; |
| 41 |
private static final int TITRE = 6; |
| 42 |
|
| 43 |
|
| 44 |
private double oldLogLikelihood =0; |
| 45 |
|
| 46 |
public enum MeasurementType { |
| 47 |
INTERVAL, |
| 48 |
POINT, |
| 49 |
THRESHOLD, |
| 50 |
MISSING |
| 51 |
} |
| 52 |
|
| 53 |
public AGLikelihoodTreeCluster( |
| 54 |
int mdsDimension, |
| 55 |
Parameter mdsPrecisionParameter, |
| 56 |
Parameter locationDriftParameter, |
| 57 |
Parameter virusDriftParameter, |
| 58 |
Parameter serumDriftParameter, |
| 59 |
MatrixParameter virusLocationsParameter, |
| 60 |
MatrixParameter serumLocationsParameter, |
| 61 |
CompoundParameter tipTraitsParameter, |
| 62 |
Parameter virusOffsetsParameter, |
| 63 |
Parameter serumOffsetsParameter, |
| 64 |
Parameter serumPotenciesParameter, |
| 65 |
Parameter serumBreadthsParameter, |
| 66 |
Parameter virusAviditiesParameter, |
| 67 |
DataTable<String[]> dataTable, |
| 68 |
boolean mergeSerumIsolates, |
| 69 |
double intervalWidth, |
| 70 |
double driftInitialLocations, |
| 71 |
boolean clusterMeans, |
| 72 |
Parameter clusterOffsetsParameter) { |
| 73 |
|
| 74 |
super(AG_LIKELIHOOD); |
| 75 |
|
| 76 |
this.intervalWidth = intervalWidth; |
| 77 |
boolean useIntervals = USE_INTERVALS && intervalWidth > 0.0; |
| 78 |
|
| 79 |
int thresholdCount = 0; |
| 80 |
|
| 81 |
double earliestDate = Double.POSITIVE_INFINITY; |
| 82 |
for (int i = 0; i < dataTable.getRowCount(); i++) { |
| 83 |
|
| 84 |
String[] values = dataTable.getRow(i); |
| 85 |
|
| 86 |
String virusName = values[VIRUS_STRAIN]; |
| 87 |
double virusDate = Double.parseDouble(values[VIRUS_DATE]); |
| 88 |
int virus = virusNames.indexOf(virusName); |
| 89 |
if (virus == -1) { |
| 90 |
virusNames.add(virusName); |
| 91 |
virusDates.add(virusDate); |
| 92 |
virus = virusNames.size() - 1; |
| 93 |
} |
| 94 |
|
| 95 |
String serumName = ""; |
| 96 |
if (mergeSerumIsolates) { |
| 97 |
serumName = values[SERUM_STRAIN]; |
| 98 |
} else { |
| 99 |
serumName = values[SERUM_ISOLATE]; |
| 100 |
} |
| 101 |
double serumDate = Double.parseDouble(values[SERUM_DATE]); |
| 102 |
int serum = serumNames.indexOf(serumName); |
| 103 |
if (serum == -1) { |
| 104 |
serumNames.add(serumName); |
| 105 |
serumDates.add(serumDate); |
| 106 |
serum = serumNames.size() - 1; |
| 107 |
} |
| 108 |
|
| 109 |
boolean isThreshold = false; |
| 110 |
boolean isLowerThreshold = false; |
| 111 |
double rawTitre = Double.NaN; |
| 112 |
if (values[TITRE].length() > 0) { |
| 113 |
try { |
| 114 |
rawTitre = Double.parseDouble(values[TITRE]); |
| 115 |
} catch (NumberFormatException nfe) { |
| 116 |
|
| 117 |
if (values[TITRE].contains("<")) { |
| 118 |
rawTitre = Double.parseDouble(values[TITRE].replace("<","")); |
| 119 |
isThreshold = true; |
| 120 |
isLowerThreshold = true; |
| 121 |
thresholdCount++; |
| 122 |
} |
| 123 |
|
| 124 |
if (values[TITRE].contains(">")) { |
| 125 |
rawTitre = Double.parseDouble(values[TITRE].replace(">","")); |
| 126 |
isThreshold = true; |
| 127 |
isLowerThreshold = false; |
| 128 |
thresholdCount++; |
| 129 |
|
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
if (serumDate < earliestDate) { |
| 135 |
earliestDate = serumDate; |
| 136 |
} |
| 137 |
|
| 138 |
if (virusDate < earliestDate) { |
| 139 |
earliestDate = virusDate; |
| 140 |
} |
| 141 |
|
| 142 |
MeasurementType type = (isThreshold ? MeasurementType.THRESHOLD : (useIntervals ? MeasurementType.INTERVAL : MeasurementType.POINT)); |
| 143 |
Measurement measurement = new Measurement(virus, serum, virusDate, serumDate, type, rawTitre, isLowerThreshold); |
| 144 |
|
| 145 |
if (USE_THRESHOLDS || !isThreshold) { |
| 146 |
measurements.add(measurement); |
| 147 |
} |
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
double[] maxColumnTitres = new double[serumNames.size()]; |
| 152 |
for (Measurement measurement : measurements) { |
| 153 |
double titre = measurement.log2Titre; |
| 154 |
if (Double.isNaN(titre)) { |
| 155 |
titre = measurement.log2Titre; |
| 156 |
} |
| 157 |
if (titre > maxColumnTitres[measurement.serum]) { |
| 158 |
maxColumnTitres[measurement.serum] = titre; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
this.mdsDimension = mdsDimension; |
| 163 |
|
| 164 |
this.mdsPrecisionParameter = mdsPrecisionParameter; |
| 165 |
addVariable(mdsPrecisionParameter); |
| 166 |
|
| 167 |
this.locationDriftParameter = locationDriftParameter; |
| 168 |
if (this.locationDriftParameter != null) { |
| 169 |
addVariable(locationDriftParameter); |
| 170 |
} |
| 171 |
|
| 172 |
this.virusDriftParameter = virusDriftParameter; |
| 173 |
if (this.virusDriftParameter != null) { |
| 174 |
addVariable(virusDriftParameter); |
| 175 |
} |
| 176 |
|
| 177 |
this.serumDriftParameter = serumDriftParameter; |
| 178 |
if (this.serumDriftParameter != null) { |
| 179 |
addVariable(serumDriftParameter); |
| 180 |
} |
| 181 |
|
| 182 |
this.virusLocationsParameter = virusLocationsParameter; |
| 183 |
if (this.virusLocationsParameter != null) { |
| 184 |
setupLocationsParameter(virusLocationsParameter, virusNames); |
| 185 |
} |
| 186 |
|
| 187 |
this.serumLocationsParameter = serumLocationsParameter; |
| 188 |
if (this.serumLocationsParameter != null) { |
| 189 |
setupLocationsParameter(serumLocationsParameter, serumNames); |
| 190 |
} |
| 191 |
|
| 192 |
this.tipTraitsParameter = tipTraitsParameter; |
| 193 |
if (tipTraitsParameter != null) { |
| 194 |
setupTipTraitsParameter(this.tipTraitsParameter, virusNames); |
| 195 |
} |
| 196 |
|
| 197 |
this.virusOffsetsParameter = virusOffsetsParameter; |
| 198 |
if (virusOffsetsParameter != null) { |
| 199 |
setupOffsetsParameter(virusOffsetsParameter, virusNames, virusDates, earliestDate); |
| 200 |
} |
| 201 |
|
| 202 |
this.serumOffsetsParameter = serumOffsetsParameter; |
| 203 |
if (serumOffsetsParameter != null) { |
| 204 |
setupOffsetsParameter(serumOffsetsParameter, serumNames, serumDates, earliestDate); |
| 205 |
} |
| 206 |
|
| 207 |
this.serumPotenciesParameter = setupSerumPotencies(serumPotenciesParameter, maxColumnTitres); |
| 208 |
this.serumBreadthsParameter = setupSerumBreadths(serumBreadthsParameter); |
| 209 |
this.virusAviditiesParameter = setupVirusAvidities(virusAviditiesParameter); |
| 210 |
|
| 211 |
StringBuilder sb = new StringBuilder(); |
| 212 |
sb.append("\tAntigenicLikelihood:\n"); |
| 213 |
sb.append("\t\t" + virusNames.size() + " viruses\n"); |
| 214 |
sb.append("\t\t" + serumNames.size() + " sera\n"); |
| 215 |
sb.append("\t\t" + measurements.size() + " assay measurements\n"); |
| 216 |
if (USE_THRESHOLDS) { |
| 217 |
sb.append("\t\t" + thresholdCount + " thresholded measurements\n"); |
| 218 |
} |
| 219 |
if (useIntervals) { |
| 220 |
sb.append("\n\t\tAssuming a log 2 measurement interval width of " + intervalWidth + "\n"); |
| 221 |
} |
| 222 |
Logger.getLogger("dr.evomodel").info(sb.toString()); |
| 223 |
|
| 224 |
virusLocationChanged = new boolean[this.virusLocationsParameter.getParameterCount()]; |
| 225 |
serumLocationChanged = new boolean[this.serumLocationsParameter.getParameterCount()]; |
| 226 |
virusEffectChanged = new boolean[virusNames.size()]; |
| 227 |
serumEffectChanged = new boolean[serumNames.size()]; |
| 228 |
logLikelihoods = new double[measurements.size()]; |
| 229 |
storedLogLikelihoods = new double[measurements.size()]; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
if(clusterMeans){ |
| 244 |
this.clusterMeans = clusterMeans; |
| 245 |
this.clusterOffsetsParameter = clusterOffsetsParameter; |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
if (clusterOffsetsParameter == null) { |
| 259 |
|
| 260 |
} else { |
| 261 |
|
| 262 |
addVariable(clusterOffsetsParameter); |
| 263 |
clusterOffsetsParameter.setDimension(virusNames.size()); |
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
System.out.println(" clusterMeans = true"); |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
makeDirty(); |
| 274 |
|
| 275 |
loadInitialSerumLocations(serumNames); |
| 276 |
|
| 277 |
|
| 278 |
System.out.println("======================================"); |
| 279 |
double sum=0; |
| 280 |
double s2 = 0; |
| 281 |
for(int i=0; i < serumLocationsParameter.getParameterCount(); i++){ |
| 282 |
sum+=serumLocationsParameter.getParameter(i).getParameterValue(0); |
| 283 |
s2+=serumLocationsParameter.getParameter(i).getParameterValue(1); |
| 284 |
} |
| 285 |
System.out.println("sum sera location dimension 1 = " + sum); |
| 286 |
System.out.println("sum sera location dimension 2 = " + s2); |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
System.out.println("======================================"); |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
private void loadInitialSerumLocations(List<String> serumNames) { |
| 300 |
|
| 301 |
FileReader fileReader2; |
| 302 |
try { |
| 303 |
|
| 304 |
|
| 305 |
fileReader2 = new FileReader("/Users/charles/Documents/researchData/clustering/output/test25/run79/H3N2_mds.serumLocs.log"); |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
BufferedReader bReader2 = new BufferedReader( fileReader2); |
| 310 |
|
| 311 |
String line; |
| 312 |
line = bReader2.readLine(); |
| 313 |
line = bReader2.readLine(); |
| 314 |
|
| 315 |
line = bReader2.readLine(); |
| 316 |
System.out.println(line); |
| 317 |
String namevalue[] = line.split("\t"); |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
String testLine; |
| 322 |
while ((testLine = bReader2.readLine()) != null){ |
| 323 |
line = testLine; |
| 324 |
} |
| 325 |
|
| 326 |
System.out.println(line); |
| 327 |
|
| 328 |
String datavalue[] = line.split("\t"); |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
for (int i = 0; i < serumLocationsParameter.getParameterCount(); i++) { |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
double dim1 = Double.parseDouble(datavalue[i*2+1])- serumDriftParameter.getParameterValue(0)*serumOffsetsParameter.getParameterValue(i); |
| 337 |
double dim2 = Double.parseDouble(datavalue[i*2+2]); |
| 338 |
|
| 339 |
serumLocationsParameter.getParameter(i).setParameterValue(0, dim1); |
| 340 |
serumLocationsParameter.getParameter(i).setParameterValue(1, dim2); |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
bReader2.close(); |
| 350 |
|
| 351 |
} catch (FileNotFoundException e) { |
| 352 |
|
| 353 |
e.printStackTrace(); |
| 354 |
} catch (IOException e) { |
| 355 |
|
| 356 |
e.printStackTrace(); |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
private Parameter setupVirusAvidities(Parameter virusAviditiesParameter) { |
| 366 |
|
| 367 |
if (virusAviditiesParameter != null) { |
| 368 |
virusAviditiesParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, Double.MIN_VALUE, 1)); |
| 369 |
virusAviditiesParameter.setDimension(virusNames.size()); |
| 370 |
addVariable(virusAviditiesParameter); |
| 371 |
String[] labelArray = new String[virusNames.size()]; |
| 372 |
virusNames.toArray(labelArray); |
| 373 |
virusAviditiesParameter.setDimensionNames(labelArray); |
| 374 |
for (int i = 0; i < virusNames.size(); i++) { |
| 375 |
virusAviditiesParameter.setParameterValueQuietly(i, 0.0); |
| 376 |
} |
| 377 |
} |
| 378 |
return virusAviditiesParameter; |
| 379 |
} |
| 380 |
|
| 381 |
private Parameter setupSerumPotencies(Parameter serumPotenciesParameter, double[] maxColumnTitres) { |
| 382 |
|
| 383 |
if (serumPotenciesParameter == null) { |
| 384 |
serumPotenciesParameter = new Parameter.Default("serumPotencies"); |
| 385 |
} else { |
| 386 |
serumPotenciesParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, 0.0, 1)); |
| 387 |
addVariable(serumPotenciesParameter); |
| 388 |
} |
| 389 |
|
| 390 |
serumPotenciesParameter.setDimension(serumNames.size()); |
| 391 |
String[] labelArray = new String[serumNames.size()]; |
| 392 |
serumNames.toArray(labelArray); |
| 393 |
serumPotenciesParameter.setDimensionNames(labelArray); |
| 394 |
for (int i = 0; i < maxColumnTitres.length; i++) { |
| 395 |
serumPotenciesParameter.setParameterValueQuietly(i, maxColumnTitres[i]); |
| 396 |
} |
| 397 |
|
| 398 |
return serumPotenciesParameter; |
| 399 |
} |
| 400 |
|
| 401 |
private Parameter setupSerumBreadths(Parameter serumBreadthsParameter) { |
| 402 |
|
| 403 |
if (serumBreadthsParameter != null) { |
| 404 |
serumBreadthsParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, 0.0, 1)); |
| 405 |
serumBreadthsParameter.setDimension(serumNames.size()); |
| 406 |
addVariable(serumBreadthsParameter); |
| 407 |
String[] labelArray = new String[serumNames.size()]; |
| 408 |
serumNames.toArray(labelArray); |
| 409 |
serumBreadthsParameter.setDimensionNames(labelArray); |
| 410 |
for (int i = 0; i < serumNames.size(); i++) { |
| 411 |
serumBreadthsParameter.setParameterValueQuietly(i, 1.0); |
| 412 |
} |
| 413 |
} |
| 414 |
return serumBreadthsParameter; |
| 415 |
} |
| 416 |
|
| 417 |
protected void setupLocationsParameter(MatrixParameter locationsParameter, List<String> strains) { |
| 418 |
locationsParameter.setColumnDimension(mdsDimension); |
| 419 |
locationsParameter.setRowDimension(strains.size()); |
| 420 |
for (int i = 0; i < strains.size(); i++) { |
| 421 |
locationsParameter.getParameter(i).setId(strains.get(i)); |
| 422 |
} |
| 423 |
addVariable(locationsParameter); |
| 424 |
} |
| 425 |
|
| 426 |
private void setupOffsetsParameter(Parameter offsetsParameter, List<String> strainNames, List<Double> strainDates, double earliest) { |
| 427 |
|
| 428 |
offsetsParameter.setDimension(strainNames.size()); |
| 429 |
String[] labelArray = new String[strainNames.size()]; |
| 430 |
strainNames.toArray(labelArray); |
| 431 |
offsetsParameter.setDimensionNames(labelArray); |
| 432 |
for (int i = 0; i < strainNames.size(); i++) { |
| 433 |
Double offset = strainDates.get(i) - new Double(earliest); |
| 434 |
if (offset == null) { |
| 435 |
throw new IllegalArgumentException("Date missing for strain: " + strainNames.get(i)); |
| 436 |
} |
| 437 |
offsetsParameter.setParameterValue(i, offset); |
| 438 |
} |
| 439 |
addVariable(offsetsParameter); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
private void setupTipTraitsParameter(CompoundParameter tipTraitsParameter, List<String> strainNames) { |
| 444 |
tipIndices = new int[strainNames.size()]; |
| 445 |
|
| 446 |
for (int i = 0; i < strainNames.size(); i++) { |
| 447 |
tipIndices[i] = -1; |
| 448 |
} |
| 449 |
|
| 450 |
for (int i = 0; i < tipTraitsParameter.getParameterCount(); i++) { |
| 451 |
Parameter tip = tipTraitsParameter.getParameter(i); |
| 452 |
String label = tip.getParameterName(); |
| 453 |
int index = findStrain(label, strainNames); |
| 454 |
if (index != -1) { |
| 455 |
if (tipIndices[index] != -1) { |
| 456 |
throw new IllegalArgumentException("Duplicated tip name: " + label); |
| 457 |
} |
| 458 |
|
| 459 |
tipIndices[index] = i; |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
} else { |
| 468 |
|
| 469 |
|
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
} |
| 475 |
|
| 476 |
private final int findStrain(String label, List<String> strainNames) { |
| 477 |
int index = 0; |
| 478 |
for (String strainName : strainNames) { |
| 479 |
if (label.startsWith(strainName)) { |
| 480 |
return index; |
| 481 |
} |
| 482 |
|
| 483 |
index ++; |
| 484 |
} |
| 485 |
return -1; |
| 486 |
} |
| 487 |
|
| 488 |
private void setupInitialLocations(double drift) { |
| 489 |
|
| 490 |
for (int i = 0; i < virusLocationsParameter.getParameterCount(); i++) { |
| 491 |
double offset = 0.0; |
| 492 |
if (virusOffsetsParameter != null) { |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
offset = drift * virusOffsetsParameter.getParameterValue(i); |
| 497 |
} |
| 498 |
else{ |
| 499 |
System.out.println("virus Offeset Parameter NOT present"); |
| 500 |
} |
| 501 |
double r = MathUtils.nextGaussian() + offset; |
| 502 |
virusLocationsParameter.getParameter(i).setParameterValue(0, r); |
| 503 |
|
| 504 |
if (mdsDimension > 1) { |
| 505 |
for (int j = 1; j < mdsDimension; j++) { |
| 506 |
r = MathUtils.nextGaussian(); |
| 507 |
virusLocationsParameter.getParameter(i).setParameterValue(j, r); |
| 508 |
} |
| 509 |
} |
| 510 |
} |
| 511 |
for (int i = 0; i < serumLocationsParameter.getParameterCount(); i++) { |
| 512 |
double offset = 0.0; |
| 513 |
if (serumOffsetsParameter != null) { |
| 514 |
offset = drift * serumOffsetsParameter.getParameterValue(i); |
| 515 |
} |
| 516 |
double r = MathUtils.nextGaussian() + offset; |
| 517 |
serumLocationsParameter.getParameter(i).setParameterValue(0, r); |
| 518 |
if (mdsDimension > 1) { |
| 519 |
for (int j = 1; j < mdsDimension; j++) { |
| 520 |
r = MathUtils.nextGaussian(); |
| 521 |
serumLocationsParameter.getParameter(i).setParameterValue(j, r); |
| 522 |
} |
| 523 |
} |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
private void loadInitialLocations(List<String> strainNames, List<String> serumNames) { |
| 531 |
|
| 532 |
FileReader fileReader; |
| 533 |
try { |
| 534 |
|
| 535 |
fileReader = new FileReader("/Users/charles/Documents/research/antigenic/GenoPheno/Gabriela/results/initialConditionWithInitialLocationDrift/lastIteration/H3N2_mds.virusLocs.log"); |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
BufferedReader bReader = new BufferedReader( fileReader); |
| 540 |
|
| 541 |
String line; |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
line = bReader.readLine(); |
| 547 |
System.out.println(line); |
| 548 |
String namevalue[] = line.split("\t"); |
| 549 |
|
| 550 |
|
| 551 |
line = bReader.readLine(); |
| 552 |
System.out.println(line); |
| 553 |
|
| 554 |
String datavalue[] = line.split("\t"); |
| 555 |
|
| 556 |
for (int i = 0; i < virusLocationsParameter.getParameterCount(); i++) { |
| 557 |
|
| 558 |
int index = findStrain( namevalue[i*2+1], strainNames); |
| 559 |
|
| 560 |
|
| 561 |
virusLocationsParameter.getParameter(index).setParameterValue(0, Double.parseDouble(datavalue[i*2+1])); |
| 562 |
virusLocationsParameter.getParameter(index).setParameterValue(1, Double.parseDouble(datavalue[i*2+2])); |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
} |
| 568 |
bReader.close(); |
| 569 |
|
| 570 |
} catch (FileNotFoundException e) { |
| 571 |
|
| 572 |
e.printStackTrace(); |
| 573 |
} catch (IOException e) { |
| 574 |
|
| 575 |
e.printStackTrace(); |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
FileReader fileReader2; |
| 580 |
try { |
| 581 |
|
| 582 |
fileReader2 = new FileReader("/Users/charles/Documents/research/antigenic/GenoPheno/Gabriela/results/initialConditionWithInitialLocationDrift/lastIteration/H3N2.serumLocs.log"); |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
BufferedReader bReader2 = new BufferedReader( fileReader2); |
| 588 |
|
| 589 |
String line; |
| 590 |
|
| 591 |
line = bReader2.readLine(); |
| 592 |
System.out.println(line); |
| 593 |
String namevalue[] = line.split("\t"); |
| 594 |
|
| 595 |
|
| 596 |
line = bReader2.readLine(); |
| 597 |
System.out.println(line); |
| 598 |
|
| 599 |
String datavalue[] = line.split("\t"); |
| 600 |
|
| 601 |
for (int i = 0; i < serumLocationsParameter.getParameterCount(); i++) { |
| 602 |
int index = findStrain( namevalue[i*2+1], serumNames); |
| 603 |
|
| 604 |
|
| 605 |
serumLocationsParameter.getParameter(index).setParameterValue(0, Double.parseDouble(datavalue[i*2+1])); |
| 606 |
serumLocationsParameter.getParameter(index).setParameterValue(1, Double.parseDouble(datavalue[i*2+2])); |
| 607 |
|
| 608 |
|
| 609 |
} |
| 610 |
bReader2.close(); |
| 611 |
|
| 612 |
} catch (FileNotFoundException e) { |
| 613 |
|
| 614 |
e.printStackTrace(); |
| 615 |
} catch (IOException e) { |
| 616 |
|
| 617 |
e.printStackTrace(); |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
@Override |
| 626 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 627 |
} |
| 628 |
|
| 629 |
@Override |
| 630 |
protected void handleVariableChangedEvent(Variable variable, int index, Variable.ChangeType type) { |
| 631 |
if (variable == virusLocationsParameter) { |
| 632 |
int loc = index / mdsDimension; |
| 633 |
virusLocationChanged[loc] = true; |
| 634 |
if (tipTraitsParameter != null && tipIndices[loc] != -1) { |
| 635 |
Parameter location = virusLocationsParameter.getParameter(loc); |
| 636 |
Parameter tip = tipTraitsParameter.getParameter(tipIndices[loc]); |
| 637 |
int dim = index % mdsDimension; |
| 638 |
tip.setParameterValue(dim, location.getParameterValue(dim)); |
| 639 |
} |
| 640 |
} else if (variable == serumLocationsParameter) { |
| 641 |
int loc = index / mdsDimension; |
| 642 |
serumLocationChanged[loc] = true; |
| 643 |
} else if (variable == mdsPrecisionParameter) { |
| 644 |
setLocationChangedFlags(true); |
| 645 |
} else if (variable == locationDriftParameter) { |
| 646 |
setLocationChangedFlags(true); |
| 647 |
} else if (variable == virusDriftParameter) { |
| 648 |
setLocationChangedFlags(true); |
| 649 |
} else if (variable == serumDriftParameter) { |
| 650 |
setLocationChangedFlags(true); |
| 651 |
} else if (variable == serumPotenciesParameter) { |
| 652 |
serumEffectChanged[index] = true; |
| 653 |
} else if (variable == serumBreadthsParameter) { |
| 654 |
serumEffectChanged[index] = true; |
| 655 |
} else if (variable == virusAviditiesParameter) { |
| 656 |
virusEffectChanged[index] = true; |
| 657 |
} else { |
| 658 |
|
| 659 |
|
| 660 |
} |
| 661 |
likelihoodKnown = false; |
| 662 |
} |
| 663 |
|
| 664 |
@Override |
| 665 |
protected void storeState() { |
| 666 |
System.arraycopy(logLikelihoods, 0, storedLogLikelihoods, 0, logLikelihoods.length); |
| 667 |
} |
| 668 |
|
| 669 |
@Override |
| 670 |
protected void restoreState() { |
| 671 |
double[] tmp = logLikelihoods; |
| 672 |
logLikelihoods = storedLogLikelihoods; |
| 673 |
storedLogLikelihoods = tmp; |
| 674 |
|
| 675 |
likelihoodKnown = false; |
| 676 |
} |
| 677 |
|
| 678 |
@Override |
| 679 |
protected void acceptState() { |
| 680 |
} |
| 681 |
|
| 682 |
public Model getModel() { |
| 683 |
return this; |
| 684 |
} |
| 685 |
|
| 686 |
public double getLogLikelihood() { |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
logLikelihood = computeLogLikelihood(); |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
return logLikelihood; |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
private double computeLogLikelihood() { |
| 707 |
|
| 708 |
|
| 709 |
double precision = mdsPrecisionParameter.getParameterValue(0); |
| 710 |
double sd = 1.0 / Math.sqrt(precision); |
| 711 |
|
| 712 |
logLikelihood = 0.0; |
| 713 |
int i = 0; |
| 714 |
|
| 715 |
for (Measurement measurement : measurements) { |
| 716 |
|
| 717 |
if (virusLocationChanged[measurement.virus] || serumLocationChanged[measurement.serum] || virusEffectChanged[measurement.virus] || serumEffectChanged[measurement.serum]) { |
| 718 |
|
| 719 |
double expectation = calculateBaseline(measurement.virus, measurement.serum) - computeDistance(measurement.virus, measurement.serum); |
| 720 |
|
| 721 |
switch (measurement.type) { |
| 722 |
case INTERVAL: { |
| 723 |
double minTitre = measurement.log2Titre; |
| 724 |
double maxTitre = measurement.log2Titre + intervalWidth; |
| 725 |
logLikelihoods[i] = computeMeasurementIntervalLikelihood(minTitre, maxTitre, expectation, sd); |
| 726 |
} break; |
| 727 |
case POINT: { |
| 728 |
logLikelihoods[i] = computeMeasurementLikelihood(measurement.log2Titre, expectation, sd); |
| 729 |
} break; |
| 730 |
case THRESHOLD: { |
| 731 |
if(measurement.isLowerThreshold){ |
| 732 |
logLikelihoods[i] = computeMeasurementThresholdLikelihood(measurement.log2Titre, expectation, sd); |
| 733 |
} |
| 734 |
else{ |
| 735 |
logLikelihoods[i] = computeMeasurementUpperThresholdLikelihood(measurement.log2Titre, expectation, sd); |
| 736 |
} |
| 737 |
} break; |
| 738 |
case MISSING: |
| 739 |
break; |
| 740 |
} |
| 741 |
} |
| 742 |
logLikelihood += logLikelihoods[i]; |
| 743 |
i++; |
| 744 |
} |
| 745 |
|
| 746 |
likelihoodKnown = true; |
| 747 |
|
| 748 |
setLocationChangedFlags(false); |
| 749 |
setSerumEffectChangedFlags(false); |
| 750 |
setVirusEffectChangedFlags(false); |
| 751 |
|
| 752 |
return logLikelihood; |
| 753 |
} |
| 754 |
|
| 755 |
private void setLocationChangedFlags(boolean flag) { |
| 756 |
for (int i = 0; i < virusLocationChanged.length; i++) { |
| 757 |
virusLocationChanged[i] = flag; |
| 758 |
} |
| 759 |
for (int i = 0; i < serumLocationChanged.length; i++) { |
| 760 |
serumLocationChanged[i] = flag; |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
private void setSerumEffectChangedFlags(boolean flag) { |
| 765 |
for (int i = 0; i < serumEffectChanged.length; i++) { |
| 766 |
serumEffectChanged[i] = flag; |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
private void setVirusEffectChangedFlags(boolean flag) { |
| 771 |
for (int i = 0; i < virusEffectChanged.length; i++) { |
| 772 |
virusEffectChanged[i] = flag; |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
protected double computeDistance(int virus, int serum) { |
| 778 |
Parameter vLoc = virusLocationsParameter.getParameter(virus); |
| 779 |
Parameter sLoc = serumLocationsParameter.getParameter(serum); |
| 780 |
double sum = 0.0; |
| 781 |
|
| 782 |
|
| 783 |
double vxOffset = 0.0; |
| 784 |
double sxOffset = 0.0; |
| 785 |
if(clusterMeans == true){ |
| 786 |
|
| 787 |
if(virusDriftParameter!= null && virusOffsetsParameter != null && serumOffsetsParameter != null && clusterOffsetsParameter!=null){ |
| 788 |
|
| 789 |
sxOffset = serumDriftParameter.getParameterValue(0) * serumOffsetsParameter.getParameterValue(serum); |
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
} |
| 795 |
|
| 796 |
|
| 797 |
if (serumDriftParameter != null && serumOffsetsParameter != null) { |
| 798 |
|
| 799 |
sxOffset = serumDriftParameter.getParameterValue(0) * serumOffsetsParameter.getParameterValue(serum); |
| 800 |
} |
| 801 |
|
| 802 |
} |
| 803 |
else{ |
| 804 |
if (locationDriftParameter != null && virusOffsetsParameter != null && serumOffsetsParameter != null) { |
| 805 |
vxOffset = locationDriftParameter.getParameterValue(0) * virusOffsetsParameter.getParameterValue(virus); |
| 806 |
sxOffset = locationDriftParameter.getParameterValue(0) * serumOffsetsParameter.getParameterValue(serum); |
| 807 |
} |
| 808 |
if (virusDriftParameter != null && virusOffsetsParameter != null) { |
| 809 |
vxOffset = virusDriftParameter.getParameterValue(0) * virusOffsetsParameter.getParameterValue(virus); |
| 810 |
} |
| 811 |
if (serumDriftParameter != null && serumOffsetsParameter != null) { |
| 812 |
sxOffset = serumDriftParameter.getParameterValue(0) * serumOffsetsParameter.getParameterValue(serum); |
| 813 |
} |
| 814 |
} |
| 815 |
|
| 816 |
double vxLoc = vLoc.getParameterValue(0) + vxOffset; |
| 817 |
double sxLoc = sLoc.getParameterValue(0) + sxOffset; |
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
double difference = vxLoc - sxLoc; |
| 824 |
sum += difference * difference; |
| 825 |
|
| 826 |
|
| 827 |
for (int i = 1; i < mdsDimension; i++) { |
| 828 |
difference = vLoc.getParameterValue(i) - sLoc.getParameterValue(i); |
| 829 |
sum += difference * difference; |
| 830 |
} |
| 831 |
|
| 832 |
double dist = Math.sqrt(sum); |
| 833 |
|
| 834 |
if (serumBreadthsParameter != null) { |
| 835 |
double serumBreadth = serumBreadthsParameter.getParameterValue(serum); |
| 836 |
dist /= serumBreadth; |
| 837 |
} |
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
return(dist); |
| 846 |
} |
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
protected double computeDistanceBasedOnArray(int virus, int serum, double[][] virusLocArray) { |
| 853 |
|
| 854 |
Parameter sLoc = serumLocationsParameter.getParameter(serum); |
| 855 |
double sum = 0.0; |
| 856 |
|
| 857 |
|
| 858 |
double vxOffset = 0.0; |
| 859 |
double sxOffset = 0.0; |
| 860 |
if(clusterMeans == true){ |
| 861 |
|
| 862 |
|
| 863 |
if(virusDriftParameter!= null && virusOffsetsParameter != null && serumOffsetsParameter != null && clusterOffsetsParameter!=null){ |
| 864 |
vxOffset = virusDriftParameter.getParameterValue(0)* clusterOffsetsParameter.getParameterValue(virus); |
| 865 |
sxOffset = virusDriftParameter.getParameterValue(0) * serumOffsetsParameter.getParameterValue(serum); |
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
} |
| 871 |
|
| 872 |
|
| 873 |
if (serumDriftParameter != null && serumOffsetsParameter != null) { |
| 874 |
|
| 875 |
sxOffset = serumDriftParameter.getParameterValue(0) * serumOffsetsParameter.getParameterValue(serum); |
| 876 |
} |
| 877 |
|
| 878 |
} |
| 879 |
else{ |
| 880 |
if (locationDriftParameter != null && virusOffsetsParameter != null && serumOffsetsParameter != null) { |
| 881 |
vxOffset = locationDriftParameter.getParameterValue(0) * virusOffsetsParameter.getParameterValue(virus); |
| 882 |
sxOffset = locationDriftParameter.getParameterValue(0) * serumOffsetsParameter.getParameterValue(serum); |
| 883 |
} |
| 884 |
if (virusDriftParameter != null && virusOffsetsParameter != null) { |
| 885 |
vxOffset = virusDriftParameter.getParameterValue(0) * virusOffsetsParameter.getParameterValue(virus); |
| 886 |
} |
| 887 |
if (serumDriftParameter != null && serumOffsetsParameter != null) { |
| 888 |
sxOffset = serumDriftParameter.getParameterValue(0) * serumOffsetsParameter.getParameterValue(serum); |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
|
| 893 |
double vxLoc = virusLocArray[virus][0] + vxOffset; |
| 894 |
double sxLoc = sLoc.getParameterValue(0) + sxOffset; |
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
double difference = vxLoc - sxLoc; |
| 901 |
sum += difference * difference; |
| 902 |
|
| 903 |
|
| 904 |
for (int i = 1; i < mdsDimension; i++) { |
| 905 |
|
| 906 |
difference = virusLocArray[virus][i] - sLoc.getParameterValue(i); |
| 907 |
sum += difference * difference; |
| 908 |
} |
| 909 |
|
| 910 |
double dist = Math.sqrt(sum); |
| 911 |
|
| 912 |
if (serumBreadthsParameter != null) { |
| 913 |
double serumBreadth = serumBreadthsParameter.getParameterValue(serum); |
| 914 |
dist /= serumBreadth; |
| 915 |
} |
| 916 |
|
| 917 |
return dist; |
| 918 |
} |
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
private double calculateBaseline(int virus, int serum) { |
| 925 |
double baseline = serumPotenciesParameter.getParameterValue(serum); |
| 926 |
if (virusAviditiesParameter != null) { |
| 927 |
baseline += virusAviditiesParameter.getParameterValue(virus); |
| 928 |
} |
| 929 |
return baseline; |
| 930 |
} |
| 931 |
|
| 932 |
private static double computeMeasurementLikelihood(double titre, double expectation, double sd) { |
| 933 |
|
| 934 |
double lnL = NormalDistribution.logPdf(titre, expectation, sd); |
| 935 |
|
| 936 |
if (CHECK_INFINITE && Double.isNaN(lnL) || Double.isInfinite(lnL)) { |
| 937 |
throw new RuntimeException("infinite point measurement"); |
| 938 |
} |
| 939 |
return lnL; |
| 940 |
} |
| 941 |
|
| 942 |
private static double computeMeasurementThresholdLikelihood(double titre, double expectation, double sd) { |
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
double lnL = NormalDistribution.cdf(titre, expectation, sd, true); |
| 948 |
|
| 949 |
if (CHECK_INFINITE && Double.isNaN(lnL) || Double.isInfinite(lnL)) { |
| 950 |
throw new RuntimeException("infinite threshold measurement"); |
| 951 |
} |
| 952 |
return lnL; |
| 953 |
} |
| 954 |
|
| 955 |
|
| 956 |
private static double computeMeasurementUpperThresholdLikelihood(double titre, double expectation, double sd) { |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
double L = NormalDistribution.cdf(titre, expectation, sd, false); |
| 961 |
double lnL = Math.log(1-L); |
| 962 |
|
| 963 |
if (CHECK_INFINITE && Double.isNaN(lnL) || Double.isInfinite(lnL)) { |
| 964 |
throw new RuntimeException("infinite threshold measurement"); |
| 965 |
} |
| 966 |
return lnL; |
| 967 |
} |
| 968 |
|
| 969 |
|
| 970 |
private static double computeMeasurementIntervalLikelihood(double minTitre, double maxTitre, double expectation, double sd) { |
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
double cdf1 = NormalDistribution.cdf(maxTitre, expectation, sd, true); |
| 975 |
double cdf2 = NormalDistribution.cdf(minTitre, expectation, sd, true); |
| 976 |
double lnL = LogTricks.logDiff(cdf1, cdf2); |
| 977 |
|
| 978 |
if (CHECK_INFINITE && Double.isNaN(lnL) || Double.isInfinite(lnL)) { |
| 979 |
|
| 980 |
|
| 981 |
lnL = NormalDistribution.logPdf(minTitre, expectation, sd); |
| 982 |
if (CHECK_INFINITE && Double.isNaN(lnL) || Double.isInfinite(lnL)) { |
| 983 |
throw new RuntimeException("infinite interval measurement"); |
| 984 |
} |
| 985 |
} |
| 986 |
return lnL; |
| 987 |
} |
| 988 |
|
| 989 |
public void makeDirty() { |
| 990 |
likelihoodKnown = false; |
| 991 |
setLocationChangedFlags(true); |
| 992 |
} |
| 993 |
|
| 994 |
private class Measurement { |
| 995 |
private Measurement(final int virus, final int serum, final double virusDate, final double serumDate, final MeasurementType type, final double titre, final boolean isLowerThreshold) { |
| 996 |
this.virus = virus; |
| 997 |
this.serum = serum; |
| 998 |
this.virusDate = virusDate; |
| 999 |
this.serumDate = serumDate; |
| 1000 |
this.type = type; |
| 1001 |
this.titre = titre; |
| 1002 |
this.log2Titre = Math.log(titre) / Math.log(2); |
| 1003 |
this.isLowerThreshold = isLowerThreshold; |
| 1004 |
} |
| 1005 |
|
| 1006 |
final int virus; |
| 1007 |
final int serum; |
| 1008 |
final double virusDate; |
| 1009 |
final double serumDate; |
| 1010 |
final MeasurementType type; |
| 1011 |
final double titre; |
| 1012 |
final double log2Titre; |
| 1013 |
final boolean isLowerThreshold; |
| 1014 |
|
| 1015 |
}; |
| 1016 |
|
| 1017 |
private final List<Measurement> measurements = new ArrayList<Measurement>(); |
| 1018 |
private final List<String> virusNames = new ArrayList<String>(); |
| 1019 |
private final List<String> serumNames = new ArrayList<String>(); |
| 1020 |
private final List<Double> virusDates = new ArrayList<Double>(); |
| 1021 |
private final List<Double> serumDates = new ArrayList<Double>(); |
| 1022 |
|
| 1023 |
private final int mdsDimension; |
| 1024 |
private final double intervalWidth; |
| 1025 |
private final Parameter mdsPrecisionParameter; |
| 1026 |
private final Parameter locationDriftParameter; |
| 1027 |
private final Parameter virusDriftParameter; |
| 1028 |
private final Parameter serumDriftParameter; |
| 1029 |
|
| 1030 |
private final MatrixParameter virusLocationsParameter; |
| 1031 |
private final MatrixParameter serumLocationsParameter; |
| 1032 |
|
| 1033 |
private final Parameter virusOffsetsParameter; |
| 1034 |
private final Parameter serumOffsetsParameter; |
| 1035 |
|
| 1036 |
private final CompoundParameter tipTraitsParameter; |
| 1037 |
private int[] tipIndices; |
| 1038 |
|
| 1039 |
private final Parameter virusAviditiesParameter; |
| 1040 |
private final Parameter serumPotenciesParameter; |
| 1041 |
private final Parameter serumBreadthsParameter; |
| 1042 |
|
| 1043 |
private double logLikelihood = 0.0; |
| 1044 |
private boolean likelihoodKnown = false; |
| 1045 |
|
| 1046 |
private final boolean[] virusLocationChanged; |
| 1047 |
private final boolean[] serumLocationChanged; |
| 1048 |
private final boolean[] serumEffectChanged; |
| 1049 |
private final boolean[] virusEffectChanged; |
| 1050 |
private double[] logLikelihoods; |
| 1051 |
private double[] storedLogLikelihoods; |
| 1052 |
|
| 1053 |
private boolean clusterMeans = false; |
| 1054 |
private Parameter clusterOffsetsParameter; |
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 1061 |
|
| 1062 |
public final static String FILE_NAME = "fileName"; |
| 1063 |
public final static String TIP_TRAIT = "tipTrait"; |
| 1064 |
public final static String VIRUS_LOCATIONS = "virusLocations"; |
| 1065 |
public final static String SERUM_LOCATIONS = "serumLocations"; |
| 1066 |
public static final String MDS_DIMENSION = "mdsDimension"; |
| 1067 |
public static final String MERGE_SERUM_ISOLATES = "mergeSerumIsolates"; |
| 1068 |
public static final String DRIFT_INITIAL_LOCATIONS = "driftInitialLocations"; |
| 1069 |
public static final String INTERVAL_WIDTH = "intervalWidth"; |
| 1070 |
public static final String MDS_PRECISION = "mdsPrecision"; |
| 1071 |
public static final String LOCATION_DRIFT = "locationDrift"; |
| 1072 |
public static final String VIRUS_DRIFT = "virusDrift"; |
| 1073 |
public static final String SERUM_DRIFT = "serumDrift"; |
| 1074 |
public static final String VIRUS_AVIDITIES = "virusAvidities"; |
| 1075 |
public static final String SERUM_POTENCIES = "serumPotencies"; |
| 1076 |
public static final String SERUM_BREADTHS = "serumBreadths"; |
| 1077 |
public final static String VIRUS_OFFSETS = "virusOffsets"; |
| 1078 |
public final static String SERUM_OFFSETS = "serumOffsets"; |
| 1079 |
public final static String CLUSTER_MEANS = "clusterMeans"; |
| 1080 |
public final static String CLUSTER_OFFSETS = "clusterOffsetsParameter"; |
| 1081 |
|
| 1082 |
public String getParserName() { |
| 1083 |
return AG_LIKELIHOOD; |
| 1084 |
} |
| 1085 |
|
| 1086 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 1087 |
|
| 1088 |
String fileName = xo.getStringAttribute(FILE_NAME); |
| 1089 |
DataTable<String[]> assayTable; |
| 1090 |
try { |
| 1091 |
assayTable = DataTable.Text.parse(new FileReader(fileName), true, false); |
| 1092 |
} catch (IOException e) { |
| 1093 |
throw new XMLParseException("Unable to read assay data from file: " + e.getMessage()); |
| 1094 |
} |
| 1095 |
System.out.println("Loaded HI table file: " + fileName); |
| 1096 |
|
| 1097 |
boolean mergeSerumIsolates = xo.getAttribute(MERGE_SERUM_ISOLATES, false); |
| 1098 |
|
| 1099 |
boolean cluster_means = xo.getAttribute(CLUSTER_MEANS, false); |
| 1100 |
|
| 1101 |
int mdsDimension = xo.getIntegerAttribute(MDS_DIMENSION); |
| 1102 |
double intervalWidth = 0.0; |
| 1103 |
if (xo.hasAttribute(INTERVAL_WIDTH)) { |
| 1104 |
intervalWidth = xo.getDoubleAttribute(INTERVAL_WIDTH); |
| 1105 |
} |
| 1106 |
|
| 1107 |
double driftInitialLocations = 0.0; |
| 1108 |
if (xo.hasAttribute(DRIFT_INITIAL_LOCATIONS)) { |
| 1109 |
driftInitialLocations = xo.getDoubleAttribute(DRIFT_INITIAL_LOCATIONS); |
| 1110 |
} |
| 1111 |
|
| 1112 |
CompoundParameter tipTraitParameter = null; |
| 1113 |
if (xo.hasChildNamed(TIP_TRAIT)) { |
| 1114 |
tipTraitParameter = (CompoundParameter) xo.getElementFirstChild(TIP_TRAIT); |
| 1115 |
} |
| 1116 |
|
| 1117 |
MatrixParameter virusLocationsParameter = null; |
| 1118 |
if (xo.hasChildNamed(VIRUS_LOCATIONS)) { |
| 1119 |
virusLocationsParameter = (MatrixParameter) xo.getElementFirstChild(VIRUS_LOCATIONS); |
| 1120 |
} |
| 1121 |
|
| 1122 |
MatrixParameter serumLocationsParameter = null; |
| 1123 |
if (xo.hasChildNamed(SERUM_LOCATIONS)) { |
| 1124 |
serumLocationsParameter = (MatrixParameter) xo.getElementFirstChild(SERUM_LOCATIONS); |
| 1125 |
} |
| 1126 |
|
| 1127 |
Parameter mdsPrecision = (Parameter) xo.getElementFirstChild(MDS_PRECISION); |
| 1128 |
|
| 1129 |
Parameter locationDrift = null; |
| 1130 |
if (xo.hasChildNamed(LOCATION_DRIFT)) { |
| 1131 |
locationDrift = (Parameter) xo.getElementFirstChild(LOCATION_DRIFT); |
| 1132 |
} |
| 1133 |
|
| 1134 |
Parameter virusDrift = null; |
| 1135 |
if (xo.hasChildNamed(VIRUS_DRIFT)) { |
| 1136 |
virusDrift = (Parameter) xo.getElementFirstChild(VIRUS_DRIFT); |
| 1137 |
} |
| 1138 |
|
| 1139 |
Parameter serumDrift = null; |
| 1140 |
if (xo.hasChildNamed(SERUM_DRIFT)) { |
| 1141 |
serumDrift = (Parameter) xo.getElementFirstChild(SERUM_DRIFT); |
| 1142 |
} |
| 1143 |
|
| 1144 |
Parameter virusOffsetsParameter = null; |
| 1145 |
if (xo.hasChildNamed(VIRUS_OFFSETS)) { |
| 1146 |
virusOffsetsParameter = (Parameter) xo.getElementFirstChild(VIRUS_OFFSETS); |
| 1147 |
} |
| 1148 |
|
| 1149 |
Parameter serumOffsetsParameter = null; |
| 1150 |
if (xo.hasChildNamed(SERUM_OFFSETS)) { |
| 1151 |
serumOffsetsParameter = (Parameter) xo.getElementFirstChild(SERUM_OFFSETS); |
| 1152 |
} |
| 1153 |
|
| 1154 |
Parameter serumPotenciesParameter = null; |
| 1155 |
if (xo.hasChildNamed(SERUM_POTENCIES)) { |
| 1156 |
serumPotenciesParameter = (Parameter) xo.getElementFirstChild(SERUM_POTENCIES); |
| 1157 |
} |
| 1158 |
|
| 1159 |
Parameter serumBreadthsParameter = null; |
| 1160 |
if (xo.hasChildNamed(SERUM_BREADTHS)) { |
| 1161 |
serumBreadthsParameter = (Parameter) xo.getElementFirstChild(SERUM_BREADTHS); |
| 1162 |
} |
| 1163 |
|
| 1164 |
Parameter virusAviditiesParameter = null; |
| 1165 |
if (xo.hasChildNamed(VIRUS_AVIDITIES)) { |
| 1166 |
virusAviditiesParameter = (Parameter) xo.getElementFirstChild(VIRUS_AVIDITIES); |
| 1167 |
} |
| 1168 |
|
| 1169 |
Parameter clusterOffsetsParameter = null; |
| 1170 |
if (xo.hasChildNamed(CLUSTER_OFFSETS)) { |
| 1171 |
clusterOffsetsParameter = (Parameter) xo.getElementFirstChild(CLUSTER_OFFSETS); |
| 1172 |
} |
| 1173 |
|
| 1174 |
|
| 1175 |
AGLikelihoodTreeCluster AGL = new AGLikelihoodTreeCluster( |
| 1176 |
mdsDimension, |
| 1177 |
mdsPrecision, |
| 1178 |
locationDrift, |
| 1179 |
virusDrift, |
| 1180 |
serumDrift, |
| 1181 |
virusLocationsParameter, |
| 1182 |
serumLocationsParameter, |
| 1183 |
tipTraitParameter, |
| 1184 |
virusOffsetsParameter, |
| 1185 |
serumOffsetsParameter, |
| 1186 |
serumPotenciesParameter, |
| 1187 |
serumBreadthsParameter, |
| 1188 |
virusAviditiesParameter, |
| 1189 |
assayTable, |
| 1190 |
mergeSerumIsolates, |
| 1191 |
intervalWidth, |
| 1192 |
driftInitialLocations, |
| 1193 |
cluster_means, |
| 1194 |
clusterOffsetsParameter); |
| 1195 |
|
| 1196 |
|
| 1197 |
Logger.getLogger("dr.evomodel").info("Using EvolutionaryCartography model. Please cite:\n" + Utils.getCitationString(AGL)); |
| 1198 |
|
| 1199 |
return AGL; |
| 1200 |
} |
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
public String getParserDescription() { |
| 1207 |
return "Provides the likelihood of immunological assay data such as Hemagglutinin inhibition (HI) given vectors of coordinates" + |
| 1208 |
"for viruses and sera/antisera in some multidimensional 'antigenic' space."; |
| 1209 |
} |
| 1210 |
|
| 1211 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 1212 |
return rules; |
| 1213 |
} |
| 1214 |
|
| 1215 |
private final XMLSyntaxRule[] rules = { |
| 1216 |
AttributeRule.newStringRule(FILE_NAME, false, "The name of the file containing the assay table"), |
| 1217 |
AttributeRule.newIntegerRule(MDS_DIMENSION, false, "The dimension of the space for MDS"), |
| 1218 |
AttributeRule.newBooleanRule(MERGE_SERUM_ISOLATES, true, "Should multiple serum isolates from the same strain have their locations merged (defaults to false)"), |
| 1219 |
AttributeRule.newDoubleRule(INTERVAL_WIDTH, true, "The width of the titre interval in log 2 space"), |
| 1220 |
AttributeRule.newDoubleRule(DRIFT_INITIAL_LOCATIONS, true, "The degree to drift initial virus and serum locations, defaults to 0.0"), |
| 1221 |
new ElementRule(TIP_TRAIT, CompoundParameter.class, "Optional parameter of tip locations from the tree", true), |
| 1222 |
new ElementRule(VIRUS_LOCATIONS, MatrixParameter.class, "Parameter of locations of all virus"), |
| 1223 |
new ElementRule(SERUM_LOCATIONS, MatrixParameter.class, "Parameter of locations of all sera"), |
| 1224 |
new ElementRule(VIRUS_OFFSETS, Parameter.class, "Optional parameter for virus dates to be stored", true), |
| 1225 |
new ElementRule(SERUM_OFFSETS, Parameter.class, "Optional parameter for serum dates to be stored", true), |
| 1226 |
new ElementRule(SERUM_POTENCIES, Parameter.class, "Optional parameter for serum potencies", true), |
| 1227 |
new ElementRule(SERUM_BREADTHS, Parameter.class, "Optional parameter for serum breadths", true), |
| 1228 |
new ElementRule(VIRUS_AVIDITIES, Parameter.class, "Optional parameter for virus avidities", true), |
| 1229 |
new ElementRule(MDS_PRECISION, Parameter.class, "Parameter for precision of MDS embedding"), |
| 1230 |
new ElementRule(LOCATION_DRIFT, Parameter.class, "Optional parameter for drifting locations with time", true), |
| 1231 |
new ElementRule(VIRUS_DRIFT, Parameter.class, "Optional parameter for drifting only virus locations, overrides locationDrift", true), |
| 1232 |
new ElementRule(SERUM_DRIFT, Parameter.class, "Optional parameter for drifting only serum locations, overrides locationDrift", true), |
| 1233 |
AttributeRule.newBooleanRule(CLUSTER_MEANS, true, "Should we use cluster means to control the virus locations"), |
| 1234 |
new ElementRule(CLUSTER_OFFSETS, Parameter.class, "Parameter of cluster offsets of all virus"), |
| 1235 |
}; |
| 1236 |
|
| 1237 |
public Class getReturnType() { |
| 1238 |
return AGLikelihoodTreeCluster.class; |
| 1239 |
} |
| 1240 |
}; |
| 1241 |
|
| 1242 |
@Override |
| 1243 |
public Citation.Category getCategory() { |
| 1244 |
return Citation.Category.TRAIT_MODELS; |
| 1245 |
} |
| 1246 |
|
| 1247 |
@Override |
| 1248 |
public String getDescription() { |
| 1249 |
return "Bayesian Antigenic Cartography framework"; |
| 1250 |
} |
| 1251 |
|
| 1252 |
public List<Citation> getCitations() { |
| 1253 |
return Arrays.asList(new Citation( |
| 1254 |
new Author[]{ |
| 1255 |
new Author("C", "Cheung"), |
| 1256 |
new Author("A", "Rambaut"), |
| 1257 |
new Author("P", "Lemey"), |
| 1258 |
new Author("MA", "Suchard"), |
| 1259 |
new Author("T", "Bedford") |
| 1260 |
}, |
| 1261 |
Citation.Status.IN_PREPARATION |
| 1262 |
), |
| 1263 |
CommonCitations.BEDFORD_2015_INTEGRATING); |
| 1264 |
} |
| 1265 |
|
| 1266 |
public double getLogLikelihoodBasedOnPrecompute(int[] clusterLabel, int numClusters, int[] oldObservationCluster, double[] oldContribution, int[] newObservationCluster, double[] newContribution) { |
| 1267 |
|
| 1268 |
double precision = mdsPrecisionParameter.getParameterValue(0); |
| 1269 |
double sd = 1.0 / Math.sqrt(precision); |
| 1270 |
|
| 1271 |
for (int i=0; i < measurements.size(); i++) { |
| 1272 |
newObservationCluster[i] = clusterLabel[measurements.get(i).virus]; |
| 1273 |
} |
| 1274 |
|
| 1275 |
double logL=0; |
| 1276 |
for(int i=0; i< measurements.size(); i++ ){ |
| 1277 |
if(oldObservationCluster != null && newObservationCluster[i] == oldObservationCluster[i]){ |
| 1278 |
|
| 1279 |
double newC = computeContribution(measurements.get(i), precision, sd); |
| 1280 |
if(newC != oldContribution[i]){ |
| 1281 |
System.out.println("newObservationCluster[i]=" + newObservationCluster[i] + " and old =" + oldObservationCluster[i]); |
| 1282 |
System.out.println("old contribution of i=" + i + " is " + oldContribution[i] + " but new is " + newC); |
| 1283 |
|
| 1284 |
System.out.println("They should be the same. Why are they different?"); |
| 1285 |
System.exit(0); |
| 1286 |
} |
| 1287 |
|
| 1288 |
|
| 1289 |
newContribution[i] = oldContribution[i]; |
| 1290 |
} |
| 1291 |
else{ |
| 1292 |
|
| 1293 |
newContribution[i] = computeContribution(measurements.get(i), precision, sd); |
| 1294 |
} |
| 1295 |
logL += newContribution[i]; |
| 1296 |
|
| 1297 |
} |
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
return(logL); |
| 1302 |
} |
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
public double computeContribution(Measurement measurement, double precision, double sd){ |
| 1307 |
double curLogL=0; |
| 1308 |
|
| 1309 |
|
| 1310 |
double expectation = calculateBaseline(measurement.virus, measurement.serum) - computeDistance(measurement.virus, measurement.serum); |
| 1311 |
switch (measurement.type) { |
| 1312 |
case INTERVAL: { |
| 1313 |
|
| 1314 |
double minTitre = measurement.log2Titre; |
| 1315 |
double maxTitre = measurement.log2Titre + intervalWidth; |
| 1316 |
curLogL = computeMeasurementIntervalLikelihood(minTitre, maxTitre, expectation, sd); |
| 1317 |
} break; |
| 1318 |
case POINT: { |
| 1319 |
|
| 1320 |
curLogL = computeMeasurementLikelihood(measurement.log2Titre, expectation, sd); |
| 1321 |
} break; |
| 1322 |
case THRESHOLD: { |
| 1323 |
|
| 1324 |
if(measurement.isLowerThreshold){ |
| 1325 |
curLogL = computeMeasurementThresholdLikelihood(measurement.log2Titre, expectation, sd); |
| 1326 |
} |
| 1327 |
else{ |
| 1328 |
curLogL = computeMeasurementUpperThresholdLikelihood(measurement.log2Titre, expectation, sd); |
| 1329 |
} |
| 1330 |
} break; |
| 1331 |
case MISSING: |
| 1332 |
break; |
| 1333 |
} |
| 1334 |
|
| 1335 |
|
| 1336 |
return curLogL; |
| 1337 |
} |
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
public double getClusterLogLikelihoodUpdate(int[] clusterLabel, int numClusters, int[] oldObservationCluster, double[] oldContribution, int[] newObservationCluster, double[] newContribution, double[] oldClusterSum, double[] newClusterSum) { |
| 1345 |
|
| 1346 |
double precision = mdsPrecisionParameter.getParameterValue(0); |
| 1347 |
double sd = 1.0 / Math.sqrt(precision); |
| 1348 |
|
| 1349 |
|
| 1350 |
List<Measurement>[] partition = new List[clusterLabel.length]; |
| 1351 |
for(int i=0; i < clusterLabel.length; i++){ |
| 1352 |
partition[i] = new LinkedList(); |
| 1353 |
} |
| 1354 |
|
| 1355 |
int []needUpdateCluster = new int[numClusters]; |
| 1356 |
|
| 1357 |
if(oldObservationCluster == null){ |
| 1358 |
for (int i=0; i < measurements.size(); i++) { |
| 1359 |
Measurement m = measurements.get(i); |
| 1360 |
newObservationCluster[i] = clusterLabel[m.virus]; |
| 1361 |
partition[clusterLabel[m.virus]].add(m); |
| 1362 |
} |
| 1363 |
for(int i=0; i<numClusters; i++){ |
| 1364 |
needUpdateCluster[i] = 1; |
| 1365 |
} |
| 1366 |
} |
| 1367 |
else{ |
| 1368 |
for (int i=0; i < measurements.size(); i++) { |
| 1369 |
Measurement m = measurements.get(i); |
| 1370 |
newObservationCluster[i] = clusterLabel[m.virus]; |
| 1371 |
partition[clusterLabel[m.virus]].add(m); |
| 1372 |
|
| 1373 |
if(newObservationCluster[i] != oldObservationCluster[i]){ |
| 1374 |
needUpdateCluster[newObservationCluster[i]] = 1; |
| 1375 |
needUpdateCluster[oldObservationCluster[i]] = 1; |
| 1376 |
} |
| 1377 |
} |
| 1378 |
} |
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
double logL=0; |
| 1407 |
for(int i=0; i < numClusters; i++ ){ |
| 1408 |
if(partition[i].size() >0){ |
| 1409 |
if(needUpdateCluster[i] == 0 && oldClusterSum != null){ |
| 1410 |
|
| 1411 |
newClusterSum[i] += oldClusterSum[i]; |
| 1412 |
} |
| 1413 |
else{ |
| 1414 |
newClusterSum[i] += computeLikelihoodBasedOnClusters(partition[i]); |
| 1415 |
} |
| 1416 |
logL += newClusterSum[i]; |
| 1417 |
} |
| 1418 |
else{ |
| 1419 |
newClusterSum[i] = 0; |
| 1420 |
} |
| 1421 |
|
| 1422 |
} |
| 1423 |
|
| 1424 |
|
| 1425 |
return(logL); |
| 1426 |
} |
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
public double getClusterLogLikelihood(int[] clusterLabel, int numClusters) { |
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
List<Measurement>[] partition = new List[clusterLabel.length]; |
| 1440 |
for(int i=0; i < clusterLabel.length; i++){ |
| 1441 |
partition[i] = new LinkedList(); |
| 1442 |
} |
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
for (Measurement measurement : measurements) { |
| 1458 |
|
| 1459 |
partition[clusterLabel[measurement.virus]].add(measurement); |
| 1460 |
} |
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
double logL=0; |
| 1466 |
|
| 1467 |
for(int i=0; i < numClusters; i++ ){ |
| 1468 |
|
| 1469 |
if(partition[i].size() >0){ |
| 1470 |
|
| 1471 |
logL += computeLikelihoodBasedOnClusters(partition[i]); |
| 1472 |
} |
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
} |
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
return(logL); |
| 1487 |
} |
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
private double computeLikelihoodBasedOnClusters( |
| 1495 |
List<Measurement> linkedList) { |
| 1496 |
|
| 1497 |
|
| 1498 |
double precision = mdsPrecisionParameter.getParameterValue(0); |
| 1499 |
double sd = 1.0 / Math.sqrt(precision); |
| 1500 |
|
| 1501 |
double logL = 0.0; |
| 1502 |
int i = 0; |
| 1503 |
|
| 1504 |
for (Measurement measurement : linkedList) { |
| 1505 |
double curLogL=0; |
| 1506 |
|
| 1507 |
|
| 1508 |
double expectation = calculateBaseline(measurement.virus, measurement.serum) - computeDistance(measurement.virus, measurement.serum); |
| 1509 |
switch (measurement.type) { |
| 1510 |
case INTERVAL: { |
| 1511 |
|
| 1512 |
double minTitre = measurement.log2Titre; |
| 1513 |
double maxTitre = measurement.log2Titre + intervalWidth; |
| 1514 |
curLogL = computeMeasurementIntervalLikelihood(minTitre, maxTitre, expectation, sd); |
| 1515 |
} break; |
| 1516 |
case POINT: { |
| 1517 |
|
| 1518 |
curLogL = computeMeasurementLikelihood(measurement.log2Titre, expectation, sd); |
| 1519 |
} break; |
| 1520 |
case THRESHOLD: { |
| 1521 |
|
| 1522 |
if(measurement.isLowerThreshold){ |
| 1523 |
curLogL = computeMeasurementThresholdLikelihood(measurement.log2Titre, expectation, sd); |
| 1524 |
} |
| 1525 |
else{ |
| 1526 |
curLogL = computeMeasurementUpperThresholdLikelihood(measurement.log2Titre, expectation, sd); |
| 1527 |
} |
| 1528 |
} break; |
| 1529 |
case MISSING: |
| 1530 |
break; |
| 1531 |
} |
| 1532 |
|
| 1533 |
logL += curLogL; |
| 1534 |
i++; |
| 1535 |
|
| 1536 |
} |
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
return logL; |
| 1545 |
} |
| 1546 |
|
| 1547 |
public int getNumObservations() { |
| 1548 |
return measurements.size(); |
| 1549 |
} |
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
public double getNumSera() { |
| 1554 |
return serumLocationsParameter.getParameterCount(); |
| 1555 |
} |
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
public MatrixParameter getSerumLocationsParameter() { |
| 1560 |
return serumLocationsParameter; |
| 1561 |
} |
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
public void printVirusLocations() { |
| 1567 |
|
| 1568 |
for(int i=0; i < virusLocationsParameter.getParameterCount(); i++){ |
| 1569 |
Parameter v = virusLocationsParameter.getParameter(i); |
| 1570 |
System.out.println(v.getId() + " " + v.getParameterValue(0) + "," + v.getParameterValue(1)); |
| 1571 |
} |
| 1572 |
|
| 1573 |
} |
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
} |