/** Constructor */
  public SampleStateAndCategoryModel(
      Parameter muParameter, Parameter categoriesParameter, Vector substitutionModels) {

    super(SampleStateAndCategoryModelParser.SAMPLE_STATE_AND_CATEGORY_MODEL);

    this.substitutionModels = substitutionModels;

    for (int i = 0; i < substitutionModels.size(); i++) {
      addModel((SubstitutionModel) substitutionModels.elementAt(i));
    }

    this.categoryCount = substitutionModels.size();
    sitesInCategory = new int[categoryCount];
    //	stateCount =
    // ((SubstitutionModel)substitutionModels.elementAt(0)).getDataType().getStateCount();

    this.muParameter = muParameter;
    addVariable(muParameter);
    muParameter.addBounds(new Parameter.DefaultBounds(1000.0, 0.0, 1));

    this.categoriesParameter = categoriesParameter;
    addVariable(categoriesParameter);

    if (categoryCount > 1) {
      for (int i = 0; i < categoryCount; i++) {
        Parameter p = (Parameter) ((YangCodonModel) substitutionModels.elementAt(i)).getVariable(0);
        Parameter lower = null;
        Parameter upper = null;

        if (i == 0) {
          upper = (Parameter) ((YangCodonModel) substitutionModels.elementAt(i + 1)).getVariable(0);
          p.addBounds(new omegaBounds(lower, upper));
        } else {
          if (i == (categoryCount - 1)) {
            lower =
                (Parameter) ((YangCodonModel) substitutionModels.elementAt(i - 1)).getVariable(0);
            p.addBounds(new omegaBounds(lower, upper));
          } else {
            upper =
                (Parameter) ((YangCodonModel) substitutionModels.elementAt(i + 1)).getVariable(0);
            lower =
                (Parameter) ((YangCodonModel) substitutionModels.elementAt(i - 1)).getVariable(0);
            p.addBounds(new omegaBounds(lower, upper));
          }
        }
      }
    }
  }
  /** Construct demographic model with default settings */
  public ExponentialLogisticModel(
      String name,
      Parameter N0Parameter,
      Parameter logisticGrowthParameter,
      Parameter logisticShapeParameter,
      Parameter exponentialGrowthParameter,
      Parameter transistionTimeParameter,
      double alpha,
      Type units) {

    super(name);

    exponentialLogistic = new ExponentialLogistic(units);

    this.N0Parameter = N0Parameter;
    addVariable(N0Parameter);
    N0Parameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));

    this.logisticGrowthParameter = logisticGrowthParameter;
    addVariable(logisticGrowthParameter);
    logisticGrowthParameter.addBounds(
        new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));

    this.logisticShapeParameter = logisticShapeParameter;
    addVariable(logisticShapeParameter);
    logisticShapeParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));

    this.exponentialGrowthParameter = exponentialGrowthParameter;
    addVariable(exponentialGrowthParameter);
    exponentialGrowthParameter.addBounds(
        new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));

    this.transistionTimeParameter = transistionTimeParameter;
    addVariable(transistionTimeParameter);
    transistionTimeParameter.addBounds(
        new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));

    this.alpha = alpha;

    setUnits(units);
  }
  public PopsIOSpeciesTreeModel(
      PopsIOSpeciesBindings piosb, Parameter popPriorScale, PriorComponent[] priorComponents) {
    super(PopsIOSpeciesTreeModelParser.PIO_SPECIES_TREE);
    this.piosb = piosb;

    this.popPriorScale = popPriorScale;
    addVariable(popPriorScale);
    popPriorScale.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));

    this.priorComponents = priorComponents;

    PopsIOSpeciesBindings.SpInfo[] species = piosb.getSpecies();
    int nTaxa = species.length;
    int nNodes = 2 * nTaxa - 1;
    pionodes = new PopsIONode[nNodes];
    for (int n = 0; n < nNodes; n++) {
      pionodes[n] = new PopsIONode(n);
    }
    ArrayList<Integer> tojoin = new ArrayList<Integer>(nTaxa);
    for (int n = 0; n < nTaxa; n++) {
      pionodes[n].setTaxon(species[n].name);
      pionodes[n].setHeight(0.0);
      pionodes[n].setUnion(piosb.tipUnionFromTaxon(pionodes[n].getTaxon()));
      tojoin.add(n);
    }
    double rate = 1.0;
    double treeheight = 0.0;
    for (int i = 0; i < nTaxa - 1; i++) {
      int numtojoin = tojoin.size();
      int j = MathUtils.nextInt(numtojoin);
      Integer child0 = tojoin.get(j);
      tojoin.remove(j);
      int k = MathUtils.nextInt(numtojoin - 1);
      Integer child1 = tojoin.get(k);
      tojoin.remove(k);
      pionodes[nTaxa + i].addChildren(pionodes[child0], pionodes[child1]);
      pionodes[nTaxa + i].setHeight(treeheight + randomnodeheight(numtojoin * rate));
      treeheight = pionodes[nTaxa + i].getHeight();
      tojoin.add(nTaxa + i);
    }
    rootn = pionodes.length - 1;

    double scale = 0.99 * piosb.initialMinGeneNodeHeight() / pionodes[rootn].height;
    scaleAllHeights(scale);
    pionodes[rootn].fillinUnionsInSubtree(piosb.getSpecies().length);

    stree = makeSimpleTree();

    Logger.getLogger("dr.evomodel.speciation.popsio")
        .info(
            "\tConstructing a PopsIO Species Tree Model, please cite:\n"
                + Citable.Utils.getCitationString(this));
  }
  /** Construct demographic model with default settings */
  public EmergingEpidemicModel(
      String name,
      Parameter growthRateParameter,
      Parameter generationTimeParameter,
      Parameter generationShapeParameter,
      Parameter offspringDispersionParameter,
      TreeModel treeModel,
      Type units) {

    super(name);

    exponentialGrowth = new ExponentialGrowth(units);

    this.growthRateParameter = growthRateParameter;
    addVariable(growthRateParameter);
    growthRateParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, 0.0, 1));

    this.generationTimeParameter = generationTimeParameter;
    addVariable(generationTimeParameter);
    generationTimeParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, 0.0, 1));

    this.generationShapeParameter = generationShapeParameter;
    addVariable(generationShapeParameter);
    generationShapeParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, 0.0, 1));

    this.offspringDispersionParameter = offspringDispersionParameter;
    addVariable(offspringDispersionParameter);
    offspringDispersionParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, 0.0, 1));

    this.treeModel = treeModel;
    addModel(treeModel);

    addStatistic(new N0Statistic("N0"));
    addStatistic(new RStatistic("R"));

    setUnits(units);
  }
Exemplo n.º 5
0
  public MixtureModelBranchRates(
      TreeModel tree,
      Parameter rateCategoryQuantilesParameter,
      ParametricDistributionModel[] models,
      Parameter distributionIndexParameter,
      boolean useQuantilesForRates,
      boolean normalize,
      double normalizeBranchRateTo) {
    super(MixtureModelBranchRatesParser.MIXTURE_MODEL_BRANCH_RATES);

    this.useQuantilesForRates = useQuantilesForRates;

    this.rateCategoryQuantiles =
        new TreeParameterModel(tree, rateCategoryQuantilesParameter, false);

    rates = new double[tree.getNodeCount()];

    this.normalize = normalize;

    this.treeModel = tree;
    this.distributionModels = models;
    this.normalizeBranchRateTo = normalizeBranchRateTo;

    this.tree = new SimpleTree(tree);

    this.distributionIndexParameter = distributionIndexParameter;
    addVariable(this.distributionIndexParameter);

    // Force the boundaries of rateCategoryParameter to match the category count
    // d Parameter.DefaultBounds bound = new Parameter.DefaultBounds(categoryCount - 1, 0,
    // rateCategoryParameter.getDimension());
    // d rateCategoryParameter.addBounds(bound);
    // rateCategoryQuantilesParameter.;

    Parameter.DefaultBounds bound =
        new Parameter.DefaultBounds(1.0, 0.0, rateCategoryQuantilesParameter.getDimension());
    rateCategoryQuantilesParameter.addBounds(bound);

    Parameter.DefaultBounds bound2 = new Parameter.DefaultBounds(models.length, 0.0, 1);
    distributionIndexParameter.addBounds(bound2);
    distributionIndexParameter.setParameterValue(0, 0);

    // Parameter distributionIndexParameter;

    for (ParametricDistributionModel distributionModel : distributionModels) {
      addModel(distributionModel);
    }
    // AR - commented out: changes to the tree are handled by model changed events fired by
    // rateCategories
    //        addModel(tree);
    // d addModel(rateCategories);

    addModel(rateCategoryQuantiles);

    // addModel(treeModel); // Maybe
    // AR - commented out: changes to rateCategoryParameter are handled by model changed events
    // fired by rateCategories
    //        addVariable(rateCategoryParameter);

    if (normalize) {
      tree.addModelListener(
          new ModelListener() {

            public void modelChangedEvent(Model model, Object object, int index) {
              computeFactor();
            }

            public void modelRestored(Model model) {
              computeFactor();
            }
          });
    }

    setupRates();
  }
  /** 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());
  }