Debian Codegraph @

src/dr/geo/GreatCircleDistances.java

raw symbols SCIP
1 /*
2 * GreatCircleDistances.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * @author Alexei Drummond
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 //System.out.println(coord);
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 }