use of dr.inference.model.Parameter in project beast-mcmc by beast-dev.
the class SitePatternsParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Alignment alignment = (Alignment) xo.getChild(Alignment.class);
TaxonList taxa = null;
int from = 0;
int to = -1;
int every = xo.getAttribute(EVERY, 1);
boolean strip = xo.getAttribute(STRIP, true);
boolean unique = xo.getAttribute(UNIQUE, true);
if (xo.hasAttribute(FROM)) {
from = xo.getIntegerAttribute(FROM) - 1;
if (from < 0)
throw new XMLParseException("illegal 'from' attribute in patterns element");
}
if (xo.hasAttribute(TO)) {
to = xo.getIntegerAttribute(TO) - 1;
if (to < 0 || to < from)
throw new XMLParseException("illegal 'to' attribute in patterns element");
}
if (every <= 0)
throw new XMLParseException("illegal 'every' attribute in patterns element");
if (xo.hasChildNamed(TAXON_LIST)) {
taxa = (TaxonList) xo.getElementFirstChild(TAXON_LIST);
}
int[] constantPatternCounts = null;
if (xo.hasChildNamed(CONSTANT_PATTERNS)) {
Parameter param = (Parameter) xo.getElementFirstChild(CONSTANT_PATTERNS);
if (param.getDimension() != alignment.getStateCount()) {
throw new XMLParseException("The " + CONSTANT_PATTERNS + " parameter length should be equal to the number of states");
}
constantPatternCounts = new int[param.getDimension()];
int i = 0;
for (double value : param.getParameterValues()) {
constantPatternCounts[i] = (int) value;
i++;
}
}
if (from > alignment.getSiteCount())
throw new XMLParseException("illegal 'from' attribute in patterns element");
if (to > alignment.getSiteCount())
throw new XMLParseException("illegal 'to' attribute in patterns element");
SitePatterns patterns = new SitePatterns(alignment, taxa, from, to, every, strip, unique, constantPatternCounts);
int f = from + 1;
// fixed a *display* error by adding + 1 for consistency with f = from + 1
int t = to + 1;
if (to == -1)
t = alignment.getSiteCount();
if (xo.hasAttribute(XMLParser.ID)) {
final Logger logger = Logger.getLogger("dr.evoxml");
logger.info("Site patterns '" + xo.getId() + "' created from positions " + Integer.toString(f) + "-" + Integer.toString(t) + " of alignment '" + alignment.getId() + "'");
if (every > 1) {
logger.info(" only using every " + every + " site");
}
logger.info(" " + (unique ? "unique " : "") + "pattern count = " + patterns.getPatternCount());
}
return patterns;
}
use of dr.inference.model.Parameter in project beast-mcmc by beast-dev.
the class GeoSpatialDistribution method main.
public static void main(String[] args) {
//test reading in KML file with multiple polygons and fillValue attribute
List<AbstractPolygon2D> polygons = Polygon2D.readKMLFile("Multiple_polygons/9.285_105.7244444.kml");
System.out.println(polygons.size() + " polygons found.");
//check if all the polygons have either a fillValue or do not have this fillValue
AbstractPolygon2D first = polygons.get(0);
boolean equalFills = first.hasFillValue();
if (polygons.size() > 1) {
for (int i = 1; i < polygons.size(); i++) {
if (!(equalFills == polygons.get(i).hasFillValue())) {
System.out.println("Inconsistent fillValue attributes provided.");
}
}
}
if (equalFills) {
double sum = 0.0;
for (AbstractPolygon2D region : polygons) {
sum += region.getFillValue();
}
if (Math.abs(sum - 1.0) > 1E-12) {
System.out.println("\nFill values do not sum to 1 : " + sum);
}
}
boolean overAllFill = equalFills;
List<GeoSpatialDistribution> geoSpatialDistributions = new ArrayList<GeoSpatialDistribution>();
for (AbstractPolygon2D region : polygons) {
geoSpatialDistributions.add(new GeoSpatialDistribution("test", region, false));
}
MultivariateDistributionLikelihood likelihood = new MultivariateDistributionLikelihood(new MultiRegionGeoSpatialDistribution("multi", geoSpatialDistributions, false, overAllFill));
Parameter.Default parameter = new Parameter.Default("coordinate", 2);
likelihood.addData(parameter);
double logL = likelihood.calculateLogLikelihood();
System.out.println("\n(" + parameter.getParameterValue(0) + "," + parameter.getParameterValue(1) + ")");
System.out.println("logL = " + logL);
System.out.println("L = " + Math.exp(logL));
parameter.setParameterValue(0, 11.40);
parameter.setParameterValue(1, 106.90);
likelihood.makeDirty();
logL = likelihood.calculateLogLikelihood();
System.out.println("\n(" + parameter.getParameterValue(0) + "," + parameter.getParameterValue(1) + ")");
System.out.println("logL = " + logL);
System.out.println("L = " + Math.exp(logL));
//test reading in KML file with a single polygon without fillValue attribute
polygons = Polygon2D.readKMLFile("Districts_polygons/21.035_106.063.kml");
System.out.println("\n" + polygons.size() + " polygons found.");
first = polygons.get(0);
equalFills = first.hasFillValue();
if (polygons.size() > 1) {
for (int i = 1; i < polygons.size(); i++) {
if (!(equalFills == polygons.get(i).hasFillValue())) {
System.out.println("Inconsistent fillValue attributes provided.");
}
}
}
if (equalFills) {
double sum = 0.0;
for (AbstractPolygon2D region : polygons) {
sum += region.getFillValue();
}
if (Math.abs(sum - 1.0) > 1E-12) {
System.out.println("\nFill values do not sum to 1 : " + sum);
}
}
overAllFill = equalFills;
geoSpatialDistributions = new ArrayList<GeoSpatialDistribution>();
for (AbstractPolygon2D region : polygons) {
geoSpatialDistributions.add(new GeoSpatialDistribution("test", region, false));
}
likelihood = new MultivariateDistributionLikelihood(new MultiRegionGeoSpatialDistribution("multi", geoSpatialDistributions, false, overAllFill));
likelihood.addData(parameter);
logL = likelihood.calculateLogLikelihood();
System.out.println("\n(" + parameter.getParameterValue(0) + "," + parameter.getParameterValue(1) + ")");
System.out.println("logL = " + logL);
System.out.println("L = " + Math.exp(logL));
parameter.setParameterValue(0, 21.0);
parameter.setParameterValue(1, 106.03);
likelihood.makeDirty();
logL = likelihood.calculateLogLikelihood();
System.out.println("\n(" + parameter.getParameterValue(0) + "," + parameter.getParameterValue(1) + ")");
System.out.println("logL = " + logL);
System.out.println("L = " + Math.exp(logL));
parameter.setParameterValue(0, 21.0);
parameter.setParameterValue(1, 106.10);
likelihood.makeDirty();
logL = likelihood.calculateLogLikelihood();
System.out.println("\n(" + parameter.getParameterValue(0) + "," + parameter.getParameterValue(1) + ")");
System.out.println("logL = " + logL);
System.out.println("L = " + Math.exp(logL));
}
use of dr.inference.model.Parameter in project beast-mcmc by beast-dev.
the class EmpiricalCodonModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Codons codons = Codons.UNIVERSAL;
if (xo.hasAttribute(GeneticCode.GENETIC_CODE)) {
String codeStr = xo.getStringAttribute(GeneticCode.GENETIC_CODE);
if (codeStr.equals(GeneticCode.UNIVERSAL.getName())) {
codons = Codons.UNIVERSAL;
} else if (codeStr.equals(GeneticCode.VERTEBRATE_MT.getName())) {
codons = Codons.VERTEBRATE_MT;
} else if (codeStr.equals(GeneticCode.YEAST.getName())) {
codons = Codons.YEAST;
} else if (codeStr.equals(GeneticCode.MOLD_PROTOZOAN_MT.getName())) {
codons = Codons.MOLD_PROTOZOAN_MT;
} else if (codeStr.equals(GeneticCode.INVERTEBRATE_MT.getName())) {
codons = Codons.INVERTEBRATE_MT;
} else if (codeStr.equals(GeneticCode.CILIATE.getName())) {
codons = Codons.CILIATE;
} else if (codeStr.equals(GeneticCode.ECHINODERM_MT.getName())) {
codons = Codons.ECHINODERM_MT;
} else if (codeStr.equals(GeneticCode.EUPLOTID_NUC.getName())) {
codons = Codons.EUPLOTID_NUC;
} else if (codeStr.equals(GeneticCode.BACTERIAL.getName())) {
codons = Codons.BACTERIAL;
} else if (codeStr.equals(GeneticCode.ALT_YEAST.getName())) {
codons = Codons.ALT_YEAST;
} else if (codeStr.equals(GeneticCode.ASCIDIAN_MT.getName())) {
codons = Codons.ASCIDIAN_MT;
} else if (codeStr.equals(GeneticCode.FLATWORM_MT.getName())) {
codons = Codons.FLATWORM_MT;
} else if (codeStr.equals(GeneticCode.BLEPHARISMA_NUC.getName())) {
codons = Codons.BLEPHARISMA_NUC;
} else if (codeStr.equals(GeneticCode.NO_STOPS.getName())) {
codons = Codons.NO_STOPS;
}
}
Parameter omegaParam = (Parameter) xo.getElementFirstChild(OMEGA);
Parameter kappaParam = null;
Parameter mntParam = null;
if (xo.hasChildNamed(KAPPATSTV)) {
kappaParam = (Parameter) xo.getElementFirstChild(KAPPATSTV);
if (kappaParam.getDimension() != 2 && kappaParam.getDimension() != 9) {
throw new XMLParseException("If you use the kappa parameter, you need to enter exactly\n" + "two values for ts and tv or nine values\n" + "according to the Kosiol ECM+F+omega+9k model");
}
} else {
mntParam = (Parameter) xo.getElementFirstChild(MULTI_NT_CHANGE);
}
String dirString = xo.getStringAttribute(ECM_DATA_DIR);
String freqString = xo.getStringAttribute(ECM_FREQ_MATRIX);
String matString = xo.getStringAttribute(ECM_DATA_MATRIX);
EmpiricalCodonRateMatrix rateMat = new EmpiricalCodonRateMatrix(EMPIRICAL_RATE_MATRIX, codons, dirString, freqString, matString);
// get frequencies from XML, from frequency csv file or estimate from data
FrequencyModel freqModel = null;
if (xo.getChild(FrequencyModel.class) != null) {
freqModel = (FrequencyModel) xo.getChild(FrequencyModel.class);
} else {
freqModel = createNewFreqModel(codons, rateMat);
}
return new EmpiricalCodonModel(codons, omegaParam, kappaParam, mntParam, rateMat, freqModel);
}
use of dr.inference.model.Parameter in project beast-mcmc by beast-dev.
the class FrequencyModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
DataType dataType = DataTypeUtils.getDataType(xo);
Parameter freqsParam = (Parameter) xo.getElementFirstChild(FREQUENCIES);
double[] frequencies = null;
for (int i = 0; i < xo.getChildCount(); i++) {
Object obj = xo.getChild(i);
if (obj instanceof PatternList) {
frequencies = ((PatternList) obj).getStateFrequencies();
break;
}
}
StringBuilder sb = new StringBuilder("Creating state frequencies model '" + freqsParam.getParameterName() + "': ");
if (frequencies != null) {
if (freqsParam.getDimension() != frequencies.length) {
throw new XMLParseException("dimension of frequency parameter and number of sequence states don't match!");
}
for (int j = 0; j < frequencies.length; j++) {
freqsParam.setParameterValue(j, frequencies[j]);
}
sb.append("Using empirical frequencies from data ");
} else {
sb.append("Initial frequencies ");
}
sb.append("= {");
double sum = 0;
for (int j = 0; j < freqsParam.getDimension(); j++) {
sum += freqsParam.getParameterValue(j);
}
if (xo.getAttribute(NORMALIZE, false)) {
for (int j = 0; j < freqsParam.getDimension(); j++) {
if (sum != 0)
freqsParam.setParameterValue(j, freqsParam.getParameterValue(j) / sum);
else
freqsParam.setParameterValue(j, 1.0 / freqsParam.getDimension());
}
sum = 1.0;
}
if (Math.abs(sum - 1.0) > 1e-8) {
throw new XMLParseException("Frequencies do not sum to 1 (they sum to " + sum + ")");
}
NumberFormat format = NumberFormat.getNumberInstance();
format.setMaximumFractionDigits(5);
sb.append(format.format(freqsParam.getParameterValue(0)));
for (int j = 1; j < freqsParam.getDimension(); j++) {
sb.append(", ");
sb.append(format.format(freqsParam.getParameterValue(j)));
}
sb.append("}");
Logger.getLogger("dr.evomodel").info(sb.toString());
return new FrequencyModel(dataType, freqsParam);
}
use of dr.inference.model.Parameter in project beast-mcmc by beast-dev.
the class SampleStateAndCategoryModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
XMLObject cxo = xo.getChild(MUTATION_RATE);
Parameter muParam = (Parameter) cxo.getChild(Parameter.class);
cxo = xo.getChild(CATEGORY_PARAMETER);
Parameter catParam = (Parameter) cxo.getChild(Parameter.class);
Vector subModels = new Vector();
for (int i = 0; i < xo.getChildCount(); i++) {
if (xo.getChild(i) instanceof SubstitutionModel) {
subModels.addElement(xo.getChild(i));
}
}
return new SampleStateAndCategoryModel(muParam, catParam, subModels);
}
Aggregations