{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Installing ete2 is not entirely straightforward, but it is described at http://etetoolkit.org/download/. \n",
"This tutorial requires installing th PyQT4 dependency for the tree vis steps. \n",
"Installing Peyotl is straightforward and is described here http://opentreeoflife.github.io/peyotl/installation/"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import peyotl\n",
"from ete2 import Tree, TreeStyle"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"These functions use peyotl wrappers around OpenTree API calls, described at https://github.com/OpenTreeOfLife/opentree/wiki/Open-Tree-of-Life-APIs"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def ot_find_tree(pair, exact=True, verbose=False, oti_wrapper=None): \n",
" '''Uses a peyotl wrapper around an Open Tree web service to get a list of trees including \n",
" values `value` for a given property to be searched on `property`.\n",
" The oti_wrapper can be None (in which case the default wrapper from peyotl.sugar will be used.\n",
" All other arguments correspond to the arguments of the web-service call.\n",
" '''\n",
" if oti_wrapper is None:\n",
" from peyotl.sugar import oti\n",
" oti_wrapper = oti\n",
" match_obj = oti_wrapper.find_trees(pair, exact=exact, verbose=verbose)\n",
" return match_obj"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def ot_get_tree(study_id, tree_id,**kwargs):\n",
" from peyotl.api import APIWrapper\n",
" api_wrapper = APIWrapper()\n",
" if 'format' in kwargs:\n",
" match_obj = api_wrapper.study.get(study_id,tree=tree_id,format=kwargs['format'])\n",
" else:\n",
" match_obj = api_wrapper.study.get(study_id,tree=tree_id)\n",
" return match_obj"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Changing the values here can allow you select trees with any values in trees' searchable properties. \n",
"Properties include: \"is_deprecated\", \"ot:treebaseTreeId\", \"ot:branchLengthMode\", \"ot:comment\", \"ot:branchLengthDescription\", \"ot:nodeLabelMode\", \"ot:originalLabel\", \"ot:studyId\", \"ot:ottId\", \"ot:treeModified\", \"ot:treebaseOTUId\", \"ot:tag\", \"ot:treeLastEdited\", \"ot:inferenceMethod\", \"ot:ottTaxonName\", \"ot:branchLengthTimeUnits\", \"ot:nodeLabelDescription\", \"oti_tree_id\" "
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"arg_dict = {\"ot:ottTaxonName\": \"Apis mellifera\"}"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"tree_list = ot_find_tree(arg_dict)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for study in tree_list:\n",
" for tree in study['matched_trees']:\n",
" study_id=study['ot:studyId']\n",
" for tree in study['matched_trees']:\n",
" tnew=ot_get_tree(study_id, tree['oti_tree_id'],format='newick')\n",
" t = Tree(tnew, format=1)\n",
" if len(t) < 100:#this is just an aribitrary way to grab a tree isn't too hard to visualize\n",
" break"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using ETE you can visualize the trees pulled down from the OpenTree database in an ipython notebook!"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"circular_style = TreeStyle()\n",
"circular_style.mode = \"c\" # draw tree in circular mode\n",
"circular_style.scale = 30\n",
"t.render(\"%%inline\", units=\"mm\", tree_style=circular_style)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 7,
"svg": [
""
],
"text": [
""
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"ts = TreeStyle()\n",
"ts.show_leaf_name = True\n",
"ts.mode = \"c\"\n",
"ts.arc_start = -180 # 0 degrees = 3 o'clock\n",
"ts.arc_span = 180\n",
"ts.scale = 40\n",
"t.render(\"%%inline\", units='mm', tree_style=ts)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"svg": [
""
],
"text": [
""
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}