<s> '''<STR_LIT>''' <EOL> import sys <EOL> import string <EOL> import re <EOL> import CGAT . Experiment as E <EOL> USAGE = """<STR_LIT>""" % sys . argv [ <NUM_LIT:0> ] <EOL> global_written2file = <NUM_LIT:0> <EOL> global_outfile = None <EOL> global_file_id = <NUM_LIT:0> <EOL> def maskSequence ( sequence , regions , mask_char = "<STR_LIT:N>" ) : <EOL> """<STR_LIT>""" <EOL> nmasked = <NUM_LIT:0> <EOL> for start , end in regions : <EOL> sequence [ start : end ] = mask_char * ( end - start ) <EOL> nmasked += end - start <EOL> return sequence , nmasked <EOL> def processSequence ( key , description_rest , sequence , options , mask_regions = None ) : <EOL> global global_outfile <EOL> global global_file_id <EOL> global global_written2file <EOL> written = <NUM_LIT:0> <EOL> lsequence = len ( sequence ) <EOL> if mask_regions : <EOL> sequence , nmasked = maskSequence ( sequence , mask_regions [ key ] ) <EOL> else : <EOL> nmasked = <NUM_LIT:0> <EOL> while written < lsequence : <EOL> chunk_size = min ( <EOL> options . chunk_size - global_written2file , lsequence - written ) <EOL> first_res = max ( <NUM_LIT:0> , written ) <EOL> last_res = min ( written + chunk_size + options . extend , lsequence ) <EOL> if options . loglevel >= <NUM_LIT:2> : <EOL> options . stdlog . write ( "<STR_LIT>" % <EOL> ( global_file_id , chunk_size , global_written2file , written , lsequence , first_res , last_res ) ) <EOL> if global_outfile is None : <EOL> global_file_id += <NUM_LIT:1> <EOL> global_outfile = open ( options . output_pattern % global_file_id , "<STR_LIT:w>" ) <EOL> offset_positive_strand = first_res <EOL> offset_negative_strand = lsequence - last_res <EOL> if options . format == "<STR_LIT>" : <EOL> global_outfile . write ( "<STR_LIT>" % ( key , <EOL> offset_positive_strand , offset_negative_strand , <EOL> description_rest , sequence [ first_res : last_res ] ) ) <EOL> elif options . format == "<STR_LIT>" : <EOL> global_outfile . write ( "<STR_LIT>" % ( key , first_res , last_res ) ) <EOL> global_written2file += chunk_size <EOL> if global_written2file >= options . chunk_size : <EOL> global_outfile . close ( ) <EOL> global_outfile = None <EOL> global_written2file = <NUM_LIT:0> <EOL> written += chunk_size <EOL> return len ( sequence ) , nmasked <EOL> def main ( argv = None ) : <EOL> """<STR_LIT>""" <EOL> if argv is None : <EOL> argv = sys . argv <EOL> parser = E . OptionParser ( <EOL> version = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:int>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT:-c>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:int>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT>" , <EOL> choices = ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . set_defaults ( <EOL> extend = <NUM_LIT:0> , <EOL> output_pattern = "<STR_LIT>" , <EOL> format = "<STR_LIT>" , <EOL> filename_sequence = None , <EOL> filename_mask_regions = None , <EOL> mask_char = "<STR_LIT:N>" ) <EOL> ( options , args ) = E . Start ( parser , add_pipe_options = True ) <EOL> if options . filename_mask_regions : <EOL> mask_regions = { } <EOL> infile = open ( options . filename_mask_regions , "<STR_LIT:r>" ) <EOL> nregions = <NUM_LIT:0> <EOL> for line in infile : <EOL> if line [ <NUM_LIT:0> ] == "<STR_LIT:#>" : <EOL> continue <EOL> contig , start , end = line [ : - <NUM_LIT:1> ] . split ( "<STR_LIT:\t>" ) <EOL> if contig not in mask_regions : <EOL> mask_regions [ contig ] = [ ] <EOL> mask_regions [ contig ] . append ( ( int ( start ) , int ( end ) ) ) <EOL> nregions += <NUM_LIT:1> <EOL> if options . loglevel >= <NUM_LIT:1> : <EOL> options . stdlog . write ( <EOL> "<STR_LIT>" % ( nregions , len ( mask_regions ) ) ) <EOL> for contig , regions in mask_regions . items ( ) : <EOL> regions . sort ( ) <EOL> infile . close ( ) <EOL> else : <EOL> mask_regions = None <EOL> if options . filename_sequence : <EOL> infile = open ( options . filename_sequence , "<STR_LIT:r>" ) <EOL> else : <EOL> infile = sys . stdin <EOL> fragments = [ ] <EOL> sequences = [ ] <EOL> key = None <EOL> description_rest = None <EOL> total_keys , total_written , total_masked = <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> <EOL> for line in infile : <EOL> if line [ <NUM_LIT:0> ] == "<STR_LIT:#>" : <EOL> continue <EOL> if line [ <NUM_LIT:0> ] == "<STR_LIT:>>" : <EOL> if fragments : <EOL> nwritten , nmasked = processSequence ( <EOL> key , description_rest , string . join ( fragments , "<STR_LIT>" ) , options , mask_regions ) <EOL> total_written += nwritten <EOL> total_masked += nmasked <EOL> total_keys += <NUM_LIT:1> <EOL> fragments = [ ] <EOL> x = re . split ( "<STR_LIT>" , line [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) <EOL> key = x [ <NUM_LIT:0> ] <EOL> description_rest = string . join ( x [ <NUM_LIT:1> : ] , "<STR_LIT:U+0020>" ) <EOL> continue <EOL> fragments . append ( line [ : - <NUM_LIT:1> ] ) <EOL> nwritten , nmasked = processSequence ( <EOL> key , description_rest , string . join ( fragments , "<STR_LIT>" ) , options , mask_regions ) <EOL> total_written += nwritten <EOL> total_masked += nmasked <EOL> total_keys += <NUM_LIT:1> <EOL> if options . filename_sequence : <EOL> infile . close ( ) <EOL> if options . loglevel >= <NUM_LIT:1> : <EOL> options . stdlog . write ( "<STR_LIT>" % ( <EOL> total_keys , total_written , total_masked ) ) <EOL> E . Stop ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( main ( sys . argv ) ) </s>
