@Test
  public void testOperator2() throws Exception {

    Tree tree;
    int taxaSize = 3;

    // make a caterpillar
    Node left = new ZeroBranchSANode();
    left.setNr(0);
    left.setHeight(0.0);
    for (int i = 1; i < taxaSize; i++) {
      Node right = new ZeroBranchSANode();
      right.setNr(i);
      right.setHeight(i);
      Node parent = new ZeroBranchSANode();
      parent.setNr(taxaSize + i - 1);
      parent.setHeight(i);
      left.setParent(parent);
      parent.setLeft(left);
      right.setParent(parent);
      parent.setRight(right);
      left = parent;
    }
    left.setHeight(left.getRight().getHeight() + 2);
    tree = new Tree(left);

    System.out.println("Tree was = " + tree.getRoot().toShortNewick(false));

    TreeDimensionJump operator = new TreeDimensionJump();
    operator.initByName("tree", tree);
    double logHastingsRatio = operator.proposal();

    System.out.println("Proposed tree = " + tree.getRoot().toShortNewick(false));
    System.out.println("Log Hastings ratio = " + logHastingsRatio);
  }
Exemplo n.º 2
0
    /**
     * Number any nodes in a clade which were not explicitly numbered by the parsed string.
     *
     * @param node clade parent
     */
    private void numberUnnumberedNodes(Node node) {
      if (node.isLeaf()) return;

      for (Node child : node.getChildren()) {
        numberUnnumberedNodes(child);
      }

      if (node.getNr() < 0) node.setNr(numberedNodeCount);

      numberedNodeCount += 1;
    }
Exemplo n.º 3
0
    @Override
    public Node visitNode(NewickParser.NodeContext ctx) {
      Node node = newNode();

      for (NewickParser.NodeContext ctxChild : ctx.node()) {
        node.addChild(visit(ctxChild));
      }

      NewickParser.PostContext postCtx = ctx.post();

      // Process metadata

      if (postCtx.meta() != null) {

        node.metaDataString = "";
        for (int i = 0; i < postCtx.meta().attrib().size(); i++) {
          if (i > 0) node.metaDataString += ",";
          node.metaDataString += postCtx.meta().attrib().get(i).getText();
        }

        if (!suppressMetadata) {
          for (NewickParser.AttribContext attribctx : postCtx.meta().attrib()) {
            String key = attribctx.attribKey.getText();

            if (attribctx.attribValue().number() != null) {
              node.setMetaData(key, Double.parseDouble(attribctx.attribValue().number().getText()));
            } else if (attribctx.attribValue().STRING() != null) {
              String stringValue = attribctx.attribValue().STRING().getText();
              if (stringValue.startsWith("\"") || stringValue.startsWith("\'")) ;
              stringValue = stringValue.substring(1, stringValue.length() - 1);
              node.setMetaData(key, stringValue);
            } else {
              // BEAST doesn't do anything with vectors yet.
            }
          }
        }
      }

      // Process edge length

      if (postCtx.length != null) node.setHeight(Double.parseDouble(postCtx.length.getText()));
      else node.setHeight(DEFAULT_LENGTH);

      // Process label

      node.setNr(-1);
      if (postCtx.label() != null) {
        node.setID(postCtx.label().getText());

        if (postCtx.label().number() == null || postCtx.label().number().INT() == null)
          integerLeafLabels = false;

        // RRB: next line is for debugging only?
        @SuppressWarnings("unused")
        String postText = postCtx.getText();

        // Treat labels as node numbers in certain situations
        if (!isLabelledNewickInput.get()
            && postCtx.label().number() != null
            && postCtx.label().number().INT() != null) {

          int nodeNr = Integer.parseInt(postCtx.label().getText()) - offsetInput.get();
          if (nodeNr < 0)
            throw new ParseCancellationException(
                "Node number given "
                    + "is smaller than current offset ("
                    + offsetInput.get()
                    + ").  Perhaps offset is "
                    + "too high?");

          node.setNr(nodeNr);
          numberedNodeCount += 1;
        } else {
          if (node.isLeaf()) {
            node.setNr(getLabelIndex(postCtx.label().getText()));
            numberedNodeCount += 1;
          }
        }
      }

      if (node.getChildCount() == 1 && !allowSingleChildInput.get())
        throw new ParseCancellationException("Node with single child found.");

      if (node.getChildCount() > 2)
        throw new ParseCancellationException("Node with two or more children found.");

      return node;
    }