Ejemplo n.º 1
0
    def _compute_adjacency(self):
        info = self.pairwise_information_

        method = self.adjacency_method
        kwargs = self.adjacency_method_kwargs
        if kwargs is None:
            kwargs = {}

        adjacency_matrix = None
        if method == 'raw':
            adjacency_matrix = info
        else:
            minet = importr('minet')

            if method == 'mrnet':
                func = minet.mrnet
            elif method == 'clr':
                func = minet.clr
            elif method == 'aracne':
                func = minet.aracne
            else:
                raise ValueError('Unsupported method {!r}'.format(method))

            ans = func(numpy2ri.numpy2rpy(np.asarray(info)), **kwargs)
            ans = np.asarray(ans)
            ans = pd.DataFrame(ans, index=info.index, columns=info.columns)
            adjacency_matrix = ans

        self.adjacency_matrix_ = adjacency_matrix
        self.adjacency_ = stack_triu(adjacency_matrix)
Ejemplo n.º 2
0
def py2rpy_pandasindex(obj):
    if obj.dtype.kind == 'O':
        return StrVector(obj)
    else:
        # pandas2ri should definitely not have to know which paths remain to be
        # converted by numpy2ri
        # Answer: the thing is that pandas2ri builds on the conversion
        # rules defined by numpy2ri - deferring to numpy2ri is allowing
        # us to reuse that code.
        return numpy2ri.numpy2rpy(obj)
Ejemplo n.º 3
0
from rpy2.robjects.packages import importr
import numpy as np

json_f = sys.argv[1]
brain_part = sys.argv[2]

with open(json_f, "r") as jf:
    ce_dict = json.load(jf)
    for region in ce_dict:
        for stage in ce_dict[region]:
            ce_wdict = []
            for c in ce_dict[region][stage]:
                cluster = ce_dict[region][stage][c]
                mat = np.array([[cluster["x"], cluster["m_x"]],
                                [cluster["T_x"], cluster["n"]]])
                r_cont = numpy2rpy(mat)
                r.assign("cont", r_cont)
                r('fmat_pval <- fisher.test(cont, alternative="greater",conf.int=TRUE)$p.value'
                  )
                r('fmat_estimate <- fisher.test(cont, alternative="greater",conf.int=TRUE)$estimate'
                  )
                r('fmat_conf <- fisher.test(cont, alternative="greater",conf.int=TRUE)$conf.int'
                  )
                pval = r("fmat_pval")
                odds = r("fmat_estimate")
                conf_int = r("fmat_conf")
                ce_wdict.append({
                    "Cluster_name":
                    cluster["Cluster_num"],
                    "CE_Stage_region":
                    cluster["x"],
            jdict[fstr]=json.load(wr)

comparisons=["prenatal_adulthood",
             "early_childhood_adulthood"]

i=1
for cnum in range(0,cluster_num+1):
    region_d={}
    for r in regions:
        comp_d={}
        for comparison in comparisons:
            jd="proportion"+"_"+r+"_"+comparison
            clus_details=jdict[jd]["Cluster"+str(cnum)]
            ce=np.array([clus_details["a"],clus_details["b"]])
            univ=np.array([clus_details["U"],clus_details["U"]])
            r_ce=numpy2rpy(ce)
            r_univ=numpy2rpy(univ)
            R.assign("ce", r_ce)
            R.assign("univ", r_univ)
            omat=FloatVector([clus_details["a"],clus_details["b"],clus_details["U"],clus_details["U"]])
            R('pmat <- prop.test(x=ce,n=univ,alternative="greater",conf.level=0.95)$p.value')
            pval=float((str(R("pmat")).split(" ")[1]).strip("\n"))
            epiod=epitools.oddsratio(omat)
            odds=epiod[1][1]
            comp_d[comparison+"_pval"]=pval
            comp_d[comparison+"_pval_adj"]=float(pval)*float(2)*(float(cluster_num+1))
            comp_d[comparison+"_odds_ratio"]=odds
        region_d[r]=comp_d
         
    with open(os.path.join(work_dir,brain_part+"_prop_summary",brain_part+"_proportion_cluster"+str(cnum)+".csv"),"w") as wh:
        writer=csv.writer(wh)