Created
June 4, 2014 05:08
-
-
Save cfriedline/0e275d528ff1a8d674c6 to your computer and use it in GitHub Desktop.
testing multiprocessing and rpy2 (ape)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:04f733fe38a12e7bbe8ee8d3f2900d3fecf0ae7a83ba728cd84650110e26b216" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import os\n", | |
"os.environ['R_HOME'] = '/home/cfriedline/lib64/R'\n", | |
"import rpy2.robjects as robjects\n", | |
"import random\n", | |
"import string\n", | |
"import tempfile\n", | |
"import dendropy\n", | |
"from multiprocessing import Pool" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 84 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"r = robjects.r\n", | |
"ape = r('library(ape)')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 85 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def create_tree(num_tips, type):\n", | |
" \"\"\"\n", | |
" creates the taxa tree in R\n", | |
" @param num_tips: number of taxa to create\n", | |
" @param type: type for naming (e.g., 'taxa')\n", | |
" @return: a dendropy Tree\n", | |
" @rtype: dendropy.Tree\n", | |
" \"\"\"\n", | |
" r = robjects.r\n", | |
" robjects.globalenv['numtips'] = num_tips\n", | |
" robjects.globalenv['treetype'] = type\n", | |
" name = _get_random_string(20)\n", | |
" if type == \"T\":\n", | |
" r(\"%s = rtree(numtips, rooted=T, tip.label=paste(treetype, seq(1:(numtips)), sep=''))\" % name)\n", | |
" else:\n", | |
" r(\"%s = rtree(numtips, rooted=F, tip.label=paste(treetype, seq(1:(numtips)), sep=''))\" % name)\n", | |
" tree = r[name]\n", | |
" return ape_to_dendropy(tree)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 86 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def ape_to_dendropy(phylo):\n", | |
" \"\"\"\n", | |
" converts an ape tree to dendropy tree\n", | |
" @param phylo: ape instance from rpy2\n", | |
" @return: a dendropy tree\n", | |
" @rtype: dendropy.Tree\n", | |
" \"\"\"\n", | |
" f = tempfile.NamedTemporaryFile()\n", | |
" robjects.r['write.nexus'](phylo, file=f.name)\n", | |
" tree = dendropy.Tree.get_from_path(f.name, \"nexus\")\n", | |
" f.close()\n", | |
" return tree" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 87 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def _get_random_string(length):\n", | |
" \"\"\"\n", | |
" gets a random string of letters/numbers, ensuring that it does not start with a\n", | |
" number\n", | |
" @param length: length of the string\n", | |
" @return: the random string\n", | |
" @rtype: string\n", | |
" \"\"\"\n", | |
" s = ''.join(random.choice(string.letters + string.digits) for i in xrange(length))\n", | |
" if not s[0] in string.letters:\n", | |
" return _get_random_string(length)\n", | |
" return s" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 88 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"tree = create_tree(100, \"T\")\n", | |
"tree.as_newick_string()[0:80]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 89, | |
"text": [ | |
"'((((((((T82:0.6231970771,(T94:0.4972914483,T65:0.1885578725):0.3080936125):0.260'" | |
] | |
} | |
], | |
"prompt_number": 89 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def create_tree_mp(num_taxa):\n", | |
" t = create_tree(num_taxa, \"T\")\n", | |
" return t\n", | |
"\n", | |
"def get_taxa_trees(num_trees, num_taxa):\n", | |
" jobs_mp = []\n", | |
" jobs = []\n", | |
" res = []\n", | |
" pool = Pool(num_trees)\n", | |
" for i in xrange(num_trees):\n", | |
" jobs_mp.append(pool.apply_async(create_tree_mp, (num_taxa,)))\n", | |
" jobs.append(create_tree_mp(num_taxa))\n", | |
" pool.close()\n", | |
" pool.join()\n", | |
" res.append(jobs)\n", | |
" res.append([x.get() for x in jobs_mp])\n", | |
" return res" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 90 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"trees = get_taxa_trees(5, 10)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 91 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"for t in trees[0]:\n", | |
" print t.as_newick_string()[0:80]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"(((T3:0.2269473344,((T2:0.1200612716,T5:0.0573881655):0.9391158754,T9:0.94033852\n", | |
"((T9:0.7055083751,(((T1:0.8150142939,T7:0.2128749883):0.5196571311,T4:0.07766074\n", | |
"((T6:0.8410235606,(T5:0.01358497259,T2:0.4641908032):0.8032400277):0.6838729084,\n", | |
"(((T2:0.8350919618,T4:0.3222864345):0.7693860317,T6:0.08913701377):0.1574694319,\n", | |
"((((T9:0.8485502249,T2:0.6824201567):0.5791750827,(T8:0.4604402625,T5:0.14715224\n" | |
] | |
} | |
], | |
"prompt_number": 92 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"for t in trees[1]:\n", | |
" print t.as_newick_string()[0:80]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"(((T3:0.2269473344,((T2:0.1200612716,T5:0.0573881655):0.9391158754,T9:0.94033852\n", | |
"(((T3:0.2269473344,((T2:0.1200612716,T5:0.0573881655):0.9391158754,T9:0.94033852\n", | |
"(((T3:0.2269473344,((T2:0.1200612716,T5:0.0573881655):0.9391158754,T9:0.94033852\n", | |
"(((T3:0.2269473344,((T2:0.1200612716,T5:0.0573881655):0.9391158754,T9:0.94033852\n", | |
"(((T3:0.2269473344,((T2:0.1200612716,T5:0.0573881655):0.9391158754,T9:0.94033852\n" | |
] | |
} | |
], | |
"prompt_number": 93 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment