<commit_before>/*
 * EmpiricalTreeDistributionModelParser.java
 *
 * Copyright (c) 2002-2016 <NAME>, <NAME> and <NAME>
 *
 * This file is part of BEAST.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership and licensing.
 *
 * BEAST is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 *  BEAST is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with BEAST; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

package dr.evomodelxml.tree;

import dr.util.FileHelpers;
import dr.xml.*;
import dr.evolution.io.NexusImporter;
import dr.evolution.io.Importer;
import dr.evolution.tree.Tree;
import dr.evolution.util.TaxonList;
import dr.evomodel.tree.EmpiricalTreeDistributionModel;

import java.io.*;
import java.util.logging.Logger;

/**
 * @author <NAME>
 *         <p/>
 *         Reads a list of trees from a NEXUS file.
 */
public class EmpiricalTreeDistributionModelParser extends AbstractXMLObjectParser {

    public static final String RATE_ATTRIBUTE_NAME = "rateAttribute";
    public static final String STARTING_TREE = "startingTree";
    public static final String ITERATE = "iterate";

    public String getParserName() {
        return EmpiricalTreeDistributionModel.EMPIRICAL_TREE_DISTRIBUTION_MODEL;
    }

    public String getParserDescription() {
        return "Read a list of trees from a NEXUS file.";
    }

    public Object parseXMLObject(XMLObject xo) throws XMLParseException {
        final String fileName = xo.getStringAttribute(FILE_NAME);

        int startingTree = xo.getAttribute(STARTING_TREE, -1); // default is random tree

        boolean iterate = xo.getAttribute(ITERATE, false); // default is random draw
        if (iterate && startingTree < 0) {
            startingTree = 0;
        }

        Logger.getLogger("dr.evomodel").info("Creating the empirical tree distribution model, '" + xo.getId() + "'");

        TaxonList taxa = (TaxonList)xo.getChild(TaxonList.class);

        final File file = FileHelpers.getFile(fileName);

        Tree[] trees = null;
        NexusImporter importer = null;
        try {
            FileReader reader = new FileReader(file);
            importer = new NexusImporter(reader);
            if (!iterate) {
                trees = importer.importTrees(taxa, true); // Re-order taxon numbers to original TaxonList order
            }
        } catch (FileNotFoundException e) {
            throw new XMLParseException(e.getMessage());
        } catch (IOException e) {
            throw new XMLParseException(e.getMessage());
        } catch (Importer.ImportException e) {
            throw new XMLParseException(e.getMessage());
        }
        
        Logger.getLogger("dr.evomodel").info("    Read " + trees.length + " trees from file, " + fileName);

        if (iterate) {
            return new EmpiricalTreeDistributionModel(importer, startingTree);
        } else {
            return new EmpiricalTreeDistributionModel(trees, startingTree);
        }
    }

    public static final String FILE_NAME = "fileName";
//    public static final String BURNIN = "burnin";

    public XMLSyntaxRule[] getSyntaxRules() {
        return new XMLSyntaxRule[]{
                AttributeRule.newIntegerRule(STARTING_TREE, true),
                AttributeRule.newBooleanRule(ITERATE, true),
                new StringAttributeRule(FILE_NAME,
                        "The name of a NEXUS tree file"),
//                AttributeRule.newIntegerRule(BURNIN, true,
//                        "The number of trees to exclude"),
                new ElementRule(TaxonList.class),
        };
    }


    public Class getReturnType() {
        return EmpiricalTreeDistributionModel.class;
    }
}<commit_msg>Created a operator schedule that runs each nested schedule for a certain number of states and then switches to the next.<commit_after>/*
 * EmpiricalTreeDistributionModelParser.java
 *
 * Copyright (c) 2002-2016 <NAME>, <NAME> and <NAME>
 *
 * This file is part of BEAST.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership and licensing.
 *
 * BEAST is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 *  BEAST is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with BEAST; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

package dr.evomodelxml.tree;

import dr.util.FileHelpers;
import dr.xml.*;
import dr.evolution.io.NexusImporter;
import dr.evolution.io.Importer;
import dr.evolution.tree.Tree;
import dr.evolution.util.TaxonList;
import dr.evomodel.tree.EmpiricalTreeDistributionModel;

import java.io.*;
import java.util.logging.Logger;

/**
 * @author <NAME>
 *         <p/>
 *         Reads a list of trees from a NEXUS file.
 */
public class EmpiricalTreeDistributionModelParser extends AbstractXMLObjectParser {

    public static final String RATE_ATTRIBUTE_NAME = "rateAttribute";
    public static final String STARTING_TREE = "startingTree";
    public static final String ITERATE = "iterate";

    public String getParserName() {
        return EmpiricalTreeDistributionModel.EMPIRICAL_TREE_DISTRIBUTION_MODEL;
    }

    public String getParserDescription() {
        return "Read a list of trees from a NEXUS file.";
    }

    public Object parseXMLObject(XMLObject xo) throws XMLParseException {
        final String fileName = xo.getStringAttribute(FILE_NAME);

        int startingTree = xo.getAttribute(STARTING_TREE, -1); // default is random tree

        boolean iterate = xo.getAttribute(ITERATE, false); // default is random draw
        if (iterate && startingTree < 0) {
            startingTree = 0;
        }

        Logger.getLogger("dr.evomodel").info("Creating the empirical tree distribution model, '" + xo.getId() + "'");

        TaxonList taxa = (TaxonList)xo.getChild(TaxonList.class);

        final File file = FileHelpers.getFile(fileName);

        Tree[] trees = null;
        NexusImporter importer = null;
        try {
            FileReader reader = new FileReader(file);
            importer = new NexusImporter(reader);
            if (!iterate) {
                trees = importer.importTrees(taxa, true); // Re-order taxon numbers to original TaxonList order
                reader.close();
            }
        } catch (FileNotFoundException e) {
            throw new XMLParseException(e.getMessage());
        } catch (IOException e) {
            throw new XMLParseException(e.getMessage());
        } catch (Importer.ImportException e) {
            throw new XMLParseException(e.getMessage());
        }
        

        if (iterate) {
            Logger.getLogger("dr.evomodel").info("    Iterate over " + trees.length + " trees from file, " + fileName);
            return new EmpiricalTreeDistributionModel(importer, startingTree);
        } else {
            Logger.getLogger("dr.evomodel").info("    Randomly jump between " + trees.length + " trees from file, " + fileName);
            return new EmpiricalTreeDistributionModel(trees, startingTree);
        }
    }

    public static final String FILE_NAME = "fileName";
//    public static final String BURNIN = "burnin";

    public XMLSyntaxRule[] getSyntaxRules() {
        return new XMLSyntaxRule[]{
                AttributeRule.newIntegerRule(STARTING_TREE, true),
                AttributeRule.newBooleanRule(ITERATE, true),
                new StringAttributeRule(FILE_NAME,
                        "The name of a NEXUS tree file"),
//                AttributeRule.newIntegerRule(BURNIN, true,
//                        "The number of trees to exclude"),
                new ElementRule(TaxonList.class),
        };
    }


    public Class getReturnType() {
        return EmpiricalTreeDistributionModel.class;
    }
}
{'fraction_non_alphanumeric': 0.08569485377465427, 'fraction_numerical': 0.0052142371344366355, 'mean_word_length': 2.749681257968551, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7150602', 'n_tokens_mistral': 2407, 'n_tokens_neox': 2301, 'n_words': 773}