use of dr.evolution.util.Date in project beast-mcmc by beast-dev.
the class TreeUtils method getTipDates.
/**
* Gets the tip dates from a tree.
*/
public static void getTipDates(Tree tree, Variate dates) {
for (int i = 0; i < tree.getExternalNodeCount(); i++) {
Taxon taxon = tree.getNodeTaxon(tree.getExternalNode(i));
Object date = taxon.getAttribute("date");
if (date != null) {
if (date instanceof Date) {
dates.add(((Date) date).getTimeValue());
} else {
try {
dates.add(Double.parseDouble(date.toString()));
} catch (NumberFormatException nfe) {
dates.add(0.0);
}
}
} else {
dates.add(0.0);
}
}
}
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) {
Date mostRecent = null;
for (int i = 0; i < tree.getExternalNodeCount(); i++) {
Taxon taxon = tree.getNodeTaxon(tree.getExternalNode(i));
Date date = (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);
Date date = (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());
}
}
use of dr.evolution.util.Date in project beast-mcmc by beast-dev.
the class TemporalRooting method setHeightsFromDates.
private void setHeightsFromDates(FlexibleTree tree) {
Date mostRecent = null;
for (int i = 0; i < taxa.getTaxonCount(); i++) {
Date date = taxa.getTaxon(i).getDate();
if ((date != null) && (mostRecent == null || date.after(mostRecent))) {
mostRecent = date;
}
}
if (mostRecent != null) {
TimeScale timeScale = new TimeScale(mostRecent.getUnits(), true, mostRecent.getAbsoluteTimeValue());
double time0 = timeScale.convertTime(mostRecent.getTimeValue(), mostRecent);
for (int i = 0; i < tree.getExternalNodeCount(); i++) {
NodeRef tip = tree.getExternalNode(i);
Date date = tree.getNodeTaxon(tip).getDate();
if (date != null) {
tree.setNodeHeight(tip, timeScale.convertTime(date.getTimeValue(), date) - time0);
} else {
tree.setNodeHeight(tip, 0.0);
}
}
}
}
use of dr.evolution.util.Date in project beast-mcmc by beast-dev.
the class PLCoalescentSimulator method main.
public static void main(String[] arg) throws IOException {
// READ DEMOGRAPHIC FUNCTION
String filename = arg[0];
BufferedReader reader = new BufferedReader(new FileReader(filename));
double popSizeScale = 1.0;
double generationTime = 1.0;
if (arg.length > 2) {
popSizeScale = Double.parseDouble(arg[2]);
}
if (arg.length > 3) {
generationTime = Double.parseDouble(arg[3]);
}
PrintWriter populationFuncLogger = null;
if (arg.length > 5) {
String logFileName = arg[5];
if (logFileName.equals("-")) {
populationFuncLogger = new PrintWriter(System.out);
} else {
populationFuncLogger = new PrintWriter(new FileWriter(logFileName));
}
}
List<Double> times = new ArrayList<Double>();
String line = reader.readLine();
String[] tokens = line.trim().split("[\t ]+");
if (tokens.length < 2)
throw new RuntimeException();
ArrayList<ArrayList> popSizes = new ArrayList<ArrayList>();
while (line != null) {
double time = Double.parseDouble(tokens[0]) / generationTime;
times.add(time);
for (int i = 1; i < tokens.length; i++) {
popSizes.add(new ArrayList<Double>());
popSizes.get(i - 1).add(Double.parseDouble(tokens[i]));
}
line = reader.readLine();
if (line != null) {
tokens = line.trim().split("[\t ]+");
if (tokens.length != popSizes.size() + 1)
throw new RuntimeException();
}
}
reader.close();
// READ SAMPLE TIMES
String samplesFilename = arg[1];
reader = new BufferedReader(new FileReader(samplesFilename));
line = reader.readLine();
Taxa taxa = new Taxa();
int id = 0;
while (line != null) {
if (!line.startsWith("#")) {
tokens = line.split("[\t ]+");
if (tokens.length == 4) {
double t0 = Double.parseDouble(tokens[0]);
double t1 = Double.parseDouble(tokens[1]);
double dt = Double.parseDouble(tokens[2]);
int k = Integer.parseInt(tokens[3]);
for (double time = t0; time <= t1; time += dt) {
double sampleTime = time / generationTime;
for (int i = 0; i < k; i++) {
Taxon taxon = new Taxon("t" + id);
taxon.setAttribute(dr.evolution.util.Date.DATE, new Date(sampleTime, Units.Type.GENERATIONS, true));
taxa.addTaxon(taxon);
id += 1;
}
}
} else {
// sample times are in the same units as simulation
double sampleTime = Double.parseDouble(tokens[0]) / generationTime;
int count = Integer.parseInt(tokens[1]);
for (int i = 0; i < count; i++) {
Taxon taxon = new Taxon(id + "");
taxon.setAttribute(dr.evolution.util.Date.DATE, new Date(sampleTime, Units.Type.GENERATIONS, true));
taxa.addTaxon(taxon);
id += 1;
}
}
}
line = reader.readLine();
}
double minTheta = Double.MAX_VALUE;
double maxTheta = 0.0;
PrintWriter out;
if (arg.length > 4) {
out = new PrintWriter(new FileWriter(arg[4]));
} else {
out = new PrintWriter(System.out);
}
int pp = 0;
for (List<Double> popSize : popSizes) {
double[] thetas = new double[popSize.size()];
double[] intervals = new double[times.size() - 1];
if (populationFuncLogger != null) {
populationFuncLogger.println("# " + pp);
++pp;
}
// must reverse the direction of the model
for (int j = intervals.length; j > 0; j--) {
intervals[intervals.length - j] = times.get(j) - times.get(j - 1);
final double theta = popSize.get(j) * popSizeScale;
thetas[intervals.length - j] = theta;
if (theta < minTheta) {
minTheta = theta;
}
if (theta > maxTheta) {
maxTheta = theta;
}
final double t = times.get(intervals.length) - times.get(j);
if (populationFuncLogger != null) {
populationFuncLogger.println(t + "\t" + theta);
}
}
if (debug != null) {
debug.println("min theta = " + minTheta);
debug.println("max theta = " + maxTheta);
}
PiecewiseLinearPopulation demo = new PiecewiseLinearPopulation(intervals, thetas, Units.Type.GENERATIONS);
CoalescentSimulator simulator = new CoalescentSimulator();
Tree tree = simulator.simulateTree(taxa, demo);
out.println(TreeUtils.newick(tree));
if (debug != null) {
debug.println(TreeUtils.newick(tree));
}
}
if (populationFuncLogger != null) {
populationFuncLogger.flush();
populationFuncLogger.close();
}
out.flush();
out.close();
}
use of dr.evolution.util.Date in project beast-mcmc by beast-dev.
the class DataLikelihoodTester method createAlignment.
private static SimpleAlignment createAlignment(Object[][] taxa_sequence, DataType dataType) {
SimpleAlignment alignment = new SimpleAlignment();
alignment.setDataType(dataType);
// alignment.setDataType(Nucleotides.INSTANCE);
// 6, 17
Taxon[] 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);
}
return alignment;
}
Aggregations