def omap_to_orths(omap): orths = [] for i in range(len(omap)): (organism1, gene1) = omap.keys()[i] (organism2, gene2) = omap[omap.keys()[i]][0] orths.append([fr.one_gene(name=gene1,organism=organism1), fr.one_gene(name=gene2, organism=organism2)]) return orths
def eval_network_roc(net, genes, tfs, priors): from matplotlib import pyplot as plt org = priors[0][0].organism priors_set = set(priors) scores = np.zeros(len(genes)*len(tfs)) labels = np.zeros(len(genes)*len(tfs)) i=0 for tfi in range(len(tfs)): for gi in range(len(genes)): tf = tfs[tfi] g = genes[gi] score = np.abs(net[tfi, gi]) label = 0 if (fl.one_gene(tf, org), fl.one_gene(g, org)) in priors_set: label = 1 if (fl.one_gene(g, org), fl.one_gene(tf, org)) in priors_set: label = 1 scores[i] = score labels[i] = label i += 1 (fpr, tpr, t) = roc_curve(labels, scores) auroc = auc(fpr, tpr) plt.plot(fpr, tpr) plt.show() print auroc return (labels, scores)
def random_orth(genes1, genes2, organisms, n_orth): orths = [] for i in range(n_orth): g1 = genes1[int(np.floor(random.random() * len(genes1)))] g2 = genes2[int(np.floor(random.random() * len(genes2)))] orth_group = [fl.one_gene(g1, organisms[0]), fl.one_gene(g2, organisms[1])] orths.append(orth_group) return orths
def load_priors(priors_fn, organism): p = file(priors_fn) ps = p.read() psn = filter(len, ps.split('\n')) psnt = map(lambda x: x.split('\t'), psn) priors = map(lambda x: (fl.one_gene(x[0], organism), fl.one_gene(x[1], organism)), psnt) signs = map(lambda x: [-1,1][x[2]=='activation'], psnt) p.close() return (priors,signs)
def load_orth(orth_fn, organisms): f = file(orth_fn) fs = f.read() fsn = filter(len, fs.split('\n')) fsnt = map(lambda x: x.split('\t'), fsn) #print fsn orths = [] for o in fsnt: #print o orths.append([fl.one_gene(name=o[0],organism=organisms[0]), fl.one_gene(name=o[1], organism=organisms[1])]) return orths
def eval_network_pr(net, genes, tfs, priors): from matplotlib import pyplot as plt org = priors[0][0].organism priors_set = set(priors) gene_to_ind = {genes[x] : x for x in range(len(genes))} tf_to_ind = {tfs[x] : x for x in range(len(tfs))} gene_marked = np.zeros(len(genes)) != 0 tf_marked = np.zeros(len(tfs)) != 0 for prior in priors: gene_marked[gene_to_ind[prior[0].name]] = True gene_marked[gene_to_ind[prior[1].name]] = True if prior[0].name in tf_to_ind: tf_marked[tf_to_ind[prior[0].name]] = True if prior[1].name in tf_to_ind: tf_marked[tf_to_ind[prior[1].name]] = True genes = np.array(genes)[gene_marked] tfs = np.array(tfs)[tf_marked] net = net[:, gene_marked] net = net[tf_marked, :] scores = np.zeros(len(genes)*len(tfs)) labels = np.zeros(len(genes)*len(tfs)) i=0 for tfi in range(len(tfs)): for gi in range(len(genes)): tf = tfs[tfi] g = genes[gi] score = np.abs(net[tfi, gi]) label = 0 if (fl.one_gene(tf, org), fl.one_gene(g, org)) in priors_set: label = 1 if (fl.one_gene(g, org), fl.one_gene(tf, org)) in priors_set: label = 1 scores[i] = score labels[i] = label i += 1 (precision, recall,t) = precision_recall_curve(labels, scores)#prc(scores, labels) aupr = auc(recall, precision) #plt.plot(recall, precision) #plt.xlabel('recall') #plt.ylabel('precision') #plt.title('B subtilis alone, no refitting') #plt.show() return aupr
def test_scadBs_refit(b1, b2, fusiongroups, xsamples1, xsamples2, noise_std1, noise_std2, p_falsep, p_falseneg, lamP, lamR, lamS, it, k, s_it): # TFs = [map(lambda x: str(x)+'tfa', range(b1.shape[0])), map(lambda x: str(x)+'tfb', range(b2.shape[0]))] # Gs = [map(lambda x: str(x)+'ga', range(b1.shape[1])), map(lambda x: str(x)+'gb', range(b2.shape[1]))] TFs = [map(lambda x: x, range(b1.shape[0])), map(lambda x: x, range(b2.shape[0]))] Gs = [map(lambda x: x, range(b1.shape[1])), map(lambda x: x, range(b2.shape[1]))] xdims1 = (xsamples1, b1.shape[0]) xdims2 = (xsamples2, b2.shape[0]) (x1, y1) = generate_from_linear(xdims1, b1, noise_std1) (x2, y2) = generate_from_linear(xdims2, b2, noise_std2) (x1test, y1test) = generate_from_linear(xdims1, b1, noise_std1) (x2test, y2test) = generate_from_linear(xdims2, b2, noise_std2) p1 = messwpriors(b1, p_falsep, p_falseneg) p2 = messwpriors(b2, p_falsep, p_falseneg) priorset = [] for i in range(len(p1)): tf = fr.one_gene(TFs[0][p1[i][0]], 0) gene = fr.one_gene(Gs[0][p1[i][1]], 0) priorset.append((tf,gene)) for i in range(len(p2)): tf = fr.one_gene(TFs[1][p2[i][0]], 1) gene = fr.one_gene(Gs[1][p2[i][1]], 1) priorset.append((tf,gene)) organisms = [0,1] # constraints=[] # for i in range(len(fusiongroups)): # fg = fusiongroups[i] # for j in range(len(fg[0])): # for m in range(len(fg[1])): # coeff1 = fr.coefficient(0,fg[0][j][0], fg[0][j][1]) # coeff2 = fr.coefficient(1,fg[1][m][0], fg[1][m][1]) # constr = fr.constraint(coeff1,coeff2, lamS) # constraints.append(constr) #bs_solve = fr.solve_ortho_scad_refit_bench(organisms, Gs, TFs, [x1, x2], [y1, y2], constraints, priorset, lamP, lamR, lamS, it, k, s_it) bs_solve = fr.solve_ortho_scad_refit(organisms, Gs, TFs, [x1, x2], [y1, y2], fusiongroups, priorset, lamP, lamR, lamS, it, k, s_it) err1 = B_err(bs_solve[0], b1) err2 = B_err(bs_solve[1], b2) support_score1 = check_support(bs_solve[0], b1) support_score2 = check_support(bs_solve[1], b2) err1p = pred_err_grps(bs_solve[0], x1test, y1test) err2p = pred_err_grps(bs_solve[1], x2test, y2test) return (bs_solve, err1, err2, err1p, err2p, support_score1, support_score2)