Skip to content

Commit

Permalink
first version of ecomap
Browse files Browse the repository at this point in the history
  • Loading branch information
cmungall committed Jun 6, 2017
1 parent 77d6103 commit a9fe0a9
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions ontobio/ecomap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
from contextlib import closing
from cachier import cachier
import datetime
import logging

SHELF_LIFE = datetime.timedelta(days=1)

def get_ecomap(url):
@cachier(stale_after=SHELF_LIFE)
def get_ecomap_str(url):
logging.info("Fetching ecomap from {}".format(url))
with closing(requests.get(url, stream=False)) as resp:
# TODO: redirects
if resp.status_code == 200:
return parse_ecomap_str(resp.text())
return resp.text

#E.g.
# IEA Default ECO:0000501
Expand All @@ -30,7 +33,8 @@ def __init__(self):

def mappings(self):
if self._mappings == None:
self._mappings = get_ecomap(PURL)
s = get_ecomap_str(self.PURL)
self._mappings = self.parse_ecomap_str(s)
return self._mappings

def parse_ecomap_str(self, str):
Expand All @@ -39,10 +43,13 @@ def parse_ecomap_str(self, str):
for line in lines:
if line.startswith('#'):
continue
if line == "":
continue
logging.info("LINE={}".format(line))
[code, ref, cls] = line.split("\t")
if ref == 'Default':
ref = None
tups.add( (code, ref, cls) )
tups.append( (code, ref, cls) )
return tups

def coderef_to_ecoclass(self, code, reference=None):
Expand All @@ -63,11 +70,11 @@ def coderef_to_ecoclass(self, code, reference=None):
ECO class CURIE/ID
"""
mcls = None
for (this_code,this_ref,cls) in self.mappings:
for (this_code,this_ref,cls) in self.mappings():
if this_code == code:
if this_reference == reference:
if this_ref == reference:
return cls
if this_reference is None:
if this_ref is None:
mcls = cls

return mcls
Expand All @@ -92,7 +99,7 @@ def ecoclass_to_coderef(self, cls):
"""
code = ''
ref = None
for (code,ref,this_cls) in self.mappings:
for (code,ref,this_cls) in self.mappings():
if cls == this_cls:
return code, ref
return None, None

0 comments on commit a9fe0a9

Please sign in to comment.