Projects/laydi
Projects
/
laydi
Archived
7
0
Fork 0

* annotations.py: The Annotations class now work. Querying along any

axis is supported, but only two dimensional Annotations can be created.
This commit is contained in:
Einar Ryeng 2006-04-16 23:30:01 +00:00
parent 23fb12f53a
commit 4bdeef05f6
2 changed files with 59 additions and 14 deletions

View File

@ -1,33 +1,65 @@
import logger
from sets import Set as set
set.update = set.union_update
class AnnotationsException(Exception):
pass
class Annotations: class Annotations:
def __init__(self, *dimensions): def __init__(self, *dimensions):
""" Initializes a new Annotation with the given dimension labels. """ Initializes a new Annotation with the given dimension labels.
dimensions is a list of dimension labels. dimensions is a list of dimension labels.
""" """
if len(dimensions) == 2: if len(dimensions) != 2:
logger.log('error', 'Annotations only supports two dimensions.') msg = 'Annotations only supports two dimensions.'
raise AnnotationsException(msg)
self.dimensions = {} self.dimensions = {}
for d in dimensions: for d in dimensions:
self.dimensions[d] = {} self.dimensions[d] = {}
def add_annotations(self, dim, id, ann_dim, annotations): def add_annotations(self, dim, id, ann_dim, annotations):
if not self.dimensions[dim]: """ Adds new annotations.
logger.log('warning', 'Annotations object does not contain dimension %s' % dim) dim: the dimension in which the new data should be added.
return None id: the identifier that should be annotated.
ann_dim: the dimension of the annotations to id.
annotations: the new annotations to id.
Returns the total set of annotations to id.
"""
if not self.has_dimension(dim):
msg = 'Annotations object does not contain dimension %s' % dim
raise AnnotationsException(msg)
if not self.has_dimension(ann_dim):
msg = 'Annotations object does not contain dimension %s' % ann_dim
raise AnnotationsException(msg)
for a in annotations: for a in annotations:
self.annotations[ann_dim][a].add(id) if not self.dimensions[ann_dim].has_key(a):
self.annotations[dim][id] = annotations self.dimensions[ann_dim][a] = set()
self.dimensions[ann_dim][a].add(id)
if not self.dimensions[dim].has_key(id):
self.dimensions[dim][id] = set()
self.dimensions[dim][id].update(annotations)
return self.dimensions[dim][id]
def get_annotations(self, dim, id): def get_annotations(self, dim, id, ann_dim):
if not self.dimensions[dim]: """Returns all annotations to id.
logger.log('warning', 'Annotations object does not contain dimension %s' % dim) dim: the dimension where id can be found.
return None id: the id to retrieve annotations for.
"""
if not self.has_dimension(dim):
msg = 'Annotations object does not contain dimension %s' % dim
raise AnnotationsException(msg)
if self.dimensions[dim].has_key(id): if self.dimensions[dim].has_key(id):
return self.dimensions[dim][id].values() return self.dimensions[dim][id]
return None return set()
def has_dimension(self, dim): def has_dimension(self, dim):
""" Retuns true if the Annotations object indexes dim.
"""
return self.dimensions.has_key(dim) return self.dimensions.has_key(dim)

View File

@ -3,6 +3,7 @@ import unittest
import sys import sys
sys.path.append('../../system') sys.path.append('../../system')
from annotations import * from annotations import *
from sets import Set as set
class AnnotationTest(unittest.TestCase): class AnnotationTest(unittest.TestCase):
@ -11,9 +12,21 @@ class AnnotationTest(unittest.TestCase):
def testCreation(self): def testCreation(self):
a = Annotations('x', 'y') a = Annotations('x', 'y')
assert a.has_dimension('x') assert a.has_dimension('x')
assert a.has_dimension('y') assert a.has_dimension('y')
assert not a.has_dimension('z') 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'])
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()
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()