<s> '''<STR_LIT>''' <EOL> import sys <EOL> import sqlite3 <EOL> import collections <EOL> import pandas . io . sql as pdsql <EOL> import numpy as np <EOL> from rpy2 . robjects import r as R <EOL> from rpy2 . robjects . numpy2ri import numpy2ri <EOL> import CGAT . Experiment as E <EOL> import CGAT . IOTools as IOTools <EOL> def testSignificanceOfMatrices ( background , <EOL> foreground , <EOL> database , <EOL> match_table , <EOL> outfile , <EOL> header_line = True , <EOL> direction = "<STR_LIT>" ) : <EOL> '''<STR_LIT>''' <EOL> R ( '''<STR_LIT>''' ) <EOL> interval_sets = { "<STR_LIT>" : set ( ) , "<STR_LIT>" : set ( ) } <EOL> for name , fn in ( ( "<STR_LIT>" , background ) , <EOL> ( "<STR_LIT>" , foreground ) ) : <EOL> inf = IOTools . openFile ( fn ) <EOL> if header_line : <EOL> inf . readline ( ) <EOL> for line in inf : <EOL> interval_sets [ name ] . add ( line [ : - <NUM_LIT:1> ] ) <EOL> dbh = sqlite3 . connect ( database ) <EOL> ids_tfs = collections . defaultdict ( set ) <EOL> all_tfs = set ( ) <EOL> state = "<STR_LIT>" % match_table <EOL> sql_df = pdsql . read_sql ( sql = state , con = dbh , index_col = None , <EOL> chunksize = <NUM_LIT> ) <EOL> for chunk in sql_df : <EOL> _df = chunk <EOL> sql_group = _df . groupby ( by = '<STR_LIT>' ) <EOL> for names , groups in sql_group : <EOL> tfs = groups [ groups [ "<STR_LIT>" ] == names ] [ '<STR_LIT>' ] . tolist ( ) <EOL> ids_tfs [ names ] . update ( tfs ) <EOL> all_tfs . update ( tfs ) <EOL> outf = IOTools . openFile ( outfile , "<STR_LIT:w>" ) <EOL> outf . write ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> fisherpy = R [ "<STR_LIT>" ] <EOL> padjpy = R [ "<STR_LIT>" ] <EOL> results = [ ] <EOL> pvalues = [ ] <EOL> for tf in all_tfs : <EOL> contingency = np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:2> ) ) <EOL> for seq_id in ids_tfs . keys ( ) : <EOL> if seq_id in interval_sets [ "<STR_LIT>" ] : <EOL> if tf in ids_tfs [ seq_id ] : <EOL> contingency [ <NUM_LIT:0> , <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> elif tf not in ids_tfs [ seq_id ] : <EOL> contingency [ <NUM_LIT:1> , <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> elif seq_id in interval_sets [ "<STR_LIT>" ] : <EOL> if tf in ids_tfs [ seq_id ] : <EOL> contingency [ <NUM_LIT:0> , <NUM_LIT:1> ] += <NUM_LIT:1> <EOL> elif tf not in ids_tfs [ seq_id ] : <EOL> contingency [ <NUM_LIT:1> , <NUM_LIT:1> ] += <NUM_LIT:1> <EOL> f = fisherpy ( numpy2ri ( contingency ) , alternative = direction ) <EOL> nforeground = contingency [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> nbackground = contingency [ <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> tforeground = contingency [ <NUM_LIT:0> , <NUM_LIT:0> ] + contingency [ <NUM_LIT:1> , <NUM_LIT:0> ] <EOL> tbackground = contingency [ <NUM_LIT:0> , <NUM_LIT:1> ] + contingency [ <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> f = [ list ( x ) for x in np . array ( f ) ] <EOL> pvalue , ci_low , ci_hi , OR = f [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , f [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , f [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] , f [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] <EOL> pvalues . append ( pvalue ) <EOL> if ( nforeground + nbackground == <NUM_LIT:0> or <EOL> ( nforeground == tforeground and nbackground == tbackground ) ) : <EOL> OR = <NUM_LIT:1> <EOL> results . append ( [ tf , OR , ci_low , ci_hi , pvalue , nforeground , <EOL> nbackground , tforeground , tbackground ] ) <EOL> qvalues = padjpy ( pvalues ) <EOL> for i in range ( len ( results ) ) : <EOL> outf . write ( "<STR_LIT:\t>" . join ( map ( str , results [ i ] + [ qvalues [ i ] ] ) ) + "<STR_LIT:\n>" ) <EOL> outf . close ( ) <EOL> def main ( argv = None ) : <EOL> """<STR_LIT>""" <EOL> if argv is None : <EOL> argv = sys . argv <EOL> parser = E . OptionParser ( version = "<STR_LIT>" , <EOL> usage = globals ( ) [ "<STR_LIT>" ] ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" ) <EOL> ( options , args ) = E . Start ( parser , argv = argv ) <EOL> PipelineTFM . testSignificanceOfMatrices ( options . background , <EOL> options . foreground , <EOL> options . database , <EOL> options . match_table , <EOL> options . outfile , <EOL> options . genesets , <EOL> options . direction ) <EOL> E . Stop ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( main ( sys . argv ) ) </s>
