<s> from __future__ import generators <EOL> from sequence import * <EOL> import classutil <EOL> import UserDict <EOL> import weakref <EOL> def getAnnotationAttr ( self , attr ) : <EOL> '<STR_LIT>' <EOL> return self . db . getSliceAttr ( self . db . sliceDB [ self . id ] , attr ) <EOL> def annotation_repr ( self ) : <EOL> if self . annotationType is not None : <EOL> title = self . annotationType <EOL> else : <EOL> title = '<STR_LIT>' <EOL> if self . orientation > <NUM_LIT:0> : <EOL> return '<STR_LIT>' % ( title , self . id , self . start , self . stop ) <EOL> else : <EOL> return '<STR_LIT>' % ( title , self . id , - self . stop , - self . start ) <EOL> class AnnotationSeqDescr ( object ) : <EOL> '<STR_LIT>' <EOL> def __get__ ( self , obj , objtype ) : <EOL> return absoluteSlice ( obj . _anno_seq , obj . _anno_start , <EOL> obj . _anno_start + obj . stop ) <EOL> class AnnotationSliceDescr ( object ) : <EOL> '<STR_LIT>' <EOL> def __get__ ( self , obj , objtype ) : <EOL> return relativeSlice ( obj . pathForward . sequence , obj . start , obj . stop ) <EOL> class AnnotationSeqtypeDescr ( object ) : <EOL> '<STR_LIT>' <EOL> def __get__ ( self , obj , objtype ) : <EOL> return obj . _anno_seq . seqtype ( ) <EOL> class AnnotationSeq ( SeqPath ) : <EOL> '<STR_LIT>' <EOL> start = <NUM_LIT:0> <EOL> step = <NUM_LIT:1> <EOL> orientation = <NUM_LIT:1> <EOL> def __init__ ( self , id , db , parent , start , stop ) : <EOL> self . id = id <EOL> self . db = db <EOL> self . stop = stop - start <EOL> self . _anno_seq = parent <EOL> self . _anno_start = start <EOL> self . path = self <EOL> __getattr__ = getAnnotationAttr <EOL> sequence = AnnotationSeqDescr ( ) <EOL> annotationType = classutil . DBAttributeDescr ( '<STR_LIT>' ) <EOL> _seqtype = AnnotationSeqtypeDescr ( ) <EOL> __repr__ = annotation_repr <EOL> def __cmp__ ( self , other ) : <EOL> if not isinstance ( other , AnnotationSeq ) : <EOL> return - <NUM_LIT:1> <EOL> if cmp ( self . sequence , other . sequence ) == <NUM_LIT:0> : <EOL> if self . id == other . id and self . db is other . db : <EOL> return cmp ( ( self . start , self . stop ) , ( other . start , other . stop ) ) <EOL> return NOT_ON_SAME_PATH <EOL> def strslice ( self , start , stop ) : <EOL> raise ValueError ( '''<STR_LIT>''' ) <EOL> class AnnotationSlice ( SeqDBSlice ) : <EOL> '<STR_LIT>' <EOL> __getattr__ = getAnnotationAttr <EOL> sequence = AnnotationSliceDescr ( ) <EOL> annotationType = classutil . DBAttributeDescr ( '<STR_LIT>' ) <EOL> __repr__ = annotation_repr <EOL> class TranslationAnnotSeqDescr ( object ) : <EOL> '<STR_LIT>' <EOL> def __get__ ( self , obj , objtype ) : <EOL> return absoluteSlice ( obj . _anno_seq , obj . _anno_start , obj . _anno_stop ) <EOL> class TranslationAnnotFrameDescr ( object ) : <EOL> """<STR_LIT>""" <EOL> def __get__ ( self , obj , objtype ) : <EOL> orig = obj . pathForward . sequence <EOL> if orig . orientation > <NUM_LIT:0> : <EOL> frame = ( orig . start % <NUM_LIT:3> ) + <NUM_LIT:1> <EOL> else : <EOL> return - ( ( orig . start + <NUM_LIT:1> ) % <NUM_LIT:3> + <NUM_LIT:1> ) <EOL> return frame <EOL> class TranslationAnnot ( AnnotationSeq ) : <EOL> '<STR_LIT>' <EOL> def __init__ ( self , id , db , parent , start , stop ) : <EOL> AnnotationSeq . __init__ ( self , id , db , parent , start , stop ) <EOL> self . stop /= <NUM_LIT:3> <EOL> self . _anno_stop = stop <EOL> sequence = TranslationAnnotSeqDescr ( ) <EOL> frame = TranslationAnnotFrameDescr ( ) <EOL> _seqtype = PROTEIN_SEQTYPE <EOL> def strslice ( self , start , stop ) : <EOL> '<STR_LIT>' <EOL> try : <EOL> aa = self . _translation <EOL> except AttributeError : <EOL> aa = self . _translation = translate_orf ( str ( self . sequence ) ) <EOL> return aa [ start : stop ] <EOL> class TranslationAnnotSliceDescr ( object ) : <EOL> '<STR_LIT>' <EOL> def __get__ ( self , obj , objtype ) : <EOL> return relativeSlice ( obj . pathForward . sequence , <NUM_LIT:3> * obj . start , <EOL> <NUM_LIT:3> * obj . stop ) <EOL> class TranslationAnnotSlice ( AnnotationSlice ) : <EOL> sequence = TranslationAnnotSliceDescr ( ) <EOL> frame = TranslationAnnotFrameDescr ( ) <EOL> class AnnotationDB ( object , UserDict . DictMixin ) : <EOL> '<STR_LIT>' <EOL> def __init__ ( self , sliceDB , seqDB , annotationType = None , <EOL> itemClass = AnnotationSeq , <EOL> itemSliceClass = AnnotationSlice , <EOL> itemAttrDict = None , <EOL> sliceAttrDict = None , maxCache = None , autoGC = True , <EOL> checkFirstID = True , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> if autoGC : <EOL> self . _weakValueDict = classutil . RecentValueDictionary ( autoGC ) <EOL> else : <EOL> self . _weakValueDict = { } <EOL> self . autoGC = autoGC <EOL> if sliceAttrDict is None : <EOL> sliceAttrDict = { } <EOL> if sliceDB is not None : <EOL> self . sliceDB = sliceDB <EOL> else : <EOL> self . sliceDB = classutil . get_shelve_or_dict ( ** kwargs ) <EOL> self . seqDB = seqDB <EOL> self . annotationType = annotationType <EOL> self . itemClass = itemClass <EOL> self . itemSliceClass = itemSliceClass <EOL> self . sliceAttrDict = sliceAttrDict <EOL> if maxCache is not None : <EOL> self . maxCache = maxCache <EOL> if checkFirstID : <EOL> try : <EOL> k = iter ( self ) . next ( ) <EOL> self . get_annot_obj ( k , self . sliceDB [ k ] ) <EOL> except KeyError : <EOL> raise KeyError ( '''<STR_LIT>''' <EOL> % ( k , repr ( seqDB ) , ) ) <EOL> except StopIteration : <EOL> pass <EOL> __getstate__ = classutil . standard_getstate <EOL> __setstate__ = classutil . standard_setstate <EOL> _pickleAttrs = dict ( sliceDB = <NUM_LIT:0> , seqDB = <NUM_LIT:0> , annotationType = <NUM_LIT:0> , autoGC = <NUM_LIT:0> , <EOL> itemClass = <NUM_LIT:0> , itemSliceClass = <NUM_LIT:0> , sliceAttrDict = <NUM_LIT:0> , <EOL> maxCache = <NUM_LIT:0> ) <EOL> def __hash__ ( self ) : <EOL> '<STR_LIT>' <EOL> return id ( self ) <EOL> def __getitem__ ( self , k ) : <EOL> '<STR_LIT>' <EOL> try : <EOL> return self . _weakValueDict [ k ] <EOL> except KeyError : <EOL> pass <EOL> return self . sliceAnnotation ( k , self . sliceDB [ k ] ) <EOL> def __setitem__ ( self , k , v ) : <EOL> raise KeyError ( '''<STR_LIT>''' ) <EOL> def getSliceAttr ( self , sliceInfo , attr ) : <EOL> try : <EOL> k = self . sliceAttrDict [ attr ] <EOL> except KeyError : <EOL> return getattr ( sliceInfo , attr ) <EOL> try : <EOL> return getattr ( sliceInfo , k ) <EOL> except TypeError : <EOL> return sliceInfo [ k ] <EOL> def get_annot_obj ( self , k , sliceInfo ) : <EOL> '<STR_LIT>' <EOL> start = int ( self . getSliceAttr ( sliceInfo , '<STR_LIT:start>' ) ) <EOL> stop = int ( self . getSliceAttr ( sliceInfo , '<STR_LIT>' ) ) <EOL> try : <EOL> orientation = self . getSliceAttr ( sliceInfo , '<STR_LIT>' ) <EOL> orientation = int ( orientation ) <EOL> if orientation < <NUM_LIT:0> and start >= <NUM_LIT:0> : <EOL> start , stop = ( - stop , - start ) <EOL> except ( AttributeError , IndexError ) : <EOL> pass <EOL> if start >= stop : <EOL> raise IndexError ( '<STR_LIT>' % ( k , start , stop ) ) <EOL> seq_id = self . getSliceAttr ( sliceInfo , '<STR_LIT:id>' ) <EOL> seq = self . seqDB [ seq_id ] <EOL> return self . itemClass ( k , self , seq , start , stop ) <EOL> def sliceAnnotation ( self , k , sliceInfo , limitCache = True ) : <EOL> '<STR_LIT>' <EOL> a = self . get_annot_obj ( k , sliceInfo ) <EOL> try : <EOL> if limitCache and self . maxCache < len ( self . _weakValueDict ) : <EOL> self . _weakValueDict . clear ( ) <EOL> except AttributeError : <EOL> pass <EOL> self . _weakValueDict [ k ] = a <EOL> return a <EOL> def new_annotation ( self , k , sliceInfo ) : <EOL> '<STR_LIT>' <EOL> a = self . sliceAnnotation ( k , sliceInfo ) <EOL> try : <EOL> self . sliceDB [ k ] = sliceInfo <EOL> except : <EOL> try : <EOL> del self . _weakValueDict [ k ] <EOL> except : <EOL> pass <EOL> raise <EOL> self . _wroteSliceDB = True <EOL> return a <EOL> def foreignKey ( self , attr , k ) : <EOL> '<STR_LIT>' <EOL> for t in self . sliceDB . foreignKey ( attr , k ) : <EOL> try : <EOL> yield self . _weakValueDict [ t . id ] <EOL> except KeyError : <EOL> yield self . sliceAnnotation ( t . id , t ) <EOL> def __contains__ ( self , k ) : <EOL> return k in self . sliceDB <EOL> def __len__ ( self ) : <EOL> return len ( self . sliceDB ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . sliceDB ) <EOL> def keys ( self ) : <EOL> return self . sliceDB . keys ( ) <EOL> def iteritems ( self ) : <EOL> '<STR_LIT>' <EOL> for k , sliceInfo in self . sliceDB . iteritems ( ) : <EOL> yield k , self . sliceAnnotation ( k , sliceInfo ) <EOL> def itervalues ( self ) : <EOL> '<STR_LIT>' <EOL> for k , v in self . iteritems ( ) : <EOL> yield v <EOL> def items ( self ) : <EOL> '<STR_LIT>' <EOL> return [ ( k , self . sliceAnnotation ( k , sliceInfo , limitCache = False ) ) <EOL> for ( k , sliceInfo ) in self . sliceDB . items ( ) ] <EOL> def values ( self ) : <EOL> '<STR_LIT>' <EOL> return [ self . sliceAnnotation ( k , sliceInfo , limitCache = False ) <EOL> for ( k , sliceInfo ) in self . sliceDB . items ( ) ] <EOL> def add_homology ( self , seq , search , id = None , idFormat = '<STR_LIT>' , <EOL> autoIncrement = False , maxAnnot = <NUM_LIT> , <EOL> maxLoss = None , sliceInfo = None , ** kwargs ) : <EOL> '<STR_LIT>' <EOL> try : <EOL> if self . sliceAttrDict [ '<STR_LIT:id>' ] != <NUM_LIT:0> : <EOL> raise KeyError <EOL> except KeyError : <EOL> sliceAttrDict [ '<STR_LIT:id>' ] = <NUM_LIT:0> <EOL> sliceAttrDict [ '<STR_LIT:start>' ] = <NUM_LIT:1> <EOL> sliceAttrDict [ '<STR_LIT>' ] = <NUM_LIT:2> <EOL> if autoIncrement : <EOL> id = len ( self . sliceDB ) <EOL> elif id is None : <EOL> id = seq . id <EOL> if isinstance ( search , str ) : <EOL> search = getattr ( self . seqDB , search ) <EOL> if isinstance ( seq , str ) : <EOL> seq = Sequence ( seq , str ( id ) ) <EOL> al = search ( seq , ** kwargs ) <EOL> if maxLoss is not None : <EOL> kwargs [ '<STR_LIT>' ] = len ( seq ) - maxLoss <EOL> hits = al [ seq ] . keys ( ** kwargs ) <EOL> if len ( hits ) > maxAnnot : <EOL> raise ValueError ( '<STR_LIT>' % ( id , len ( hits ) ) ) <EOL> out = [ ] <EOL> i = <NUM_LIT:0> <EOL> k = id <EOL> for ival in hits : <EOL> if len ( hits ) > <NUM_LIT:1> : <EOL> if autoIncrement : <EOL> k = len ( self . sliceDB ) <EOL> else : <EOL> k = idFormat % ( id , i ) <EOL> i += <NUM_LIT:1> <EOL> if sliceInfo is not None : <EOL> a = self . new_annotation ( k , ( ival . id , ival . start , ival . stop ) <EOL> + sliceInfo ) <EOL> else : <EOL> a = self . new_annotation ( k , ( ival . id , ival . start , ival . stop ) ) <EOL> out . append ( a ) <EOL> return out <EOL> def close ( self ) : <EOL> '<STR_LIT>' <EOL> try : <EOL> if self . _wroteSliceDB : <EOL> self . sliceDB . close ( ) <EOL> self . _wroteSliceDB = False <EOL> return True <EOL> except AttributeError : <EOL> pass <EOL> return False <EOL> def __del__ ( self ) : <EOL> if self . close ( ) : <EOL> import sys <EOL> print >> sys . stderr , '''<STR_LIT>''' <EOL> def clear_cache ( self ) : <EOL> '<STR_LIT>' <EOL> self . _weakValueDict . clear ( ) <EOL> def copy ( self ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def setdefault ( self , k , d = None ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def update ( self , other ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def clear ( self ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def pop ( self ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def popitem ( self ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> class AnnotationServer ( AnnotationDB ) : <EOL> '<STR_LIT>' <EOL> xmlrpc_methods = { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:0> } <EOL> def get_slice_tuple ( self , k ) : <EOL> '<STR_LIT>' <EOL> try : <EOL> sliceInfo = self . sliceDB [ k ] <EOL> except KeyError : <EOL> return '<STR_LIT>' <EOL> start = int ( self . getSliceAttr ( sliceInfo , '<STR_LIT:start>' ) ) <EOL> stop = int ( self . getSliceAttr ( sliceInfo , '<STR_LIT>' ) ) <EOL> try : <EOL> if int ( self . getSliceAttr ( sliceInfo , '<STR_LIT>' ) ) < <NUM_LIT:0> and start >= <NUM_LIT:0> : <EOL> start , stop = ( - stop , - start ) <EOL> except AttributeError : <EOL> pass <EOL> return ( self . getSliceAttr ( sliceInfo , '<STR_LIT:id>' ) , start , stop ) <EOL> def get_slice_items ( self ) : <EOL> '<STR_LIT>' <EOL> l = [ ] <EOL> for k in self . sliceDB : <EOL> l . append ( ( k , self . get_slice_tuple ( k ) ) ) <EOL> return l <EOL> def get_annotation_attr ( self , k , attr ) : <EOL> '<STR_LIT>' <EOL> try : <EOL> sliceInfo = self . sliceDB [ k ] <EOL> except KeyError : <EOL> return '<STR_LIT>' <EOL> try : <EOL> return self . getSliceAttr ( sliceInfo , attr ) <EOL> except AttributeError : <EOL> return '<STR_LIT>' <EOL> class AnnotationClientSliceDB ( dict ) : <EOL> '<STR_LIT>' <EOL> def __init__ ( self , db ) : <EOL> self . db = db <EOL> dict . __init__ ( self ) <EOL> def __getitem__ ( self , k ) : <EOL> try : <EOL> return dict . __getitem__ ( self , k ) <EOL> except KeyError : <EOL> t = self . db . server . get_slice_tuple ( k ) <EOL> if t == '<STR_LIT>' : <EOL> raise KeyError ( '<STR_LIT>' + str ( k ) ) <EOL> dict . __setitem__ ( self , k , t ) <EOL> return t <EOL> def __setitem__ ( self , k , v ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> def keys ( self ) : <EOL> return self . db . server . keys ( ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . keys ( ) ) <EOL> def items ( self ) : <EOL> return self . db . server . get_slice_items ( ) <EOL> def iteritems ( self ) : <EOL> return iter ( self . items ( ) ) <EOL> def __len__ ( self ) : <EOL> return self . db . server . __len__ ( ) <EOL> def __contains__ ( self , k ) : <EOL> return self . db . server . __contains__ ( k ) <EOL> class AnnotationClient ( AnnotationDB ) : <EOL> '<STR_LIT>' <EOL> def __init__ ( self , url , name , seqDB , itemClass = AnnotationSeq , <EOL> itemSliceClass = AnnotationSlice , autoGC = True , ** kwargs ) : <EOL> if autoGC : <EOL> self . _weakValueDict = classutil . RecentValueDictionary ( autoGC ) <EOL> else : <EOL> self . _weakValueDict = { } <EOL> self . autoGC = autoGC <EOL> import coordinator <EOL> self . server = coordinator . get_connection ( url , name ) <EOL> self . url = url <EOL> self . name = name <EOL> self . seqDB = seqDB <EOL> self . sliceDB = AnnotationClientSliceDB ( self ) <EOL> self . itemClass = itemClass <EOL> self . itemSliceClass = itemSliceClass <EOL> def __getstate__ ( self ) : <EOL> return dict ( url = self . url , name = self . name , seqDB = self . seqDB , <EOL> autoGC = self . autoGC ) <EOL> def getSliceAttr ( self , sliceInfo , attr ) : <EOL> if attr == '<STR_LIT:id>' : <EOL> return sliceInfo [ <NUM_LIT:0> ] <EOL> elif attr == '<STR_LIT:start>' : <EOL> return sliceInfo [ <NUM_LIT:1> ] <EOL> elif attr == '<STR_LIT>' : <EOL> return sliceInfo [ <NUM_LIT:2> ] <EOL> elif attr == '<STR_LIT>' : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> else : <EOL> v = self . server . get_annotation_attr ( sliceInfo [ <NUM_LIT:0> ] , attr ) <EOL> if v == '<STR_LIT>' : <EOL> raise AttributeError ( '<STR_LIT>' + attr ) <EOL> return v </s>
