<s> '''<STR_LIT>''' <EOL> import sys <EOL> import re <EOL> import collections <EOL> import CGAT . Experiment as E <EOL> import CGAT . IOTools as IOTools <EOL> DATA = collections . namedtuple ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def main ( argv = None ) : <EOL> """<STR_LIT>""" <EOL> if not argv : <EOL> argv = sys . argv <EOL> parser = E . OptionParser ( version = "<STR_LIT>" , <EOL> 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 ( <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_option ( <EOL> "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . set_defaults ( min_overlap = <NUM_LIT:10> , <EOL> invert = False , <EOL> pattern_window = "<STR_LIT>" ) , <EOL> ( options , args ) = E . Start ( parser , argv = argv , add_output_options = True ) <EOL> outfiles = IOTools . FilePool ( options . output_filename_pattern ) <EOL> if options . invert : <EOL> test_f = lambda l2fold : l2fold < <NUM_LIT:0> <EOL> else : <EOL> test_f = lambda l2fold : l2fold > <NUM_LIT:0> <EOL> def read ( ) : <EOL> rx_window = re . compile ( options . pattern_window ) <EOL> for data in IOTools . iterate ( options . stdin ) : <EOL> contig , start , end = rx_window . match ( data . test_id ) . groups ( ) <EOL> start , end = map ( int , ( start , end ) ) <EOL> yield DATA . _make ( ( data . test_id , <EOL> contig , start , end , <EOL> data . treatment_name , <EOL> float ( data . treatment_mean ) , <EOL> float ( data . treatment_std ) , <EOL> data . control_name , <EOL> float ( data . control_mean ) , <EOL> float ( data . control_std ) , <EOL> float ( data . pvalue ) , <EOL> float ( data . qvalue ) , <EOL> float ( data . l2fold ) , <EOL> float ( data . fold ) , <EOL> int ( data . significant ) , <EOL> data . status , <EOL> <NUM_LIT:0> ) ) <EOL> def grouper ( data , distance = <NUM_LIT:10> ) : <EOL> last = data . next ( ) <EOL> entries = [ last ] <EOL> while <NUM_LIT:1> : <EOL> d = data . next ( ) <EOL> if d is None : <EOL> break <EOL> if d . contig == last . contig and d . start < last . start : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if ( ( d . contig != last . contig ) or <EOL> ( d . start - last . end > distance ) or <EOL> ( d . status != last . status ) or <EOL> ( d . significant != last . significant ) or <EOL> ( d . l2fold * last . l2fold < <NUM_LIT:0> ) ) : <EOL> yield entries <EOL> entries = [ ] <EOL> entries . append ( d ) <EOL> last = d <EOL> yield entries <EOL> counter = E . Counter ( ) <EOL> options . stdout . write ( "<STR_LIT:\t>" . join ( DATA . _fields ) + "<STR_LIT:\n>" ) <EOL> samples = set ( ) <EOL> all_data = list ( read ( ) ) <EOL> all_data . sort ( key = lambda x : ( x . contig , x . start ) ) <EOL> group_id = <NUM_LIT:0> <EOL> for group in grouper ( iter ( all_data ) , distance = options . min_overlap ) : <EOL> group_id += <NUM_LIT:1> <EOL> start , end = group [ <NUM_LIT:0> ] . start , group [ - <NUM_LIT:1> ] . end <EOL> assert start < end , '<STR_LIT>' % str ( group ) <EOL> n = float ( len ( group ) ) <EOL> counter . input += n <EOL> g = group [ <NUM_LIT:0> ] <EOL> if g . l2fold < <NUM_LIT:0> : <EOL> l2fold = max ( [ x . l2fold for x in group ] ) <EOL> fold = max ( [ x . fold for x in group ] ) <EOL> else : <EOL> l2fold = min ( [ x . l2fold for x in group ] ) <EOL> fold = min ( [ x . fold for x in group ] ) <EOL> outdata = DATA . _make ( ( <EOL> str ( group_id ) , <EOL> g . contig , start , end , <EOL> g . treatment_name , <EOL> sum ( [ x . treatment_mean for x in group ] ) / n , <EOL> max ( [ x . treatment_std for x in group ] ) , <EOL> g . control_name , <EOL> sum ( [ x . control_mean for x in group ] ) / n , <EOL> max ( [ x . control_std for x in group ] ) , <EOL> max ( [ x . pvalue for x in group ] ) , <EOL> max ( [ x . qvalue for x in group ] ) , <EOL> l2fold , <EOL> fold , <EOL> g . significant , <EOL> g . status , <EOL> int ( n ) ) ) <EOL> samples . add ( g . treatment_name ) <EOL> samples . add ( g . control_name ) <EOL> if g . significant : <EOL> if test_f ( g . l2fold ) : <EOL> outfiles . write ( <EOL> g . treatment_name , "<STR_LIT>" % ( <EOL> g . contig , g . start , g . end , <EOL> group_id , <EOL> sum ( [ x . treatment_mean for x in group ] ) / n ) ) <EOL> else : <EOL> outfiles . write ( <EOL> g . control_name , "<STR_LIT>" % ( <EOL> g . contig , g . start , g . end , <EOL> group_id , <EOL> sum ( [ x . control_mean for x in group ] ) / n ) ) <EOL> options . stdout . write ( "<STR_LIT:\t>" . join ( map ( str , outdata ) ) + "<STR_LIT:\n>" ) <EOL> counter . output += <NUM_LIT:1> <EOL> for sample in samples : <EOL> outfiles . write ( sample , "<STR_LIT>" ) <EOL> outfiles . close ( ) <EOL> E . info ( "<STR_LIT:%s>" % counter ) <EOL> E . Stop ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( main ( sys . argv ) ) </s>
