| 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.geo; |
| 27 |
|
| 28 |
import dr.evolution.util.Taxa; |
| 29 |
import dr.evolution.util.Taxon; |
| 30 |
import dr.geo.math.SphericalPolarCoordinates; |
| 31 |
import dr.inference.model.Statistic; |
| 32 |
import dr.evoxml.TaxaParser; |
| 33 |
import dr.evoxml.TaxonParser; |
| 34 |
import dr.evoxml.DateParser; |
| 35 |
import dr.xml.XMLParseException; |
| 36 |
import dr.xml.XMLParser; |
| 37 |
import dr.xml.AttributeParser; |
| 38 |
import org.xml.sax.SAXException; |
| 39 |
|
| 40 |
import javax.xml.parsers.ParserConfigurationException; |
| 41 |
import java.io.IOException; |
| 42 |
import java.io.File; |
| 43 |
import java.io.FileReader; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class GreatCircleDistances { |
| 49 |
|
| 50 |
double[][] distances; |
| 51 |
|
| 52 |
public GreatCircleDistances(Taxa taxa, String attributeName) { |
| 53 |
|
| 54 |
distances = new double[taxa.getTaxonCount()][taxa.getTaxonCount()]; |
| 55 |
|
| 56 |
for (int i = 0; i < taxa.getTaxonCount(); i++) { |
| 57 |
|
| 58 |
Taxon taxon = taxa.getTaxon(i); |
| 59 |
|
| 60 |
String attr = (String)taxon.getAttribute(attributeName); |
| 61 |
String[] loc = attr.split(" "); |
| 62 |
double latitude = Double.parseDouble(loc[0]); |
| 63 |
double longitude = Double.parseDouble(loc[1]); |
| 64 |
|
| 65 |
|
| 66 |
SphericalPolarCoordinates coord = new SphericalPolarCoordinates(latitude, longitude); |
| 67 |
|
| 68 |
|
| 69 |
for (int j = i+1; j < taxa.getTaxonCount(); j++) { |
| 70 |
Taxon taxon2 = taxa.getTaxon(j); |
| 71 |
attr = (String)taxon2.getAttribute(attributeName); |
| 72 |
String[] loc2 = attr.split(" "); |
| 73 |
latitude = Double.parseDouble(loc2[0]); |
| 74 |
longitude = Double.parseDouble(loc2[1]); |
| 75 |
|
| 76 |
SphericalPolarCoordinates coord2 = new SphericalPolarCoordinates(latitude, longitude); |
| 77 |
|
| 78 |
distances[i][j] = distances[j][i] = coord.distance(coord2); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
class DistancesStatistic extends Statistic.Abstract { |
| 84 |
|
| 85 |
double[] dists = new double[distances.length*distances.length]; |
| 86 |
|
| 87 |
public DistancesStatistic(boolean logTransformed) { |
| 88 |
|
| 89 |
int k = 0; |
| 90 |
for (double[] distance : distances) { |
| 91 |
for (int j = 0; j < distances.length; j++) { |
| 92 |
dists[k] = distance[j]; |
| 93 |
if (logTransformed) dists[k] = Math.log(dists[k]); |
| 94 |
k += 1; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
public int getDimension() { |
| 100 |
return dists.length; |
| 101 |
} |
| 102 |
|
| 103 |
public double getStatisticValue(int dim) { |
| 104 |
return dists[dim]; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
public Statistic getDistanceStatistic(boolean logTransformed) { |
| 109 |
return new DistancesStatistic(logTransformed); |
| 110 |
} |
| 111 |
|
| 112 |
public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, XMLParseException { |
| 113 |
|
| 114 |
XMLParser parser = new XMLParser(false, true, true, null); |
| 115 |
parser.addXMLObjectParser(new TaxonParser()); |
| 116 |
parser.addXMLObjectParser(new TaxaParser()); |
| 117 |
parser.addXMLObjectParser(new AttributeParser()); |
| 118 |
parser.addXMLObjectParser(new DateParser()); |
| 119 |
|
| 120 |
parser.parse(new FileReader(new File(args[0])), true); |
| 121 |
|
| 122 |
Taxa taxa = (Taxa)parser.getRoot().getChild(0); |
| 123 |
|
| 124 |
System.out.println("Found " + taxa.getTaxonCount() + " taxa"); |
| 125 |
|
| 126 |
GreatCircleDistances distances = new GreatCircleDistances(taxa, "location"); |
| 127 |
|
| 128 |
Statistic statistic = distances.getDistanceStatistic(true); |
| 129 |
|
| 130 |
for (int i = 0; i < statistic.getDimension(); i++) { |
| 131 |
System.out.println(statistic.getStatisticValue(i)); |
| 132 |
} |
| 133 |
|
| 134 |
} |
| 135 |
} |