| 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.antigenic; |
| 27 |
|
| 28 |
import dr.inference.model.*; |
| 29 |
import dr.math.MathUtils; |
| 30 |
import dr.util.*; |
| 31 |
import dr.xml.*; |
| 32 |
|
| 33 |
import java.io.*; |
| 34 |
import java.util.*; |
| 35 |
import java.util.logging.Logger; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
@Deprecated |
| 43 |
public class DiscreteAntigenicTraitLikelihood extends AntigenicTraitLikelihood implements Citable { |
| 44 |
|
| 45 |
public final static String DISCRETE_ANTIGENIC_TRAIT_LIKELIHOOD = "discreteAntigenicTraitLikelihood"; |
| 46 |
|
| 47 |
private static final int CLUSTER_COUNT = -1; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public DiscreteAntigenicTraitLikelihood( |
| 60 |
int mdsDimension, |
| 61 |
Parameter mdsPrecision, |
| 62 |
Parameter clusterIndexParameter, |
| 63 |
MatrixParameter locationsParameter, |
| 64 |
CompoundParameter tipTraitParameter, |
| 65 |
DataTable<String[]> dataTable, |
| 66 |
Map<String, String> virusAntiserumMap, |
| 67 |
Map<String, String> assayAntiserumMap, |
| 68 |
List<String> virusLocationStatisticList, |
| 69 |
final boolean log2Transform) { |
| 70 |
|
| 71 |
super(DISCRETE_ANTIGENIC_TRAIT_LIKELIHOOD); |
| 72 |
|
| 73 |
String[] virusNames = dataTable.getRowLabels(); |
| 74 |
String[] assayNames = dataTable.getColumnLabels(); |
| 75 |
|
| 76 |
|
| 77 |
int virusCount = dataTable.getRowCount(); |
| 78 |
int assayCount = dataTable.getColumnCount(); |
| 79 |
|
| 80 |
int[] assayToSerumIndices = new int[assayNames.length]; |
| 81 |
|
| 82 |
double[][] observationValueTable = new double[virusCount][assayCount]; |
| 83 |
ObservationType[][] observationTypeTable = new ObservationType[virusCount][assayCount]; |
| 84 |
|
| 85 |
initalizeTable(dataTable, observationValueTable, observationTypeTable, log2Transform); |
| 86 |
|
| 87 |
|
| 88 |
List<String> tipLabels = null; |
| 89 |
if (tipTraitParameter != null) { |
| 90 |
tipLabels = new ArrayList<String>(); |
| 91 |
int tipCount = tipTraitParameter.getParameterCount(); |
| 92 |
|
| 93 |
for (int i = 0; i < tipCount; i++) { |
| 94 |
String label = tipTraitParameter.getParameter(i).getParameterName(); |
| 95 |
if (label.endsWith(".antigenic")) { |
| 96 |
label = label.substring(0, label.indexOf(".antigenic")); |
| 97 |
} |
| 98 |
tipLabels.add(label); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
List<String> locationLabelsList = new ArrayList<String>(); |
| 104 |
int[] virusToLocationIndices = new int[virusCount]; |
| 105 |
int count = 0; |
| 106 |
for (String virusName : virusNames) { |
| 107 |
String name = null; |
| 108 |
|
| 109 |
if (virusAntiserumMap != null) { |
| 110 |
name = virusAntiserumMap.get(virusName); |
| 111 |
} |
| 112 |
|
| 113 |
if (name == null) { |
| 114 |
name = virusName; |
| 115 |
} |
| 116 |
|
| 117 |
virusToLocationIndices[count] = locationLabelsList.size(); |
| 118 |
locationLabelsList.add(name); |
| 119 |
|
| 120 |
count++; |
| 121 |
} |
| 122 |
|
| 123 |
List<String> serumNamesList = new ArrayList<String>(); |
| 124 |
count = 0; |
| 125 |
for (String assayName : assayNames) { |
| 126 |
String name = null; |
| 127 |
|
| 128 |
if (assayAntiserumMap != null) { |
| 129 |
name = assayAntiserumMap.get(assayName); |
| 130 |
} |
| 131 |
|
| 132 |
if (name == null) { |
| 133 |
name = assayName; |
| 134 |
} |
| 135 |
|
| 136 |
int index = serumNamesList.indexOf(name); |
| 137 |
if (index == -1) { |
| 138 |
index = serumNamesList.size(); |
| 139 |
serumNamesList.add(name); |
| 140 |
} |
| 141 |
assayToSerumIndices[count] = index; |
| 142 |
count++; |
| 143 |
} |
| 144 |
String[] serumNames = new String[serumNamesList.size()]; |
| 145 |
serumNamesList.toArray(serumNames); |
| 146 |
|
| 147 |
int serumCount = serumNames.length; |
| 148 |
int[] serumToLocationIndices = new int[serumCount]; |
| 149 |
count = 0; |
| 150 |
for (String serumName : serumNames) { |
| 151 |
int index = locationLabelsList.indexOf(serumName); |
| 152 |
if (index == -1) { |
| 153 |
index = locationLabelsList.size(); |
| 154 |
locationLabelsList.add(serumName); |
| 155 |
} |
| 156 |
serumToLocationIndices[count] = index; |
| 157 |
count++; |
| 158 |
} |
| 159 |
|
| 160 |
String[] locationLabels = new String[locationLabelsList.size()]; |
| 161 |
locationLabelsList.toArray(locationLabels); |
| 162 |
int locationCount = locationLabels.length; |
| 163 |
|
| 164 |
List<Double> observationList = new ArrayList<Double>(); |
| 165 |
List<ObservationType> observationTypeList = new ArrayList<ObservationType>(); |
| 166 |
|
| 167 |
int[] virusObservationCounts = new int[virusCount]; |
| 168 |
int[] serumObservationCounts = new int[serumCount]; |
| 169 |
|
| 170 |
List<Pair> locationPairs = new ArrayList<Pair>(); |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
for (int i = 0; i < virusCount; i++) { |
| 181 |
|
| 182 |
if (virusToLocationIndices[i] != -1) { |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
for (int j = 0; j < assayCount; j++) { |
| 188 |
int k = assayToSerumIndices[j]; |
| 189 |
|
| 190 |
Double value = observationValueTable[i][j]; |
| 191 |
ObservationType type = observationTypeTable[i][j]; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
if (type != ObservationType.MISSING) { |
| 196 |
observationList.add(value); |
| 197 |
observationTypeList.add(type); |
| 198 |
|
| 199 |
locationPairs.add(new Pair(virusToLocationIndices[i], serumToLocationIndices[k])); |
| 200 |
|
| 201 |
virusObservationCounts[i]++; |
| 202 |
serumObservationCounts[k]++; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
for (int i = 0; i < virusCount; i++) { |
| 211 |
if (virusToLocationIndices[i] != -1 && virusObservationCounts[i] == 0) { |
| 212 |
System.err.println("WARNING: Virus " + virusNames[i] + " has 0 observations"); |
| 213 |
} |
| 214 |
} |
| 215 |
for (int j = 0; j < serumCount; j++) { |
| 216 |
if (serumObservationCounts[j] == 0) { |
| 217 |
System.err.println("WARNING: Antisera " + serumNames[j] + " has 0 observations"); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
double[] observations = new double[observationList.size()]; |
| 223 |
for (int i = 0; i < observationList.size(); i++) { |
| 224 |
observations[i] = observationList.get(i); |
| 225 |
} |
| 226 |
|
| 227 |
int[] rowLocationIndices = new int[locationPairs.size()]; |
| 228 |
for (int i = 0; i < rowLocationIndices.length; i++) { |
| 229 |
rowLocationIndices[i] = locationPairs.get(i).location1; |
| 230 |
} |
| 231 |
|
| 232 |
int[] columnLocationIndices = new int[locationPairs.size()]; |
| 233 |
for (int i = 0; i < columnLocationIndices.length; i++) { |
| 234 |
columnLocationIndices[i] = locationPairs.get(i).location2; |
| 235 |
} |
| 236 |
|
| 237 |
ObservationType[] observationTypes = new ObservationType[observationTypeList.size()]; |
| 238 |
observationTypeList.toArray(observationTypes); |
| 239 |
|
| 240 |
int thresholdCount = 0; |
| 241 |
for (int i = 0; i < observations.length; i++) { |
| 242 |
thresholdCount += (observationTypes[i] != ObservationType.POINT ? 1 : 0); |
| 243 |
} |
| 244 |
|
| 245 |
if (tipTraitParameter != null) { |
| 246 |
|
| 247 |
tipIndices = new int[locationCount]; |
| 248 |
|
| 249 |
for (int i = 0; i < locationCount; i++) { |
| 250 |
tipIndices[i] = tipLabels.indexOf(locationLabels[i]); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
for (String tipLabel : tipLabels) { |
| 260 |
if (!locationLabelsList.contains(tipLabel)) { |
| 261 |
System.err.println("Tip, " + tipLabel + ", not found in location list"); |
| 262 |
} |
| 263 |
} |
| 264 |
} else { |
| 265 |
tipIndices = null; |
| 266 |
} |
| 267 |
|
| 268 |
this.tipTraitParameter = tipTraitParameter; |
| 269 |
|
| 270 |
StringBuilder sb = new StringBuilder(); |
| 271 |
sb.append("\tDiscreteAntigenicTraitLikelihood:\n"); |
| 272 |
sb.append("\t\t" + virusNames.length + " viruses\n"); |
| 273 |
sb.append("\t\t" + assayNames.length + " assays\n"); |
| 274 |
sb.append("\t\t" + serumNames.length + " antisera\n"); |
| 275 |
sb.append("\t\t" + locationLabels.length + " locations\n"); |
| 276 |
sb.append("\t\t" + locationPairs.size() + " distances\n"); |
| 277 |
sb.append("\t\t" + observations.length + " observations\n"); |
| 278 |
sb.append("\t\t" + thresholdCount + " threshold observations\n"); |
| 279 |
Logger.getLogger("dr.evomodel").info(sb.toString()); |
| 280 |
|
| 281 |
initialize( |
| 282 |
mdsDimension, |
| 283 |
false, |
| 284 |
mdsPrecision, |
| 285 |
locationsParameter, |
| 286 |
locationLabels, |
| 287 |
observations, |
| 288 |
observationTypes, |
| 289 |
rowLocationIndices, |
| 290 |
columnLocationIndices); |
| 291 |
|
| 292 |
|
| 293 |
for (int i = 0; i < locationsParameter.getParameterCount(); i++) { |
| 294 |
for (int j = 0; j < mdsDimension; j++) { |
| 295 |
|
| 296 |
double r = 0.0; |
| 297 |
if (j == 0) { |
| 298 |
r = (double) i * 0.05; |
| 299 |
} |
| 300 |
else { |
| 301 |
r = MathUtils.nextGaussian(); |
| 302 |
} |
| 303 |
locationsParameter.getParameter(i).setParameterValueQuietly(j, r); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
if (CLUSTER_COUNT > 0) { |
| 309 |
maxClusterCount = CLUSTER_COUNT; |
| 310 |
} else { |
| 311 |
maxClusterCount = getLocationCount(); |
| 312 |
} |
| 313 |
|
| 314 |
this.clusterIndexParameter = clusterIndexParameter; |
| 315 |
|
| 316 |
clusterIndexParameter.setDimension(getLocationCount()); |
| 317 |
clusterSizes = new int[maxClusterCount]; |
| 318 |
|
| 319 |
|
| 320 |
Parameter.DefaultBounds bound = new Parameter.DefaultBounds(maxClusterCount - 1, 0, getLocationCount()); |
| 321 |
clusterIndexParameter.addBounds(bound); |
| 322 |
|
| 323 |
for (int i = 0; i < getLocationCount(); i++) { |
| 324 |
int r = i; |
| 325 |
clusterIndexParameter.setParameterValue(i, r); |
| 326 |
|
| 327 |
for (int dim = 0; dim < mdsDimension; dim++) { |
| 328 |
tipTraitParameter.setParameterValue((tipIndices[r] * mdsDimension) + dim, |
| 329 |
locationsParameter.getParameterValue((r * mdsDimension) + dim)); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
updateClusterSizes(); |
| 334 |
|
| 335 |
addVariable(clusterIndexParameter); |
| 336 |
addStatistic(new ClusterMask()); |
| 337 |
addStatistic(new ClusterIndices()); |
| 338 |
addStatistic(new ClusterCount()); |
| 339 |
addStatistic(new ClusterSizes()); |
| 340 |
addStatistic(new ClusteredLocations()); |
| 341 |
|
| 342 |
int i = 0; |
| 343 |
for (String virusName : virusNames) { |
| 344 |
if (virusLocationStatisticList.contains(virusName)) { |
| 345 |
addStatistic(new VirusLocation(virusName + "." + "location", i)); |
| 346 |
} |
| 347 |
i++; |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
@Override |
| 352 |
protected void setupLocationsParameter(MatrixParameter locationsParameter) { |
| 353 |
locationsParameter.setColumnDimension(getMDSDimension()); |
| 354 |
int n = CLUSTER_COUNT; |
| 355 |
if (n < 1) { |
| 356 |
n = getLocationCount(); |
| 357 |
} |
| 358 |
locationsParameter.setRowDimension(n); |
| 359 |
for (int i = 0; i < n; i++) { |
| 360 |
locationsParameter.getParameter(i).setId("cluster_" + (i+1)); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
@Override |
| 365 |
protected void handleVariableChangedEvent(Variable variable, int index, Variable.ChangeType type) { |
| 366 |
if (variable == clusterIndexParameter) { |
| 367 |
for (int i = 0; i < distanceUpdated.length; i++) { |
| 368 |
distanceUpdated[i] = true; |
| 369 |
} |
| 370 |
residualsKnown = false; |
| 371 |
thresholdsKnown = false; |
| 372 |
clusterMaskKnown = false; |
| 373 |
} |
| 374 |
|
| 375 |
if (tipTraitParameter != null) { |
| 376 |
Parameter locations = getLocationsParameter(); |
| 377 |
int mdsDimension = getMDSDimension(); |
| 378 |
|
| 379 |
if (variable == locations) { |
| 380 |
int location = index / mdsDimension; |
| 381 |
int dim = index % mdsDimension; |
| 382 |
|
| 383 |
for (int i = 0; i < clusterIndexParameter.getDimension(); i++) { |
| 384 |
|
| 385 |
if (((int)clusterIndexParameter.getParameterValue(i)) == location) { |
| 386 |
if (tipIndices[i] != -1) { |
| 387 |
tipTraitParameter.setParameterValue((tipIndices[i] * mdsDimension) + dim, locations.getParameterValue(index)); |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
} else if (variable == clusterIndexParameter) { |
| 392 |
|
| 393 |
if (tipIndices[index] != -1) { |
| 394 |
int location = (int)clusterIndexParameter.getParameterValue(index); |
| 395 |
for (int dim = 0; dim < mdsDimension; dim++) { |
| 396 |
tipTraitParameter.setParameterValue((tipIndices[index] * mdsDimension) + dim, locations.getParameterValue((location * mdsDimension) + dim)); |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
super.handleVariableChangedEvent(variable, index, type); |
| 403 |
} |
| 404 |
|
| 405 |
public CompoundParameter getTipTraitParameter() { |
| 406 |
return tipTraitParameter; |
| 407 |
} |
| 408 |
|
| 409 |
public int[] getTipIndices() { |
| 410 |
return tipIndices; |
| 411 |
} |
| 412 |
|
| 413 |
private CompoundParameter tipTraitParameter; |
| 414 |
private int[] tipIndices; |
| 415 |
|
| 416 |
@Override |
| 417 |
public void makeDirty() { |
| 418 |
super.makeDirty(); |
| 419 |
clusterMaskKnown = false; |
| 420 |
} |
| 421 |
|
| 422 |
@Override |
| 423 |
protected void storeState() { |
| 424 |
super.storeState(); |
| 425 |
} |
| 426 |
|
| 427 |
@Override |
| 428 |
protected void restoreState() { |
| 429 |
super.restoreState(); |
| 430 |
|
| 431 |
clusterMaskKnown = false; |
| 432 |
} |
| 433 |
|
| 434 |
@Override |
| 435 |
protected int getLocationIndex(final int index) { |
| 436 |
return (int)clusterIndexParameter.getParameterValue(index); |
| 437 |
} |
| 438 |
|
| 439 |
private void updateClusterSizes() { |
| 440 |
for (int i = 0; i < maxClusterCount; i++) { |
| 441 |
clusterSizes[i] = 0; |
| 442 |
} |
| 443 |
for (int i = 0; i < getLocationCount(); i++) { |
| 444 |
int j = (int)clusterIndexParameter.getParameterValue(i); |
| 445 |
clusterSizes[j] ++; |
| 446 |
} |
| 447 |
clusterCount = 0; |
| 448 |
for (int i = 0; i < maxClusterCount; i++) { |
| 449 |
if (clusterSizes[i] > 0) { |
| 450 |
clusterCount++; |
| 451 |
} |
| 452 |
} |
| 453 |
clusterMaskKnown = true; |
| 454 |
} |
| 455 |
|
| 456 |
private int maxClusterCount; |
| 457 |
private final Parameter clusterIndexParameter; |
| 458 |
private final int[] clusterSizes; |
| 459 |
private int clusterCount; |
| 460 |
|
| 461 |
private boolean clusterMaskKnown; |
| 462 |
|
| 463 |
public class ClusterMask extends Statistic.Abstract { |
| 464 |
|
| 465 |
public ClusterMask() { |
| 466 |
super("clusterMask"); |
| 467 |
} |
| 468 |
|
| 469 |
public int getDimension() { |
| 470 |
return maxClusterCount; |
| 471 |
} |
| 472 |
|
| 473 |
public double getStatisticValue(int i) { |
| 474 |
if (!clusterMaskKnown) { |
| 475 |
updateClusterSizes(); |
| 476 |
} |
| 477 |
return clusterSizes[i] > 0 ? 1.0 : 0.0; |
| 478 |
} |
| 479 |
|
| 480 |
} |
| 481 |
|
| 482 |
public class ClusterIndices extends Statistic.Abstract { |
| 483 |
|
| 484 |
public ClusterIndices() { |
| 485 |
super("clusterIndices"); |
| 486 |
} |
| 487 |
|
| 488 |
public int getDimension() { |
| 489 |
return clusterIndexParameter.getDimension(); |
| 490 |
} |
| 491 |
|
| 492 |
@Override |
| 493 |
public String getDimensionName(final int i) { |
| 494 |
return getLocationLabels()[i]; |
| 495 |
} |
| 496 |
|
| 497 |
public double getStatisticValue(int i) { |
| 498 |
return clusterIndexParameter.getParameterValue(i); |
| 499 |
} |
| 500 |
|
| 501 |
} |
| 502 |
|
| 503 |
public class ClusterCount extends Statistic.Abstract { |
| 504 |
|
| 505 |
public ClusterCount() { |
| 506 |
super("clusterCount"); |
| 507 |
} |
| 508 |
|
| 509 |
public int getDimension() { |
| 510 |
return 1; |
| 511 |
} |
| 512 |
|
| 513 |
public double getStatisticValue(int i) { |
| 514 |
if (!clusterMaskKnown) { |
| 515 |
updateClusterSizes(); |
| 516 |
} |
| 517 |
return clusterCount; |
| 518 |
} |
| 519 |
|
| 520 |
} |
| 521 |
|
| 522 |
public class ClusterSizes extends Statistic.Abstract { |
| 523 |
|
| 524 |
public ClusterSizes() { |
| 525 |
super("clusterSizes"); |
| 526 |
} |
| 527 |
|
| 528 |
public int getDimension() { |
| 529 |
return maxClusterCount; |
| 530 |
} |
| 531 |
|
| 532 |
public double getStatisticValue(int i) { |
| 533 |
if (!clusterMaskKnown) { |
| 534 |
updateClusterSizes(); |
| 535 |
} |
| 536 |
return clusterSizes[i]; |
| 537 |
} |
| 538 |
|
| 539 |
} |
| 540 |
|
| 541 |
public class ClusteredLocations extends Statistic.Abstract { |
| 542 |
|
| 543 |
public ClusteredLocations() { |
| 544 |
super("clusteredLocations"); |
| 545 |
} |
| 546 |
|
| 547 |
@Override |
| 548 |
public String getDimensionName(final int i) { |
| 549 |
int location = i / getMDSDimension(); |
| 550 |
int dim = i % getMDSDimension(); |
| 551 |
|
| 552 |
String label = getLocationLabels()[location]; |
| 553 |
if (getMDSDimension() == 2) { |
| 554 |
return label + "_" + (dim == 0 ? "X" : "Y"); |
| 555 |
} else { |
| 556 |
return label + "_" + (dim + 1); |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
public int getDimension() { |
| 561 |
return getLocationCount() * getMDSDimension(); |
| 562 |
} |
| 563 |
|
| 564 |
public double getStatisticValue(final int i) { |
| 565 |
int location = i / getMDSDimension(); |
| 566 |
int dim = i % getMDSDimension(); |
| 567 |
|
| 568 |
int j = (int)clusterIndexParameter.getParameterValue(location); |
| 569 |
Parameter loc = getLocationsParameter().getParameter(j); |
| 570 |
|
| 571 |
return loc.getParameterValue(dim); |
| 572 |
} |
| 573 |
|
| 574 |
} |
| 575 |
|
| 576 |
public class VirusLocation extends Statistic.Abstract { |
| 577 |
|
| 578 |
public VirusLocation(String statisticName, int virusIndex) { |
| 579 |
super(statisticName); |
| 580 |
this.virusIndex = virusIndex; |
| 581 |
} |
| 582 |
|
| 583 |
@Override |
| 584 |
public String getDimensionName(final int dim) { |
| 585 |
if (getMDSDimension() == 2) { |
| 586 |
return getStatisticName() + "_" + (dim == 0 ? "X" : "Y"); |
| 587 |
} else { |
| 588 |
return getStatisticName() + "_" + (dim + 1); |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
public int getDimension() { |
| 593 |
return getMDSDimension(); |
| 594 |
} |
| 595 |
|
| 596 |
public double getStatisticValue(final int dim) { |
| 597 |
int cluster = (int)clusterIndexParameter.getParameterValue(virusIndex); |
| 598 |
Parameter loc = getLocationsParameter().getParameter(cluster); |
| 599 |
return loc.getParameterValue(dim); |
| 600 |
} |
| 601 |
|
| 602 |
private final int virusIndex; |
| 603 |
} |
| 604 |
|
| 605 |
private class Pair { |
| 606 |
Pair(final int location1, final int location2) { |
| 607 |
if (location1 < location2) { |
| 608 |
this.location1 = location1; |
| 609 |
this.location2 = location2; |
| 610 |
} else { |
| 611 |
this.location1 = location2; |
| 612 |
this.location2 = location1; |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
int location1; |
| 617 |
int location2; |
| 618 |
|
| 619 |
@Override |
| 620 |
public boolean equals(final Object o) { |
| 621 |
return ((Pair)o).location1 == location1 && ((Pair)o).location2 == location2; |
| 622 |
} |
| 623 |
|
| 624 |
@Override |
| 625 |
public String toString() { |
| 626 |
return "" + location1 + ", " + location2; |
| 627 |
} |
| 628 |
}; |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 634 |
public final static String FILE_NAME = "fileName"; |
| 635 |
public final static String VIRUS_MAP_FILE_NAME = "virusMapFile"; |
| 636 |
public final static String ASSAY_MAP_FILE_NAME = "assayMapFile"; |
| 637 |
|
| 638 |
public static final String CLUSTER_INDICES = "clusterIndices"; |
| 639 |
public final static String TIP_TRAIT = "tipTrait"; |
| 640 |
public final static String LOCATIONS = "locations"; |
| 641 |
public static final String MDS_DIMENSION = "mdsDimension"; |
| 642 |
public static final String MDS_PRECISION = "mdsPrecision"; |
| 643 |
|
| 644 |
public static final String VIRUS_LOCATIONS = "virusLocations"; |
| 645 |
|
| 646 |
public static final String LOG_2_TRANSFORM = "log2Transform"; |
| 647 |
public static final String TITRATION_THRESHOLD = "titrationThreshold"; |
| 648 |
|
| 649 |
public String getParserName() { |
| 650 |
return DISCRETE_ANTIGENIC_TRAIT_LIKELIHOOD; |
| 651 |
} |
| 652 |
|
| 653 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 654 |
|
| 655 |
String fileName = xo.getStringAttribute(FILE_NAME); |
| 656 |
DataTable<String[]> assayTable; |
| 657 |
try { |
| 658 |
assayTable = DataTable.Text.parse(new FileReader(fileName)); |
| 659 |
} catch (IOException e) { |
| 660 |
throw new XMLParseException("Unable to read assay data from file: " + e.getMessage()); |
| 661 |
} |
| 662 |
|
| 663 |
Map<String, String> virusAntiserumMap = null; |
| 664 |
if (xo.hasAttribute(VIRUS_MAP_FILE_NAME)) { |
| 665 |
try { |
| 666 |
virusAntiserumMap = readMap(xo.getStringAttribute(VIRUS_MAP_FILE_NAME)); |
| 667 |
} catch (IOException e) { |
| 668 |
throw new XMLParseException("Virus map file not found: " + xo.getStringAttribute(VIRUS_MAP_FILE_NAME)); |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
Map<String, String> assayAntiserumMap = null; |
| 673 |
if (xo.hasAttribute(ASSAY_MAP_FILE_NAME)) { |
| 674 |
try { |
| 675 |
assayAntiserumMap = readMap(xo.getStringAttribute(ASSAY_MAP_FILE_NAME)); |
| 676 |
} catch (IOException e) { |
| 677 |
throw new XMLParseException("Assay map file not found: " + xo.getStringAttribute(ASSAY_MAP_FILE_NAME)); |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
int mdsDimension = xo.getIntegerAttribute(MDS_DIMENSION); |
| 682 |
|
| 683 |
boolean log2Transform = false; |
| 684 |
if (xo.hasAttribute(LOG_2_TRANSFORM)) { |
| 685 |
log2Transform = xo.getBooleanAttribute(LOG_2_TRANSFORM); |
| 686 |
} |
| 687 |
|
| 688 |
Parameter clusterIndicesParameter = (Parameter) xo.getElementFirstChild(CLUSTER_INDICES); |
| 689 |
|
| 690 |
List<String> virusLocationStatisticList = null; |
| 691 |
String[] virusLocations = xo.getStringArrayAttribute(VIRUS_LOCATIONS); |
| 692 |
if (virusLocations != null) { |
| 693 |
virusLocationStatisticList = Arrays.asList(virusLocations); |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
CompoundParameter tipTraitParameter = null; |
| 702 |
if (xo.hasChildNamed(TIP_TRAIT)) { |
| 703 |
tipTraitParameter = (CompoundParameter) xo.getElementFirstChild(TIP_TRAIT); |
| 704 |
} |
| 705 |
|
| 706 |
MatrixParameter locationsParameter = (MatrixParameter) xo.getElementFirstChild(LOCATIONS); |
| 707 |
|
| 708 |
Parameter mdsPrecision = (Parameter) xo.getElementFirstChild(MDS_PRECISION); |
| 709 |
|
| 710 |
AntigenicTraitLikelihood AGTL = new DiscreteAntigenicTraitLikelihood(mdsDimension, mdsPrecision, clusterIndicesParameter, locationsParameter, tipTraitParameter, assayTable, virusAntiserumMap, assayAntiserumMap, virusLocationStatisticList, log2Transform); |
| 711 |
|
| 712 |
Logger.getLogger("dr.evomodel").info("Using Discrete Evolutionary Cartography model. Please cite:\n" + Utils.getCitationString(AGTL)); |
| 713 |
|
| 714 |
return AGTL; |
| 715 |
} |
| 716 |
|
| 717 |
private Map<String, String> readMap(String fileName) throws IOException { |
| 718 |
BufferedReader reader = new BufferedReader(new FileReader(fileName)); |
| 719 |
|
| 720 |
Map<String, String> map = new HashMap<String, String>(); |
| 721 |
|
| 722 |
String line = reader.readLine(); |
| 723 |
while (line != null) { |
| 724 |
if (line.trim().length() > 0) { |
| 725 |
String[] parts = line.split("\t"); |
| 726 |
if (parts.length > 1) { |
| 727 |
map.put(parts[0], parts[1]); |
| 728 |
} |
| 729 |
} |
| 730 |
line = reader.readLine(); |
| 731 |
} |
| 732 |
|
| 733 |
reader.close(); |
| 734 |
|
| 735 |
return map; |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
public String getParserDescription() { |
| 745 |
return "Provides the likelihood of immunological assay data such as Hemagglutinin inhibition (HI) given vectors of coordinates" + |
| 746 |
"for viruses and sera/antisera in some multidimensional 'antigenic' space. This is a discrete classifier form of the model" + |
| 747 |
"which assigns viruses to discrete antigenic classes."; |
| 748 |
} |
| 749 |
|
| 750 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 751 |
return rules; |
| 752 |
} |
| 753 |
|
| 754 |
private final XMLSyntaxRule[] rules = { |
| 755 |
AttributeRule.newStringRule(FILE_NAME, false, "The name of the file containing the assay table"), |
| 756 |
AttributeRule.newStringRule(VIRUS_MAP_FILE_NAME, true, "The name of the file containing the virus to serum map"), |
| 757 |
AttributeRule.newStringRule(ASSAY_MAP_FILE_NAME, true, "The name of the file containing the assay to serum map"), |
| 758 |
AttributeRule.newIntegerRule(MDS_DIMENSION, false, "The dimension of the space for MDS"), |
| 759 |
AttributeRule.newBooleanRule(LOG_2_TRANSFORM, true, "Whether to log2 transform the data"), |
| 760 |
AttributeRule.newStringArrayRule(VIRUS_LOCATIONS, true, "A list of virus names to create location statistics for"), |
| 761 |
new ElementRule(CLUSTER_INDICES, Parameter.class, "The parameter of cluster indices for each virus/serum"), |
| 762 |
new ElementRule(TIP_TRAIT, CompoundParameter.class, "The parameter of tip locations from the tree", true), |
| 763 |
new ElementRule(LOCATIONS, MatrixParameter.class), |
| 764 |
new ElementRule(MDS_PRECISION, Parameter.class) |
| 765 |
}; |
| 766 |
|
| 767 |
public Class getReturnType() { |
| 768 |
return DiscreteAntigenicTraitLikelihood.class; |
| 769 |
} |
| 770 |
}; |
| 771 |
|
| 772 |
@Override |
| 773 |
public Citation.Category getCategory() { |
| 774 |
return Citation.Category.TRAIT_MODELS; |
| 775 |
} |
| 776 |
|
| 777 |
@Override |
| 778 |
public String getDescription() { |
| 779 |
return "Bayesian Antigenic Cartography framework"; |
| 780 |
} |
| 781 |
|
| 782 |
public List<Citation> getCitations() { |
| 783 |
return Arrays.asList(CommonCitations.BEDFORD_2015_INTEGRATING); |
| 784 |
} |
| 785 |
} |