<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import re <EOL> import optparse <EOL> import numpy <EOL> import CGAT . Experiment as E <EOL> import CGAT . Nubiscan as Nubiscan <EOL> import CGAT . FastaIterator as FastaIterator <EOL> import CGAT . Stats as Stats <EOL> import CGAT . Genomics as Genomics <EOL> import CGAT . Masker as Masker <EOL> RXRVDR = numpy . matrix ( ( ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> ) ) <EOL> RXRVDR1 = numpy . matrix ( ( ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> ) ) <EOL> RXRVDR2 = numpy . matrix ( ( ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> ) ) <EOL> RXVDR = ( RXRVDR1 + RXRVDR2 ) / <NUM_LIT> <EOL> NR = numpy . matrix ( ( ( <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> ) ) <EOL> def maskSequences ( sequences , masker ) : <EOL> if masker == "<STR_LIT>" : <EOL> masked_seq = sequences <EOL> elif masker == "<STR_LIT>" : <EOL> masker_object = Masker . MaskerDustMasker ( ) <EOL> masked_seq = [ masker_object ( x . upper ( ) ) for x in sequences ] <EOL> else : <EOL> masked_seq = [ x . upper ( ) for x in sequences ] <EOL> masked_seq = [ re . sub ( "<STR_LIT>" , "<STR_LIT:N>" , x ) for x in masked_seq ] <EOL> return masked_seq <EOL> def main ( argv = None ) : <EOL> """<STR_LIT>""" <EOL> if not argv : <EOL> argv = sys . argv <EOL> parser = E . OptionParser ( <EOL> version = "<STR_LIT>" , usage = globals ( ) [ "<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:float>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT>" , <EOL> choices = ( "<STR_LIT>" , "<STR_LIT:all>" , "<STR_LIT>" ) , <EOL> help = "<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>" ) , <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>" , <EOL> choices = ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . set_defaults ( <EOL> iterations = <NUM_LIT:100> , <EOL> qvalue_threshold = <NUM_LIT> , <EOL> motif = "<STR_LIT>" , <EOL> fdr_control = "<STR_LIT:all>" , <EOL> combine = True , <EOL> arrangements = None , <EOL> mask = None , <EOL> output_stats = False , <EOL> add_sequence = False , <EOL> ) <EOL> ( options , args ) = E . Start ( parser , argv = argv , add_output_options = True ) <EOL> ninput , nskipped , noutput = <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> <EOL> if options . arrangements is None : <EOL> options . arrangements = [ <EOL> "<STR_LIT>" % x for x in range ( <NUM_LIT:0> , <NUM_LIT:15> ) ] + [ "<STR_LIT>" % x for x in range ( <NUM_LIT:0> , <NUM_LIT:15> ) ] <EOL> else : <EOL> options . arrangements = options . arrangements . split ( "<STR_LIT:U+002C>" ) <EOL> options . stdout . write ( "<STR_LIT:%s>" % "<STR_LIT:\t>" . join ( Nubiscan . NubiscanMatch . _fields ) ) <EOL> if options . add_sequence : <EOL> options . stdout . write ( "<STR_LIT>" ) <EOL> options . stdout . write ( "<STR_LIT:\n>" ) <EOL> if options . motif == '<STR_LIT>' : <EOL> sense_matrix = NR <EOL> elif options . motif == "<STR_LIT>" : <EOL> sense_matrix = RXRVDR <EOL> elif options . motif == "<STR_LIT>" : <EOL> sense_matrix = RXRVDR1 <EOL> elif options . motif == "<STR_LIT>" : <EOL> sense_matrix = RXRVDR2 <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % options . motif ) <EOL> if options . fdr_control == "<STR_LIT:all>" : <EOL> seqs = list ( FastaIterator . iterate ( options . stdin ) ) <EOL> if options . mask : <EOL> masked_seqs = maskSequences ( <EOL> [ x . sequence for x in seqs ] , options . mask ) <EOL> else : <EOL> masked_seqs = [ x . sequence for x in seqs ] <EOL> ninput = len ( seqs ) <EOL> map_id2title = dict ( <EOL> enumerate ( [ re . sub ( "<STR_LIT>" , "<STR_LIT>" , x . title ) for x in seqs ] ) ) <EOL> matcher = Nubiscan . MatcherRandomisationSequences ( sense_matrix , <EOL> samples = options . iterations ) <EOL> results = matcher . run ( masked_seqs , <EOL> options . arrangements , <EOL> qvalue_threshold = options . qvalue_threshold ) <EOL> if options . combine : <EOL> results = Nubiscan . combineMotifs ( results ) <EOL> for r in results : <EOL> if r . alternatives : <EOL> alternatives = "<STR_LIT:U+002C>" . join ( <EOL> [ x . arrangement for x in r . alternatives ] ) <EOL> else : <EOL> alternatives = "<STR_LIT>" <EOL> options . stdout . write ( "<STR_LIT:\t>" . join ( ( <EOL> map_id2title [ r . id ] , <EOL> "<STR_LIT>" % r . start , <EOL> "<STR_LIT>" % r . end , <EOL> r . strand , <EOL> r . arrangement , <EOL> "<STR_LIT>" % r . score , <EOL> "<STR_LIT>" % r . zscore , <EOL> "<STR_LIT>" % r . pvalue , <EOL> "<STR_LIT>" % r . qvalue , <EOL> alternatives ) ) ) <EOL> if options . add_sequence : <EOL> s = masked_seqs [ int ( r . id ) ] [ r . start : r . end ] <EOL> if r . strand == "<STR_LIT:->" : <EOL> s = Genomics . complement ( s ) <EOL> s = s [ : <NUM_LIT:6> ] . upper ( ) + s [ <NUM_LIT:6> : - <NUM_LIT:6> ] . lower ( ) + s [ - <NUM_LIT:6> : ] . upper ( ) <EOL> options . stdout . write ( "<STR_LIT>" % s ) <EOL> options . stdout . write ( "<STR_LIT:\n>" ) <EOL> noutput += <NUM_LIT:1> <EOL> if options . output_stats : <EOL> outfile = E . openOutputFile ( "<STR_LIT>" ) <EOL> outfile . write ( "<STR_LIT>" ) <EOL> for bin , hist , nobs in zip ( matcher . bin_edges , matcher . hist , matcher . nobservations ) : <EOL> outfile . write ( "<STR_LIT>" % ( bin , hist , nobs ) ) <EOL> outfile . close ( ) <EOL> elif options . fdr_control == "<STR_LIT>" : <EOL> matcher = Nubiscan . MatcherRandomisationSequence ( sense_matrix , <EOL> samples = options . iterations ) <EOL> matches = [ ] <EOL> for seq in FastaIterator . iterate ( options . stdin ) : <EOL> ninput += <NUM_LIT:1> <EOL> mm = matcher . run ( seq . sequence , <EOL> options . arrangements , <EOL> qvalue_threshold = None ) <EOL> for m in mm : <EOL> matches . append ( m . _replace ( sequence = seq . title ) ) <EOL> pvalues = [ x . pvalue for x in matches ] <EOL> fdr = Stats . doFDR ( pvalues ) <EOL> qvalues = fdr . mQValues <EOL> results = [ ] <EOL> for m , qvalue in zip ( matches , qvalues ) : <EOL> if qvalue > options . qvalue_threshold : <EOL> continue <EOL> results . append ( m . _replace ( qvalue = qvalue ) ) <EOL> if options . combine : <EOL> results = Nubiscan . combineMotifs ( results ) <EOL> for r in results : <EOL> options . stdout . write ( "<STR_LIT:\t>" . join ( ( <EOL> r . id , <EOL> "<STR_LIT>" % r . start , <EOL> "<STR_LIT>" % r . end , <EOL> r . strand , <EOL> r . arrangement , <EOL> "<STR_LIT>" % r . score , <EOL> "<STR_LIT>" % r . zscore , <EOL> "<STR_LIT>" % r . pvalue , <EOL> "<STR_LIT>" % r . qvalue ) ) + "<STR_LIT:\n>" ) <EOL> noutput += <NUM_LIT:1> <EOL> elif options . fdr_control == "<STR_LIT>" : <EOL> matcher = Nubiscan . MatcherRandomisationSequence ( sense_matrix , <EOL> samples = options . iterations ) <EOL> for seq in FastaIterator . iterate ( options . stdin ) : <EOL> ninput += <NUM_LIT:1> <EOL> result = matcher . run ( seq . sequence , <EOL> options . arrangements , <EOL> qvalue_threshold = options . qvalue_threshold ) <EOL> if options . combine : <EOL> result = Nubiscan . combineMotifs ( result ) <EOL> t = re . sub ( "<STR_LIT>" , "<STR_LIT>" , seq . title ) <EOL> for r in result : <EOL> options . stdout . write ( "<STR_LIT:\t>" . join ( ( <EOL> t , <EOL> "<STR_LIT>" % r . start , <EOL> "<STR_LIT>" % r . end , <EOL> r . strand , <EOL> r . arrangement , <EOL> "<STR_LIT>" % r . score , <EOL> "<STR_LIT>" % r . zscore , <EOL> "<STR_LIT>" % r . pvalue , <EOL> "<STR_LIT>" % r . qvalue ) ) + "<STR_LIT:\n>" ) <EOL> noutput += <NUM_LIT:1> <EOL> E . info ( "<STR_LIT>" % ( ninput , noutput , nskipped ) ) <EOL> E . Stop ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( main ( sys . argv ) ) </s>
