<s> '''<STR_LIT>''' <EOL> import sys <EOL> import string <EOL> import re <EOL> import array <EOL> import CGAT . Experiment as E <EOL> def maskSequence ( sequence , regions , mask_char = "<STR_LIT:N>" ) : <EOL> """<STR_LIT>""" <EOL> nmasked = <NUM_LIT:0> <EOL> v = array . array ( '<STR_LIT:c>' ) <EOL> v . fromstring ( sequence ) <EOL> errors = [ ] <EOL> for start , end in regions : <EOL> if start < <NUM_LIT:0> or end > len ( sequence ) : <EOL> errors . append ( "<STR_LIT>" % ( start , end ) ) <EOL> continue <EOL> for x in range ( start , end ) : <EOL> if v [ x ] != mask_char : <EOL> v [ x ] = mask_char <EOL> nmasked += <NUM_LIT:1> <EOL> return v . tostring ( ) , nmasked , errors <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> 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 , nerrors = <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> <EOL> outfile = options . stdout <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> if options . loglevel >= <NUM_LIT:1> : <EOL> options . stdlog . write ( "<STR_LIT>" % key ) <EOL> options . stdlog . flush ( ) <EOL> if key in mask_regions : <EOL> sequence , nmasked , errors = maskSequence ( <EOL> string . join ( fragments , "<STR_LIT>" ) , mask_regions [ key ] , options . mask_char ) <EOL> if errors : <EOL> nerrors += len ( errors ) <EOL> options . stdlog . write ( <EOL> "<STR_LIT>" % len ( errors ) ) <EOL> for e in errors : <EOL> options . stdlog . write ( "<STR_LIT>" % e ) <EOL> else : <EOL> sequence = string . join ( fragments , "<STR_LIT>" ) <EOL> if description_rest : <EOL> outfile . write ( "<STR_LIT>" % <EOL> ( key , description_rest , sequence ) ) <EOL> else : <EOL> outfile . write ( "<STR_LIT>" % ( key , sequence ) ) <EOL> total_masked += nmasked <EOL> total_keys += <NUM_LIT:1> <EOL> total_written += len ( sequence ) <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> if fragments : <EOL> if key in mask_regions : <EOL> sequence , nmasked , errors = maskSequence ( <EOL> string . join ( fragments , "<STR_LIT>" ) , mask_regions [ key ] , options . mask_char ) <EOL> if errors : <EOL> nerrors += len ( errors ) <EOL> options . stdlog . write ( <EOL> "<STR_LIT>" % len ( errors ) ) <EOL> for e in errors : <EOL> options . stdlog . write ( "<STR_LIT>" % e ) <EOL> else : <EOL> sequence = string . join ( fragments , "<STR_LIT>" ) <EOL> total_masked += nmasked <EOL> total_keys += <NUM_LIT:1> <EOL> total_written += len ( sequence ) <EOL> if description_rest : <EOL> outfile . write ( "<STR_LIT>" % ( key , description_rest , sequence ) ) <EOL> else : <EOL> outfile . write ( "<STR_LIT>" % ( key , sequence ) ) <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 , nerrors ) ) <EOL> E . Stop ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( main ( sys . argv ) ) </s>
