<s> '''<STR_LIT>''' <EOL> from CGAT import NCL as ncl <EOL> from bx . intervals . intersection import Intersecter , Interval <EOL> class IndexedGenome : <EOL> '''<STR_LIT>''' <EOL> index_factory = ncl . NCL <EOL> def __init__ ( self ) : <EOL> self . mIndex = { } <EOL> def add ( self , contig , start , end , value ) : <EOL> if contig not in self . mIndex : <EOL> self . mIndex [ contig ] = self . index_factory ( ) <EOL> self . mIndex [ contig ] . add ( start , end , value ) <EOL> def __getitem__ ( self , args ) : <EOL> '''<STR_LIT>''' <EOL> if args [ <NUM_LIT:0> ] not in self . mIndex : <EOL> raise KeyError ( "<STR_LIT>" % args [ <NUM_LIT:0> ] ) <EOL> return self . mIndex [ args [ <NUM_LIT:0> ] ] . find ( args [ <NUM_LIT:1> ] , args [ <NUM_LIT:2> ] ) <EOL> def contains ( self , contig , start , end ) : <EOL> if contig not in self . mIndex : <EOL> return False <EOL> return len ( list ( self . mIndex [ contig ] . find ( start , end ) ) ) > <NUM_LIT:0> <EOL> def get ( self , contig , start , end ) : <EOL> '''<STR_LIT>''' <EOL> if contig not in self . mIndex : <EOL> raise KeyError ( "<STR_LIT>" % contig ) <EOL> return self . mIndex [ contig ] . find ( start , end ) <EOL> def __len__ ( self ) : <EOL> '''<STR_LIT>''' <EOL> return len ( self . mIndex ) <EOL> class Simple ( IndexedGenome ) : <EOL> '''<STR_LIT>''' <EOL> index_factory = ncl . NCLSimple <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> IndexedGenome . __init__ ( self , * args , ** kwargs ) <EOL> def add ( self , contig , start , end ) : <EOL> if contig not in self . mIndex : <EOL> self . mIndex [ contig ] = self . index_factory ( ) <EOL> self . mIndex [ contig ] . add ( start , end ) <EOL> class Quicksect ( IndexedGenome ) : <EOL> '''<STR_LIT>''' <EOL> index_factory = Intersecter <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> IndexedGenome . __init__ ( self , * args , ** kwargs ) <EOL> def add ( self , contig , start , end , value ) : <EOL> if contig not in self . mIndex : <EOL> self . mIndex [ contig ] = self . index_factory ( ) <EOL> self . mIndex [ contig ] . add_interval ( Interval ( start , end , value ) ) <EOL> def get ( self , contig , start , end ) : <EOL> '''<STR_LIT>''' <EOL> if contig not in self . mIndex : <EOL> raise KeyError ( "<STR_LIT>" % contig ) <EOL> return [ ( x . start , x . end , x . value ) <EOL> for x in self . mIndex [ contig ] . find ( start , end ) ] <EOL> def before ( self , contig , start , end , num_intervals = <NUM_LIT:1> , max_dist = <NUM_LIT> ) : <EOL> '''<STR_LIT>''' <EOL> if contig not in self . mIndex : <EOL> raise KeyError ( "<STR_LIT>" % contig ) <EOL> return [ ( x . start , x . end , x . value ) <EOL> for x in self . mIndex [ contig ] . before_interval ( <EOL> Interval ( start , end ) , <EOL> num_intervals = <NUM_LIT:1> , <EOL> max_dist = max_dist ) ] <EOL> def after ( self , contig , start , end , num_intervals = <NUM_LIT:1> , max_dist = <NUM_LIT> ) : <EOL> '''<STR_LIT>''' <EOL> if contig not in self . mIndex : <EOL> raise KeyError ( "<STR_LIT>" % contig ) <EOL> return [ ( x . start , x . end , x . value ) <EOL> for x in self . mIndex [ contig ] . after_interval ( <EOL> Interval ( start , end ) , <EOL> num_intervals = <NUM_LIT:1> , <EOL> max_dist = max_dist ) ] </s>
