def run(self): """ 06-23-05 only import rpy when in type 2 because import rpy doesn't work in qsubbed jobs run --dstruc_loadin --[type==1] --reformat --get_weight --[type==2] --draw_graph --visualize_clusters --draw_graph """ self.dstruc_loadin() if self.type == 1: if self.output_file: self.reformat(self.input_graph, self.output_file, self.no_of_genes) else: sys.stderr.write("Please the output file.\n") sys.exit(2) elif self.type == 2: from rpy import r if self.r_fname: if self.cluster_file: self.visualize_clusters(self.cluster_file, self.input_graph, self.label_dict[self.label],\ self.r_fname, self.functioncolor, self.plot_type) else: self.draw_graph(self.input_graph, self.label_dict[self.label], self.r_fname,\ self.functioncolor, self.plot_type) r.source(self.r_fname) stop = raw_input("Exit? Y/n:") else: sys.stderr.write("Please give the R_FILE.\n")
def calcKinship(snps): """ Requires EMMA to be installed. """ a = array(snps) #r.library("emma") r.source("emma.R") return r.emma_kinship(a)
def calcKinship(snps): """ Requires EMMA to be installed. """ a = array(snps) #r.library("emma") print r.source("emma.R") print a #print r.emma_kinship(a) return r.emma_kinship(a)
def runEmma(phed, p_i, k, snps): #Assume that the accessions are ordered. i = phed.getPhenIndex(p_i) #r.library("emma") r.source("emma.R") phenValues = [] for vals in phed.phenotypeValues: phenValues.append(float(vals[i])) phenArray = array([phenValues]) snpsArray = array(snps) res = r.emma_REML_t(phenArray, snpsArray, k) #print res return res
def runEmma(phed,p_i,k,snps): #Assume that the accessions are ordered. i = phed.getPhenIndex(p_i) #r.library("emma") r.source("emma.R") phenValues = [] for vals in phed.phenotypeValues: phenValues.append(float(vals[i])) phenArray = array([phenValues]) snpsArray = array(snps) res = r.emma_REML_t(phenArray,snpsArray,k) #print res return res
def getRobject(): global robj if robj: log.debug("manalysis.getRobject(): R already imported.") return robj else: log.debug("manalysis.getRobject(): R not found. Importing.") from rpy import r as robj # Set up R interface... manalysis_file=config.get('analysis','manalysis_file') robj.source(manalysis_file) log.debug("R interface to manalysis set up via file %s" % manalysis_file) return robj
def testLapackLoad(self): from rpy import r fi = 'logit.r' #--load the functions r.source(fi) #--test a example x =50000 n = [x, x-500,x+460,x-400,x-100,x-4] y = [12, 24, 23, 2,4,5] f = [2,2,2,1,1,1] jk = r.logit_1fact(n,y,f)
def run(self): """ 03-09-05 04-01-05 """ (conn, curs) = db_connect(self.hostname, self.dbname, self.schema) self.dstruc_loadin(curs) if self.r_fname: self.r_f = open(self.r_fname, 'w') if self.type == 1: subgraph = self.get_subgraph(curs, self.table, self.mcl_table, self.mcl_id) #unweighted weighted=0 self.subgraph_output(self.r_f, subgraph, self.label_dict[self.label], self.global_gene_to_go_dict, \ self.centralnode, self.function, self.functioncolor, self.plot_type, weighted) self.r_f.close() r.source(self.r_fname) raw_input("Pause:\t") elif self.type == 2: if self.gene_table==None or self.gene_p_table==None: sys.stderr.write("Error: Please specify both the gene_p_table and gene_table.\n") sys.exit(2) subgraph = self.context_subgraph(curs, self.table, self.mcl_table, self.gene_p_table, self.gene_table, \ self.centralnode, self.function) self.subgraph_output(self.r_f, subgraph, self.label_dict[self.label], self.global_gene_to_go_dict, \ self.centralnode, self.function, self.functioncolor, self.plot_type) self.r_f.close() r.source(self.r_fname) raw_input("Pause:\t") elif self.type == 3: for i in range(self.no_of_datasets): sys.stdout.write("Dataset %s\n"%(i+1)) if self.edge_table==None: sys.stderr.write("Error: Please specify both the edge_table.\n") sys.exit(2) sub_subgraph = self.subgraph_in_one_dataset(curs, self.table, self.mcl_table, self.edge_table, self.mcl_id, i) self.subgraph_output(self.r_f, sub_subgraph, self.label_dict[self.label], self.global_gene_to_go_dict, \ self.centralnode, self.function, self.functioncolor, self.plot_type) self.r_f.close() r.source(self.r_fname) #asking to continue or not no_stop = raw_input("Continue? Y/n:\t") if no_stop == 'n' or no_stop == 'N': sys.exit(3) #open it again for the next dataset self.r_f = open(self.r_fname, 'w') elif self.type==4: subgraph = self.get_subgraph(curs, self.table, self.mcl_table, self.mcl_id) original_subgraph = self.get_original_graph(curs, subgraph) self.subgraph_output(self.r_f, original_subgraph, self.label_dict[self.label], self.global_gene_to_go_dict, \ self.centralnode, self.function, self.functioncolor, self.plot_type) self.r_f.close() r.source(self.r_fname) raw_input("Pause:\t")
def __call__(self, gen, semis, morpho_f, phyllo_f, sen_f, prev_f, alpha_f, plan_f, fname): os.chdir(path) r.source('writeparsGL.R') r.readExcell.local_mode(0) r.as_data_frame.local_mode(0) bloc = "B1" #B1 par defaut (changer si bcp de variabilite entre blocs) # dimensions des organes (en, gaine, longueur largeur de feuilles) f(gen,semis) morpho = r.readExcell(morpho_f, gen+'_'+semis) # tableau angles des feuilles f(gen,semis) anglesPrev = r.readExcell(prev_f, gen+'_'+semis) # dynamique nbvis et nblig dev = r.readExcell(phyllo_f, gen+'_'+semis) n_max = round(dev.as_py()['nbvis'][-1]) # dynamique de senescence r.readExcell.local_mode(1) sen = r.readExcell(sen_f, gen+'_'+semis) r.readExcell.local_mode(0) sen['TT'].append(3000.) sen['nb_sen'].append(n_max+1) sen = r.as_data_frame(sen) # formes de limbes alpha = r.readExcell(alpha_f,gen) #plan parcelle plan = r.readExcell(plan_f, gen+'_'+semis+'_'+bloc) #fname = "_05_geomF2_S1B1.h" txt1 = r.writenumf (fname,dev,morpho,anglesPrev) print txt1 r.writePO (fname) r.writecarto(fname, plan, 0.8, 0.125) txt2 = r.writedimkin(fname, dev, sen, morpho) print txt2 r.writeprev(fname, alpha, anglesPrev, dev) os.chdir(path_ini) return join(join(path, 'temp'),fname)
def __call__(self, gen, semis, morpho_f, phyllo_f, sen_f, prev_f, alpha_f, plan_f, fname): os.chdir(path) r.source('writeparsGL.R') r.readExcell.local_mode(0) r.as_data_frame.local_mode(0) bloc = "B1" #B1 par defaut (changer si bcp de variabilite entre blocs) # dimensions des organes (en, gaine, longueur largeur de feuilles) f(gen,semis) morpho = r.readExcell(morpho_f, gen + '_' + semis) # tableau angles des feuilles f(gen,semis) anglesPrev = r.readExcell(prev_f, gen + '_' + semis) # dynamique nbvis et nblig dev = r.readExcell(phyllo_f, gen + '_' + semis) n_max = round(dev.as_py()['nbvis'][-1]) # dynamique de senescence r.readExcell.local_mode(1) sen = r.readExcell(sen_f, gen + '_' + semis) r.readExcell.local_mode(0) sen['TT'].append(3000.) sen['nb_sen'].append(n_max + 1) sen = r.as_data_frame(sen) # formes de limbes alpha = r.readExcell(alpha_f, gen) #plan parcelle plan = r.readExcell(plan_f, gen + '_' + semis + '_' + bloc) #fname = "_05_geomF2_S1B1.h" txt1 = r.writenumf(fname, dev, morpho, anglesPrev) print txt1 r.writePO(fname) r.writecarto(fname, plan, 0.8, 0.125) txt2 = r.writedimkin(fname, dev, sen, morpho) print txt2 r.writeprev(fname, alpha, anglesPrev, dev) os.chdir(path_ini) return join(join(path, 'temp'), fname)
def run_emma_w_missing_data(snpsd, phed, p_i, chromosome, k, missing_val='NA'): """ Runs Emma w. missing data. """ #Assume that the accessions are ordered. snps = snpsd.snps r.source("emma.R") phen_vals = phed.getPhenVals(p_i, noNAs=False) if phen_vals.count('NA'): print "Coordinating SNPs and phenotypes, removing %d accessions which were missing phenotypic values." % phen_vals.count( 'NA') ai_to_keep = [i for i in range(len(phen_vals)) if phen_vals[i] != 'NA'] #print ai_to_keep new_snps = [] for snp in snps: new_snps.append([snp[i] for i in ai_to_keep]) snps = new_snps #print snps[0] phen_vals = [phen_vals[i] for i in ai_to_keep] #print phen_vals new_k = zeros((len(ai_to_keep), len(ai_to_keep))) for j1 in range(len(ai_to_keep)): for j2 in range(len(ai_to_keep)): new_k[j1, j2] = k[ai_to_keep[j1], ai_to_keep[j2]] k = new_k p_vals = [] for snp in snps: if snp.count(missing_val): acc_to_keep = [] new_snp = [] for j, nt in enumerate(snp): if not nt == missing_val: acc_to_keep.append(j) new_snp.append(nt) new_k = zeros((len(acc_to_keep), len(acc_to_keep))) new_phen_vals = [] for j1 in range(len(acc_to_keep)): for j2 in range(len(acc_to_keep)): new_k[j1, j2] = k[acc_to_keep[j1], acc_to_keep[j2]] new_phen_vals.append(phen_vals[acc_to_keep[j1]]) if len(set(snp)) == 1: p_vals.append(1) print phenArray print snpsArray print new_k else: phenArray = array([new_phen_vals]) snpsArray = array([new_snp]) #print "Running EMMA w. missing data." try: p_vals.extend( r.emma_REML_t(phenArray, snpsArray, new_k)['ps']) except Exception, err_str: print err_str print phenArray print snpsArray print new_k else: phenArray = array([phen_vals]) snpsArray = array([snp]) #print "Running EMMA w.o. missing data." try: p_vals.extend(r.emma_REML_t(phenArray, snpsArray, k)['ps']) except Exception, err_str: print err_str print phenArray print snpsArray print k
from fast_krige import * import st_cov_fun from parse_and_check import * import time import copy as cp from mbgw.master_grid import * from mbgw import auxiliary_data import os curpath = os.getcwd() import mbgw mbgw_root = __root__ = mbgw.__path__[0] r_path = mbgw_root+'/joint_simulation/CONDSIMalgorithm' os.chdir(r_path) from examineRealization import * from rpy import r r.source("CONDSIMpreloop.R") r.source("CONDSIMmonthloop.R") r.source('MVRNORM.R') mvrnormPY=r['MVRNORM'] os.chdir(curpath) import scipy from scipy import ndimage, mgrid from map_utils import grid_convert #####################TEMP #from map_utils import getEmpiricalCovarianceFunction_STmarginals #from map_utils import plotEmpiricalCovarianceFunction #####################TEMP from IPython.Debugger import Pdb
return data, is_correct_list known_fname = '/tmp/hs_fim_92m5x25bfsdfl10q0_7gf1.known' unknown_fname = '/tmp/hs_fim_92m5x25bfsdfl10q0_7gf1.unknown' known_data, known_is_correct_list = read_data(known_fname) unknown_data, unknown_is_correct_list = read_data(unknown_fname) from numarray import array from rpy import r, set_default_mode,NO_CONVERSION,BASIC_CONVERSION set_default_mode(NO_CONVERSION) #pack data into data_frame known_data = array(known_data) known_data_frame = r.as_data_frame({"p_value":known_data[:,0], "recurrence":known_data[:,1], "connectivity":known_data[:,2], \ "cluster_size":known_data[:,3], "gradient":known_data[:,4]}) unknown_data = array(unknown_data) unknown_data_frame = r.as_data_frame({"p_value":unknown_data[:,0], "recurrence":unknown_data[:,1], "connectivity":unknown_data[:,2], \ "cluster_size":unknown_data[:,3], "gradient":unknown_data[:,4]}) #start to call randomF.r to run randomForest r.library('randomForest') r.source('randomF.r') #rf_model still needs to be in pure R object rf_model = r.randomF(known_data_frame, known_data[:,-1]) set_default_mode(BASIC_CONVERSION) unknown_pred = r.predictRandomF(rf_model, unknown_data_frame) rf_model= rf_model.as_py(BASIC_CONVERSION) print rf_model.keys() print rf_model['confusion']
from fast_krige import * import st_cov_fun from parse_and_check import * import time import copy as cp from mbgw.master_grid import * from mbgw import auxiliary_data import os curpath = os.getcwd() import mbgw mbgw_root = __root__ = mbgw.__path__[0] r_path = mbgw_root + '/joint_simulation/CONDSIMalgorithm' os.chdir(r_path) from examineRealization import * from rpy import r r.source("CONDSIMpreloop.R") r.source("CONDSIMmonthloop.R") r.source('MVRNORM.R') mvrnormPY = r['MVRNORM'] os.chdir(curpath) import scipy from scipy import ndimage, mgrid from map_utils import grid_convert #####################TEMP #from map_utils import getEmpiricalCovarianceFunction_STmarginals #from map_utils import plotEmpiricalCovarianceFunction #####################TEMP from IPython.Debugger import Pdb