| 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.app.phylogeography.tools; |
| 27 |
|
| 28 |
|
| 29 |
import dr.app.tools.TimeSlicer; |
| 30 |
import dr.app.util.Arguments; |
| 31 |
import dr.geo.math.SphericalPolarCoordinates; |
| 32 |
import dr.stats.DiscreteStatistics; |
| 33 |
import org.jdom.Element; |
| 34 |
import org.jdom.output.Format; |
| 35 |
import org.jdom.output.XMLOutputter; |
| 36 |
|
| 37 |
import java.io.*; |
| 38 |
import java.util.ArrayList; |
| 39 |
import java.util.List; |
| 40 |
import java.util.StringTokenizer; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public class DiscreteRatePriorGenerator { |
| 48 |
|
| 49 |
public static final String HELP = "help"; |
| 50 |
public static final String COORDINATES = "coordinates"; |
| 51 |
public static final String DENSITIES = "densities"; |
| 52 |
public static final String GENERICS = "generics"; |
| 53 |
public static final String FORMAT = "format"; |
| 54 |
public static final String MODEL = "model"; |
| 55 |
|
| 56 |
public DiscreteRatePriorGenerator(String[] locations, Double[] latitudes, Double[] longitudes, Double[] densities) { |
| 57 |
|
| 58 |
if (locations == null) { |
| 59 |
|
| 60 |
System.err.println("no locations specified!"); |
| 61 |
} else { |
| 62 |
this.locations = locations; |
| 63 |
} |
| 64 |
|
| 65 |
this.latitudes = latitudes; |
| 66 |
this.longitudes = longitudes; |
| 67 |
this.densities = densities; |
| 68 |
|
| 69 |
if ((latitudes == null)||(longitudes == null)) { |
| 70 |
progressStream.println("no latitudes or longitudes specified!"); |
| 71 |
} else { |
| 72 |
distances = getUpperTriangleDistanceMatrix(latitudes,longitudes); |
| 73 |
} |
| 74 |
|
| 75 |
if (densities != null) { |
| 76 |
densityDonorMatrix = getDensityMatrix(densities, true); |
| 77 |
densityRecipientMatrix = getDensityMatrix(densities, false); |
| 78 |
} |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
private String[] locations; |
| 83 |
private Double[] latitudes; |
| 84 |
private Double[] longitudes; |
| 85 |
private final Double[] densities; |
| 86 |
private double[] distances; |
| 87 |
private double[] densityDonorMatrix; |
| 88 |
private double[] densityRecipientMatrix; |
| 89 |
|
| 90 |
private double[] getUpperTriangleDistanceMatrix(Double[] latitudes, Double[] longitudes) { |
| 91 |
|
| 92 |
double[] distances = new double[(latitudes.length * (latitudes.length - 1))/2]; |
| 93 |
|
| 94 |
int distanceCounter = 0; |
| 95 |
int pairwiseCounter1 = 0; |
| 96 |
for (int c = 0; c < latitudes.length; c++) { |
| 97 |
pairwiseCounter1 ++; |
| 98 |
for (int d = pairwiseCounter1; d < latitudes.length; d++) { |
| 99 |
distances[distanceCounter] = getKilometerGreatCircleDistance(latitudes[c],longitudes[c],latitudes[d],longitudes[d]); |
| 100 |
distanceCounter++; |
| 101 |
} |
| 102 |
} |
| 103 |
return distances; |
| 104 |
} |
| 105 |
|
| 106 |
private double[] getFullDistanceMatrix(Double[] latitudes, Double[] longitudes) { |
| 107 |
|
| 108 |
double[] distances = new double[latitudes.length * latitudes.length]; |
| 109 |
|
| 110 |
int distanceCounter = 0; |
| 111 |
for (int a = 0; a < latitudes.length; a++) { |
| 112 |
|
| 113 |
for (int b = 0; b < latitudes.length; b++) { |
| 114 |
distances[distanceCounter] = getKilometerGreatCircleDistance(latitudes[a],longitudes[a],latitudes[b],longitudes[b]); |
| 115 |
distanceCounter++; |
| 116 |
} |
| 117 |
} |
| 118 |
return distances; |
| 119 |
} |
| 120 |
|
| 121 |
private double[] getFullCoordDiffMatrix(Double[] coordinates, boolean positive, boolean negative) { |
| 122 |
|
| 123 |
double[] differences = new double[coordinates.length * coordinates.length]; |
| 124 |
|
| 125 |
int differenceCounter = 0; |
| 126 |
for (int a = 0; a < coordinates.length; a++) { |
| 127 |
for (int b = 0; b < coordinates.length; b++) { |
| 128 |
double difference = coordinates[b]-coordinates[a]; |
| 129 |
if (difference > 0 && positive) { |
| 130 |
differences[differenceCounter] = difference; |
| 131 |
} else if (difference < 0 && negative) { |
| 132 |
differences[differenceCounter] = difference; |
| 133 |
} else { |
| 134 |
differences[differenceCounter] = 0; |
| 135 |
} |
| 136 |
differenceCounter++; |
| 137 |
} |
| 138 |
} |
| 139 |
return differences; |
| 140 |
} |
| 141 |
|
| 142 |
private static double getKilometerGreatCircleDistance(double lat1, double long1, double lat2, double long2) { |
| 143 |
SphericalPolarCoordinates coord1 = new SphericalPolarCoordinates(lat1, long1); |
| 144 |
SphericalPolarCoordinates coord2 = new SphericalPolarCoordinates(lat2, long2); |
| 145 |
return (coord1.distance(coord2)); |
| 146 |
} |
| 147 |
|
| 148 |
private double[] getDensityMatrix(Double[] densities, boolean donor) { |
| 149 |
|
| 150 |
double[] returnMatrix = new double[densities.length * (densities.length - 1)]; |
| 151 |
int distanceCounter = 0; |
| 152 |
int matrixEntry; |
| 153 |
for (int c = 0; c < densities.length; c++) { |
| 154 |
for (int d = 0; d < densities.length; d++) { |
| 155 |
if (c == d) { |
| 156 |
continue; |
| 157 |
} |
| 158 |
if (donor) { |
| 159 |
matrixEntry = c; |
| 160 |
} else { |
| 161 |
matrixEntry = d; |
| 162 |
} |
| 163 |
returnMatrix[distanceCounter] = densities[matrixEntry]; |
| 164 |
distanceCounter++; |
| 165 |
} |
| 166 |
} |
| 167 |
return returnMatrix; |
| 168 |
} |
| 169 |
|
| 170 |
private void printLocations(String[] locs, boolean upper) { |
| 171 |
|
| 172 |
int pairwiseCounter1 = 0; |
| 173 |
for (int c = 0; c < locs.length; c++) { |
| 174 |
pairwiseCounter1 ++; |
| 175 |
for (int d = pairwiseCounter1; d < locs.length; d++) { |
| 176 |
if (upper) { |
| 177 |
System.out.println(locs[c]+"\t"+locs[d]); |
| 178 |
} else { |
| 179 |
System.out.println(locs[d]+"\t"+locs[c]); |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
private void printLocationsDensitiesDistances (String[] locs, boolean upper) { |
| 186 |
|
| 187 |
double[] firstDensity = getSingleElementFromFullMatrix(densities,false); |
| 188 |
double[] secondDensity = getSingleElementFromFullMatrix(densities,true); |
| 189 |
|
| 190 |
System.out.println("location1\tlocation2\tdistance\tdensity1\tdensity2"); |
| 191 |
int pairwiseCounter1 = 0; |
| 192 |
int arrayCounter = 0; |
| 193 |
for (int c = 0; c < locs.length; c++) { |
| 194 |
pairwiseCounter1 ++; |
| 195 |
for (int d = pairwiseCounter1; d < locs.length; d++) { |
| 196 |
if (upper) { |
| 197 |
System.out.println(locs[c]+"\t"+locs[d]+"\t"+distances[arrayCounter]+"\t"+firstDensity[arrayCounter]+"\t"+secondDensity[arrayCounter]); |
| 198 |
arrayCounter ++; |
| 199 |
} else { |
| 200 |
System.out.println(locs[d]+"\t"+locs[c]+"\t"+distances[arrayCounter]+"\t"+firstDensity[(firstDensity.length/2)+arrayCounter]+"\t"+secondDensity[(secondDensity.length/2)+arrayCounter]); |
| 201 |
arrayCounter ++; |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
private void printFullDistanceMatrix(String name, boolean locationNames) { |
| 208 |
try { |
| 209 |
PrintWriter outFile = new PrintWriter(new FileWriter(name), true); |
| 210 |
|
| 211 |
outFile.print("location"); |
| 212 |
for (int a = 0; a < locations.length; a++) { |
| 213 |
outFile.print(locations[a]+"\t"); |
| 214 |
for (int b = 0; b < locations.length; b++) { |
| 215 |
double lat1 = latitudes[a]; |
| 216 |
double lat2 = latitudes[b]; |
| 217 |
double long1 = longitudes[a]; |
| 218 |
double long2 = longitudes[b]; |
| 219 |
double distance = getKilometerGreatCircleDistance(lat1,long1,lat2,long2); |
| 220 |
outFile.print(distance+"\t"); |
| 221 |
} |
| 222 |
outFile.print("\r"); |
| 223 |
} |
| 224 |
outFile.close(); |
| 225 |
|
| 226 |
} catch(IOException io) { |
| 227 |
System.err.print("Error writing to file: " + name); |
| 228 |
} |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
enum OutputFormat { |
| 233 |
TAB, |
| 234 |
SPACE, |
| 235 |
XML |
| 236 |
} |
| 237 |
|
| 238 |
enum Model { |
| 239 |
PRIOR, |
| 240 |
GLM, |
| 241 |
JUMP |
| 242 |
} |
| 243 |
|
| 244 |
public void output(String outputFileName, OutputFormat outputFormat, Model model) { |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
resultsStream = System.out; |
| 251 |
|
| 252 |
if (outputFileName != null) { |
| 253 |
try { |
| 254 |
resultsStream = new PrintStream(new File(outputFileName)); |
| 255 |
} catch (IOException e) { |
| 256 |
System.err.println("Error opening file: "+outputFileName); |
| 257 |
System.exit(1); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
if (model == model.PRIOR) { |
| 262 |
|
| 263 |
int predictor = 0; |
| 264 |
|
| 265 |
if (distances != null) { |
| 266 |
outputStringLine("reversible priors: distances",outputFormat); |
| 267 |
resultsStream.print("\r"); |
| 268 |
outputArray(distances, outputFormat, model, predictor, false); |
| 269 |
resultsStream.print("\r"); |
| 270 |
outputStringLine("reversible priors: normalized inverse distances",outputFormat); |
| 271 |
resultsStream.print("\r"); |
| 272 |
outputArray(transform(distances,true, true, false, false), outputFormat, model, predictor, false); |
| 273 |
resultsStream.print("\r"); |
| 274 |
outputStringLine("reversible priors: normalized inverse log distances",outputFormat); |
| 275 |
resultsStream.print("\r"); |
| 276 |
outputArray(transform(distances,true, true, false, true), outputFormat, model, predictor, false); |
| 277 |
resultsStream.print("\r"); |
| 278 |
} |
| 279 |
if (densities != null) { |
| 280 |
outputStringLine("reversible priors: densities",outputFormat); |
| 281 |
resultsStream.print("\r"); |
| 282 |
outputArray(transform(getUpperTrianglePairwiseProductMatrix(densities),false,false,false,false), outputFormat, model, predictor, false); |
| 283 |
resultsStream.print("\r"); |
| 284 |
outputStringLine("reversible priors: normalized densities",outputFormat); |
| 285 |
resultsStream.print("\r"); |
| 286 |
outputArray(transform(getUpperTrianglePairwiseProductMatrix(densities),false,true,false,false), outputFormat, model, predictor, false); |
| 287 |
resultsStream.print("\r"); |
| 288 |
outputStringLine("reversible priors: normalized log densities",outputFormat); |
| 289 |
resultsStream.print("\r"); |
| 290 |
outputArray(transform(getUpperTrianglePairwiseProductMatrix(densities),false,true,false,true), outputFormat, model, predictor, false); |
| 291 |
resultsStream.print("\r"); |
| 292 |
|
| 293 |
if (distances != null) { |
| 294 |
outputStringLine("reversible priors: product of normalized densities divided by normalized distances",outputFormat); |
| 295 |
resultsStream.print("\r"); |
| 296 |
outputArray(productOfArrays(transform(getUpperTrianglePairwiseProductMatrix(densities),false,true,false,false),transform(distances,false, true,false,false),true), outputFormat, model, predictor, false); |
| 297 |
resultsStream.print("\r"); |
| 298 |
outputStringLine("reversible priors: normalized product of densities divided by distances",outputFormat); |
| 299 |
resultsStream.print("\r"); |
| 300 |
outputArray(transform(productOfArrays(getUpperTrianglePairwiseProductMatrix(densities),distances, true),false,true,false,false), outputFormat, model, predictor, false); |
| 301 |
resultsStream.print("\r"); |
| 302 |
outputStringLine("reversible priors: normalized log product of densities divided by distances",outputFormat); |
| 303 |
resultsStream.print("\r"); |
| 304 |
outputArray(transform(productOfArrays(getUpperTrianglePairwiseProductMatrix(densities),distances, true),false,true,false,true), outputFormat, model, predictor, false); |
| 305 |
resultsStream.print("\r"); |
| 306 |
} |
| 307 |
|
| 308 |
} |
| 309 |
if (distances != null) { |
| 310 |
outputStringLine("nonreversible priors: distances",outputFormat); |
| 311 |
resultsStream.print("\r"); |
| 312 |
outputArray(distances, outputFormat, model, predictor, true); |
| 313 |
resultsStream.print("\r"); |
| 314 |
outputStringLine("nonreversible priors: normalized inverse distances",outputFormat); |
| 315 |
resultsStream.print("\r"); |
| 316 |
outputArray(transform(distances,true, true, false, false), outputFormat, model, predictor, true); |
| 317 |
resultsStream.print("\r"); |
| 318 |
outputStringLine("nonreversible priors: normalized inverse log distances",outputFormat); |
| 319 |
resultsStream.print("\r"); |
| 320 |
outputArray(transform(distances,true, true, false, true), outputFormat, model,predictor, true); |
| 321 |
resultsStream.print("\r"); |
| 322 |
} |
| 323 |
if (densities != null) { |
| 324 |
outputStringLine("nonreversible priors: densities",outputFormat); |
| 325 |
resultsStream.print("\r"); |
| 326 |
outputArray(transform(getUpperTrianglePairwiseProductMatrix(densities),false,false,false,false), outputFormat, model, predictor, true); |
| 327 |
resultsStream.print("\r"); |
| 328 |
outputStringLine("nonreversible priors: normalized densities",outputFormat); |
| 329 |
resultsStream.print("\r"); |
| 330 |
outputArray(transform(getUpperTrianglePairwiseProductMatrix(densities),false,true,false,false), outputFormat, model, predictor, true); |
| 331 |
resultsStream.print("\r"); |
| 332 |
outputStringLine("nonreversible priors: normalized log densities",outputFormat); |
| 333 |
resultsStream.print("\r"); |
| 334 |
outputArray(transform(getUpperTrianglePairwiseProductMatrix(densities),false,true,false,true), outputFormat, model, predictor, true); |
| 335 |
resultsStream.print("\r"); |
| 336 |
|
| 337 |
if (distances != null) { |
| 338 |
outputStringLine("nonreversible priors: product of normalized densities divided by normalized distances",outputFormat); |
| 339 |
resultsStream.print("\r"); |
| 340 |
outputArray(productOfArrays(transform(getUpperTrianglePairwiseProductMatrix(densities),false,true,false,false),transform(distances,false, true, false, false),true), outputFormat, model, predictor, true); |
| 341 |
resultsStream.print("\r"); |
| 342 |
outputStringLine("nonreversible priors: normalized product of densities divided by distances",outputFormat); |
| 343 |
resultsStream.print("\r"); |
| 344 |
outputArray(transform(productOfArrays(getUpperTrianglePairwiseProductMatrix(densities),distances, true),false,true,false,false), outputFormat, model, predictor, true); |
| 345 |
resultsStream.print("\r"); |
| 346 |
outputStringLine("nonreversible priors: normalized log product of densities divided by distances",outputFormat); |
| 347 |
resultsStream.print("\r"); |
| 348 |
outputArray(transform(productOfArrays(getUpperTrianglePairwiseProductMatrix(densities),distances, true),false,true,false,true), outputFormat, model, predictor, true); |
| 349 |
resultsStream.print("\r"); |
| 350 |
} |
| 351 |
|
| 352 |
} |
| 353 |
} else if (model == model.GLM) { |
| 354 |
|
| 355 |
int predictor = 1; |
| 356 |
|
| 357 |
|
| 358 |
if (distances != null) { |
| 359 |
|
| 360 |
|
| 361 |
outputStringLine("predictor: distances",outputFormat); |
| 362 |
resultsStream.print("\r"); |
| 363 |
outputArray(transform(distances,false, false, true, false), outputFormat, model, predictor, true); |
| 364 |
predictor++; |
| 365 |
outputStringLine("predictor: standardized log distances",outputFormat); |
| 366 |
resultsStream.print("\r"); |
| 367 |
outputArray(transform(distances,false, false, true, true), outputFormat, model, predictor, true); |
| 368 |
} |
| 369 |
if (densities != null) { |
| 370 |
predictor++; |
| 371 |
outputStringLine("predictor: standardized donor densities",outputFormat); |
| 372 |
resultsStream.print("\r"); |
| 373 |
outputArray(transform(getSingleElementFromFullMatrix(densities, false),false,false,true,true), outputFormat, model, predictor, false); |
| 374 |
predictor++; |
| 375 |
outputStringLine("predictor: standardized recipient densities",outputFormat); |
| 376 |
resultsStream.print("\r"); |
| 377 |
outputArray(transform(getSingleElementFromFullMatrix(densities, true),false,false,true,true), outputFormat, model, predictor, false); |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
} else if (model == model.JUMP) { |
| 393 |
|
| 394 |
int predictor = 0; |
| 395 |
|
| 396 |
outputStringLine("great circle distance jump matrix",outputFormat); |
| 397 |
resultsStream.print("\r"); |
| 398 |
outputArray(transform(getFullDistanceMatrix(latitudes, longitudes),false,false,false,false), outputFormat, model, predictor, false); |
| 399 |
resultsStream.print("\r"); |
| 400 |
predictor ++; |
| 401 |
|
| 402 |
outputStringLine("latitude jump matrix",outputFormat); |
| 403 |
resultsStream.print("\r"); |
| 404 |
outputArray(transform(getFullCoordDiffMatrix(latitudes, true, true),false,false,false,false), outputFormat, model, predictor, false); |
| 405 |
resultsStream.print("\r"); |
| 406 |
predictor ++; |
| 407 |
|
| 408 |
outputStringLine("longitude jump matrix",outputFormat); |
| 409 |
resultsStream.print("\r"); |
| 410 |
outputArray(transform(getFullCoordDiffMatrix(longitudes, true, true),false,false,false,false), outputFormat, model, predictor, false); |
| 411 |
resultsStream.print("\r"); |
| 412 |
predictor ++; |
| 413 |
|
| 414 |
outputStringLine("westward jump matrix",outputFormat); |
| 415 |
resultsStream.print("\r"); |
| 416 |
outputArray(transform(getFullCoordDiffMatrix(longitudes, false, true),false,false,false,false), outputFormat, model, predictor, false); |
| 417 |
resultsStream.print("\r"); |
| 418 |
predictor ++; |
| 419 |
|
| 420 |
outputStringLine("eastward jump matrix",outputFormat); |
| 421 |
resultsStream.print("\r"); |
| 422 |
outputArray(transform(getFullCoordDiffMatrix(longitudes, true, false),false,false,false,false), outputFormat, model, predictor, false); |
| 423 |
resultsStream.print("\r"); |
| 424 |
predictor ++; |
| 425 |
|
| 426 |
outputStringLine("northward jump matrix",outputFormat); |
| 427 |
resultsStream.print("\r"); |
| 428 |
outputArray(transform(getFullCoordDiffMatrix(latitudes, true, false),false,false,false,false), outputFormat, model, predictor, false); |
| 429 |
resultsStream.print("\r"); |
| 430 |
predictor ++; |
| 431 |
|
| 432 |
outputStringLine("southward jump matrix",outputFormat); |
| 433 |
resultsStream.print("\r"); |
| 434 |
outputArray(transform(getFullCoordDiffMatrix(latitudes, false, true),false,false,false,false), outputFormat, model, predictor, false); |
| 435 |
resultsStream.print("\r"); |
| 436 |
predictor ++; |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
for (int i = 0; i < locations.length; i++) { |
| 451 |
|
| 452 |
double[] locationReward = getLocationReward(i,locations.length); |
| 453 |
outputStringLine(locations[i]+" reward parameter",outputFormat); |
| 454 |
resultsStream.print("\r"); |
| 455 |
outputArray(transform(locationReward,false,false,false,false), outputFormat, model, predictor, false); |
| 456 |
resultsStream.print("\r"); |
| 457 |
predictor ++; |
| 458 |
|
| 459 |
} |
| 460 |
|
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
private double[] getUpperTrianglePairwiseProductMatrix(Double[] matrixValues) { |
| 465 |
|
| 466 |
double[] pairwiseProducts = new double[(matrixValues.length * (matrixValues.length - 1))/2]; |
| 467 |
|
| 468 |
int counter = 0; |
| 469 |
int pairwiseCounter1 = 0; |
| 470 |
for(Double matrixValue : matrixValues) { |
| 471 |
pairwiseCounter1++; |
| 472 |
for(int d = pairwiseCounter1; d < matrixValues.length; d++) { |
| 473 |
pairwiseProducts[counter] = matrixValue * matrixValues[d]; |
| 474 |
counter++; |
| 475 |
} |
| 476 |
} |
| 477 |
return pairwiseProducts; |
| 478 |
} |
| 479 |
|
| 480 |
private double[] getSingleElementFromFullMatrix(Double[] matrixValues, boolean secondElement) { |
| 481 |
|
| 482 |
double[] singleElements = new double[(matrixValues.length * (matrixValues.length - 1))]; |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
int counter = 0; |
| 487 |
int pairwiseCounter1 = 0; |
| 488 |
for(Double matrixValue : matrixValues) { |
| 489 |
pairwiseCounter1++; |
| 490 |
for(int d = pairwiseCounter1; d < matrixValues.length; d++) { |
| 491 |
if (secondElement) { |
| 492 |
singleElements[counter] = matrixValues[d]; |
| 493 |
} else { |
| 494 |
singleElements[counter] = matrixValue; |
| 495 |
|
| 496 |
} |
| 497 |
counter++; |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
int pairwiseCounter2 = 0; |
| 502 |
for(Double matrixValue : matrixValues) { |
| 503 |
pairwiseCounter2++; |
| 504 |
for(int d = pairwiseCounter2; d < matrixValues.length; d++) { |
| 505 |
if (secondElement) { |
| 506 |
singleElements[counter] = matrixValue; |
| 507 |
} else { |
| 508 |
singleElements[counter] = matrixValues[d]; |
| 509 |
} |
| 510 |
counter++; |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
return singleElements; |
| 517 |
} |
| 518 |
|
| 519 |
private double[] productOfArrays(double[] inputArray1, double[] inputArray2, boolean devision) { |
| 520 |
|
| 521 |
if (inputArray1.length != inputArray2.length) { |
| 522 |
System.err.println("trying to get a product of arrays of unequals size!"); |
| 523 |
System.exit(1); |
| 524 |
} |
| 525 |
|
| 526 |
double[] productOfArray = new double[inputArray1.length]; |
| 527 |
|
| 528 |
for (int i = 0; i < inputArray1.length; i++) { |
| 529 |
if (devision) { |
| 530 |
productOfArray[i] = inputArray1[i]/inputArray2[i]; |
| 531 |
} else { |
| 532 |
} |
| 533 |
productOfArray[i] = inputArray1[i]*inputArray2[i]; |
| 534 |
} |
| 535 |
|
| 536 |
return productOfArray; |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
private double[] transform(double[] inputArray, boolean inverse, boolean normalize, boolean standardize, boolean log) { |
| 541 |
|
| 542 |
double[] transformedDistances = new double[inputArray.length]; |
| 543 |
|
| 544 |
for(int u=0; u<inputArray.length; u++) { |
| 545 |
|
| 546 |
double distance = inputArray[u]; |
| 547 |
|
| 548 |
if (log) { |
| 549 |
distance = Math.log(distance); |
| 550 |
} |
| 551 |
if (inverse) { |
| 552 |
distance = 1/distance; |
| 553 |
} |
| 554 |
|
| 555 |
transformedDistances[u] = distance; |
| 556 |
} |
| 557 |
|
| 558 |
double meanDistance = 1; |
| 559 |
double stdev = 0; |
| 560 |
|
| 561 |
if (normalize || standardize) { |
| 562 |
meanDistance = DiscreteStatistics.mean(transformedDistances); |
| 563 |
} |
| 564 |
if (standardize) { |
| 565 |
stdev = Math.sqrt(DiscreteStatistics.variance(transformedDistances)); |
| 566 |
} |
| 567 |
|
| 568 |
for(int v=0; v<inputArray.length; v++) { |
| 569 |
if (normalize) { |
| 570 |
transformedDistances[v] = (transformedDistances[v]/meanDistance); |
| 571 |
} else if (standardize) { |
| 572 |
transformedDistances[v] = ((transformedDistances[v] - meanDistance)/stdev); |
| 573 |
} |
| 574 |
} |
| 575 |
return transformedDistances; |
| 576 |
} |
| 577 |
|
| 578 |
private void outputArray(double[] array, OutputFormat outputFormat, Model model, int predictor, boolean nonreversible) { |
| 579 |
|
| 580 |
StringBuilder sb1 = new StringBuilder(); |
| 581 |
StringBuilder sb2 = new StringBuilder(); |
| 582 |
String sep; |
| 583 |
if (outputFormat == OutputFormat.TAB ) { |
| 584 |
sep = "\t"; |
| 585 |
} else { |
| 586 |
sep = " "; |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
for(double anArray : array) { |
| 612 |
sb1.append(anArray + sep); |
| 613 |
sb2.append(1 + sep); |
| 614 |
} |
| 615 |
|
| 616 |
if (outputFormat == OutputFormat.XML ) { |
| 617 |
if (model == model.GLM) { |
| 618 |
Element parameter = new Element("parameter"); |
| 619 |
parameter.setAttribute("id","predictor"+predictor); |
| 620 |
if (nonreversible) { |
| 621 |
parameter.setAttribute("value",(sb1.toString()+sb1.toString())); |
| 622 |
} else { |
| 623 |
parameter.setAttribute("value",sb1.toString()); |
| 624 |
} |
| 625 |
XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat().setTextMode(Format.TextMode.PRESERVE)); |
| 626 |
try { |
| 627 |
xmlOutputter.output(parameter,resultsStream); |
| 628 |
} catch (IOException e) { |
| 629 |
System.err.println("IO Exception encountered: "+e.getMessage()); |
| 630 |
System.exit(-1); |
| 631 |
} |
| 632 |
resultsStream.print("\r"); |
| 633 |
} else if (model == model.PRIOR) { |
| 634 |
Element priorElement = new Element("multivariateGammaPrior"); |
| 635 |
Element data = new Element("data"); |
| 636 |
Element parameter1 = new Element("parameter"); |
| 637 |
parameter1.setAttribute("idref","rates"); |
| 638 |
data.addContent(parameter1); |
| 639 |
Element meanParameter = new Element("meanParameter"); |
| 640 |
Element parameter2 = new Element("parameter"); |
| 641 |
if (nonreversible) { |
| 642 |
parameter2.setAttribute("value",(sb1.toString()+sb1.toString())); |
| 643 |
} else { |
| 644 |
parameter2.setAttribute("value",sb1.toString()); |
| 645 |
} |
| 646 |
meanParameter.addContent(parameter2); |
| 647 |
Element coefficientOfVariation = new Element("coefficientOfVariation"); |
| 648 |
Element parameter3 = new Element("parameter"); |
| 649 |
if (nonreversible) { |
| 650 |
parameter3.setAttribute("value",sb2.toString()+sb2.toString()); |
| 651 |
} else { |
| 652 |
parameter3.setAttribute("value",sb2.toString()); |
| 653 |
} |
| 654 |
coefficientOfVariation.addContent(parameter3); |
| 655 |
priorElement.addContent(data); |
| 656 |
priorElement.addContent(meanParameter); |
| 657 |
priorElement.addContent(coefficientOfVariation); |
| 658 |
XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat().setTextMode(Format.TextMode.PRESERVE)); |
| 659 |
try { |
| 660 |
xmlOutputter.output(priorElement,resultsStream); |
| 661 |
} catch (IOException e) { |
| 662 |
System.err.println("IO Exception encountered: "+e.getMessage()); |
| 663 |
System.exit(-1); |
| 664 |
} |
| 665 |
resultsStream.print("\r"); |
| 666 |
} else if (model == model.JUMP) { |
| 667 |
|
| 668 |
Element parameter = new Element("parameter"); |
| 669 |
parameter.setAttribute("id","jump"+predictor); |
| 670 |
parameter.setAttribute("value",sb1.toString()); |
| 671 |
XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat().setTextMode(Format.TextMode.PRESERVE)); |
| 672 |
try { |
| 673 |
xmlOutputter.output(parameter,resultsStream); |
| 674 |
} catch (IOException e) { |
| 675 |
System.err.println("IO Exception encountered: "+e.getMessage()); |
| 676 |
System.exit(-1); |
| 677 |
} |
| 678 |
resultsStream.print("\r"); |
| 679 |
} |
| 680 |
} else { |
| 681 |
if (nonreversible) { |
| 682 |
resultsStream.print(sb1.toString()+sb1.toString()+"\r"); |
| 683 |
} else { |
| 684 |
resultsStream.print(sb1+"\r"); |
| 685 |
} |
| 686 |
|
| 687 |
} |
| 688 |
|
| 689 |
} |
| 690 |
|
| 691 |
private void outputStringLine(String outputString, OutputFormat outputFormat) { |
| 692 |
if (outputFormat == OutputFormat.XML ) { |
| 693 |
resultsStream.print("<!-- "); |
| 694 |
} |
| 695 |
resultsStream.print(outputString); |
| 696 |
if (outputFormat == OutputFormat.XML ) { |
| 697 |
resultsStream.print(" -->"); |
| 698 |
} else { |
| 699 |
resultsStream.print("\r"); |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
private double[] objectToPrimitiveArray(Double[] array) { |
| 704 |
double[] returnArray = new double[array.length]; |
| 705 |
int counter = 0; |
| 706 |
for(Double anArray : array) { |
| 707 |
returnArray[counter] = anArray.doubleValue(); |
| 708 |
counter ++; |
| 709 |
} |
| 710 |
return returnArray; |
| 711 |
} |
| 712 |
|
| 713 |
private double[] getLocationReward(int indicator, int length) { |
| 714 |
double[] returnArray = new double[length]; |
| 715 |
for (int i = 0; i < length; i++) { |
| 716 |
if (i == indicator) { |
| 717 |
returnArray[i] = 1.0; |
| 718 |
} else { |
| 719 |
returnArray[i] = 0.0; |
| 720 |
} |
| 721 |
} |
| 722 |
return returnArray; |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
private static final PrintStream progressStream = System.err; |
| 727 |
private PrintStream resultsStream; |
| 728 |
private static final String commandName = "discreteRatePriorGenerator"; |
| 729 |
|
| 730 |
public static void printUsage(Arguments arguments) { |
| 731 |
|
| 732 |
arguments.printUsage(commandName, "[<output-file-name>]"); |
| 733 |
progressStream.println(); |
| 734 |
progressStream.println(" Example: " + commandName + " coordinates.txt ratePriors.txt"); |
| 735 |
progressStream.println(); |
| 736 |
} |
| 737 |
|
| 738 |
private static ArrayList parseCoordinatesFile(String inputFile, String[] locations, Double[] latitudes, Double[] longitudes) { |
| 739 |
ArrayList<Object[]> returnList = new ArrayList<Object[]>(); |
| 740 |
List<String> countList = new ArrayList<String>(); |
| 741 |
try{ |
| 742 |
BufferedReader reader = new BufferedReader(new FileReader(inputFile)); |
| 743 |
String line = reader.readLine(); |
| 744 |
while (line != null && !line.equals("")) { |
| 745 |
countList.add(line); |
| 746 |
line = reader.readLine(); |
| 747 |
} |
| 748 |
} catch (IOException e) { |
| 749 |
System.err.println("Error reading " + inputFile); |
| 750 |
System.exit(1); |
| 751 |
} |
| 752 |
|
| 753 |
if (countList.size()>0) { |
| 754 |
locations = new String[countList.size()]; |
| 755 |
latitudes = new Double[countList.size()]; |
| 756 |
longitudes = new Double[countList.size()]; |
| 757 |
for(int i=0; i<countList.size(); i++) { |
| 758 |
StringTokenizer tokens = new StringTokenizer(countList.get(i)); |
| 759 |
locations[i] = tokens.nextToken("\t"); |
| 760 |
latitudes[i] = Double.parseDouble(tokens.nextToken("\t")); |
| 761 |
longitudes[i] = Double.parseDouble(tokens.nextToken("\t")); |
| 762 |
|
| 763 |
} |
| 764 |
} |
| 765 |
returnList.add(locations); |
| 766 |
returnList.add(latitudes); |
| 767 |
returnList.add(longitudes); |
| 768 |
return returnList; |
| 769 |
|
| 770 |
} |
| 771 |
|
| 772 |
private static ArrayList parseSingleMeasureFile(String inputFile, String[] locations, Double[] densities) { |
| 773 |
|
| 774 |
ArrayList<Object[]> returnList = new ArrayList<Object[]>(); |
| 775 |
|
| 776 |
boolean locationsSpecified = true; |
| 777 |
if (locations == null) { |
| 778 |
locationsSpecified = false; |
| 779 |
} |
| 780 |
|
| 781 |
List<String> countList = new ArrayList<String>(); |
| 782 |
try{ |
| 783 |
BufferedReader reader = new BufferedReader(new FileReader(inputFile)); |
| 784 |
String line = reader.readLine(); |
| 785 |
while (line != null && !line.equals("")) { |
| 786 |
countList.add(line); |
| 787 |
line = reader.readLine(); |
| 788 |
} |
| 789 |
} catch (IOException e) { |
| 790 |
System.err.println("Error reading " + inputFile); |
| 791 |
System.exit(1); |
| 792 |
} |
| 793 |
|
| 794 |
if (countList.size()>0) { |
| 795 |
if (!locationsSpecified) { |
| 796 |
locations = new String[countList.size()]; |
| 797 |
} |
| 798 |
densities = new Double[countList.size()]; |
| 799 |
for(int i=0; i<countList.size(); i++) { |
| 800 |
StringTokenizer tokens = new StringTokenizer(countList.get(i)); |
| 801 |
String location = tokens.nextToken("\t"); |
| 802 |
if (!locationsSpecified) { |
| 803 |
locations[i] = location; |
| 804 |
} else { |
| 805 |
if (!(locations[i].equals(location))) { |
| 806 |
System.err.println("Error in location specification in different files: " + locations[i] + "is not = " + location); |
| 807 |
} |
| 808 |
} |
| 809 |
densities[i] = Double.parseDouble(tokens.nextToken("\t")); |
| 810 |
} |
| 811 |
} |
| 812 |
returnList.add(locations); |
| 813 |
returnList.add(densities); |
| 814 |
return returnList; |
| 815 |
} |
| 816 |
|
| 817 |
public static void main(String[] args) throws IOException { |
| 818 |
|
| 819 |
String outputFileName = null; |
| 820 |
String[] locations = null; |
| 821 |
Double[] latitudes = null; |
| 822 |
Double[] longitudes = null; |
| 823 |
Double[] densities = null; |
| 824 |
OutputFormat outputFormat = OutputFormat.XML; |
| 825 |
Model model = Model.PRIOR; |
| 826 |
|
| 827 |
Arguments arguments = new Arguments( |
| 828 |
new Arguments.Option[]{ |
| 829 |
new Arguments.StringOption(COORDINATES, "coordinate file", "specifies a tab-delimited file with coordinates for the locations"), |
| 830 |
new Arguments.StringOption(DENSITIES, "density file", "specifies a tab-delimited file with densities for the locations"), |
| 831 |
new Arguments.StringOption(GENERICS, "generics file", "specifies a tab-delimited file-list to use as measures for the locations"), |
| 832 |
new Arguments.StringOption(FORMAT, TimeSlicer.enumNamesToStringArray(OutputFormat.values()),false, |
| 833 |
"prior output format [default = XML]"), |
| 834 |
new Arguments.StringOption(MODEL, TimeSlicer.enumNamesToStringArray(Model.values()),false, |
| 835 |
"model output [default = rate priors]"), |
| 836 |
|
| 837 |
new Arguments.Option(HELP, "option to print this message"), |
| 838 |
}); |
| 839 |
|
| 840 |
try { |
| 841 |
arguments.parseArguments(args); |
| 842 |
} catch (Arguments.ArgumentException ae) { |
| 843 |
progressStream.println(ae); |
| 844 |
printUsage(arguments); |
| 845 |
System.exit(1); |
| 846 |
} |
| 847 |
|
| 848 |
if (arguments.hasOption(HELP)) { |
| 849 |
printUsage(arguments); |
| 850 |
System.exit(0); |
| 851 |
} |
| 852 |
|
| 853 |
String coordinatesFileString = arguments.getStringOption(COORDINATES); |
| 854 |
if (coordinatesFileString != null) { |
| 855 |
ArrayList LocsLatsLongs = parseCoordinatesFile(coordinatesFileString,locations,latitudes,longitudes); |
| 856 |
locations = (String[]) LocsLatsLongs.get(0); |
| 857 |
latitudes = (Double[]) LocsLatsLongs.get(1); |
| 858 |
longitudes = (Double[]) LocsLatsLongs.get(2); |
| 859 |
} |
| 860 |
|
| 861 |
String densitiesFileString = arguments.getStringOption(DENSITIES); |
| 862 |
if (densitiesFileString != null) { |
| 863 |
ArrayList LocsDens = parseSingleMeasureFile(densitiesFileString,locations,densities); |
| 864 |
locations = (String[]) LocsDens.get(0); |
| 865 |
densities = (Double[]) LocsDens.get(1); |
| 866 |
} |
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
String summaryFormat = arguments.getStringOption(FORMAT); |
| 871 |
if (summaryFormat != null) { |
| 872 |
outputFormat = OutputFormat.valueOf(summaryFormat.toUpperCase()); |
| 873 |
} |
| 874 |
|
| 875 |
String modelComponent = arguments.getStringOption(MODEL); |
| 876 |
if (modelComponent != null) { |
| 877 |
model = Model.valueOf(modelComponent.toUpperCase()); |
| 878 |
} |
| 879 |
|
| 880 |
final String[] args2 = arguments.getLeftoverArguments(); |
| 881 |
|
| 882 |
switch (args2.length) { |
| 883 |
case 0: |
| 884 |
printUsage(arguments); |
| 885 |
System.exit(1); |
| 886 |
case 1: |
| 887 |
outputFileName = args2[0]; |
| 888 |
break; |
| 889 |
default: { |
| 890 |
System.err.println("Unknown option: " + args2[1]); |
| 891 |
System.err.println(); |
| 892 |
printUsage(arguments); |
| 893 |
System.exit(1); |
| 894 |
} |
| 895 |
} |
| 896 |
DiscreteRatePriorGenerator rates = new DiscreteRatePriorGenerator(locations, latitudes, longitudes, densities); |
| 897 |
rates.output(outputFileName, outputFormat, model); |
| 898 |
|
| 899 |
System.exit(0); |
| 900 |
|
| 901 |
} |
| 902 |
private static void printArray(Double[] array, String name) { |
| 903 |
try { |
| 904 |
PrintWriter outFile = new PrintWriter(new FileWriter(name), true); |
| 905 |
|
| 906 |
for(Double anArray : array) { |
| 907 |
outFile.print(anArray + "\t"); |
| 908 |
} |
| 909 |
outFile.close(); |
| 910 |
} catch(IOException io) { |
| 911 |
System.err.print("Error writing to file: " + name); |
| 912 |
} |
| 913 |
} |
| 914 |
private static void printArray(String[] array, String name) { |
| 915 |
try { |
| 916 |
PrintWriter outFile = new PrintWriter(new FileWriter(name), true); |
| 917 |
|
| 918 |
for(String anArray : array) { |
| 919 |
outFile.print(anArray + "\t"); |
| 920 |
} |
| 921 |
outFile.close(); |
| 922 |
} catch(IOException io) { |
| 923 |
System.err.print("Error writing to file: " + name); |
| 924 |
} |
| 925 |
} |
| 926 |
|
| 927 |
} |