<s> try : <EOL> from itertools import izip as zip <EOL> except ImportError : <EOL> pass <EOL> from . smartfile import smartfile <EOL> class BedRecord ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( '<STR_LIT>' , '<STR_LIT:start>' , '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> field_names = __slots__ <EOL> def __init__ ( self , contig , start , stop , name = None , score = None , strand = None , thick_start = None , <EOL> thick_end = None , item_rgb = None ) : <EOL> self . contig = contig <EOL> self . start = start <EOL> self . stop = stop <EOL> self . name = name <EOL> self . score = score <EOL> self . strand = strand <EOL> self . thick_start = thick_start <EOL> self . thick_end = thick_end <EOL> self . item_rgb = item_rgb <EOL> @ staticmethod <EOL> def from_line ( line ) : <EOL> line = line . rstrip ( ) <EOL> fields = line . split ( '<STR_LIT:\t>' ) <EOL> contig , start , stop = fields [ <NUM_LIT:0> ] , int ( fields [ <NUM_LIT:1> ] ) , int ( fields [ <NUM_LIT:2> ] ) <EOL> n = len ( fields ) <EOL> name = fields [ <NUM_LIT:3> ] or None if n >= <NUM_LIT:4> else None <EOL> score = fields [ <NUM_LIT:4> ] or None if n >= <NUM_LIT:5> else None <EOL> strand = fields [ <NUM_LIT:5> ] or None if n >= <NUM_LIT:6> else None <EOL> thick_start = fields [ <NUM_LIT:6> ] or None if n >= <NUM_LIT:7> else None <EOL> thick_end = fields [ <NUM_LIT:7> ] or None if n >= <NUM_LIT:8> else None <EOL> item_rgb = fields [ <NUM_LIT:8> ] or None if n >= <NUM_LIT:9> else None <EOL> return BedRecord ( contig , start , stop , name , score , strand , thick_start , thick_end , item_rgb ) <EOL> def to_tuple ( self ) : <EOL> return ( self . contig , self . start , self . stop , self . name , self . score , <EOL> self . strand , self . thick_start , self . thick_end , self . item_rgb ) <EOL> def to_line ( self ) : <EOL> line = '<STR_LIT:\t>' . join ( str ( f if f is not None else '<STR_LIT>' ) for f in self . to_tuple ( ) ) <EOL> return line . rstrip ( ) <EOL> def __repr__ ( self ) : <EOL> fields = ( '<STR_LIT>' % ( k , v ) for k , v in zip ( self . field_names , self . to_tuple ( ) ) if v not in ( None , '<STR_LIT>' ) ) <EOL> return '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( fields ) <EOL> class BedFile ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filename ) : <EOL> self . filename = filename <EOL> self . _tabix = None <EOL> def __iter__ ( self ) : <EOL> return BedFile . parse_bed_lines ( smartfile ( self . filename ) ) <EOL> @ property <EOL> def tabix ( self ) : <EOL> if self . _tabix : <EOL> return self . _tabix <EOL> import pysam <EOL> self . _tabix = pysam . Tabixfile ( self . filename ) <EOL> return self . _tabix <EOL> def query ( self , contig = None , start = None , stop = None ) : <EOL> records = self . tabix . fetch ( contig , start , stop ) <EOL> return BedFile . parse_bed_lines ( records ) <EOL> @ property <EOL> def contigs ( self ) : <EOL> return self . tabix . contigs <EOL> @ staticmethod <EOL> def parse_bed_lines ( lines ) : <EOL> for line in lines : <EOL> line = line . rstrip ( ) <EOL> if not line or line . startswith ( '<STR_LIT:;>' ) or line . startswith ( '<STR_LIT>' ) : <EOL> continue <EOL> yield BedRecord . from_line ( line ) </s>
