Added annotations to identifiers, and converted the unused annotationtest.py to
test the new module.
This commit is contained in:
parent
1863bfc564
commit
74cb30428c
|
@ -0,0 +1,60 @@
|
|||
|
||||
from fluents import dataset
|
||||
|
||||
_dim_annotation_handlers = {}
|
||||
|
||||
def get_dim_annotations(dimname, annotation, ids):
|
||||
"""Returns a list of annotations corresponding to the given ids in
|
||||
dimension dimname"""
|
||||
global _dim_annotation_handlers
|
||||
|
||||
if _dim_annotation_handlers.has_key(dimname):
|
||||
return _dim_annotation_handlers[dimname].get_annotations(annotation, ids)
|
||||
return None
|
||||
|
||||
def set_dim_handler(dimname, handler):
|
||||
"""Set the handler for the given dimension."""
|
||||
global _dim_annotation_handlers
|
||||
_dim_annotation_handlers[dimname] = handler
|
||||
|
||||
def get_dim_handler(dimname):
|
||||
"""Get the handler for the given dimension."""
|
||||
global _dim_annotation_handlers
|
||||
if _dim_annotation_handlers.has_key(dimname):
|
||||
return _dim_annotation_handlers[dimname]
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
class AnnotationHandler:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def get_annotations(self, annotationname, ids, default=None):
|
||||
return None
|
||||
|
||||
def get_annotation_names(self):
|
||||
return []
|
||||
|
||||
|
||||
class DictAnnotationHandler(AnnotationHandler):
|
||||
|
||||
def __init__(self, d={}):
|
||||
self._dict = d
|
||||
|
||||
def get_annotations(self, annotationname, ids, default=None):
|
||||
d = self._dict
|
||||
retval = []
|
||||
for id in ids:
|
||||
if d[annotationname].has_key(id):
|
||||
retval.append(d[annotationname][id])
|
||||
else:
|
||||
retval.append(default)
|
||||
return retval
|
||||
|
||||
def add_annotations(self, annotationname, d):
|
||||
self._dict[annotationname] = d
|
||||
|
||||
def get_annotation_names(self):
|
||||
return self._dict.keys()
|
||||
|
|
@ -1,32 +1,57 @@
|
|||
|
||||
import unittest
|
||||
import sys
|
||||
sys.path.append('../../system')
|
||||
from annotations import *
|
||||
from sets import Set as set
|
||||
sys.path.append('../../')
|
||||
from fluents import annotations
|
||||
|
||||
class AnnotationTest(unittest.TestCase):
|
||||
class AnnotationsTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def testCreation(self):
|
||||
a = Annotations('x', 'y')
|
||||
|
||||
assert a.has_dimension('x')
|
||||
assert a.has_dimension('y')
|
||||
assert not a.has_dimension('z')
|
||||
|
||||
def testAddAnnotations(self):
|
||||
ann = Annotations('genes', 'go')
|
||||
go = set(['GO:0', 'GO:1'])
|
||||
ann.add_annotations('genes', 'BadGene', 'go', go)
|
||||
genes = set(['BadGene'])
|
||||
h = annotations.DictAnnotationHandler()
|
||||
annotations.set_dim_handler('go-terms', h)
|
||||
assert annotations.get_dim_handler('go-terms') == h
|
||||
assert annotations.get_dim_handler('foobar') == None
|
||||
|
||||
assert ann.get_annotations('genes', 'BadGene', 'go') == go
|
||||
assert ann.get_annotations('go', 'GO:0', 'genes') == genes
|
||||
assert ann.get_annotations('go', 'GO:1', 'genes') == genes
|
||||
assert ann.get_annotations('go', 'GO:2', 'genes') == set()
|
||||
def testGetDimAnnotations(self):
|
||||
h = annotations.DictAnnotationHandler()
|
||||
annotations.set_dim_handler('go-terms', h)
|
||||
d = {'GO:0': 'biological_process',
|
||||
'GO:1': 'foo',
|
||||
'GO:2': 'bar'}
|
||||
h.add_annotations('name', d)
|
||||
|
||||
ann0 = annotations.get_dim_annotations('go-terms', 'name', [])
|
||||
assert ann0 == []
|
||||
|
||||
ann1 = annotations.get_dim_annotations('go-terms', 'name', ['GO:0'])
|
||||
assert len(ann1) == 1
|
||||
assert ann1[0] == 'biological_process'
|
||||
|
||||
ann2 = annotations.get_dim_annotations('go-terms', 'name', ['GO:1', 'baz'])
|
||||
assert len(ann2) == 2
|
||||
assert ann2 == ['foo', None]
|
||||
|
||||
def testDictAnnotationHandler(self):
|
||||
h = annotations.DictAnnotationHandler()
|
||||
annotations.set_dim_handler('go-terms', h)
|
||||
d = {'GO:0': 'biological_process',
|
||||
'GO:1': 'foo',
|
||||
'GO:2': 'bar'}
|
||||
h.add_annotations('name', d)
|
||||
|
||||
ann0 = h.get_annotations('name', [])
|
||||
assert ann0 == []
|
||||
|
||||
ann1 = h.get_annotations('name', ['unexisting'])
|
||||
assert ann1 == [None]
|
||||
|
||||
ann2 = h.get_annotations('name', ['unexisting'], 42)
|
||||
assert ann2 == [42]
|
||||
|
||||
assert h.get_annotation_names() == ['name']
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
Reference in New Issue