<s> from seqdb import SequenceDB , BasicSeqInfoDict <EOL> from annotation import AnnotationDB , TranslationAnnot , TranslationAnnotSlice <EOL> import classutil <EOL> import sequence <EOL> import UserDict <EOL> class SeqTranslator ( sequence . SequenceBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , db , id , reversePath = None ) : <EOL> self . id = id <EOL> sequence . SequenceBase . __init__ ( self ) <EOL> if reversePath : <EOL> self . orientation = - <NUM_LIT:1> <EOL> self . start = - len ( self ) <EOL> self . stop = <NUM_LIT:0> <EOL> self . _reverse = reversePath <EOL> if self . id not in self . db . seqDB : <EOL> raise KeyError ( '<STR_LIT>' % ( self . id , self . db ) ) <EOL> def __getitem__ ( self , k ) : <EOL> """<STR_LIT>""" <EOL> start = k . start <EOL> if start is None : <EOL> start = self . start <EOL> stop = k . stop <EOL> if stop is None : <EOL> stop = self . stop <EOL> annoID = self . _get_anno_id ( start ) <EOL> a = self . db . annodb [ annoID ] <EOL> s = a . sequence <EOL> return a [ ( start - s . start ) / <NUM_LIT:3> : ( stop - s . start ) / <NUM_LIT:3> ] <EOL> def absolute_slice ( self , start , stop ) : <EOL> """<STR_LIT>""" <EOL> if start < <NUM_LIT:0> : <EOL> return ( - self ) [ start : stop ] <EOL> else : <EOL> return self [ start : stop ] <EOL> def __len__ ( self ) : <EOL> return self . db . seqInfoDict [ self . id ] . length <EOL> def __neg__ ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _reverse <EOL> except AttributeError : <EOL> self . _reverse = self . __class__ ( self . db , self . id , <EOL> reversePath = self ) <EOL> return self . _reverse <EOL> def _get_anno_id ( self , start ) : <EOL> """<STR_LIT>""" <EOL> if self . orientation > <NUM_LIT:0> : <EOL> return '<STR_LIT>' % ( self . id , start % <NUM_LIT:3> ) <EOL> else : <EOL> return '<STR_LIT>' % ( self . id , ( - start ) % <NUM_LIT:3> ) <EOL> def iter_frames ( self ) : <EOL> '<STR_LIT>' <EOL> for frame in ( '<STR_LIT:0>' , '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> yield self . db . annodb [ '<STR_LIT>' % ( self . id , frame ) ] <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' + sequence . SequenceBase . __repr__ ( self ) + '<STR_LIT:)>' <EOL> class TranslationDB ( SequenceDB ) : <EOL> """<STR_LIT>""" <EOL> itemClass = SeqTranslator <EOL> _seqtype = sequence . DNA_SEQTYPE <EOL> def __init__ ( self , seqDB , ** kwargs ) : <EOL> self . seqDB = seqDB <EOL> try : <EOL> self . seqInfoDict = seqDB . seqInfoDict <EOL> except AttributeError : <EOL> self . seqInfoDict = BasicSeqInfoDict ( seqDB ) <EOL> self . annodb = AnnotationDB ( SixFrameInfo ( seqDB ) , seqDB , <EOL> itemClass = TranslationAnnot , <EOL> itemSliceClass = TranslationAnnotSlice , <EOL> sliceAttrDict = dict ( id = <NUM_LIT:0> , start = <NUM_LIT:1> , stop = <NUM_LIT:2> ) , <EOL> checkFirstID = False ) <EOL> SequenceDB . __init__ ( self , ** kwargs ) <EOL> class SixFrameInfo ( object , UserDict . DictMixin ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , seqDB ) : <EOL> self . seqDB = seqDB <EOL> def __getitem__ ( self , k ) : <EOL> "<STR_LIT>" <EOL> i = k . rfind ( '<STR_LIT::>' ) <EOL> if i < <NUM_LIT:0> : <EOL> raise KeyError ( '<STR_LIT>' % ( k , ) ) <EOL> seqID = k [ : i ] <EOL> length = len ( self . seqDB [ seqID ] ) <EOL> frame = int ( k [ i + <NUM_LIT:1> : ] ) <EOL> if k [ i + <NUM_LIT:1> ] == '<STR_LIT:->' : <EOL> return ( seqID , - ( length - ( ( length + frame ) % <NUM_LIT:3> ) ) , frame ) <EOL> else : <EOL> return ( seqID , frame , length - ( ( length - frame ) % <NUM_LIT:3> ) ) <EOL> def __len__ ( self ) : <EOL> return <NUM_LIT:6> * len ( self . seqDB ) <EOL> def __iter__ ( self ) : <EOL> for seqID in self . seqDB : <EOL> for frame in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> yield seqID + frame <EOL> def keys ( self ) : <EOL> return list ( self ) <EOL> clear = setdefault = pop = popitem = copy = update = classutil . read_only_error <EOL> def get_translation_db ( seqDB ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return seqDB . translationDB <EOL> except AttributeError : <EOL> tdb = TranslationDB ( seqDB ) <EOL> try : <EOL> seqDB . translationDB = tdb <EOL> except AttributeError : <EOL> pass <EOL> return tdb </s>
