<s> '''<STR_LIT>''' <EOL> import sys <EOL> import CGAT . Experiment as E <EOL> import CGAT . Bed as Bed <EOL> import CGAT . IndexedFasta as IndexedFasta <EOL> import CGAT . Masker as Masker <EOL> def main ( argv = None ) : <EOL> if argv is None : <EOL> argv = sys . argv <EOL> parser = E . OptionParser ( <EOL> version = "<STR_LIT>" , <EOL> usage = globals ( ) [ "<STR_LIT>" ] ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT>" , <EOL> choices = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:none>" ) , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT>" , <EOL> choices = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:int>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT:max_length>" , type = "<STR_LIT:int>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( <EOL> "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT>" , <EOL> choices = ( "<STR_LIT:none>" , "<STR_LIT:3>" , "<STR_LIT:5>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_option ( <EOL> "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:int>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( <EOL> "<STR_LIT>" , dest = "<STR_LIT>" , <EOL> action = "<STR_LIT>" , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . set_defaults ( <EOL> genome_file = None , <EOL> masker = None , <EOL> output_mode = "<STR_LIT>" , <EOL> min_length = <NUM_LIT:0> , <EOL> max_length = <NUM_LIT:0> , <EOL> extend_at = None , <EOL> extend_by = <NUM_LIT:100> , <EOL> ignore_strand = True , <EOL> ) <EOL> ( options , args ) = E . Start ( parser ) <EOL> if options . genome_file : <EOL> fasta = IndexedFasta . IndexedFasta ( options . genome_file ) <EOL> contigs = fasta . getContigSizes ( ) <EOL> fasta . setConverter ( IndexedFasta . getConverter ( "<STR_LIT>" ) ) <EOL> counter = E . Counter ( ) <EOL> ids , seqs = [ ] , [ ] <EOL> E . info ( "<STR_LIT>" ) <EOL> for bed in Bed . setName ( Bed . iterator ( options . stdin ) ) : <EOL> counter . input += <NUM_LIT:1> <EOL> lcontig = fasta . getLength ( bed . contig ) <EOL> if options . ignore_strand : <EOL> strand = "<STR_LIT:+>" <EOL> else : <EOL> strand = bed . strand <EOL> if options . output_mode == "<STR_LIT>" and bed . columns == <NUM_LIT:12> : <EOL> ids . append ( "<STR_LIT>" % <EOL> ( bed . name , bed . contig , bed . start , bed . end , strand , <EOL> bed [ "<STR_LIT>" ] , bed [ "<STR_LIT>" ] ) ) <EOL> seg_seqs = [ fasta . getSequence ( bed . contig , strand , start , end ) <EOL> for start , end in bed . toIntervals ( ) ] <EOL> seqs . append ( "<STR_LIT>" . join ( seg_seqs ) ) <EOL> elif ( options . output_mode == "<STR_LIT>" or <EOL> options . output_mode == "<STR_LIT>" ) : <EOL> ids . append ( "<STR_LIT>" % <EOL> ( bed . name , bed . contig , bed . start , bed . end , strand ) ) <EOL> seqs . append ( <EOL> fasta . getSequence ( bed . contig , strand , bed . start , bed . end ) ) <EOL> elif options . output_mode == "<STR_LIT>" : <EOL> l = bed . end - bed . start <EOL> start , end = max ( <NUM_LIT:0> , bed . start - l ) , bed . end - l <EOL> ids . append ( "<STR_LIT>" % <EOL> ( bed . name , bed . contig , start , end , strand ) ) <EOL> seqs . append ( fasta . getSequence ( bed . contig , strand , start , end ) ) <EOL> start , end = bed . start + l , min ( lcontig , bed . end + l ) <EOL> ids . append ( "<STR_LIT>" % <EOL> ( bed . name , bed . contig , start , end , strand ) ) <EOL> seqs . append ( fasta . getSequence ( bed . contig , strand , start , end ) ) <EOL> E . info ( "<STR_LIT>" % len ( seqs ) ) <EOL> masked = Masker . maskSequences ( seqs , options . masker ) <EOL> options . stdout . write ( <EOL> "<STR_LIT:\n>" . join ( [ "<STR_LIT>" % ( x , y ) for x , y in zip ( ids , masked ) ] ) + "<STR_LIT:\n>" ) <EOL> E . info ( "<STR_LIT>" % len ( seqs ) ) <EOL> counter . output = len ( seqs ) <EOL> E . info ( "<STR_LIT:%s>" % counter ) <EOL> E . Stop ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( main ( sys . argv ) ) </s>
