| 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.inference.model; |
| 27 |
|
| 28 |
import dr.evolution.datatype.DataType; |
| 29 |
import dr.geo.GreatCircleDistances; |
| 30 |
import dr.geo.math.SphericalPolarCoordinates; |
| 31 |
import dr.stats.DiscreteStatistics; |
| 32 |
import dr.xml.*; |
| 33 |
import org.w3c.dom.Document; |
| 34 |
import org.w3c.dom.Element; |
| 35 |
|
| 36 |
import java.util.ArrayList; |
| 37 |
import java.util.List; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class OriginDestinationDesignMatrix extends DesignMatrix { |
| 43 |
public static final String DESIGN_MATRIX = "originDestinationDesignMatrix"; |
| 44 |
public static final String LAT_TRAIT = "latitudeTrait"; |
| 45 |
public static final String LONG_TRAIT = "longitudeTrait"; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public static final String DYNAMIC_STANDARDIZATION = "dynamicStandardization"; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
final private DesignMatrix baseMatrix; |
| 57 |
final DataType dataType; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
final int[] originIndex; |
| 62 |
final int[] destinationIndex; |
| 63 |
|
| 64 |
abstract class Covariate { |
| 65 |
final protected Parameter parameter; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
protected Covariate(Parameter parameter) { |
| 73 |
this.parameter = parameter; |
| 74 |
} |
| 75 |
|
| 76 |
abstract double getValue(int row); |
| 77 |
} |
| 78 |
|
| 79 |
class Origin extends Covariate { |
| 80 |
|
| 81 |
protected Origin(Parameter parameter) { |
| 82 |
super(parameter); |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
double getValue(int row) { |
| 87 |
return parameter.getParameterValue(originIndex[row]); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
class Destination extends Covariate { |
| 92 |
|
| 93 |
protected Destination(Parameter parameter) { |
| 94 |
super(parameter); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
double getValue(int row) { |
| 99 |
return parameter.getParameterValue(destinationIndex[row]); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
class Distance extends Covariate { |
| 104 |
|
| 105 |
final private Parameter latParameter; |
| 106 |
final private Parameter longParameter; |
| 107 |
|
| 108 |
protected Distance(Parameter latParameter, Parameter longParameter) { |
| 109 |
super(latParameter); |
| 110 |
this.latParameter = latParameter; |
| 111 |
this.longParameter = longParameter; |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
double getValue(int row) { |
| 116 |
|
| 117 |
double originLat = latParameter.getParameterValue(originIndex[row]); |
| 118 |
double originLong = longParameter.getParameterValue(originIndex[row]); |
| 119 |
|
| 120 |
double destinationLat = latParameter.getParameterValue(destinationIndex[row]); |
| 121 |
double destinationLong = longParameter.getParameterValue(destinationIndex[row]); |
| 122 |
|
| 123 |
SphericalPolarCoordinates origin = new SphericalPolarCoordinates(originLat, originLong); |
| 124 |
SphericalPolarCoordinates destination = new SphericalPolarCoordinates(destinationLat, destinationLong); |
| 125 |
return origin.distance(destination); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
final private List<Covariate> covariates = new ArrayList<Covariate>(); |
| 130 |
|
| 131 |
final int rowDimension; |
| 132 |
|
| 133 |
public OriginDestinationDesignMatrix(String name, |
| 134 |
DesignMatrix baseMatrix, |
| 135 |
DataType dataType, |
| 136 |
boolean dynamicStandardization, |
| 137 |
Parameter latParameter, |
| 138 |
Parameter longParameter) { |
| 139 |
super(name, dynamicStandardization); |
| 140 |
|
| 141 |
this.dataType = dataType; |
| 142 |
this.baseMatrix = baseMatrix; |
| 143 |
|
| 144 |
System.err.println("Iterating over " + baseMatrix.getParameters().size() + " parameters"); |
| 145 |
|
| 146 |
System.err.println("unique count: " + baseMatrix.getUniqueParameterCount()); |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
for (int i = 0; i < baseMatrix.getUniqueParameterCount(); ++i) { |
| 152 |
Parameter param = baseMatrix.getUniqueParameter(i); |
| 153 |
|
| 154 |
super.addParameter(param); |
| 155 |
if (param != latParameter && param != longParameter) { |
| 156 |
covariates.add(new Origin(param)); |
| 157 |
covariates.add(new Destination(param)); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
if (latParameter != null && longParameter != null) { |
| 162 |
covariates.add(new Distance(latParameter, longParameter)); |
| 163 |
} |
| 164 |
|
| 165 |
this.rowDimension = dataType.getStateCount() * (dataType.getStateCount() - 1); |
| 166 |
|
| 167 |
originIndex = new int[rowDimension]; |
| 168 |
destinationIndex = new int[rowDimension]; |
| 169 |
|
| 170 |
int index = 0; |
| 171 |
for (int origin = 0; origin < dataType.getStateCount(); ++origin) { |
| 172 |
for (int destination = origin + 1; destination < dataType.getStateCount(); ++destination) { |
| 173 |
originIndex[index] = origin; |
| 174 |
destinationIndex[index] = destination; |
| 175 |
++index; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
for (int destination = 0; destination < dataType.getStateCount(); ++destination) { |
| 180 |
for (int origin = destination + 1; origin < dataType.getStateCount(); ++origin) { |
| 181 |
originIndex[index] = origin; |
| 182 |
destinationIndex[index] = destination; |
| 183 |
++index; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
System.err.println("#row = " + baseMatrix.getRowDimension()); |
| 188 |
System.err.println("#col = " + baseMatrix.getColumnDimension()); |
| 189 |
|
| 190 |
System.err.println("#row = " + getRowDimension()); |
| 191 |
System.err.println("#col = " + getColumnDimension()); |
| 192 |
System.exit(-1); |
| 193 |
} |
| 194 |
|
| 195 |
public int getColumnDimension() { |
| 196 |
return covariates.size(); |
| 197 |
} |
| 198 |
|
| 199 |
public int getRowDimension() { |
| 200 |
return rowDimension; |
| 201 |
} |
| 202 |
|
| 203 |
public void addParameter(Parameter param) { |
| 204 |
throw new RuntimeException("Should not be called"); |
| 205 |
} |
| 206 |
|
| 207 |
public void removeParameter(Parameter param) { |
| 208 |
throw new RuntimeException("Should not be called"); |
| 209 |
} |
| 210 |
|
| 211 |
protected double getRawParameterValue(int row, int col) { |
| 212 |
|
| 213 |
double value = covariates.get(col).getValue(row); |
| 214 |
return value; |
| 215 |
} |
| 216 |
|
| 217 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 218 |
|
| 219 |
|
| 220 |
private Parameter getMatchingParameter(DesignMatrix designMatrix, String name) { |
| 221 |
for (int i = 0; i < designMatrix.getUniqueParameterCount(); ++i) { |
| 222 |
Parameter param = designMatrix.getUniqueParameter(i); |
| 223 |
if (param.getId().compareTo(name) == 0) { |
| 224 |
return param; |
| 225 |
} |
| 226 |
} |
| 227 |
return null; |
| 228 |
} |
| 229 |
|
| 230 |
public String getParserName() { |
| 231 |
return DESIGN_MATRIX; |
| 232 |
} |
| 233 |
|
| 234 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 235 |
|
| 236 |
boolean dynamicStandardization = xo.getAttribute(DYNAMIC_STANDARDIZATION, false); |
| 237 |
String name = (xo.hasId() ? xo.getId() : DESIGN_MATRIX); |
| 238 |
|
| 239 |
final DataType dataType = (DataType) xo.getChild(DataType.class); |
| 240 |
final DesignMatrix designMatrix = (DesignMatrix) xo.getChild(DesignMatrix.class); |
| 241 |
|
| 242 |
Parameter latParameter = null; |
| 243 |
Parameter longParameter = null; |
| 244 |
|
| 245 |
if (xo.hasAttribute(LAT_TRAIT)) { |
| 246 |
if (xo.hasAttribute(LONG_TRAIT)) { |
| 247 |
String latName = xo.getStringAttribute(LAT_TRAIT); |
| 248 |
String longName = xo.getStringAttribute(LONG_TRAIT); |
| 249 |
|
| 250 |
latParameter = getMatchingParameter(designMatrix, latName); |
| 251 |
longParameter = getMatchingParameter(designMatrix, longName); |
| 252 |
|
| 253 |
if (latParameter == null) { |
| 254 |
throw new XMLParseException("Unable to find trait named `" + latName + "`"); |
| 255 |
} |
| 256 |
|
| 257 |
if (longParameter == null) { |
| 258 |
throw new XMLParseException("Unable to find trait named `" + longName + "`"); |
| 259 |
} |
| 260 |
|
| 261 |
} else { |
| 262 |
throw new XMLParseException("Must provide both latitude and longitude trait names"); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
OriginDestinationDesignMatrix matrix = new OriginDestinationDesignMatrix(name, |
| 267 |
designMatrix, dataType, dynamicStandardization, |
| 268 |
latParameter, longParameter); |
| 269 |
|
| 270 |
return matrix; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public String getParserDescription() { |
| 278 |
return "A matrix parameter constructed from its component parameters."; |
| 279 |
} |
| 280 |
|
| 281 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 282 |
return rules; |
| 283 |
} |
| 284 |
|
| 285 |
private final XMLSyntaxRule[] rules = { |
| 286 |
|
| 287 |
new ElementRule(DesignMatrix.class), |
| 288 |
new ElementRule(DataType.class), |
| 289 |
AttributeRule.newStringRule(LAT_TRAIT, true), |
| 290 |
AttributeRule.newStringRule(LONG_TRAIT, true), |
| 291 |
}; |
| 292 |
|
| 293 |
public Class getReturnType() { |
| 294 |
return OriginDestinationDesignMatrix.class; |
| 295 |
} |
| 296 |
}; |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
} |