Ejemplo n.º 1
0
  public static void checkTree(TreeModel treeModel) {

    // todo Should only be run if there exists a zero-length interval

    //        TreeModel treeModel = (TreeModel) tree;
    for (int i = 0; i < treeModel.getInternalNodeCount(); i++) {
      NodeRef node = treeModel.getInternalNode(i);
      if (node != treeModel.getRoot()) {
        double parentHeight = treeModel.getNodeHeight(treeModel.getParent(node));
        double childHeight0 = treeModel.getNodeHeight(treeModel.getChild(node, 0));
        double childHeight1 = treeModel.getNodeHeight(treeModel.getChild(node, 1));
        double maxChild = childHeight0;
        if (childHeight1 > maxChild) maxChild = childHeight1;
        double newHeight = maxChild + MathUtils.nextDouble() * (parentHeight - maxChild);
        treeModel.setNodeHeight(node, newHeight);
      }
    }
    treeModel.pushTreeChangedEvent();
  }
  /** Constructor. */
  public CenancestorTreeLikelihood(
      PatternList patternList,
      TreeModel treeModel,
      SiteModel siteModel,
      CenancestorBranchRateModel branchRateModel,
      TipStatesModel tipStatesModel,
      Parameter cenancestorHeight,
      Parameter cenancestorBranch,
      // Parameter asStatistic,
      boolean useAmbiguities,
      boolean allowMissingTaxa,
      boolean storePartials,
      boolean forceJavaCore,
      boolean forceRescaling) {

    super(CenancestorTreeLikelihoodParser.TREE_LIKELIHOOD, patternList, treeModel);

    this.storePartials = storePartials;
    nodeCount = treeModel.getNodeCount() + 1;
    updateNode = new boolean[nodeCount];
    for (int i = 0; i < nodeCount; i++) {
      updateNode[i] = true;
    }

    try {
      this.siteModel = siteModel;
      addModel(siteModel);

      this.frequencyModel = siteModel.getFrequencyModel();
      addModel(frequencyModel);

      this.tipStatesModel = tipStatesModel;

      integrateAcrossCategories = siteModel.integrateAcrossCategories();

      this.categoryCount = siteModel.getCategoryCount();

      this.cenancestorHeight = cenancestorHeight;
      addVariable(cenancestorHeight);
      cenancestorHeight.addBounds(
          new Parameter.DefaultBounds(
              Double.POSITIVE_INFINITY,
              0.0,
              1)); // TODO The lower bound should be the maximum leaf height

      this.cenancestorBranch = cenancestorBranch;
      cenancestorBranch.addBounds(
          new Parameter.DefaultBounds(
              Double.POSITIVE_INFINITY,
              0.0,
              1)); // TODO The upper bound should be the maximum leaf height
      addVariable(cenancestorBranch);

      // if (asStatistic == cenancestorHeight){
      //	this.branchRules=true;
      // }

      //	if (branchRules==true){
      updateCenancestorHeight(); // Trying to avoid improper initial values
      //	}
      // 	else{
      //		updateCenancestorBranch();
      //	}

      final Logger logger = Logger.getLogger("dr.evomodel");
      String coreName = "Java general";

      /** TODO: Check if is worthy to implement other datatypes. */
      final DataType dataType = patternList.getDataType();

      if (dataType instanceof dr.evolution.datatype.TwoStates) {
        coreName = "Java cenancestor binary";
        cenancestorlikelihoodCore =
            new GeneralCenancestorLikelihoodCore(patternList.getStateCount());
      } else if (dataType instanceof dr.evolution.datatype.GeneralDataType) {
        coreName = "Java cenancestor CNV";
        cenancestorlikelihoodCore =
            new GeneralCenancestorLikelihoodCore(patternList.getStateCount());
      }

      /*            if (integrateAcrossCategories) {

          final DataType dataType = patternList.getDataType();

          if (dataType instanceof dr.evolution.datatype.Nucleotides) {

              if (!forceJavaCore && NativeNucleotideLikelihoodCore.isAvailable()) {
                  coreName = "native nucleotide";
                  likelihoodCore = new NativeNucleotideLikelihoodCore();
              } else {
                  coreName = "Java nucleotide";
                  likelihoodCore = new NucleotideLikelihoodCore();
              }

          } else if (dataType instanceof dr.evolution.datatype.AminoAcids) {
              if (!forceJavaCore && NativeAminoAcidLikelihoodCore.isAvailable()) {
                  coreName = "native amino acid";
                  likelihoodCore = new NativeAminoAcidLikelihoodCore();
              } else {
                  coreName = "Java amino acid";
                  likelihoodCore = new AminoAcidLikelihoodCore();
              }

              // The codon core was out of date and did nothing more than the general core...
          } else if (dataType instanceof dr.evolution.datatype.Codons) {
              if (!forceJavaCore && NativeGeneralLikelihoodCore.isAvailable()) {
                  coreName = "native general";
                  likelihoodCore = new NativeGeneralLikelihoodCore(patternList.getStateCount());
              } else {
                  coreName = "Java general";
                  likelihoodCore = new GeneralLikelihoodCore(patternList.getStateCount());
              }
              useAmbiguities = true;
          } else {
              if (!forceJavaCore && NativeGeneralLikelihoodCore.isAvailable()) {
                  coreName = "native general";
                  likelihoodCore = new NativeGeneralLikelihoodCore(patternList.getStateCount());
              } else {
                  	coreName = "Java general";
                  	likelihoodCore = new GeneralLikelihoodCore(patternList.getStateCount());
              }
          }
      } else {
          likelihoodCore = new GeneralLikelihoodCore(patternList.getStateCount());
      }*/
      {
        final String id = getId();
        logger.info(
            "TreeLikelihood("
                + ((id != null) ? id : treeModel.getId())
                + ") using "
                + coreName
                + " likelihood core");

        logger.info(
            "  " + (useAmbiguities ? "Using" : "Ignoring") + " ambiguities in tree likelihood.");
        logger.info("  With " + patternList.getPatternCount() + " unique site patterns.");
      }

      if (branchRateModel != null) {
        this.branchRateModel = branchRateModel;
        logger.info("Branch rate model used: " + branchRateModel.getModelName());
      } else {
        this.branchRateModel = new DefaultCenancestorBranchRateModel();
      }
      addModel(this.branchRateModel);

      probabilities = new double[stateCount * stateCount];

      cenancestorlikelihoodCore.initialize(
          nodeCount, patternCount, categoryCount, integrateAcrossCategories);

      int extNodeCount = treeModel.getExternalNodeCount();
      int intNodeCount = treeModel.getInternalNodeCount();

      if (tipStatesModel != null) {
        tipStatesModel.setTree(treeModel);

        tipPartials = new double[patternCount * stateCount];

        for (int i = 0; i < extNodeCount; i++) {
          // Find the id of tip i in the patternList
          String id = treeModel.getTaxonId(i);
          int index = patternList.getTaxonIndex(id);

          if (index == -1) {
            throw new TaxonList.MissingTaxonException(
                "Taxon, "
                    + id
                    + ", in tree, "
                    + treeModel.getId()
                    + ", is not found in patternList, "
                    + patternList.getId());
          }

          tipStatesModel.setStates(patternList, index, i, id);
          cenancestorlikelihoodCore.createNodePartials(i);
        }

        addModel(tipStatesModel);
      } else {
        for (int i = 0; i < extNodeCount; i++) {
          // Find the id of tip i in the patternList
          String id = treeModel.getTaxonId(i);
          int index = patternList.getTaxonIndex(id);

          if (index == -1) {
            if (!allowMissingTaxa) {
              throw new TaxonList.MissingTaxonException(
                  "Taxon, "
                      + id
                      + ", in tree, "
                      + treeModel.getId()
                      + ", is not found in patternList, "
                      + patternList.getId());
            }
            if (useAmbiguities) {
              setMissingPartials((LikelihoodCore) cenancestorlikelihoodCore, i);
            } else {
              setMissingStates((LikelihoodCore) cenancestorlikelihoodCore, i);
            }
          } else {
            if (useAmbiguities) {
              setPartials(
                  (LikelihoodCore) cenancestorlikelihoodCore, patternList, categoryCount, index, i);
            } else {
              setStates((LikelihoodCore) cenancestorlikelihoodCore, patternList, index, i);
            }
          }
        }
      }
      for (int i = 0; i <= intNodeCount; i++) { // Added one step for the cenancestor
        cenancestorlikelihoodCore.createNodePartials(extNodeCount + i);
      }

      if (forceRescaling) {
        cenancestorlikelihoodCore.setUseScaling(true);
        logger.info("  Forcing use of partials rescaling.");
      }

    } catch (TaxonList.MissingTaxonException mte) {
      throw new RuntimeException(mte.toString());
    }

    addStatistic(new SiteLikelihoodsStatistic());
  }