<commit_before>/*
 * LikelihoodTreeTraversal.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.evomodel.treedatalikelihood;

import dr.evolution.tree.NodeRef;
import dr.evolution.tree.Tree;
import dr.evomodel.branchratemodel.BranchRateModel;

import java.util.*;

/**
 * Created by msuchard on 10/6/16.
 */
public final class LikelihoodTreeTraversal extends TreeTraversal {


    public LikelihoodTreeTraversal(final Tree treeModel,
                                   final BranchRateModel branchRateModel,
                                   final TraversalType traversalType) {
        super(treeModel, branchRateModel, traversalType);
    }

    @Override
    public final void dispatchTreeTraversalCollectBranchAndNodeOperations() {
        branchOperations.clear();
        nodeOperations.clear();

        switch (traversalType) {

            case POST_ORDER:
                traversePostOrder(treeModel);
                break;
            case REVERSE_LEVEL_ORDER:
                traverseReverseLevelOrder(treeModel);
                break;
            default:
                assert false : "Unknown traversal type";
        }
    }

    public final List<DataLikelihoodDelegate.BranchOperation> getBranchOperations() {
        return branchOperations;
    }

    public final List<DataLikelihoodDelegate.NodeOperation> getNodeOperations() {
        return nodeOperations;
    }

    /**
     * Traverse the tree in post order.
     *
     * @param tree tree
     * @return boolean
     */
    private void traversePostOrder(Tree tree) {
        traversePostOrder(tree, tree.getRoot());
    }

    /**
     * Traverse the tree in post order.
     *
     * @param tree tree
     * @param node node
     * @return boolean
     */
    private boolean traversePostOrder(Tree tree, NodeRef node) {

        boolean update = false;

        int nodeNum = node.getNumber();

        // First update the transition probability matrix(ices) for this branch
        if (tree.getParent(node) != null && updateNode[nodeNum]) {
            // @todo - at the moment a matrix is updated even if a branch length doesn't change

            addBranchUpdateOperation(tree, node);

            update = true;
        }

        // If the node is internal, update the partial likelihoods.<commit_msg>Getting rid of a redundant todo.<commit_after>/*
 * LikelihoodTreeTraversal.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.evomodel.treedatalikelihood;

import dr.evolution.tree.NodeRef;
import dr.evolution.tree.Tree;
import dr.evomodel.branchratemodel.BranchRateModel;

import java.util.*;

/**
 * Created by msuchard on 10/6/16.
 */
public final class LikelihoodTreeTraversal extends TreeTraversal {


    public LikelihoodTreeTraversal(final Tree treeModel,
                                   final BranchRateModel branchRateModel,
                                   final TraversalType traversalType) {
        super(treeModel, branchRateModel, traversalType);
    }

    @Override
    public final void dispatchTreeTraversalCollectBranchAndNodeOperations() {
        branchOperations.clear();
        nodeOperations.clear();

        switch (traversalType) {

            case POST_ORDER:
                traversePostOrder(treeModel);
                break;
            case REVERSE_LEVEL_ORDER:
                traverseReverseLevelOrder(treeModel);
                break;
            default:
                assert false : "Unknown traversal type";
        }
    }

    public final List<DataLikelihoodDelegate.BranchOperation> getBranchOperations() {
        return branchOperations;
    }

    public final List<DataLikelihoodDelegate.NodeOperation> getNodeOperations() {
        return nodeOperations;
    }

    /**
     * Traverse the tree in post order.
     *
     * @param tree tree
     * @return boolean
     */
    private void traversePostOrder(Tree tree) {
        traversePostOrder(tree, tree.getRoot());
    }

    /**
     * Traverse the tree in post order.
     *
     * @param tree tree
     * @param node node
     * @return boolean
     */
    private boolean traversePostOrder(Tree tree, NodeRef node) {

        boolean update = false;

        int nodeNum = node.getNumber();

        // First update the transition probability matrix(ices) for this branch
        if (tree.getParent(node) != null && updateNode[nodeNum]) {
            addBranchUpdateOperation(tree, node);

            update = true;
        }

        // If the node is internal, update the partial likelihoods.
{'fraction_non_alphanumeric': 0.07210626185958255, 'fraction_numerical': 0.007906388361796331, 'mean_word_length': 2.5335195530726256, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5395262', 'n_tokens_mistral': 1702, 'n_tokens_neox': 1602, 'n_words': 583}