<s> from __future__ import generators <EOL> import types <EOL> from sequtil import * <EOL> NOT_ON_SAME_PATH = - <NUM_LIT:2> <EOL> class ReadOnlyAttribute ( object ) : <EOL> def __init__ ( self , attr ) : <EOL> self . attr = attr <EOL> def __get__ ( self , obj , klass ) : <EOL> return getattr ( obj , self . attr ) <EOL> class IntervalTransform ( object ) : <EOL> "<STR_LIT>" <EOL> srcPath = ReadOnlyAttribute ( '<STR_LIT>' ) <EOL> destPath = ReadOnlyAttribute ( '<STR_LIT>' ) <EOL> def __init__ ( self , srcPath , destPath , edgeInfo = None , <EOL> edgeAttr = None , edgeIndex = None ) : <EOL> "<STR_LIT>" <EOL> self . scale = len ( destPath ) / float ( len ( srcPath ) ) <EOL> self . _srcPath = srcPath <EOL> self . _destPath = destPath <EOL> if edgeInfo != None and edgeAttr != None : <EOL> try : <EOL> edgeInfo = getattr ( edgeInfo , edgeAttr ) <EOL> except AttributeError : <EOL> edgeInfo = None <EOL> if edgeInfo != None : <EOL> if edgeIndex != None : <EOL> edgeInfo = edgeInfo [ edgeIndex ] <EOL> self . edgeInfo = edgeInfo <EOL> def xform ( self , i ) : <EOL> '<STR_LIT>' <EOL> return int ( i * self . scale ) <EOL> def xformBack ( self , i ) : <EOL> '<STR_LIT>' <EOL> return int ( i / self . scale ) <EOL> def getStartStop ( self , srcPath , ourPath ) : <EOL> '<STR_LIT>' <EOL> if srcPath . path is ourPath . path : <EOL> return srcPath . start - ourPath . start , srcPath . stop - ourPath . start <EOL> try : <EOL> if srcPath . path . _reverse is ourPath . path : <EOL> return - ( srcPath . start ) - ourPath . start , - ( srcPath . stop ) - ourPath . start <EOL> except AttributeError : <EOL> pass <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> def __call__ ( self , srcPath ) : <EOL> """<STR_LIT>""" <EOL> start , stop = self . getStartStop ( srcPath , self . srcPath ) <EOL> return SeqPath ( self . destPath , self . xform ( start ) , self . xform ( stop ) , <EOL> relativeToStart = True ) <EOL> def reverse ( self , destPath ) : <EOL> "<STR_LIT>" <EOL> start , stop = self . getStartStop ( destPath , self . destPath ) <EOL> return SeqPath ( self . srcPath , self . xformBack ( start ) , <EOL> self . xformBack ( stop ) , relativeToStart = True ) <EOL> def __getitem__ ( self , srcPath ) : <EOL> """<STR_LIT>""" <EOL> return self ( srcPath * self . srcPath ) <EOL> def __iter__ ( self ) : <EOL> yield self . srcPath <EOL> def items ( self ) : <EOL> yield self . srcPath , self . destPath <EOL> def __getattr__ ( self , attr ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> return getattr ( self . __dict__ [ '<STR_LIT>' ] , attr ) <EOL> except ( KeyError , AttributeError ) : <EOL> raise AttributeError ( '<STR_LIT>' + attr ) <EOL> def repr_dict ( self ) : <EOL> "<STR_LIT>" <EOL> s = self . srcPath . repr_dict ( ) <EOL> d = self . destPath . repr_dict ( ) <EOL> out = { } <EOL> for k , val in s . items ( ) : <EOL> out [ '<STR_LIT>' + k ] = val <EOL> out [ '<STR_LIT>' + k ] = d [ k ] <EOL> try : <EOL> e = self . edgeInfo . repr_dict ( ) <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> out . update ( e ) <EOL> return out <EOL> def nidentity ( self ) : <EOL> "<STR_LIT>" <EOL> nid = <NUM_LIT:0> <EOL> src = str ( self . srcPath ) . upper ( ) <EOL> dest = str ( self . destPath ) . upper ( ) <EOL> slen = len ( src ) <EOL> i = <NUM_LIT:0> <EOL> while i < slen : <EOL> if src [ i ] == dest [ i ] : <EOL> nid += <NUM_LIT:1> <EOL> i += <NUM_LIT:1> <EOL> return nid <EOL> def percent_id ( self ) : <EOL> "<STR_LIT>" <EOL> return self . nidentity / float ( len ( self . srcPath ) ) <EOL> class LetterEdgeDescriptor ( object ) : <EOL> "<STR_LIT>" <EOL> def __get__ ( self , edge , objtype ) : <EOL> try : <EOL> return edge . _seqs <EOL> except AttributeError : <EOL> edge . _seqs = edge . origin . getEdgeSeqs ( edge . target ) <EOL> return edge . _seqs <EOL> class LetterEdge ( object ) : <EOL> "<STR_LIT>" <EOL> seqs = LetterEdgeDescriptor ( ) <EOL> def __init__ ( self , origin , target ) : <EOL> self . origin = origin <EOL> self . target = target <EOL> def __iter__ ( self ) : <EOL> '<STR_LIT>' <EOL> for seq in self . seqs : <EOL> yield self . origin . getSeqPos ( seq ) <EOL> def iteritems ( self ) : <EOL> '<STR_LIT>' <EOL> for seq in self . seqs : <EOL> yield self . origin . getSeqPos ( seq ) , self . target . getSeqPos ( seq ) <EOL> def __getitem__ ( self , k ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> k = k . path <EOL> except AttributeError : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> return ( self . origin . getSeqPos ( k . path ) , self . target . getSeqPos ( k . path ) ) <EOL> def __cmp__ ( self , other ) : <EOL> '<STR_LIT>' <EOL> try : <EOL> if self . origin == other . origin and self . target == other . target : <EOL> return <NUM_LIT:0> <EOL> except AttributeError : <EOL> pass <EOL> return cmp ( id ( self ) , id ( other ) ) <EOL> def absoluteSlice ( seq , start , stop ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> if start < <NUM_LIT:0> : <EOL> return - ( seq . pathForward [ - stop : - start ] ) <EOL> else : <EOL> return seq . pathForward [ start : stop ] <EOL> except AttributeError : <EOL> if seq is None : <EOL> return slice ( start , stop ) <EOL> def relativeSlice ( seq , start , stop ) : <EOL> '''<STR_LIT>''' <EOL> if start < <NUM_LIT:0> : <EOL> return - ( seq [ - stop : - start ] ) <EOL> else : <EOL> return seq [ start : stop ] <EOL> def sumSliceIndex ( i , myslice , relativeToStart ) : <EOL> '''<STR_LIT>''' <EOL> if myslice is None : <EOL> return i <EOL> if i is None : <EOL> i = <NUM_LIT:0> <EOL> i *= myslice . step <EOL> if relativeToStart : <EOL> return i + myslice . start <EOL> else : <EOL> return i + myslice . stop <EOL> class SeqOriDescriptor ( object ) : <EOL> "<STR_LIT>" <EOL> def __get__ ( self , seq , objtype ) : <EOL> if seq . start >= <NUM_LIT:0> : <EOL> return <NUM_LIT:1> <EOL> else : <EOL> return - <NUM_LIT:1> <EOL> class PathForwardDescr ( object ) : <EOL> '<STR_LIT>' <EOL> def __get__ ( self , seq , objtype ) : <EOL> if seq . orientation > <NUM_LIT:0> : <EOL> return seq . path <EOL> else : <EOL> return seq . path . _reverse <EOL> class AbsIntervalDescr ( object ) : <EOL> '<STR_LIT>' <EOL> def __get__ ( self , seq , objtype ) : <EOL> if seq . orientation > <NUM_LIT:0> : <EOL> return seq . start , seq . stop <EOL> else : <EOL> return - ( seq . stop ) , - ( seq . start ) <EOL> class SeqPath ( object ) : <EOL> '''<STR_LIT>''' <EOL> orientation = SeqOriDescriptor ( ) <EOL> pathForward = PathForwardDescr ( ) <EOL> _abs_interval = AbsIntervalDescr ( ) <EOL> def __init__ ( self , path , start = <NUM_LIT:0> , stop = None , step = None , reversePath = False , <EOL> relativeToStart = False , absoluteCoords = False ) : <EOL> '''<STR_LIT>''' <EOL> if reversePath : <EOL> start = - ( path . stop ) <EOL> stop = <NUM_LIT:0> <EOL> self . _reverse = path <EOL> path = None <EOL> if absoluteCoords : <EOL> if start >= <NUM_LIT:0> : <EOL> path = path . pathForward <EOL> else : <EOL> path = - ( path . pathForward ) <EOL> else : <EOL> start = sumSliceIndex ( start , path , relativeToStart or start is None <EOL> or start >= <NUM_LIT:0> ) <EOL> stop = sumSliceIndex ( stop , path , relativeToStart or <EOL> ( stop is not None and stop >= <NUM_LIT:0> ) ) <EOL> if start is not None and stop is not None and start > stop : <EOL> start = - start <EOL> stop = - stop <EOL> if path is not None : <EOL> path = - path <EOL> if path is not None : <EOL> if start < path . path . start : <EOL> start = path . path . start <EOL> if stop > path . path . stop : <EOL> stop = path . path . stop <EOL> if start is None or stop is None or start >= stop : <EOL> raise IndexError ( '<STR_LIT>' ) <EOL> self . start = start <EOL> self . stop = stop <EOL> if step is None : <EOL> step = <NUM_LIT:1> <EOL> if path is None : <EOL> self . path = self <EOL> self . step = step <EOL> else : <EOL> self . path = path . path <EOL> self . step = step * path . step <EOL> def classySlice ( self , path , * l , ** kwargs ) : <EOL> '<STR_LIT>' <EOL> try : <EOL> klass = path . pathForward . db . itemSliceClass <EOL> except AttributeError : <EOL> klass = SeqPath <EOL> return klass ( path , * l , ** kwargs ) <EOL> def absolute_slice ( self , start , stop ) : <EOL> '<STR_LIT>' <EOL> return self . classySlice ( self , start , stop , absoluteCoords = True ) <EOL> def __getitem__ ( self , k ) : <EOL> if isinstance ( k , types . IntType ) : <EOL> if k == - <NUM_LIT:1> : <EOL> k = slice ( k , None , <NUM_LIT:1> ) <EOL> else : <EOL> k = slice ( k , k + <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> if isinstance ( k , types . SliceType ) : <EOL> return self . classySlice ( self , k . start , k . stop , k . step ) <EOL> elif isinstance ( k , SeqPath ) : <EOL> if k . path is not self . path : <EOL> raise KeyError ( '<STR_LIT>' ) <EOL> try : <EOL> target = self . classySlice ( self . path , k . stop , <EOL> k . stop + len ( k ) * k . step , k . step ) <EOL> return { target : LetterEdge ( k , target ) } <EOL> except IndexError : <EOL> return { } <EOL> raise KeyError ( '<STR_LIT>' ) <EOL> def __len__ ( self ) : <EOL> if self . path is self and self . orientation < <NUM_LIT:0> : <EOL> return len ( self . _reverse ) <EOL> d = ( self . stop - self . start ) / self . step <EOL> if d > <NUM_LIT:0> : <EOL> return d <EOL> else : <EOL> return <NUM_LIT:1> <EOL> def __iter__ ( self ) : <EOL> for i in range ( len ( self ) ) : <EOL> yield self [ i ] <EOL> def iteritems ( self ) : <EOL> '<STR_LIT>' <EOL> src = self [ <NUM_LIT:0> ] <EOL> for i in range ( <NUM_LIT:1> , len ( self ) ) : <EOL> target = self [ i ] <EOL> yield src , { target : LetterEdge ( src , target ) } <EOL> src = target <EOL> yield src , { } <EOL> def getEdgeSeqs ( self , other ) : <EOL> '<STR_LIT>' <EOL> if self . path is other . path and self . stop == other . start : <EOL> return { self . path : self . stop - self . step } <EOL> else : <EOL> return { } <EOL> def getSeqPos ( self , seq ) : <EOL> '<STR_LIT>' <EOL> if seq . path is self . path : <EOL> return self <EOL> raise KeyError ( '<STR_LIT>' ) <EOL> def __hash__ ( self ) : <EOL> '<STR_LIT>' <EOL> return id ( self . path ) ^ hash ( self . start ) ^ hash ( self . stop ) <EOL> def __cmp__ ( self , other ) : <EOL> '<STR_LIT>' <EOL> if not isinstance ( other , SeqPath ) : <EOL> return - <NUM_LIT:1> <EOL> if self . path is other . path : <EOL> return cmp ( ( self . start , self . stop ) , ( other . start , other . stop ) ) <EOL> else : <EOL> return NOT_ON_SAME_PATH <EOL> def __contains__ ( self , k ) : <EOL> if isinstance ( k , SeqPath ) : <EOL> if k . path == self . path and self . start <= k . start and k . stop <= self . stop : <EOL> return True <EOL> else : <EOL> return False <EOL> elif isinstance ( k , types . IntType ) : <EOL> return self . start <= k and k < self . stop <EOL> def overlaps ( self , p ) : <EOL> "<STR_LIT>" <EOL> if self . path is not p . path : <EOL> return False <EOL> if ( self . start <= p . start and p . start < self . stop ) or ( p . start <= self . start and self . start < p . stop ) : <EOL> return True <EOL> else : <EOL> return False <EOL> def __mul__ ( self , other ) : <EOL> "<STR_LIT>" <EOL> if isinstance ( other , SeqPath ) : <EOL> if self . path is not other . path : <EOL> return None <EOL> start = max ( self . start , other . start ) <EOL> stop = min ( self . stop , other . stop ) <EOL> if start < stop : <EOL> if stop == <NUM_LIT:0> : <EOL> stop = None <EOL> return self . classySlice ( self . path , start , stop ) <EOL> else : <EOL> return None <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __div__ ( self , other ) : <EOL> "<STR_LIT>" <EOL> return IntervalTransform ( other , self ) <EOL> def __neg__ ( self ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> if self . seqtype ( ) == PROTEIN_SEQTYPE : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> except AttributeError : <EOL> pass <EOL> if self is self . path : <EOL> try : <EOL> return self . _reverse <EOL> except AttributeError : <EOL> self . _reverse = self . classySlice ( self , reversePath = True ) <EOL> return self . _reverse <EOL> elif self . orientation > <NUM_LIT:0> : <EOL> return self . classySlice ( self . path , self . stop , self . start , <EOL> self . step ) <EOL> else : <EOL> return self . classySlice ( self . path . _reverse , - ( self . stop ) , <EOL> - ( self . start ) , self . step ) <EOL> def __add__ ( self , other ) : <EOL> "<STR_LIT>" <EOL> if self . path is not other . path : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if self . start < other . start : <EOL> start = self . start <EOL> else : <EOL> start = other . start <EOL> if self . stop > other . stop : <EOL> stop = self . stop <EOL> else : <EOL> stop = other . stop <EOL> if stop == <NUM_LIT:0> : <EOL> stop = None <EOL> return self . classySlice ( self . path , start , stop , self . step ) <EOL> def __iadd__ ( self , other ) : <EOL> "<STR_LIT>" <EOL> if self . path is not other . path : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if other . start < self . start : <EOL> self . start = other . start <EOL> if other . stop > self . stop : <EOL> self . stop = other . stop <EOL> return self <EOL> def before ( self ) : <EOL> '<STR_LIT>' <EOL> return self . classySlice ( self . path , None , self . start ) <EOL> def after ( self ) : <EOL> '<STR_LIT>' <EOL> if self . stop == <NUM_LIT:0> : <EOL> raise IndexError ( '<STR_LIT>' ) <EOL> return self . classySlice ( self . path , self . stop , None ) <EOL> def is_full_length ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self == self . path <EOL> _complement = { '<STR_LIT:a>' : '<STR_LIT:t>' , '<STR_LIT:c>' : '<STR_LIT:g>' , '<STR_LIT:g>' : '<STR_LIT:c>' , '<STR_LIT:t>' : '<STR_LIT:a>' , '<STR_LIT:u>' : '<STR_LIT:a>' , '<STR_LIT:n>' : '<STR_LIT:n>' , <EOL> '<STR_LIT:A>' : '<STR_LIT:T>' , '<STR_LIT:C>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT:C>' , '<STR_LIT:T>' : '<STR_LIT:A>' , '<STR_LIT>' : '<STR_LIT:A>' , '<STR_LIT:N>' : '<STR_LIT:N>' } <EOL> def reverse_complement ( self , s ) : <EOL> '<STR_LIT>' <EOL> return '<STR_LIT>' . join ( [ self . _complement . get ( s [ i ] , s [ i ] ) for i in <EOL> range ( len ( s ) - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) ] ) <EOL> def seqtype ( self ) : <EOL> "<STR_LIT>" <EOL> path = self . pathForward <EOL> try : <EOL> return path . _seqtype <EOL> except AttributeError : <EOL> try : <EOL> return path . db . _seqtype <EOL> except AttributeError : <EOL> path . _seqtype = guess_seqtype ( str ( self [ <NUM_LIT:0> : <NUM_LIT> ] ) ) <EOL> return path . _seqtype <EOL> def __str__ ( self ) : <EOL> '''<STR_LIT>''' <EOL> if self . orientation > <NUM_LIT:0> : <EOL> return self . path . strslice ( self . start , self . stop ) <EOL> else : <EOL> s = self . path . _reverse . strslice ( - ( self . stop ) , - ( self . start ) ) <EOL> return self . reverse_complement ( s ) <EOL> def __repr__ ( self ) : <EOL> try : <EOL> id = self . pathForward . id <EOL> except AttributeError : <EOL> id = '<STR_LIT>' <EOL> if self . orientation < <NUM_LIT:0> : <EOL> return '<STR_LIT>' % ( id , - self . stop , - self . start ) <EOL> else : <EOL> return '<STR_LIT>' % ( id , self . start , self . stop ) <EOL> def repr_dict ( self ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> id = self . path . id <EOL> except AttributeError : <EOL> id = self . id <EOL> return { '<STR_LIT:id>' : id , '<STR_LIT:start>' : self . start , '<STR_LIT:end>' : self . stop , <EOL> '<STR_LIT>' : self . orientation } <EOL> class LengthDescriptor ( object ) : <EOL> '<STR_LIT>' <EOL> def __get__ ( self , seq , klass ) : <EOL> return len ( seq ) <EOL> class SequenceBase ( SeqPath ) : <EOL> '''<STR_LIT>''' <EOL> start = <NUM_LIT:0> <EOL> stop = LengthDescriptor ( ) <EOL> step = <NUM_LIT:1> <EOL> orientation = <NUM_LIT:1> <EOL> def __init__ ( self ) : <EOL> self . path = self <EOL> def update ( self , seq ) : <EOL> '<STR_LIT>' <EOL> self . seq = seq <EOL> def __len__ ( self ) : <EOL> '<STR_LIT>' <EOL> return len ( self . seq ) <EOL> def strslice ( self , start , stop ) : <EOL> '<STR_LIT>' <EOL> return self . seq [ start : stop ] <EOL> class Sequence ( SequenceBase ) : <EOL> '<STR_LIT>' <EOL> def __init__ ( self , s , id ) : <EOL> SequenceBase . __init__ ( self ) <EOL> self . id = id <EOL> self . seq = s <EOL> class SeqFilterDict ( dict ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , l = [ ] ) : <EOL> '<STR_LIT>' <EOL> dict . __init__ ( self ) <EOL> for ival in l : <EOL> self [ ival ] = None <EOL> def __getitem__ ( self , k ) : <EOL> try : <EOL> ival = dict . __getitem__ ( self , k . path ) <EOL> except KeyError : <EOL> raise KeyError ( '<STR_LIT>' ) <EOL> result = k * ival <EOL> if result is None : <EOL> raise KeyError <EOL> return result <EOL> def __setitem__ ( self , ival , junk ) : <EOL> dict . __setitem__ ( self , ival . path , ival ) <EOL> def __iter__ ( self ) : <EOL> return dict . itervalues ( self ) <EOL> class Seq2SeqEdge ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , msaSlice , targetPath , sourcePath = None , <EOL> matchIntervals = False ) : <EOL> self . msaSlice = msaSlice <EOL> self . targetPath = targetPath <EOL> if sourcePath is not None : <EOL> self . sourcePath = sourcePath <EOL> self . matchIntervals = matchIntervals <EOL> else : <EOL> si = msaSlice . groupByIntervals ( filterList = [ targetPath ] , <EOL> mergeAll = True ) <EOL> l = msaSlice . groupBySequences ( si ) <EOL> try : <EOL> self . sourcePath = l [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> raise KeyError ( '<STR_LIT>' ) <EOL> self . matchIntervals = l [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] <EOL> def items ( self , mergeAll = False , ** kwargs ) : <EOL> '<STR_LIT>' <EOL> if self . matchIntervals is None : <EOL> return [ ( self . sourcePath , self . targetPath ) ] <EOL> elif self . matchIntervals is False : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> l = [ ] <EOL> for srcStart , srcEnd , destStart , destEnd in self . matchIntervals : <EOL> l . append ( ( absoluteSlice ( self . sourcePath , srcStart , srcEnd ) , <EOL> absoluteSlice ( self . targetPath , destStart , destEnd ) ) ) <EOL> return l <EOL> def get_gaps ( self ) : <EOL> '<STR_LIT>' <EOL> if self . matchIntervals is False : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> elif self . matchIntervals is None or len ( self . matchIntervals ) < <NUM_LIT:2> : <EOL> return [ ] <EOL> srcLast = self . matchIntervals [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> destLast = self . matchIntervals [ <NUM_LIT:0> ] [ <NUM_LIT:3> ] <EOL> l = [ ] <EOL> for t in self . matchIntervals [ <NUM_LIT:1> : ] : <EOL> if t [ <NUM_LIT:0> ] > srcLast : <EOL> srcIval = absoluteSlice ( self . sourcePath , srcLast , t [ <NUM_LIT:0> ] ) <EOL> else : <EOL> srcIval = None <EOL> if t [ <NUM_LIT:2> ] > destLast : <EOL> destIval = absoluteSlice ( self . targetPath , destLast , t [ <NUM_LIT:2> ] ) <EOL> else : <EOL> destIval = None <EOL> if srcIval is not None or destIval is not None : <EOL> l . append ( ( srcIval , destIval ) ) <EOL> srcLast = t [ <NUM_LIT:1> ] <EOL> destLast = t [ <NUM_LIT:3> ] <EOL> return l <EOL> def __iter__ ( self , sourceOnly = True , ** kwargs ) : <EOL> return iter ( [ t [ <NUM_LIT:0> ] for t in self . items ( sourceOnly = sourceOnly , <EOL> ** kwargs ) ] ) <EOL> def length ( self , mode = max ) : <EOL> '<STR_LIT>' <EOL> return mode ( len ( self . sourcePath ) , len ( self . targetPath ) ) <EOL> def pIdentity ( self , mode = max , trapOverflow = True ) : <EOL> "<STR_LIT>" <EOL> nid = <NUM_LIT:0> <EOL> start1 = self . sourcePath . start <EOL> s1 = str ( self . sourcePath ) . upper ( ) <EOL> start2 = self . targetPath . start <EOL> s2 = str ( self . targetPath ) . upper ( ) <EOL> for srcPath , destPath in self . items ( ) : <EOL> isrc = srcPath . start - start1 <EOL> idest = destPath . start - start2 <EOL> for i in xrange ( len ( srcPath ) ) : <EOL> if s1 [ isrc + i ] == s2 [ idest + i ] : <EOL> nid += <NUM_LIT:1> <EOL> x = nid / float ( self . length ( mode ) ) <EOL> if trapOverflow and x > <NUM_LIT:1.> : <EOL> raise ValueError ( '''<STR_LIT>''' ) <EOL> return x <EOL> def longestSegment ( self , segment , pIdentityMin = <NUM_LIT> , minAlignSize = <NUM_LIT:1> , <EOL> mode = max , ** kwargs ) : <EOL> besthit = None <EOL> for i in xrange ( len ( segment ) ) : <EOL> ni = <NUM_LIT:0> <EOL> nm = <NUM_LIT:0> <EOL> for j in xrange ( i , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> ni += segment [ j ] [ <NUM_LIT:2> ] <EOL> l = mode ( segment [ i ] [ <NUM_LIT:0> ] + segment [ i ] [ <NUM_LIT:2> ] - segment [ j ] [ <NUM_LIT:0> ] , <EOL> segment [ i ] [ <NUM_LIT:1> ] + segment [ i ] [ <NUM_LIT:2> ] - segment [ j ] [ <NUM_LIT:1> ] ) <EOL> pIdentity = float ( ni ) / l <EOL> if pIdentity >= pIdentityMin and ( besthit is None or <EOL> ni + nm > besthit [ <NUM_LIT:4> ] ) : <EOL> besthit = ( segment [ j ] [ <NUM_LIT:0> ] , segment [ i ] [ <NUM_LIT:0> ] + segment [ i ] [ <NUM_LIT:2> ] , <EOL> segment [ j ] [ <NUM_LIT:1> ] , segment [ i ] [ <NUM_LIT:1> ] + segment [ i ] [ <NUM_LIT:2> ] , <EOL> ni + nm ) <EOL> nm += segment [ j ] [ <NUM_LIT:3> ] <EOL> if besthit is None : <EOL> return None <EOL> elif besthit [ <NUM_LIT:4> ] >= minAlignSize : <EOL> return besthit [ : <NUM_LIT:4> ] <EOL> else : <EOL> return None <EOL> def conservedSegment ( self , ** kwargs ) : <EOL> "<STR_LIT>" <EOL> start1 = self . sourcePath . start <EOL> s1 = str ( self . sourcePath ) . upper ( ) <EOL> start2 = self . targetPath . start <EOL> s2 = str ( self . targetPath ) . upper ( ) <EOL> i1 = None <EOL> segment = [ ] <EOL> n = <NUM_LIT:0> <EOL> for srcPath , destPath in self . items ( ) : <EOL> isrc = srcPath . start - start1 <EOL> idest = destPath . start - start2 <EOL> for i in xrange ( len ( srcPath ) ) : <EOL> if s1 [ isrc + i ] == s2 [ idest + i ] : <EOL> if i1 is None : <EOL> seg1 , i1 = isrc + i , isrc + i <EOL> seg2 , i2 = idest + i , idest + i <EOL> elif i1 + <NUM_LIT:1> != isrc + i or i2 + <NUM_LIT:1> != idest + i : <EOL> segment . append ( ( seg1 + start1 , seg2 + start2 , <EOL> i1 + <NUM_LIT:1> - seg1 , n ) ) <EOL> n = <NUM_LIT:0> <EOL> seg1 , i1 = isrc + i , isrc + i <EOL> seg2 , i2 = idest + i , idest + i <EOL> else : <EOL> i1 = isrc + i <EOL> i2 = idest + i <EOL> else : <EOL> if i1 is not None : <EOL> segment . append ( ( seg1 + start1 , seg2 + start2 , <EOL> i1 + <NUM_LIT:1> - seg1 , n ) ) <EOL> i1 = None <EOL> n = <NUM_LIT:0> <EOL> n += <NUM_LIT:1> <EOL> if i1 is not None : <EOL> segment . append ( ( seg1 + start1 , seg2 + start2 , i1 + <NUM_LIT:1> - seg1 , n ) ) <EOL> return self . longestSegment ( segment , ** kwargs ) <EOL> def pAligned ( self , mode = max , trapOverflow = True ) : <EOL> '<STR_LIT>' <EOL> nid = <NUM_LIT:0> <EOL> for srcPath , destPath in self . items ( ) : <EOL> nid += len ( destPath ) <EOL> x = nid / float ( self . length ( mode ) ) <EOL> if trapOverflow and x > <NUM_LIT:1.> : <EOL> raise ValueError ( '''<STR_LIT>''' ) <EOL> return x <EOL> class SeqDBDescriptor ( object ) : <EOL> '<STR_LIT>' <EOL> def __init__ ( self , attr ) : <EOL> self . attr = attr <EOL> def __get__ ( self , obj , objtype ) : <EOL> return getattr ( obj . pathForward , self . attr ) <EOL> class SeqDBSlice ( SeqPath ) : <EOL> '<STR_LIT>' <EOL> id = SeqDBDescriptor ( '<STR_LIT:id>' ) <EOL> db = SeqDBDescriptor ( '<STR_LIT>' ) </s>
