use of dr.evolution.util.Date in project beast-mcmc by beast-dev.
the class TraceCorrelationAssert method createAlignment.
protected void createAlignment(Object[][] taxa_sequence, DataType dataType) {
alignment = new SimpleAlignment();
alignment.setDataType(dataType);
// alignment.setDataType(Nucleotides.INSTANCE);
// 6, 17
taxa = new Taxon[taxa_sequence[0].length];
System.out.println("Taxon len = " + taxa_sequence[0].length);
System.out.println("Alignment len = " + taxa_sequence[1].length);
if (taxa_sequence.length > 2)
System.out.println("Date len = " + taxa_sequence[2].length);
for (int i = 0; i < taxa_sequence[0].length; i++) {
taxa[i] = new Taxon(taxa_sequence[0][i].toString());
if (taxa_sequence.length > 2) {
Date date = new Date((Double) taxa_sequence[2][i], Units.Type.YEARS, (Boolean) taxa_sequence[3][0]);
taxa[i].setDate(date);
}
// taxonList.addTaxon(taxon);
Sequence sequence = new Sequence(taxa_sequence[1][i].toString());
sequence.setTaxon(taxa[i]);
sequence.setDataType(dataType);
alignment.addSequence(sequence);
}
}
use of dr.evolution.util.Date in project beast-mcmc by beast-dev.
the class DateGuesser method guessDates.
public void guessDates(List<Taxon> taxonList, Map<Taxon, String> taxonDateMap) {
dateFormat = new SimpleDateFormat(calendarDateFormat);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
for (int i = 0; i < taxonList.size(); i++) {
Taxon taxon = taxonList.get(i);
// Allocates a Date object and initializes it to represent the specified number of milliseconds since the
// standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT
java.util.Date origin = new java.util.Date(0);
double[] values = new double[2];
try {
if (taxonDateMap != null) {
String dateString = taxonDateMap.get(taxon);
if (dateString == null) {
continue;
}
parseDate(taxon.getId(), dateString, values);
} else {
switch(guessType) {
case ORDER:
guessDateFromOrder(taxonList.get(i).getId(), order, fromLast, values);
break;
case PREFIX:
guessDateFromPrefix(taxonList.get(i).getId(), prefix, order, fromLast, values);
break;
case REGEX:
guessDateFromRegex(taxonList.get(i).getId(), regex, values);
break;
default:
throw new IllegalArgumentException("unknown GuessType");
}
}
} catch (GuessDatesException gfe) {
// @todo catch errors and give to user
}
double d = values[0];
if (!parseCalendarDates && !parseCalendarDatesAndPrecision) {
if (offset > 0) {
if (unlessLessThan > 0) {
if (d < unlessLessThan) {
d += offset2;
} else {
d += offset;
}
} else {
d += offset;
}
}
}
// @todo if any taxa aren't set then return warning
Date date = Date.createTimeSinceOrigin(d, Units.Type.YEARS, origin);
date.setUncertainty(values[1]);
taxon.setAttribute("date", date);
}
}
use of dr.evolution.util.Date in project beast-mcmc by beast-dev.
the class DateGuesser method parseDate.
private void parseDate(String label, String value, double[] values) throws GuessDatesException {
double d;
double p = 0.0;
if (dateFormat1 == null) {
// set the timezones to GMT so they match the origin date...
dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
dateFormat1.setTimeZone(TimeZone.getTimeZone("GMT"));
dateFormat2 = new SimpleDateFormat("yyyy-MM");
dateFormat2.setTimeZone(TimeZone.getTimeZone("GMT"));
dateFormat3 = new SimpleDateFormat("yyyy");
dateFormat3.setTimeZone(TimeZone.getTimeZone("GMT"));
}
if (parseCalendarDatesAndPrecision) {
try {
Date date = new Date(dateFormat1.parse(value));
d = date.getTimeValue();
p = 0.0;
} catch (ParseException pe) {
try {
Date date = new Date(dateFormat2.parse(value));
d = date.getTimeValue();
p = 1.0 / 12.0;
} catch (ParseException pe2) {
try {
Date date = new Date(dateFormat3.parse(value));
d = date.getTimeValue();
p = 1.0;
} catch (ParseException pe3) {
throw new GuessDatesException("Badly formatted date for taxon, " + label);
}
}
}
} else if (parseCalendarDates) {
try {
Date date = new Date(dateFormat.parse(value));
d = date.getTimeValue();
} catch (ParseException pe) {
throw new GuessDatesException("Badly formatted date for taxon, " + label);
}
} else {
try {
d = Double.parseDouble(value);
} catch (NumberFormatException nfe) {
throw new GuessDatesException("Badly formatted date for taxon, " + label);
}
}
values[0] = d;
values[1] = p;
}
use of dr.evolution.util.Date in project beast-mcmc by beast-dev.
the class TipDateSamplingComponentOptions method getTaxonSet.
public TaxonList getTaxonSet() {
TaxonList taxa = options.taxonList;
if (tipDateSamplingTaxonSet != null) {
taxa = tipDateSamplingTaxonSet;
}
if (tipDateSamplingType == TipDateSamplingType.SAMPLE_PRECISION) {
Taxa precisionTaxonList = new Taxa();
for (int i = 0; i < taxa.getTaxonCount(); i++) {
Taxon taxon = taxa.getTaxon(i);
Date date = taxon.getDate();
if (date.getUncertainty() > 0.0) {
precisionTaxonList.addTaxon(taxon);
}
}
taxa = precisionTaxonList;
}
return taxa;
}
use of dr.evolution.util.Date in project beast-mcmc by beast-dev.
the class TreeUtils method setHeightsFromDates.
/**
* Sets the tip heights from the tip dates
*/
public static void setHeightsFromDates(MutableTree tree) {
dr.evolution.util.Date mostRecent = null;
for (int i = 0; i < tree.getExternalNodeCount(); i++) {
Taxon taxon = tree.getNodeTaxon(tree.getExternalNode(i));
dr.evolution.util.Date date = (dr.evolution.util.Date) taxon.getAttribute("date");
if (date != null) {
if ((mostRecent == null) || date.after(mostRecent)) {
mostRecent = date;
}
}
}
TimeScale timeScale = new TimeScale(mostRecent.getUnits(), true, mostRecent.getAbsoluteTimeValue());
for (int i = 0; i < tree.getExternalNodeCount(); i++) {
NodeRef node = tree.getExternalNode(i);
Taxon taxon = tree.getNodeTaxon(node);
dr.evolution.util.Date date = (dr.evolution.util.Date) taxon.getAttribute("date");
if (date != null) {
double height = timeScale.convertTime(date.getTimeValue(), date);
tree.setNodeHeight(node, height);
} else {
tree.setNodeHeight(node, 0.0);
}
}
adjustInternalHeights(tree, tree.getRoot());
if (mostRecent != null) {
tree.setUnits(mostRecent.getUnits());
}
}
Aggregations