Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added async api function, handle updated m5nr api result, code clean #78

Merged
merged 1 commit into from May 3, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 15 additions & 39 deletions ipyMKMQ/ipyTools.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python


from time import localtime, strftime from time import localtime, strftime, sleep
from collections import defaultdict from collections import defaultdict
import os, sys, urllib, urllib2, json, pickle, copy, glob import os, sys, urllib, urllib2, json, pickle, copy, glob
import string, random import string, random
Expand Down Expand Up @@ -175,11 +175,24 @@ def obj_from_url(url, auth=None):
if obj is None: if obj is None:
sys.stderr.write("ERROR (%s): return structure not valid json format\n"%url) sys.stderr.write("ERROR (%s): return structure not valid json format\n"%url)
return None return None
if len(obj.keys()) == 0:
sys.stderr.write("ERROR (%s): no data available\n"%url)
return None
if 'ERROR' in obj: if 'ERROR' in obj:
sys.stderr.write("ERROR (%s): %s\n"%(url, obj['ERROR'])) sys.stderr.write("ERROR (%s): %s\n"%(url, obj['ERROR']))
return None return None
return obj return obj


def async_rest_api(url, auth=None, delay=30):
submit = obj_from_url(url, auth)
if not (('status' in submit) and (submit['status'] == 'Submitted') and ('url' in submit)):
sys.stderr.write("ERROR: return data invalid format\n:%s"%json.dumps(submit))
result = obj_from_url(submit['url'])
while result['status'] != 'done':
sleep(delay)
result = obj_from_url(submit['url'])
return result['data']

def slice_column(matrix, index): def slice_column(matrix, index):
data = [] data = []
for row in matrix: for row in matrix:
Expand Down Expand Up @@ -304,43 +317,6 @@ def random_str(size=8):
chars = string.ascii_letters + string.digits chars = string.ascii_letters + string.digits
return ''.join(random.choice(chars) for x in range(size)) return ''.join(random.choice(chars) for x in range(size))


"""
def sub_biom(b, text):
str_re = re.compile(text, re.IGNORECASE)
sBiom = { "generated_by": b['generated_by'],
"matrix_type": 'dense',
"date": strftime("%Y-%m-%dT%H:%M:%S", localtime()),
"data": [],
"rows": [],
"matrix_element_value": b['matrix_element_value'],
"matrix_element_type": b['matrix_element_type'],
"format_url": "http://biom-format.org",
"format": "Biological Observation Matrix 1.0",
"columns": b['columns'],
"id": b['id']+'_sub_'+text,
"type": b['type'],
"shape": [] }
hier = ''
if b['type'].startswith('Taxon'):
hier = 'taxonomy'
elif b['type'].startswith('Function'):
hier = 'ontology'
seen = set()
matrix = b['data'] if b['matrix_type'] == 'dense' else sparse_to_dense(b['data'], b['shape'][0], b['shape'][1])
for r, row in enumerate(b['rows']):
name = None
if row['metadata'] and hier and (hier in row['metadata']) and str_re.search(row['metadata'][hier][-1]):
name = row['metadata'][hier][-1]
elif str_re.search(row['id']):
name = row['id']
if (name is not None) and (name not in seen):
sBiom['data'].append(matrix[r])
sBiom['rows'].append(row)
seen.add(name)
sBiom['shape'] = [len(sBiom['rows']), b['shape'][1]]
return biom_remove_empty(sBiom)
"""

def merge_columns(b, merge_set): def merge_columns(b, merge_set):
"""input: 1. biom object, 2. merge_set -> { merge_name_1 : [list of col ids], merge_name_2 : [list of col ids], ... } """input: 1. biom object, 2. merge_set -> { merge_name_1 : [list of col ids], merge_name_2 : [list of col ids], ... }
return: biom object. same row count, column count = names in merge_set + column_ids not in merge_set""" return: biom object. same row count, column count = names in merge_set + column_ids not in merge_set"""
Expand Down Expand Up @@ -530,7 +506,7 @@ def get_hierarchy(htype='taxonomy', level='species', source='Subsystems', parent
params.append(('source', source)) params.append(('source', source))
if parent is not None: if parent is not None:
params.append(('parent_name', parent)) params.append(('parent_name', parent))
child = obj_from_url(Ipy.API_URL+'/m5nr/'+htype+'?'+urllib.urlencode(params, True)) child = obj_from_url(Ipy.API_URL+'/m5nr/'+htype+'?'+urllib.urlencode(params, True))['data']
if not child: if not child:
child = [] child = []
return child return child
Expand Down