/
bacteria.py
757 lines (618 loc) · 24.2 KB
/
bacteria.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
import numpy as np
import fused_L2 as fl
import random
from sklearn.metrics import roc_curve, auc, precision_recall_curve
iron_conds = 24
timeseries_conds = 51
subtilis_conds = 359
def write_bs(genes, tfs, B, outf):
Bs_str_l = []
Bs_str_l.append('\t'.join(tfs))
for gi in range(len(genes)):
gene = genes[gi]
regulators = B[:, gi]
Bs_str_l.append(gene +'\t'+ '\t'.join(map(str, regulators)))
f = file(outf, 'w')
f.write('\n'.join(Bs_str_l))
f.close()
def run_both(lamP, lamR, lamS, outf, sub_s, sub_i, sub_t):
(bs_e, bs_t , bs_genes, bs_tfs) = load_B_subtilis(sub_s)
(BS_priors, sign) = load_priors('gsSDnamesWithActivitySign082213','B_subtilis')
(ba_e, ba_t, ba_genes, ba_tfs) = load_B_anthracis(sub_i, sub_t)
(BA_priors, sign) = ([], [])
Xs = [bs_t, ba_t]
Ys = [bs_e, ba_e]
priors = BS_priors + BA_priors
orth = load_orth('bs_ba_ortho_804',['B_anthracis','B_subtilis'])
#orth = load_orth('',['B_subtilis'])
organisms = ['B_subtilis','B_anthracis']
#ortht = random_orth(bs_tfs, ba_tfs, organisms, 250)
#orthg = random_orth(bs_genes, ba_genes, organisms, 2500)
#orth = ortht+orthg
#print orth
#return
gene_ls = [bs_genes, ba_genes]
tf_ls = [bs_tfs, ba_tfs]
Bs = fl.solve_ortho_direct(organisms, gene_ls, tf_ls, Xs, Ys, orth, priors, 'no_adjust', lamP, lamR, lamS)
write_bs(bs_genes, bs_tfs, Bs[0], outf+'_subtilis')
write_bs(ba_genes, ba_tfs, Bs[1], outf+'_anthracis')
def run_both_adjust(lamP, lamR, lamS, outf, sub_s, sub_i, sub_t):
(bs_e, bs_t , bs_genes, bs_tfs) = load_B_subtilis(sub_s)
(BS_priors, sign) = load_priors('gsSDnamesWithActivitySign082213','B_subtilis')
(ba_e, ba_t, ba_genes, ba_tfs) = load_B_anthracis(sub_i, sub_t)
(BA_priors, sign) = ([], [])
Xs = [bs_t, ba_t]
Ys = [bs_e, ba_e]
priors = BS_priors + BA_priors
orth = load_orth('bs_ba_ortho_804',['B_anthracis','B_subtilis'])
#orth = load_orth('',['B_subtilis'])
organisms = ['B_subtilis','B_anthracis']
#ortht = random_orth(bs_tfs, ba_tfs, organisms, 250)
#orthg = random_orth(bs_genes, ba_genes, organisms, 2500)
#orth = ortht+orthg
#print orth
#return
gene_ls = [bs_genes, ba_genes]
tf_ls = [bs_tfs, ba_tfs]
Bs = fl.solve_ortho_direct(organisms, gene_ls, tf_ls, Xs, Ys, orth, priors, 'yes', lamP, lamR, lamS)
write_bs(bs_genes, bs_tfs, Bs[0], outf+'_subtilis')
write_bs(ba_genes, ba_tfs, Bs[1], outf+'_anthracis')
def run_both_refit(lamP, lamR, lamS, outf, sub_s, sub_i, sub_t, it, k):
(bs_e, bs_t , bs_genes, bs_tfs) = load_B_subtilis(sub_s)
(BS_priors, sign) = load_priors('gsSDnamesWithActivitySign082213','B_subtilis')
(ba_e, ba_t, ba_genes, ba_tfs) = load_B_anthracis(sub_i, sub_t)
(BA_priors, sign) = ([], [])
Xs = [bs_t, ba_t]
Ys = [bs_e, ba_e]
priors = BS_priors + BA_priors
orth = load_orth('bs_ba_ortho_804',['B_anthracis','B_subtilis'])
#orth = load_orth('',['B_subtilis'])
organisms = ['B_subtilis','B_anthracis']
#ortht = random_orth(bs_tfs, ba_tfs, organisms, 250)
#orthg = random_orth(bs_genes, ba_genes, organisms, 2500)
#orth = ortht+orthg
#print orth
#return
gene_ls = [bs_genes, ba_genes]
tf_ls = [bs_tfs, ba_tfs]
Bs = fl.solve_ortho_direct_refit(organisms, gene_ls, tf_ls, Xs, Ys, orth, priors, lamP, lamR, lamS, it, k)
write_bs(bs_genes, bs_tfs, Bs[0], outf+'_subtilis')
write_bs(ba_genes, ba_tfs, Bs[1], outf+'_anthracis')
#with saturation
def run_both_scad(lamP, lamR, lamS, outf, sub_s, sub_i, sub_t, it,k, it_s):
(bs_e, bs_t , bs_genes, bs_tfs) = load_B_subtilis(sub_s)
(BS_priors, sign) = load_priors('gsSDnamesWithActivitySign082213','B_subtilis')
(ba_e, ba_t, ba_genes, ba_tfs) = load_B_anthracis(sub_i, sub_t)
(BA_priors, sign) = ([], [])
Xs = [bs_t, ba_t]
Ys = [bs_e, ba_e]
priors = BS_priors + BA_priors
orth = load_orth('bs_ba_ortho_804',['B_anthracis','B_subtilis'])
#orth = load_orth('',['B_subtilis'])
organisms = ['B_subtilis','B_anthracis']
#ortht = random_orth(bs_tfs, ba_tfs, organisms, 250)
#orthg = random_orth(bs_genes, ba_genes, organisms, 2500)
#orth = ortht+orthg
#print orth
#return
gene_ls = [bs_genes, ba_genes]
tf_ls = [bs_tfs, ba_tfs]
Bs = fl.solve_ortho_scad_refit(organisms, gene_ls, tf_ls, Xs, Ys, orth, priors, lamP, lamR, lamS, it, k, it_s)
write_bs(bs_genes, bs_tfs, Bs[0], outf+'_subtilis')
write_bs(ba_genes, ba_tfs, Bs[1], outf+'_anthracis')
def run_scr(lamP, lamR, lamS, outf, sub):
#(ba,tf, genes, tfs) = load_bacteria('B_subtilis.csv','tfNames.txt',range(250))
(ba,tf, genes, tfs) = load_B_subtilis(sub)
(BC_priors, sign) = load_priors('gsSDnamesWithActivitySign082213','B_subtilis')
orth = []#load_orth('',['B_subtilis'])
print ba.shape
organisms = ['B_subtilis']
gene_ls = [genes]
tf_ls = [tfs]
Xs = [tf]
Ys = [ba]
priors = BC_priors
Bs = fl.solve_ortho_direct_refit(organisms, gene_ls, tf_ls, Xs, Ys, orth, priors, lamP, lamR, lamS, 0, 100)
#Bs = fl.solve_ortho_direct(organisms, gene_ls, tf_ls, Xs, Ys, orth, priors, lamP, lamR, lamS)
Bs_str_l = []
Bs_str_l.append('\t'.join(tfs))
for gi in range(len(genes)):
gene = genes[gi]
regulators = Bs[0][:, gi]
Bs_str_l.append(gene +'\t'+ '\t'.join(map(str, regulators)))
f = file(outf, 'w')
f.write('\n'.join(Bs_str_l))
f.close()
def run_scr2(lamP, lamR, lamS, outf, subi, subt):
#(ba,tf, genes, tfs) = load_bacteria('B_subtilis.csv','tfNames.txt',range(250))
(ba,tf, genes, tfs) = load_B_anthracis(subi, subt)
(BC_priors, sign) = ([], [])
#(BC_priors, sign) = load_priors('gsSDnamesWithActivitySign082213','B_subtilis')
orth = []
print ba.shape
organisms = ['B_subtilis']
gene_ls = [genes]
tf_ls = [tfs]
Xs = [tf]
Ys = [ba]
priors = BC_priors
Bs = fl.solve_ortho_direct(organisms, gene_ls, tf_ls, Xs, Ys, orth, priors, lamP, lamR, lamS)
Bs_str_l = []
Bs_str_l.append('\t'.join(tfs))
for gi in range(len(genes)):
gene = genes[gi]
regulators = Bs[0][:, gi]
Bs_str_l.append(gene +'\t'+ '\t'.join(map(str, regulators)))
f = file(outf, 'w')
f.write('\n'.join(Bs_str_l))
f.close()
#this returns pairs of genes, not coefficients, along with interaction valences
#that's weird, but was done so decouple loading priors from needing to know the tfs
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)
#returns a list of random 2 element ortho groups for testing purposes
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
#TODO pending seeing the orth file format
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 load_network(net_fn):
f = file(net_fn)
fs = f.read()
fsl = filter(len, fs.split('\n'))
fslt = map(lambda x: x.split('\t'), fsl)
tfs = fslt[0]
genes = map(lambda x: x[0], fslt[1:])
#the network is written as the transpose of the matrix we want
net = np.zeros((len(genes), len(tfs)))
for g in range(len(genes)):
targets = np.array(map(float, fslt[g+1][1:]))
net[g, :] = targets
return (net.T, genes, tfs)
def eval_prediction(net_fn, e, t, genes, tfs, metric):
(net, genes, tfs) = load_network(net_fn)
#(e, t, genes, tfs) = load_bacteria(expr_fn, tfs_fn, sub_conds)
print net.shape
err = fl.prediction_error(t, net, e, metric)
return err
#looks at a network and returns an array of interaction weights for all priors in the network, along with an array containing the sign of the prior (+1, -1)
def check_prior_recovery(net_fn, priors_fn):
(net, genes, tfs) = load_network(net_fn)
(p, sign) = load_priors(priors_fn, '1')
pcon = fl.priors_to_constraints(['1'], [genes],[tfs],p,0.5)
prior_inter = []
for con in pcon:
prior_inter.append(net[con.c1.r, con.c1.c])
return (np.array(prior_inter), np.array(sign))
#returns a list of tf gene correlations for each prior interaction, as well as the sign of each prior
def check_prior_corr(expr_fn, tfs_fn, priors_fn):
(e, t, genes, tfs) = load_bacteria(expr_fn, tfs_fn)
(p, s) = load_priors(priors_fn,'1')
pcon = fl.priors_to_constraints(['1'], [genes],[tfs],p,0.5)
prior_corr = []
for con in pcon:
tfi = con.c1.r
gi = con.c1.c
tf_expr = t[:, tfi]
g_expr = e[:, gi]
corr = np.corrcoef(tf_expr, g_expr)[0,1]
prior_corr.append(corr)
return (prior_corr, s)
#creates a new expression matrix by joining two exp mats, with arbitrary names
#order of names in output is arbitrary
def join_expr_data(names1, names2, exp_a1, exp_a2):
names = list(set(names1).intersection(set(names2)))
name_to_ind = {names[x] : x for x in range(len(names))}
#print name_to_ind
def n_to_i(n):
if n in name_to_ind:
return name_to_ind[n]
return -1
exp_a = np.zeros((exp_a1.shape[0] + exp_a2.shape[0], len(names)))
i=0
#name_ind_to_name1_ind[i] maps an index in names to an index in names1
name_ind_to_name1_ind = {n_to_i(names1[x]) : x for x in range(len(names1))}
#name1_fr_inds[i] is the index in names1 that corresponds ti names[i]
name1_fr_inds = map(lambda x: name_ind_to_name1_ind[x], range(len(names)))
#array of the previous
name1_fr_inds_a = np.array(name1_fr_inds)
#copy into the array
for r1 in range(exp_a1.shape[0]):
exp_a[r1, :] = exp_a1[r1, name1_fr_inds_a]
name_ind_to_name2_ind = {n_to_i(names2[x]) : x for x in range(len(names2))}
name2_fr_inds = map(lambda x: name_ind_to_name2_ind[x], range(len(names)))
name2_fr_inds_a = np.array(name2_fr_inds)
for r2 in range(exp_a2.shape[0]):
exp_a[r2+exp_a1.shape[0], :] = exp_a2[r2, name2_fr_inds_a]
return (exp_a, names)
#quantile normalizes conditions AND scales to mean zero/unit variance
#AND DOES NOT divides expression matrices by the square root of the sample size
#CHANGED! mean subtract after quantile normalizing
def normalize(exp_a, mean_zero = False):
canonical_dist = np.sort(exp_a, axis=1).mean(axis=0)
#if mean_zero:
# canonical_dist = canonical_dist - canonical_dist.mean()
canonical_dist = canonical_dist / canonical_dist.std()
exp_n_a = np.zeros(exp_a.shape)
for r in range(exp_a.shape[0]):
order = np.argsort(exp_a[r, :])
exp_n_a[r, order] = canonical_dist
#exp_n_a / np.sqrt(exp_n_a.shape[0])
if mean_zero:
exp_n_a = exp_n_a - exp_n_a.mean(axis=0)
return exp_n_a
#sub_conds is a bit weird here
def load_B_anthracis(subi = [], subt = []):
(e1, t1, genes1, tfs1) = load_ba_iron()
(e2, t2, genes2, tfs2) = load_ba_timeseries()
(e, genes) = join_expr_data(genes1, genes2, e1, e2)
(t, tfs) = join_expr_data(tfs1, tfs2, t1, t2)
#drop the _at from the name
genes = map(lambda x: x.replace('_at',''), genes)
tfs = map(lambda x: x.replace('_at',''), tfs)
e = normalize(e, True)
t = normalize(t, False)
#if empty, use everything
if not len(subi):
subi = range(e1.shape[0])
if not len(subt):
subt = range(e2.shape[0])
subi = np.array(subi)
subt = e1.shape[0] + np.array(subt)
e = e[np.concatenate((subi, subt)), :]
t = t[np.concatenate((subi, subt)), :]
#(t, tfs) = join_expr_data(tfs1, tfs2, t1, t2)
return (e, t, genes, tfs)
#this is a NAIVE loader.
#does not consider time series relationships
#returns (gene expr, tf expr, genes, tfs)
def load_ba_timeseries(sub_conds=[]):
f = file('Normalized_data_RMA._txt')
fs = f.read()
fsn = filter(len, fs.split('\n'))
fsnt = map(lambda x: x.split('\t'), fsn)
conds = fsnt[0][1:]
#first line is SCAN REF
#second line is composite element REF
#lines 3-end are data
exp_mat_t = np.zeros((len(fsnt)-2, len(conds)))#first col gene
genes = []
f_tf = file('tfNamesAnthracis')
f_tfs = f_tf.read()
tfs = filter(len, f_tfs.split('\n'))
for r in range(exp_mat_t.shape[0]):
gene_str_full = fsnt[r+2][0]
#gene name is 4th element, separated by ':'
gene_str = gene_str_full.split(':')[3]
gene_str = gene_str.replace('_pXO1_','').replace('_pXO2','')#what is this? dunno!
expr = np.array(fsnt[r+2][1:])
exp_mat_t[r, :] = expr
genes.append(gene_str)
#require that tfs be genes that we have data for!
tfs = filter(lambda x: x in genes, tfs)
tf_mat_t = np.zeros((len(tfs), len(conds)))
gene_to_ind = {genes[x] : x for x in range(len(genes))}
for ti in range(len(tfs)):
gi = gene_to_ind[tfs[ti]]
tf_mat_t[ti, :] = exp_mat_t[gi, :]
exp_mat = exp_mat_t.T
#exp_mat = (exp_mat_t - np.mean(exp_mat_t, axis=0)).T
tf_mat = tf_mat_t.T
if sub_conds == []:
sub_conds = range(exp_mat.shape[0])
sub_conds = np.array(sub_conds)
return (exp_mat[sub_conds, :], tf_mat[sub_conds, :], genes, tfs)
def load_ba_iron(sub_conds = []):
f = file('normalizedgeneexpressionvalues.txt')
fs = f.read()
fsn = filter(len, fs.split('\n'))
fsnt = map(lambda x: x.split('\t'), fsn)
conds = fsnt[0][1:]
#first line is SCAN REF
#second line is composite element REF
#lines 3-end are data
f_tf = file('tfNamesAnthracis')
f_tfs = f_tf.read()
tfs = filter(len, f_tfs.split('\n'))
exp_mat_t = np.zeros((len(fsnt)-2, len(conds)))#first col gene
genes = []
#tfs = []
for r in range(exp_mat_t.shape[0]):
gene_str = fsnt[r+2][0]
#gene name is 4th element, separated by ':'
expr = np.array(fsnt[r+2][1:])
exp_mat_t[r, :] = expr
genes.append(gene_str)
#require that tfs be genes that we have data for!
tfs = filter(lambda x: x in genes, tfs)
tf_mat_t = np.zeros((len(tfs), len(conds)))
gene_to_ind = {genes[x] : x for x in range(len(genes))}
for ti in range(len(tfs)):
gi = gene_to_ind[tfs[ti]]
tf_mat_t[ti, :] = exp_mat_t[gi, :]
exp_mat = exp_mat_t.T
#exp_mat = (exp_mat_t - np.mean(exp_mat_t, axis=0)).T
tf_mat = tf_mat_t.T
if sub_conds == []:
sub_conds = range(exp_mat.shape[0])
sub_conds = np.array(sub_conds)
return (exp_mat[sub_conds, :], tf_mat[sub_conds, :], genes, tfs)
#this is a NAIVE loader.
#does not consider time series relationships
#returns (gene expr, tf expr, genes, tfs)
#changed to normalize after subsetting conditions
def load_B_subtilis(sub_conds=[]):
(e, t, genes, tfs) = load_bacteria('B_subtilis.csv', 'tfNames_subtilis.txt',[])
e = normalize(e, True)
t = normalize(t, False)
sub_conds = np.array(sub_conds)
if len(sub_conds):
e = e[sub_conds,:]
t = t[sub_conds,:]
return (e, t, genes, tfs)
#genes are mean 0
#not as general as I had hoped!
def load_bacteria(expr_fn, tfs_fn, sub_conds=[]):
f = file(expr_fn)
fs = f.read()
fsl = filter(len, fs.split('\n'))
fslc = map(lambda x: x.split(','), fsl)
f.close()
t = file(tfs_fn)
ts = t.read()
tfs = filter(len, ts.split('\n'))
t.close()
tfs_set = set(tfs)
conds = fslc[0]
genes = map(lambda x: x[0], fslc[1:])
exp_mat_t = np.zeros((len(genes), len(conds)))
for r in range(len(genes)):
conds_f = map(float, fslc[1+r][1:])
conds_a = np.array(conds_f)
exp_mat_t[r, :] = conds_a
tf_mat_t = np.zeros((len(tfs), len(conds)))
gene_to_ind = {genes[x] : x for x in range(len(genes))}
for ti in range(len(tfs)):
gi = gene_to_ind[tfs[ti]]
tf_mat_t[ti, :] = exp_mat_t[gi, :]
exp_mat = exp_mat_t.T
#exp_mat = exp_mat - exp_mat.mean(axis=0)
#where is the right place to subtract the mean?
tf_mat = tf_mat_t.T
if sub_conds == []:
sub_conds = range(exp_mat.shape[0])
sub_conds = np.array(sub_conds)
exp_mat = exp_mat[sub_conds, :]
return (exp_mat, tf_mat[sub_conds, :], genes, tfs)
def order_corr_mat(cmat, init):
cols = set(range(cmat.shape[1]))
curr = init
cols.remove(init)
order = [init, ]
while len(cols):
best_c = 0
best_val = np.inf
for c in cols:
nrm = np.linalg.norm(cmat[:, c] - cmat[:,curr])
if nrm < best_val:
best_c = c
best_val = nrm
order.append(best_c)
cols.remove(best_c)
curr = best_c
return order
#no args!
def betas_fused_visualize(net_s, net_a, orth):
from matplotlib import pyplot as plt
(ns, gs, ts) = load_network(net_s)
(na, ga, ta) = load_network(net_a)
#we want to enumerate the constraints, fused_L2 can do that
constraints = fl.orth_to_constraints(['B_subtilis','B_anthracis'], [gs, ga], [ts, ta], orth, 0)
coeffs_s = []
coeffs_a = []
for con in constraints:
if con.c1.sub == 1:
continue
beta_s = ns[con.c1.r, con.c1.c]
beta_a = na[con.c2.r, con.c2.c]
coeffs_s.append(beta_s)
coeffs_a.append(beta_a)
print np.corrcoef(coeffs_s, coeffs_a)
plt.scatter(coeffs_s, coeffs_a)
plt.xlabel('B subtilis')
plt.ylabel('B anthracis')
plt.show()
cs = np.array(coeffs_s)
ca = np.array(coeffs_a)
#plt.hist(np.abs(cs-ca)/(0.5*(np.abs(cs)+np.abs(ca))), bins=50)
#plt.show()
def betas_fused_corr(net_s, net_a, orth):
(ns, gs, ts) = load_network(net_s)
(na, ga, ta) = load_network(net_a)
#we want to enumerate the constraints, fused_L2 can do that
constraints = fl.orth_to_constraints(['B_subtilis','B_anthracis'], [gs, ga], [ts, ta], orth, 0)
coeffs_s = []
coeffs_a = []
for con in constraints:
if con.c1.sub == 1:
continue
beta_s = ns[con.c1.r, con.c1.c]
beta_a = na[con.c2.r, con.c2.c]
coeffs_s.append(beta_s)
coeffs_a.append(beta_a)
return np.corrcoef(coeffs_s, coeffs_a)[0,1]
def corr_visualize(genes1, genes2, exp1, exp2, organisms, orth):
from matplotlib import pyplot as plt
def ind_rc(m, inds):
return m[:, inds][inds, :]
orth12 = dict()
orth21 = dict()
for ogroup in orth:
org1 = filter(lambda x: x.organism == organisms[0], ogroup)
org2 = filter(lambda x: x.organism == organisms[1], ogroup)
#take only the first pair in each orthology group
if len(org1) and len(org2):
orth12[org1[0].name] = org2[0].name
orth21[org2[0].name] = org1[0].name
genes1o = filter(lambda x: x in orth12, genes1)
genes2o = filter(lambda x: x in orth21, genes2)
genes1o_s = set(genes1o)
genes2o_s = set(genes2o)
genes1o = filter(lambda x: orth12[x] in genes2o_s, genes1o)
genes2o = filter(lambda x: orth21[x] in genes1o_s, genes2o)
genes1o_s = set(genes1o)
genes2o_s = set(genes2o)
#print len(genes1o)
#print len(genes2o)
gi1 = {genes1o[x]:x for x in range(len(genes1o))}
gi2 = {genes2o[x]:x for x in range(len(genes2o))}
org2_order = np.array(map(lambda x: gi1[orth21[x]], genes2o))
in_orth1 = np.array(map(lambda x: x in genes1o_s, genes1))
in_orth2 = np.array(map(lambda x: x in genes2o_s, genes2))
exp1 = exp1[:, in_orth1]
exp2 = exp2[:, in_orth2]
#exp2 = exp2[:, org2_order]
#return (genes1o, genes2o, org2_order, orth21)
#print org2_order
#plt.matshow(exp1)
#plt.show()
cmat1 = np.corrcoef(exp1, rowvar=False)
cmat2 = np.corrcoef(exp2, rowvar=False)
cmat2 = ind_rc(cmat2, org2_order)
#cmat1 = ind_rc(cmat1, org2_order)
nice_order = order_corr_mat(cmat1,int(cmat1.shape[1]*random.random()))
#nice_order = org2_order
cmat1 = ind_rc(cmat1, nice_order)
#
cmat2 = ind_rc(cmat2, nice_order)
#plt.subplot(121)
plt.matshow(cmat1)
#plt.subplot(122)
plt.matshow(cmat2)
plt.show()
cflat1 = cmat1.ravel().copy()
cflat2 = cmat2.ravel().copy()
#print sum(in_orth1)
#print sum(in_orth2)
#print cmat1.shape
#print cmat2.shape
#print np.corrcoef(cflat1, cflat2)
#print exp1.shape
#print exp2.shape
random.shuffle(cflat1)
random.shuffle(cflat2)
plt.scatter(cflat1[0:1000], cflat2[0:1000])
plt.xlabel('B subtilis correlation')
plt.ylabel('B anthracis correlation')
plt.show()
return (cmat1, cmat2)
#scores: R
#labels: 0/1
def prc(scores, labels):
i_scores = np.argsort(-1*scores)
s_labels = np.array(labels)[i_scores]
cs = np.cumsum(s_labels)
true_positive = np.sum(labels)
precision = cs / np.arange(1, len(cs)+1)
recall = cs / true_positive
precision2 = []
recall2 = []
#prev = -np.inf
#for sci in range(1, len(scores[i_scores])):
# if scores[i_scores[sci]] != prev:
# precision2.append(precision[sci-1])
# recall2.append(recall[sci-1])
# prev = scores[i_scores[sci]]
return (precision, recall)
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 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)
f = lambda x: run_scr(0.001,5,0,'wat4.tsv')