Search in sources :

Example 56 with Taxa

use of dr.evolution.util.Taxa in project beast-mcmc by beast-dev.

the class VariableCoalescentSimulator method readSampleFile.

private static Taxa readSampleFile(String fileName, double generationTime) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(fileName));
    String line = reader.readLine();
    Taxa taxa = new Taxa();
    int id = 0;
    while (line != null) {
        if (!line.startsWith("#")) {
            String[] tokens = line.split("[\t ]+");
            // 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();
    }
    return taxa;
}
Also used : Taxa(dr.evolution.util.Taxa) Taxon(dr.evolution.util.Taxon) Date(dr.evolution.util.Date)

Example 57 with Taxa

use of dr.evolution.util.Taxa in project beast-mcmc by beast-dev.

the class GibbsIndependentCoalescentOperator method doOperation.

/**
	 * change the parameter and return the hastings ratio.
     */
public double doOperation() {
    CoalescentSimulator simulator = new CoalescentSimulator();
    List<TaxonList> taxonLists = new ArrayList<TaxonList>();
    double rootHeight = -1.0;
    double oldLikelihood = 0.0;
    double newLikelihood = 0.0;
    // should have one child that is node
    for (int i = 0; i < xo.getChildCount(); i++) {
        final Object child = xo.getChild(i);
        //careful: Trees are TaxonLists ... (AER); see OldCoalescentSimulatorParser
        if (child instanceof Tree) {
        //do nothing
        } else if (child instanceof TaxonList) {
            //taxonLists.add((TaxonList) child);
            taxonLists.add((Taxa) child);
            //taxa added
            break;
        }
    }
    try {
        Tree[] trees = new Tree[taxonLists.size()];
        // simulate each taxonList separately
        for (int i = 0; i < taxonLists.size(); i++) {
            trees[i] = simulator.simulateTree(taxonLists.get(i), demoModel);
        }
        oldLikelihood = coalescent.getLogLikelihood();
        SimpleTree simTree = simulator.simulateTree(trees, demoModel, rootHeight, trees.length != 1);
        //this would be the normal way to do it
        treeModel.beginTreeEdit();
        //now it's allowed to adjust the tree structure
        treeModel.adoptTreeStructure(simTree);
        //endTreeEdit() would then fire the events
        treeModel.endTreeEdit();
        newLikelihood = coalescent.getLogLikelihood();
    } catch (IllegalArgumentException iae) {
        try {
            throw new XMLParseException(iae.getMessage());
        } catch (XMLParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    //return oldLikelihood - newLikelihood;
    return 0;
}
Also used : TaxonList(dr.evolution.util.TaxonList) ArrayList(java.util.ArrayList) SimpleTree(dr.evolution.tree.SimpleTree) CoalescentSimulator(dr.evomodel.coalescent.CoalescentSimulator) Taxa(dr.evolution.util.Taxa) SimpleTree(dr.evolution.tree.SimpleTree) Tree(dr.evolution.tree.Tree) XMLObject(dr.xml.XMLObject) XMLParseException(dr.xml.XMLParseException)

Example 58 with Taxa

use of dr.evolution.util.Taxa in project beast-mcmc by beast-dev.

the class ExternalLengthStatisticParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    String name = xo.getAttribute(Statistic.NAME, xo.getId());
    Tree tree = (Tree) xo.getChild(Tree.class);
    TaxonList taxa = (TaxonList) xo.getChild(Taxa.class);
    try {
        return new ExternalLengthStatistic(name, tree, taxa);
    } catch (TreeUtils.MissingTaxonException mte) {
        throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + "was not found in the tree.");
    }
}
Also used : Taxa(dr.evolution.util.Taxa) TaxonList(dr.evolution.util.TaxonList) Tree(dr.evolution.tree.Tree) ExternalLengthStatistic(dr.evomodel.tree.ExternalLengthStatistic) TreeUtils(dr.evolution.tree.TreeUtils)

Example 59 with Taxa

use of dr.evolution.util.Taxa in project beast-mcmc by beast-dev.

the class RandomSubsetTaxaParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    Taxa originalTaxa = new Taxa();
    for (int i = 0; i < xo.getChildCount(); i++) {
        Object child = xo.getChild(i);
        if (child instanceof Taxon) {
            Taxon taxon = (Taxon) child;
            originalTaxa.addTaxon(taxon);
        } else if (child instanceof TaxonList) {
            TaxonList taxonList1 = (TaxonList) child;
            for (int j = 0; j < taxonList1.getTaxonCount(); j++) {
                originalTaxa.addTaxon(taxonList1.getTaxon(j));
            }
        } else {
            throwUnrecognizedElement(xo);
        }
    }
    List<Taxon> originalTaxonList = originalTaxa.asList();
    int sampleTotal = xo.getAttribute(COUNT, originalTaxonList.size());
    if (sampleTotal < 2) {
        throw new XMLParseException("Must sample atleast two taxa");
    }
    boolean withReplacement = xo.getAttribute(WITH_REPLACEMENT, false);
    Taxa sampledTaxa = new Taxa();
    for (int i = 0; i < sampleTotal; i++) {
        int choice = MathUtils.nextInt(originalTaxonList.size());
        Taxon taxonToAdd = originalTaxonList.get(choice);
        sampledTaxa.addTaxon(taxonToAdd);
        if (!withReplacement) {
            originalTaxonList.remove(choice);
        }
    }
    return sampledTaxa;
}
Also used : Taxa(dr.evolution.util.Taxa) TaxonList(dr.evolution.util.TaxonList) Taxon(dr.evolution.util.Taxon)

Example 60 with Taxa

use of dr.evolution.util.Taxa in project beast-mcmc by beast-dev.

the class RandomTaxaSampleParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    Taxa population = (Taxa) xo.getChild(Taxa.class);
    int n = xo.getIntegerAttribute(SAMPLE);
    int N = population.getTaxonCount();
    if (n <= 0 || n > N) {
        throw new XMLParseException("sample must be greater than 0 and less than or equal to the population size");
    }
    Taxa sample = new Taxa();
    ArrayList<Integer> indexes = new ArrayList<Integer>(N);
    for (int i = 0; i < N; i++) indexes.add(i);
    Logger.getLogger("dr.evolution").info("Generating a random taxa sample of size: " + n);
    for (int i = 0; i < n; i++) {
        int randomIndex = MathUtils.nextInt(indexes.size());
        Taxon selectedTaxon = population.getTaxon(indexes.get(randomIndex));
        sample.addTaxon(selectedTaxon);
        indexes.remove(randomIndex);
    }
    if (xo.hasAttribute(PRINT_TAXA) && xo.getBooleanAttribute(PRINT_TAXA)) {
        String fileName = null;
        if (xo.hasAttribute(FILE_NAME)) {
            fileName = xo.getStringAttribute(FILE_NAME);
        }
        if (fileName != null) {
            try {
                Writer write;
                File file = new File(fileName);
                String name = file.getName();
                String parent = file.getParent();
                if (!file.isAbsolute()) {
                    parent = System.getProperty("user.dir");
                }
                write = new FileWriter(new File(parent, name));
                write.write("<taxa id=\"randomTaxaSample\">\n");
                for (int i = 0; i < n; i++) {
                    write.write("\t<taxon idref=\"" + sample.getTaxonId(i) + "\"/>\n");
                }
                write.write("</taxa id=\"randomTaxaSample\">\n");
                write.flush();
            } catch (IOException fnfe) {
                throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element.");
            }
        } else {
            Logger.getLogger("dr.evomodel").info("<taxa id=\"randomTaxaSample\">");
            for (int i = 0; i < n; i++) {
                Logger.getLogger("dr.evomodel").info("\t<taxon idref=\" " + sample.getTaxonId(i) + " \"> ");
            }
            Logger.getLogger("dr.evomodel").info("</taxa id=\"randomTaxaSample\">");
        }
    }
    return sample;
}
Also used : Taxa(dr.evolution.util.Taxa) Taxon(dr.evolution.util.Taxon) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File) FileWriter(java.io.FileWriter) Writer(java.io.Writer)

Aggregations

Taxa (dr.evolution.util.Taxa)65 Taxon (dr.evolution.util.Taxon)31 ArrayList (java.util.ArrayList)22 Tree (dr.evolution.tree.Tree)17 TaxonList (dr.evolution.util.TaxonList)14 Attribute (dr.util.Attribute)11 Patterns (dr.evolution.alignment.Patterns)9 Microsatellite (dr.evolution.datatype.Microsatellite)9 TreeModel (dr.evomodel.tree.TreeModel)9 NewickImporter (dr.evolution.io.NewickImporter)6 SimpleTree (dr.evolution.tree.SimpleTree)5 Parameter (dr.inference.model.Parameter)5 GammaSiteModel (dr.oldevomodel.sitemodel.GammaSiteModel)5 IOException (java.io.IOException)5 SimpleAlignment (dr.evolution.alignment.SimpleAlignment)4 ImportException (dr.evolution.io.Importer.ImportException)4 Date (dr.evolution.util.Date)4 BranchRateModel (dr.evomodel.branchratemodel.BranchRateModel)4 CoalescentSimulator (dr.evomodel.coalescent.CoalescentSimulator)4 CoalescentSimulator (dr.evolution.coalescent.CoalescentSimulator)3