Skip to content

Instantly share code, notes, and snippets.

@grabear
Forked from jhcepas/Newick2JSON.py
Created April 21, 2017 19:21
Show Gist options
  • Save grabear/9e44bbd571ee44623fb1886e5c4195f4 to your computer and use it in GitHub Desktop.
Save grabear/9e44bbd571ee44623fb1886e5c4195f4 to your computer and use it in GitHub Desktop.

Revisions

  1. @jhcepas jhcepas renamed this gist Jun 23, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @jhcepas jhcepas created this gist Feb 25, 2014.
    43 changes: 43 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    import sys
    from ete2 import Tree
    import random

    def get_json(node):
    # Read ETE tag for duplication or speciation events
    if not hasattr(node, 'evoltype'):
    dup = random.sample(['N','Y'], 1)[0]
    elif node.evoltype == "S":
    dup = "N"
    elif node.evoltype == "D":
    dup = "Y"

    node.name = node.name.replace("'", '')

    json = { "name": node.name,
    "display_label": node.name,
    "duplication": dup,
    "branch_length": str(node.dist),
    "common_name": node.name,
    "seq_length": 0,
    "type": "node" if node.children else "leaf",
    "uniprot_name": "Unknown",
    }
    if node.children:
    json["children"] = []
    for ch in node.children:
    json["children"].append(get_json(ch))
    return json


    if __name__ == '__main__':
    if len(sys.argv) > 1:
    t = Tree(sys.argv[1])

    else:
    # create a random example tree
    t = Tree()

    t.populate(100, random_branches=True)

    # TreeWidget seems to fail with simple quotes
    print str(get_json(t)).replace("'", '"')