def GetMetapopSpeciesData(speciesName): years = np.array([2006, 2007, 2012, 2013]) r('source("inSituDataMethods/createSpeciesRaster.R")') r('sampled <- getAllSampledSpeciesMatrix(\'' + speciesName + '\')') r('observed <- getAllObservedSpeciesMatrix(\'' + speciesName + '\')') Observed = np.array(r.get("observed")) Sampled = np.array(r.get("sampled")) return MetapopSpeciesData(speciesName, years, Observed, Sampled)
def load_predict_func(file_path): """Load Predict Function""" LOG.info("Loading predict function from rds file {}".format(file_path)) try: r.source("../mlfmodelserver/deserialize_model.R") return r.get(r.deserialize_model(file_path)) except Exception as generic_exception: LOG.error( "Exception occured while unpickling {}".format(generic_exception)) raise generic_exception
def convert_hgnc2ensembl(hgnc_id): init_biomaRt() v = R.c(hgnc_id) res = R.getBM(attributes=R.c("ensembl_gene_id"), filters="hgnc_symbol", values=v, mart=__mart) try: return R.get("ensembl_gene_id", res)[0] except: print 'Error convert_hgnc2ensembl: '+str(hgnc_id)+' not found in database' return None
def convert_list_ensembl2hgnc(ensembl_id_list): init_biomaRt() v = R.c(ensembl_id_list) res = R.getBM(attributes=R.c("hgnc_symbol"), filters="ensembl_gene_id", values=v, mart=__mart) try: return R.get("hgnc_symbol", res) except: print 'Error convert_ensembl2hgnc: '+str(ensembl_id)+' not found in database' return None
def _read_ExpressionSet_RData(RData): """Read ExpressionSet RData to Rpy2 robjects. RData: Path to the input RData file. ExpressionSet must be the only object in the RData. Return Rpy2's eSet object, assayData, featureData, phenotypeData. """ importr('Biobase') rdata = r.load(RData) eSet = r.get(rdata) # rpy2 ExpressionSet object (assumed) assayData = r.assayData(eSet) # rpy2 environment object fData = r.fData(eSet) # rpy2 DataFrame object pData = r.pData(eSet) # rpy2 DataFrame object return eSet, assayData, fData, pData
def execute_r( r_code: str, packages: list, inputs: dict, outputs: list, overwrite=False, side_effect=False, ) -> dict: """ Execute R code and return the result. """ call = (r_env if side_effect else r_env.eval ) # Ignore print statements if side_effect is False for pkg in packages: call(f"library({pkg})") for k, val in inputs.items(): if overwrite or not r_env.exists(k)[0]: r_env.assign(k, val) call(r_code) return {o: convert(r_env.get(o)) for o in outputs}
def getEOdataframeAtTraps(): pandas2ri.activate() r('source("EOData/EOData_Traps.R")') r('EOMatrix <- getEOMatrix_Traps()') EO_df = pandas2ri.ri2py_dataframe(r.get("EOMatrix")) return EO_df
def GetInitialPresenceProbabilitySDM(speciesName): r('source("SDM/SDM_Metapop.R")') r('P <- getInitialPresenceGuessSDM(\'' + speciesName + '\')') P = np.array(r.get("P")) return P
def getMetapopVariables(res): pandas2ri.activate() r('source("EOData/EOData_Metapop.R")') r('EOMatrix <- getEOMatrix_Metapop()') EOMatrix = np.array(r.get("EOMatrix")) return MetapopVariables(EOMatrix)