<commit_before>/*
 * CoalescentSimulatorParser.java
 *
 * Copyright (c) 2002-2015 <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.coalescent;

import dr.evolution.tree.Tree;
import dr.evolution.util.Taxa;
import dr.evolution.util.TaxonList;
import dr.evomodel.coalescent.CoalescentSimulator;
import dr.evomodel.coalescent.demographicmodel.DemographicModel;
import dr.xml.*;

import java.util.ArrayList;
import java.util.List;

/**
 * CoalescentSimulator parser. Replaces the one now called 'OldCoalescentSimulator' which
 * used the parser name 'coalescentTree'. Simulates a tree using arbitrarily nested subtrees
 * which can be scaled to have particular node heights to be compatible with calibrations.
 */
public class CoalescentSimulatorParser extends AbstractXMLObjectParser {

    public static final String COALESCENT_SIMULATOR = "coalescentSimulator";
    public static final String HEIGHT = "height";
    public static final String CONSTRAINTS_TREE = "constraintsTree";

    public String getParserName() {
        return COALESCENT_SIMULATOR;
    }

    public Object parseXMLObject(XMLObject xo) throws XMLParseException {

        CoalescentSimulator simulator = new CoalescentSimulator();

        DemographicModel demoModel = (DemographicModel) xo.getChild(DemographicModel.class);
        List<TaxonList> taxonLists = new ArrayList<TaxonList>();
        List<Tree> subtrees = new ArrayList<Tree>();

        double height = xo.getAttribute(HEIGHT, Double.NaN);

        Tree constraintsTree =null;
        if(xo.hasChildNamed((CONSTRAINTS_TREE))){
            XMLObject cxo = xo.getChild(CONSTRAINTS_TREE);
            constraintsTree = (Tree) cxo.getChild(Tree.class);
        }

        // should have one child that is node
        for (int i = 0; i < xo.getChildCount(); i++) {
            final Object child = xo.getChild(i);
            if(child != constraintsTree){
                // AER - swapped the order of these round because Trees are TaxonLists...
                if (child instanceof Tree) {
                    subtrees.add((Tree) child);
                } else if (child instanceof TaxonList) {
                    taxonLists.add((TaxonList) child);
                }
            }
          }

        if (taxonLists.size() == 0) {
            if (subtrees.size() == 1) {
                return subtrees.get(0);
            } if (constraintsTree==null){
                throw new XMLParseException("Expected at least one taxonList or two subtrees or a constraints tree in "
                        + getParserName() + " element.");
            }
        }

        Taxa remainingTaxa = new Taxa();
        for (int i = 0; i < taxonLists.size(); i++) {
            remainingTaxa.addTaxa(taxonLists.get(i));
        }

        for (int i = 0; i < subtrees.size(); i++) {
            remainingTaxa.removeTaxa(subtrees.get(i));
        }
        if(constraintsTree!=null){
            remainingTaxa.removeTaxa(constraintsTree);
        }

        try {
            if(constraintsTree!=null){
                subtrees.add(simulator.simulateTree(constraintsTree,constraintsTree.getRoot(),demoModel));
            }

            Tree[] trees = new Tree[subtrees.size() + remainingTaxa.getTaxonCount()];
            // add the preset trees<commit_msg>Fix some logic in the parser<commit_after>/*
 * CoalescentSimulatorParser.java
 *
 * Copyright (c) 2002-2015 <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.coalescent;

import dr.evolution.tree.Tree;
import dr.evolution.util.Taxa;
import dr.evolution.util.TaxonList;
import dr.evomodel.coalescent.CoalescentSimulator;
import dr.evomodel.coalescent.demographicmodel.DemographicModel;
import dr.xml.*;

import java.util.ArrayList;
import java.util.List;

/**
 * CoalescentSimulator parser. Replaces the one now called 'OldCoalescentSimulator' which
 * used the parser name 'coalescentTree'. Simulates a tree using arbitrarily nested subtrees
 * which can be scaled to have particular node heights to be compatible with calibrations.
 */
public class CoalescentSimulatorParser extends AbstractXMLObjectParser {

    public static final String COALESCENT_SIMULATOR = "coalescentSimulator";
    public static final String HEIGHT = "height";
    public static final String CONSTRAINTS_TREE = "constraintsTree";

    public String getParserName() {
        return COALESCENT_SIMULATOR;
    }

    public Object parseXMLObject(XMLObject xo) throws XMLParseException {

        CoalescentSimulator simulator = new CoalescentSimulator();

        DemographicModel demoModel = (DemographicModel) xo.getChild(DemographicModel.class);
        List<TaxonList> taxonLists = new ArrayList<TaxonList>();
        List<Tree> subtrees = new ArrayList<Tree>();

        double height = xo.getAttribute(HEIGHT, Double.NaN);

        Tree constraintsTree =null;
        if(xo.hasChildNamed((CONSTRAINTS_TREE))){
            XMLObject cxo = xo.getChild(CONSTRAINTS_TREE);
            constraintsTree = (Tree) cxo.getChild(Tree.class);
        }

        // should have one child that is node
        for (int i = 0; i < xo.getChildCount(); i++) {
            final Object child = xo.getChild(i);
            if(child != constraintsTree){
                // AER - swapped the order of these round because Trees are TaxonLists...
                if (child instanceof Tree) {
                    subtrees.add((Tree) child);
                } else if (child instanceof TaxonList) {
                    taxonLists.add((TaxonList) child);
                }
            }
          }

        if (taxonLists.size() == 0) {
            if (subtrees.size() == 1) {
                return subtrees.get(0);
            } else if (subtrees.size() == 0 && constraintsTree==null) {
                throw new XMLParseException("Expected at least one taxonList or two subtrees or a constraints tree in "
                        + getParserName() + " element.");
            }
        }

        Taxa remainingTaxa = new Taxa();
        for (int i = 0; i < taxonLists.size(); i++) {
            remainingTaxa.addTaxa(taxonLists.get(i));
        }

        for (int i = 0; i < subtrees.size(); i++) {
            remainingTaxa.removeTaxa(subtrees.get(i));
        }
        if(constraintsTree!=null){
            remainingTaxa.removeTaxa(constraintsTree);
        }

        try {
            if(constraintsTree!=null){
                subtrees.add(simulator.simulateTree(constraintsTree,constraintsTree.getRoot(),demoModel));
            }

            Tree[] trees = new Tree[subtrees.size() + remainingTaxa.getTaxonCount()];
            // add the preset trees
{'fraction_non_alphanumeric': 0.08941148382475826, 'fraction_numerical': 0.006326847320042975, 'mean_word_length': 2.6665207877461707, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 17, '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': '2274318', 'n_tokens_mistral': 2399, 'n_tokens_neox': 2262, 'n_words': 739}