예제 #1
0
  /**
   * Given a map of name translations (string to string), rewrites all leaf ids that match a key in
   * the map to the respective value in the matching key/value pair. If current leaf id is null,
   * then interpret translation keys as node numbers (origin 1) and set leaf id of node n to
   * map.get(n-1).
   *
   * @param translationMap map of name translations
   */
  public void translateLeafIds(final Map<String, String> translationMap) {

    for (final Node leaf : getExternalNodes()) {
      String id = leaf.getID();

      if (id == null || !integerLeafLabels) {
        id = Integer.toString(leaf.getNr() + 1);
      }

      final String newId = translationMap.get(id);
      if (newId != null) {
        leaf.setID(newId);
      }
    }
  }
예제 #2
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;
    }