use of dr.evolution.util.Taxon in project beast-mcmc by beast-dev.
the class PlinkImporter method addTaxonAttribute.
public void addTaxonAttribute(TaxonList inTaxonList, String traitName) {
taxonList = new Taxa();
for (Taxon taxon : inTaxonList) {
List<Double> valueList = taxonHash.get(taxon.getId());
if (valueList == null) {
Logger.getLogger("dr.evolution").warning("Taxon " + taxon.getId() + " not found in PLINK data");
} else {
String string = makeStringAttribute(valueList);
((Taxa) taxonList).addTaxon(taxon);
taxon.setAttribute(traitName, string);
}
if (DEBUG) {
System.err.println("Added trait for " + taxon.getId());
}
}
}
use of dr.evolution.util.Taxon in project beast-mcmc by beast-dev.
the class BEAUTiImporter method importTraits.
public void importTraits(final File file) throws Exception {
List<TraitData> importedTraits = new ArrayList<TraitData>();
Taxa taxa = options.taxonList;
DataTable<String[]> dataTable = DataTable.Text.parse(new FileReader(file));
String[] traitNames = dataTable.getColumnLabels();
String[] taxonNames = dataTable.getRowLabels();
for (int i = 0; i < dataTable.getColumnCount(); i++) {
boolean warningGiven = false;
String traitName = traitNames[i];
String[] values = dataTable.getColumn(i);
Class c = null;
if (!isMissingValue(values[0])) {
c = Utils.detectType(values[0]);
}
for (int j = 1; j < values.length; j++) {
if (!isMissingValue(values[j])) {
if (c == null) {
c = Utils.detectType(values[j]);
} else {
Class c1 = Utils.detectType(values[j]);
if (c == Integer.class && c1 == Double.class) {
// change the type to double
c = Double.class;
}
if (c1 != c && !(c == Double.class && c1 == Integer.class) && !warningGiven) {
JOptionPane.showMessageDialog(frame, "Not all values of same type for trait" + traitName, "Incompatible values", JOptionPane.WARNING_MESSAGE);
warningGiven = true;
}
}
}
}
TraitData.TraitType t = (c == Boolean.class || c == String.class || c == null) ? TraitData.TraitType.DISCRETE : (c == Integer.class) ? TraitData.TraitType.INTEGER : TraitData.TraitType.CONTINUOUS;
TraitData newTrait = new TraitData(options, traitName, file.getName(), t);
if (validateTraitName(traitName)) {
importedTraits.add(newTrait);
}
int j = 0;
for (final String taxonName : taxonNames) {
final int index = taxa.getTaxonIndex(taxonName);
Taxon taxon;
if (index >= 0) {
taxon = taxa.getTaxon(index);
} else {
taxon = new Taxon(taxonName);
taxa.addTaxon(taxon);
}
if (!isMissingValue(values[j])) {
taxon.setAttribute(traitName, Utils.constructFromString(c, values[j]));
} else {
// AR - merge rather than replace existing trait values
if (taxon.getAttribute(traitName) == null) {
taxon.setAttribute(traitName, "?");
}
}
j++;
}
}
setData(file.getName(), taxa, null, null, null, importedTraits, null);
}
use of dr.evolution.util.Taxon in project beast-mcmc by beast-dev.
the class BEAUTiImporter method checkTaxonList.
private void checkTaxonList(TaxonList taxonList) throws ImportException {
// check the taxon names for invalid characters
boolean foundAmp = false;
for (Taxon taxon : taxonList) {
String name = taxon.getId();
if (name.indexOf('&') >= 0) {
foundAmp = true;
}
}
if (foundAmp) {
throw new ImportException("One or more taxon names include an illegal character ('&').\n" + "These characters will prevent BEAST from reading the resulting XML file.\n\n" + "Please edit the taxon name(s) before reloading the data file.");
}
// make sure they all have dates...
for (int i = 0; i < taxonList.getTaxonCount(); i++) {
if (taxonList.getTaxonAttribute(i, "date") == null) {
Date origin = new Date(0);
dr.evolution.util.Date date = dr.evolution.util.Date.createTimeSinceOrigin(0.0, Units.Type.YEARS, origin);
taxonList.getTaxon(i).setAttribute("date", date);
}
}
}
use of dr.evolution.util.Taxon in project beast-mcmc by beast-dev.
the class FastaImporter method importAlignment.
/**
* importAlignment.
*/
public Alignment importAlignment() throws IOException, ImportException {
SimpleAlignment alignment = null;
try {
// find fasta line start
while (read() != FASTA_FIRST_CHAR) {
}
do {
final String name = readLine().trim();
StringBuffer seq = new StringBuffer();
readSequence(seq, dataType, "" + FASTA_FIRST_CHAR, Integer.MAX_VALUE, "-", "?", "", "");
if (alignment == null) {
alignment = new SimpleAlignment();
}
alignment.addSequence(new Sequence(new Taxon(name.toString()), seq.toString()));
} while (getLastDelimiter() == FASTA_FIRST_CHAR);
} catch (EOFException e) {
// catch end of file the ugly way.
}
return alignment;
}
use of dr.evolution.util.Taxon 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();
}
Aggregations