<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">"""
Wrappers for common R functions.
"""

import os
import csv
import numpy
import tempfile

def runRString(str):
    """ Runs str and returns whatever it outputs """
    tmpFile = open('/tmp/rscript.R', 'w')
    tmpFile.write(str)
    tmpFile.close()
    
    os.system("Rscript /tmp/rscript.R &gt; /tmp/r.out")
    
    return open('/tmp/r.out').read()

def wilcox_test(x,y):
    """ Runs R wilcox.test(x,y), returns the p-value """
    cx = "c(" + ','.join([str(v) for v in x]) + ")"
    cy = "c(" + ','.join([str(v) for v in y]) + ")"
    
    script = """
x = %s
y = %s
print( wilcox.test(x,y)$p.value )
""" % (cx,cy)
    
    return float(runRString(script).strip('\n ').split(' ')[-1])

def tTest(x,y):
    """ Runs R t.test(x,y), returns the p-value """
    
    cx = "c(" + ','.join([str(v) for v in x]) + ")"
    cy = "c(" + ','.join([str(v) for v in y]) + ")"
    
    script = """
x = %s
y = %s
print( t.test(x,y)$p.value )
""" % (cx,cy)
    
    return float(runRString(script).strip('\n ').split(' ')[-1])

def chisquared_test(x):
    """ Runs a chisquared test and rturns the p-value. """
    cx = "matrix(c(%d,%d,%d,%d),nrow=2)" % (x[0,0], x[1,0], x[0,1], x[1,1])
    script = """x = %s\nprint( chisq.test(x)$p.value )\n""" % cx
    return float(runRString(script).strip('\n ').split(' ')[-1])

#
def fisher_test(x):
    """ Runs a fisher's exact test and rturns the p-value. """
    cx = "matrix(c(%d,%d,%d,%d),nrow=2)" % (x[0,0], x[1,0], x[0,1], x[1,1])
    script = """x = %s\nprint( fisher.test(x)$p.value )\n""" % cx
    return float(runRString(script).strip('\n ').split(' ')[-1])

def apply_cor_test(data):
    """
    Runs a correlation test on each row of the data, which is a
    list of lists.
    """
    tmp_file = tempfile.mkstemp(suffix=".csv")[-1]
    out_file = tempfile.mkstemp(suffix=".csv")[-1]
    tmp_fh = open(tmp_file, 'w')
    writer = csv.writer(tmp_fh)
    writer.writerows(data)
    tmp_fh.close()
    
    script = """
data &lt;- read.csv('%s', header=F)
run.cor.test &lt;- function(row) {
    x = c(rep(1,row[1]), rep(1,row[2]), rep(0,row[3]), rep(0,row[4]))
    y = c(rep(1,row[1]), rep(0,row[2]), rep(1,row[3]), rep(0,row[4]))
    res &lt;- cor.test(x,y)
    return(c( res$p.value, res$estimate ))
}
result &lt;- t(apply(data, 1, run.cor.test))
write.csv(result, file='%s')
""" % (tmp_file, out_file)
    
    print script
    
    tmp_fh = open(out_file)
    result = [row for row in csv.reader(tmp_fh)]
    tmp_fh.close()
    
    return result

def apply_fisher_test(data):
    """
    Runs a fisher's exact test on each row of data, which is a 
    list of lists.
    """
    tmp_file = '/tmp/fisher_data.csv'
    tmp_fh = open(tmp_file,'w')
    writer = csv.writer(tmp_fh)
    for row in data:
        writer.writerow(row)
    tmp_fh.close()
    
    script = """
data &lt;- read.csv('%s',header=F)
pvals &lt;- rep(NA,nrow(data))
run.fisher &lt;- function(row) {
    fisher.test( matrix(row, nrow=2) )$p.value
}
pvals &lt;- apply(data, 1, run.fisher)
for (i in 1:length(pvals)) {
    print(pvals[i])
}
""" % tmp_file
    
    return [x.split(' ')[1] for x in runRString(script).split('\n') if not x == '']
    </pre></body></html>