Search in sources :

Example 1 with TreeTraitLogger

use of dr.evomodel.treelikelihood.utilities.TreeTraitLogger in project beast-mcmc by beast-dev.

the class StratifiedTraitLoggerParser method parseXMLObject.

//public static final String LOG_FORMAT = "format";
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
    TreeTraitProvider traitProvider = (TreeTraitProvider) xo.getChild(TreeTraitProvider.class);
    String traitName = (String) xo.getAttribute(TRAIT_NAME);
    TreeTrait trait = traitProvider.getTreeTrait(traitName);
    if (trait == null || trait.getIntent() != TreeTrait.Intent.WHOLE_TREE) {
        StringBuilder sb = new StringBuilder("Unable to find whole tree trait '" + traitName + "' in '" + xo.getId() + "\n");
        sb.append("\tPossible traits:");
        for (TreeTrait existingTrait : traitProvider.getTreeTraits()) {
            if (existingTrait.getIntent() == TreeTrait.Intent.WHOLE_TREE) {
                sb.append(" " + existingTrait.getTraitName());
            }
        }
        sb.append("\n");
        throw new XMLParseException(sb.toString());
    }
    boolean partition = xo.getAttribute(PARTITION, false);
    if (trait.getTraitClass() == Double.class || trait.getTraitClass() == Integer.class || !partition) {
        return new TreeTraitLogger(treeModel, new TreeTrait[] { trait });
    }
    int length;
    Object obj = trait.getTrait(treeModel, treeModel.getNode(0));
    if (obj instanceof double[]) {
        length = ((double[]) obj).length;
    } else if (obj instanceof int[]) {
        length = ((int[]) obj).length;
    } else {
        throw new XMLParseException("Unknown trait type for partitioning");
    }
    TreeTrait[] partitionedTraits = new TreeTrait[length];
    for (int i = 0; i < length; i++) {
        partitionedTraits[i] = new TreeTrait.PickEntryD(trait, i);
    }
    return new TreeTraitLogger(treeModel, partitionedTraits);
}
Also used : TreeTraitLogger(dr.evomodel.treelikelihood.utilities.TreeTraitLogger) TreeTraitProvider(dr.evolution.tree.TreeTraitProvider) TreeTrait(dr.evolution.tree.TreeTrait) TreeModel(dr.evomodel.tree.TreeModel)

Example 2 with TreeTraitLogger

use of dr.evomodel.treelikelihood.utilities.TreeTraitLogger in project beast-mcmc by beast-dev.

the class CodonPartitionedRobustCounting method setupTraits.

private void setupTraits() {
    TreeTrait baseTrait = new TreeTrait.DA() {

        public String getTraitName() {
            return BASE_TRAIT_PREFIX + codonLabeling.getText();
        }

        public Intent getIntent() {
            return Intent.BRANCH;
        }

        public double[] getTrait(Tree tree, NodeRef node) {
            return getExpectedCountsForBranch(node);
        }

        public boolean getLoggable() {
            return false;
        }
    };
    if (saveCompleteHistory) {
        TreeTrait stringTrait = new TreeTrait.SA() {

            public String getTraitName() {
                return COMPLETE_HISTORY_PREFIX + codonLabeling.getText();
            }

            public Intent getIntent() {
                return Intent.BRANCH;
            }

            public boolean getFormatAsArray() {
                return true;
            }

            public String[] getTrait(Tree tree, NodeRef node) {
                // Lazy simulation of complete histories
                double[] count = getExpectedCountsForBranch(node);
                List<String> events = new ArrayList<String>();
                for (int i = 0; i < numCodons; i++) {
                    String eventString = completeHistoryPerNode[node.getNumber()][i];
                    if (eventString != null) {
                        if (eventString.contains("},{")) {
                            // There are multiple events
                            String[] elements = eventString.split("(?<=\\}),(?=\\{)");
                            for (String e : elements) {
                                events.add(e);
                            }
                        } else {
                            events.add(eventString);
                        }
                    }
                }
                if (DEBUG) {
                    double sum = 0.0;
                    for (double d : count) {
                        if (d > 0.0) {
                            sum += 1;
                        }
                    }
                    System.err.println(events.size() + " " + sum);
                    if (Math.abs(events.size() - sum) > 0.5) {
                        System.err.println("Error");
                        for (int i = 0; i < count.length; ++i) {
                            if (count[i] != 0.0) {
                                System.err.println(i + ": " + count[i] + completeHistoryPerNode[node.getNumber()][i]);
                            }
                        }
                        System.err.println("Error");
                        int c = 0;
                        for (String s : events) {
                            c++;
                            System.err.println(c + ":" + s);
                        }
                        System.exit(-1);
                    }
                }
                String[] array = new String[events.size()];
                events.toArray(array);
                return array;
            }

            public boolean getLoggable() {
                return true;
            }
        };
        treeTraits.addTrait(stringTrait);
    }
    TreeTrait unconditionedSum;
    if (!TRIAL) {
        unconditionedSum = new TreeTrait.D() {

            public String getTraitName() {
                return UNCONDITIONED_PREFIX + codonLabeling.getText();
            }

            public Intent getIntent() {
                return Intent.WHOLE_TREE;
            }

            public Double getTrait(Tree tree, NodeRef node) {
                return getUnconditionedTraitValue();
            }

            public boolean getLoggable() {
                return false;
            }
        };
    } else {
        unconditionedSum = new TreeTrait.DA() {

            public String getTraitName() {
                return UNCONDITIONED_PREFIX + codonLabeling.getText();
            }

            public Intent getIntent() {
                return Intent.WHOLE_TREE;
            }

            public double[] getTrait(Tree tree, NodeRef node) {
                return getUnconditionedTraitValues();
            }

            public boolean getLoggable() {
                return false;
            }
        };
    }
    TreeTrait sumOverTreeTrait = new TreeTrait.SumOverTreeDA(SITE_SPECIFIC_PREFIX + codonLabeling.getText(), baseTrait, includeExternalBranches, includeInternalBranches) {

        @Override
        public boolean getLoggable() {
            return false;
        }
    };
    // This should be the default output in tree logs
    TreeTrait sumOverSitesTrait = new TreeTrait.SumAcrossArrayD(codonLabeling.getText(), baseTrait) {

        @Override
        public boolean getLoggable() {
            return true;
        }
    };
    // This should be the default output in columns logs
    String name = prefix != null ? prefix + TOTAL_PREFIX + codonLabeling.getText() : TOTAL_PREFIX + codonLabeling.getText();
    TreeTrait sumOverSitesAndTreeTrait = new TreeTrait.SumOverTreeD(name, sumOverSitesTrait, includeExternalBranches, includeInternalBranches) {

        @Override
        public boolean getLoggable() {
            return true;
        }
    };
    treeTraitLogger = new TreeTraitLogger(tree, new TreeTrait[] { sumOverSitesAndTreeTrait });
    treeTraits.addTrait(baseTrait);
    treeTraits.addTrait(unconditionedSum);
    treeTraits.addTrait(sumOverSitesTrait);
    treeTraits.addTrait(sumOverTreeTrait);
    treeTraits.addTrait(sumOverSitesAndTreeTrait);
    if (doUnconditionedPerBranch) {
        TreeTrait unconditionedBase = new TreeTrait.DA() {

            public String getTraitName() {
                return UNCONDITIONED_PER_BRANCH_PREFIX + codonLabeling.getText();
            }

            public Intent getIntent() {
                return Intent.BRANCH;
            }

            public double[] getTrait(Tree tree, NodeRef node) {
                return getUnconditionalCountsForBranch(node);
            }

            public boolean getLoggable() {
                // TODO Should be switched to true to log unconditioned values per branch
                return false;
            }
        };
        TreeTrait sumUnconditionedOverSitesTrait = new TreeTrait.SumAcrossArrayD(UNCONDITIONED_PER_BRANCH_PREFIX + codonLabeling.getText(), unconditionedBase) {

            @Override
            public boolean getLoggable() {
                return true;
            }
        };
        String nameU = prefix != null ? prefix + UNCONDITIONED_TOTAL_PREFIX + codonLabeling.getText() : UNCONDITIONED_TOTAL_PREFIX + codonLabeling.getText();
        TreeTrait sumUnconditionedOverSitesAndTreeTrait = new TreeTrait.SumOverTreeD(nameU, sumUnconditionedOverSitesTrait, includeExternalBranches, includeInternalBranches) {

            public boolean getLoggable() {
                return true;
            }
        };
        treeTraitLogger = new TreeTraitLogger(tree, new TreeTrait[] { sumOverSitesAndTreeTrait, sumUnconditionedOverSitesAndTreeTrait });
        treeTraits.addTrait(unconditionedBase);
        treeTraits.addTrait(sumUnconditionedOverSitesTrait);
    }
}
Also used : TreeTraitLogger(dr.evomodel.treelikelihood.utilities.TreeTraitLogger) ArrayList(java.util.ArrayList) TreeTrait(dr.evolution.tree.TreeTrait) NodeRef(dr.evolution.tree.NodeRef) Tree(dr.evolution.tree.Tree)

Aggregations

TreeTrait (dr.evolution.tree.TreeTrait)2 TreeTraitLogger (dr.evomodel.treelikelihood.utilities.TreeTraitLogger)2 NodeRef (dr.evolution.tree.NodeRef)1 Tree (dr.evolution.tree.Tree)1 TreeTraitProvider (dr.evolution.tree.TreeTraitProvider)1 TreeModel (dr.evomodel.tree.TreeModel)1 ArrayList (java.util.ArrayList)1