-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOntology.py
1715 lines (1289 loc) · 81.2 KB
/
Ontology.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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import itertools, multiprocessing, logging, os, collections, random, math, sys, time, scipy, scipy.sparse, igraph
from itertools import groupby, combinations
from operator import *
from scipy.stats import hypergeom
import numpy as np, pandas as pd
def load_table_as_list(f, delimiter='\t', encoding=None):
if encoding is None:
f = open(f)
else:
import codecs
f = codecs.open(f, encoding=encoding)
tmp = [x.strip().split(delimiter) for x in f.read().splitlines()]
f.close()
return tmp
def time_print(*s):
from datetime import datetime
print ' '.join(map(str, s)), datetime.today()
# print s, datetime.today()
import sys
sys.stdout.flush()
def igraph_2_ontology_file(g, output):
"""
Writes a DAG represented as an igraph.Graph in a 3-column (parent, child, relation) ontology table format.
The 'relation' attribute of edges in the igraph.Graph object is used for the 3rd column.
"""
tmp = np.array(g.get_edgelist())
names = np.array(g.vs['name'])
edge_array = np.hstack([names[tmp], np.array(g.es['relation']).reshape(-1, 1)])[:, [1,0,2]]
np.savetxt(output,
edge_array,
fmt='%s', delimiter='\t')
def make_tree(graph, method='priority', edge_name='smallest_parent', parent_priority=None, edge_priority=None, default_priority=None, optim='max'):
"""Returns copy of graph with new edge attribute marking spanning tree"""
graph = graph.copy()
if method=='priority':
assert 1 == (parent_priority is not None) + (edge_priority is not None)
if edge_priority is not None: assert default_priority is not None
if optim=='min': optim=min
if optim=='max': optim=max
graph.es[edge_name] = False
for v in graph.vs:
parents = graph.neighbors(v.index, mode='out')
if len(parents) > 0:
"""Choose the parent with the highest valued priority"""
if parent_priority is not None:
small_parent = optim(parents, key=lambda p: parent_priority[p])
elif edge_priority is not None:
small_parent = optim(parents, key=lambda p: edge_priority.get(graph.get_eid(v.index, p), default_priority))
graph.es[graph.get_eid(v.index, small_parent)][edge_name] = True
else:
raise Exception('Method not supported')
return graph
def get_ancestor_matrix(graph, node_order, verbose=False):
"""
Compute common ancestor matrix.
lca[a,b] = index of least common ancestor of terms a and b
"""
print 'graph size:', len(graph.vs)
if verbose: time_print('Calculating connectivity matrix')
d = np.int8(np.isfinite(np.array(graph.shortest_paths(graph.vs, graph.vs, mode='out'), order='C')))
ancestor_matrix = np.zeros(d.shape, dtype=np.int32)
ancestor_matrix.fill(-1)
if verbose: time_print('Iterating:')
for idx, i in enumerate(node_order):
# Note: includes self as a child
children_list = np.where(d[:,i] == 1)[0]
# For those descendants without a computed LCA yet, set their LCA to this term
lca_sub = ancestor_matrix[children_list.reshape(-1,1), children_list]
lca_sub[lca_sub == -1] = i
ancestor_matrix[children_list.reshape(-1,1), children_list] = lca_sub
# Check symmetry
assert (ancestor_matrix.T == ancestor_matrix).all()
assert (-1 == ancestor_matrix).sum() == 0, 'The ontology may have more than one root'
return ancestor_matrix
def get_smallest_ancestor(graph, node_sizes, method='ancestor_matrix', verbose=False):
# Node order: smallest to largest terms
return get_ancestor_matrix(graph,
[a[0] for a in sorted(enumerate(node_sizes), key=lambda a: a[1])],
verbose)
def get_lca_matrix(graph):
## I might deprecate this. Consider using get_smallest_ancestor instead
return get_ancestor_matrix(graph, graph.topological_sorting(mode='out'))
def collapse_node(g, v, edge_filter=None, use_v_name=False, combine_attrs=None, default_attr=None, verbose=True, fast_collapse=False, delete=True):
if use_v_name:
assert isinstance(v, (unicode, str))
v = g.vs.find(name_eq=v).index
if fast_collapse:
parents = g.neighbors(v, mode='out')
children = g.neighbors(v, mode='in')
if len(parents) > 0 and len(children) > 0:
# A faster collapse that adds all new edges simultaneously. Ignores edge attributes
new_edges = [(c, p) for p in parents for c in children]
new_edges = [x for x, y in zip(new_edges, g.get_eids(new_edges, error=False)) if y == -1]
g.add_edges(new_edges)
else:
in_edges = g.es[g.incident(v, mode='in')]
out_edges = g.es[g.incident(v, mode='out')]
if edge_filter is not None:
in_edges = [e for e in in_edges if edge_filter(e)]
out_edges = [e for e in out_edges if edge_filter(e)]
for e_in in in_edges:
for e_out in out_edges:
in_neigh, out_neigh = e_in.source, e_out.target
# Only add an edge if it doesn't already exist
if g[in_neigh, out_neigh] == 0:
g.add_edge(in_neigh, out_neigh)
e = g.es[g.get_eid(in_neigh, out_neigh)]
if combine_attrs is not None:
# Set default value of edge attributes to 0
for key in combine_attrs: e[key] = None
e = g.es[g.get_eid(in_neigh, out_neigh)]
# Update attributes
if combine_attrs is not None:
for key in combine_attrs:
e[key] = combine_attrs[key](e_in, e_out, e)
if verbose and key=='triangle_edge_priority':
print 'Setting', key, g.vs[in_neigh]['name'], g.vs[out_neigh]['name'], 'to', combine_attrs[key](e_in, e_out, e), (e_in[key], e_out[key])
e['collapsed_length'] = e_in['collapsed_length'] + e_out['collapsed_length']
e['collapsed_terms'] = e_in['collapsed_terms'] + [g.vs[v]['name']] + e_out['collapsed_terms']
if delete:
g.delete_vertices(v)
return g
def map_gene_pairs_2_triangles(ontology, gsets):
"""Assign each gene pair to it's smallest common ancestor. This is
it's 'triangle'. It also works out that this is both of their lowest
terms, such that this is like a within-term interaction"""
graph_orig = ontology.get_igraph()
graph = ontology.get_igraph().copy()
graph.add_vertices(ontology.genes)
assert graph.vs[-len(ontology.genes):]['name'] == ontology.genes
assert graph.vs[:len(ontology.terms)]['name'] == ontology.terms
graph.add_edges([(g, t) for g, terms in [(len(ontology.terms) + ontology.genes_index[g], terms) for g, terms in ontology.gene_2_terms.items()] for t in terms])
gene_sca_matrix = get_smallest_ancestor(graph, ontology.get_term_sizes().tolist() + [1 for i in ontology.genes], verbose=True)
term_2_genes = {ontology.terms_index[t] : set(g) for t, g in ontology.get_term_2_genes().items()}
# tmp = [(i, j, gene_sca_matrix[len(ontology.terms) + i, len(ontology.terms) + j], graph_orig.neighbors(gene_sca_matrix[len(ontology.terms) + i, len(ontology.terms) + j], mode='in')) for i, j in [(ontology.genes_index[g1], ontology.genes_index[g2]) for g1, g2 in gsets]]
# triangles = [(sca, [t for t in terms if i in term_2_genes[t]], [t for t in terms if j in term_2_genes[t]]) for i, j, sca, terms in tmp]
#--- Alternate triangle calculation. Allow for multiple SCA in anti-chain paths
# Make sure the order is 'C'. Set the diagonal to True (even though it should technically be False) for easier use of np.all below
connectivity = ontology.get_connectivity_matrix()
not_connectivity = np.logical_not(np.array(connectivity, dtype=np.bool, order='C'))
np.fill_diagonal(not_connectivity, True)
children_dict = {v.index : np.array(graph_orig.neighbors(v.index, mode='in')) for v in graph_orig.vs}
gene_2_terms_set = {g : np.array(terms) for g, terms in ontology.gene_2_terms.items()}
term_2_genes = {ontology.terms_index[t] : set(g) for t, g in ontology.get_term_2_genes().items()}
time_print('Calculating triangles')
triangles = ((g1, g2, gene_2_terms_set[g1], gene_2_terms_set[g2]) for g1, g2 in gsets)
triangles = ((g1, g2, g1_terms, g2_terms, np.intersect1d(g1_terms, g2_terms, assume_unique=True)) for g1, g2, g1_terms, g2_terms in triangles)
# This could be potentially double-counting a term pair if for a single gene pair, they have more than one sca
# triangles = [[(sca, np.intersect1d(g1_terms, children_dict[sca], assume_unique=True), np.intersect1d(g2_terms, children_dict[sca], assume_unique=True)) for sca in terms[not_connectivity[terms, :][:, terms].all(axis=0)]] for g1, g2, g1_terms, g2_terms, terms in triangles]
triangles = [[(sca,
tuple(np.intersect1d(g1_terms, children_dict[sca], assume_unique=True)),
tuple(np.intersect1d(g2_terms, children_dict[sca], assume_unique=True))) \
for sca in terms[not_connectivity[terms, :][:, terms].all(axis=0)]] \
for g1, g2, g1_terms, g2_terms, terms in triangles]
## Each element corresponds to a gene pair. Each element is a
## list of triplets (LCA, children terms spanned by gene 1,
## children terms spanned by gene 2)
return triangles
def count_siblings(triangles, num_terms, term_pair_triangles=None):
"""Returns the number of times that a sibling occurs in a set of triangles"""
triu_indices = np.triu_indices(num_terms, k=1)
if term_pair_triangles is None:
term_pair_triangles = [set([ (t1, t2) if t1 < t2 else (t2, t1) for sca, side1, side2 in path for t1 in side1 for t2 in side2]) \
for path in triangles]
return scipy.sparse.coo_matrix(([1 for term_pairs in term_pair_triangles for t1, t2 in term_pairs],
([t1 for term_pairs in term_pair_triangles for t1, t2 in term_pairs],
([t2 for term_pairs in term_pair_triangles for t1, t2 in term_pairs]))),
shape=(num_terms, num_terms)).toarray()[triu_indices]
def siblings_2_gene_pairs(triangles):
"""Returns dictionary mapping a sibling (t1, t2), where the term
indices are t1<t2, to the indices of gene pairs. This is the same as
indices to elements in <triangles>"""
return {a : [y for x, y in b] for a, b in groupby(sorted([((t1, t2) if t1<t2 else (t2, t1), i) for i, tri in enumerate(triangles) for \
sca, t1_list, t2_list in tri for t1 in t1_list for t2 in t2_list],
key=lambda a:a[0]), key=lambda a:a[0])}
def lcas_2_gene_pairs(triangles):
"""Returns dictionary mapping a term to the indices of gene pairs.
This is the same as indices to elements in <triangles> """
return {a : [y for x, y in b] for a, b in groupby(sorted([(sca, i) for i, tri in enumerate(triangles) for \
sca, t1_list, t2_list in tri],
key=lambda a:a[0]), key=lambda a:a[0])}
def explain_gene_pairs(triangles, lcas, siblings):
"""Given a list of enriched siblings and lcas, returns those siblings
and lcas that a gene pair falls under.
For each element in triangles, returns a pair (x, y) where x are
terms in lcas and y are term pairs in siblings.
"""
lcas, siblings = set(lcas), set([(t1,t2) if t1<t2 else (t2, t1) for t1, t2 in siblings])
return [(set([sca for sca, t1_list, t2_list in tri]) & lcas,
set([(t1,t2) if t1<t2 else (t2,t1) for sca, t1_list, t2_list in tri for t1 in t1_list for t2 in t2_list])) \
for tri in triangles]
class Ontology:
def __init__(self,
ontology_file,
mapping_file=None,
combined_file=False,
shuffle_genes=False,
parent_child=False,
genes_as_terms=False,
encoding=None,
add_root=None,
verbose=True):
"""
ontology: File path to term-to-term edges
mapping: File path to gene-to-term edges
"""
if isinstance(ontology_file, str) and (not os.path.exists(ontology_file)) and ontology_file in ['all', 'bp', 'cc', 'mf']:
ontology_file = os.path.join(os.getenv('HOME'), 'GI/NNstructures/start_mkramer-collapsed/evidence_no_IGI/propagate_forward/ontology_%s/precollapse.propagated' % ontology_file)
if verbose: print 'Assuming ontology file is', ontology_file
# else:
# raise Exception("%s isn't recognized as an ontology" % ontology_file)
## <ontology_file> can be of different formats.
## TODO: change variable name to <ontology>
if isinstance(ontology_file, str):
if os.path.exists(ontology_file):
ontology_table = load_table_as_list(ontology_file, encoding=encoding)
else:
ontology_table = ontology_file
elif isinstance(ontology_file, igraph.Graph):
## Assumes the igraph object is directed such that the
## source node is the child term or gene and the target
## node is the parent term
names = np.array(ontology_file.vs['name'])
tmp = np.array(ontology_file.get_edgelist())
if 'relation' in ontology_file.edge_attributes():
ontology_table = np.hstack([names[tmp], np.array(ontology_file.es['relation']).reshape(-1, 1)])
else:
ontology_table = names[tmp]
ontology_table = ontology_table.tolist()
else:
## Assume that the input is already in the desired format:
## a list of (child, parent, relation (optional) ) tuples
assert not isinstance(ontology_file, (str, unicode)) and hasattr(ontology_file, '__iter__')
ontology_table = ontology_file
if combined_file:
mapping_table = [x for x in ontology_table if x[2]=='gene']
ontology_table = [x for x in ontology_table if x[2]!='gene']
else:
if mapping_file is None:
mapping_file = ontology_file + '.mapping'
if verbose: print 'Assuming that mapping file is', mapping_file
if (isinstance(mapping_file, str) and os.path.exists(mapping_file)):
mapping_table = load_table_as_list(mapping_file, encoding=encoding)
elif isinstance(mapping_file, igraph.Graph):
names = np.array(mapping_file.vs['name'])
tmp = np.array(mapping_file.get_edgelist())
mapping_table = np.hstack([names[tmp], np.array(mapping_file.es['relation']).reshape(-1, 1)])
mapping_table = mapping_table.tolist()
else:
assert not isinstance(mapping_file, (str, unicode)) and hasattr(mapping_file, '__iter__')
mapping_table = mapping_file
if parent_child:
ontology_table = [[x[1],x[0]]+list(x[2:]) for x in ontology_table]
mapping_table = [[x[1],x[0]]+list(x[2:]) for x in mapping_table]
if genes_as_terms:
# Turn each gene into a term, and represent the genes with
# dummy copies. E.g. if gene g is annotated to term t, then
# make g into a term, and then create a copy g_dummy that
# is annotated to g and is now the "gene"
ontology_table = ontology_table + mapping_table
mapping_table = [[u'dummy_'+g, g, 'gene'] for g in set([x[0] for x in mapping_table])]
## Read term-to-term edges
# term_2_terms[<term_name>] --> list of <term_name>'s children terms
term_2_terms = {r: tuple(p[0] for p in q) for r, q in \
itertools.groupby(sorted(ontology_table,
key=lambda a:a[1]),
key=lambda a:a[1])}
# Extract parent-child relations, if they exist
if any([len(x)>=3 for x in ontology_table]):
# relation_dict[(child, parent)] --> edge relation, e.g. 'is_a' or 'part_of'
self.relation_dict = {(x[0],x[1]) : x[2] if len(x)>=3 else None for x in ontology_table}
if add_root is not None:
## Check if there is a single unifying root term of the ontology. If not, then identify the multiple roots and join them under an artificial root
root_list = set(term_2_terms.keys()) - set([y for x in term_2_terms.values() for y in x])
print len(root_list)
print root_list
if len(root_list) > 1:
root_name = add_root
term_2_terms[root_name] = root_list
if self.has_attr('relation_dict'):
for r in root_list:
relation_dict[(r, root_name)] = 'artificial_root'
## Read term-to-term edges
# child_2_parents[<term_name>] --> list of <term_name>'s parent term names
child_2_parents = {r: tuple(p[1] for p in q) for r, q in \
itertools.groupby(sorted(ontology_table,
key=lambda a:a[0]),
key=lambda a:a[0])}
## Read gene-to-term edges
# gene_2_terms[<gene_name>] --> list of terms that <gene_name> is mapped to
gene_2_terms = {key: tuple(set([a[1] for a in group])) for key, group in \
itertools.groupby(sorted(mapping_table,
key=lambda a:a[0]),
key=lambda a:a[0])}
if shuffle_genes:
# Shuffle all annotated genes in the ontology
if isinstance(shuffle_genes, bool):
shuffle_genes = list(gene_2_terms.keys())
# Only shuffle a subset of genes
elif isinstance(shuffle_genes, collections.Iterable):
shuffle_genes = list(set(gene_2_terms.keys()) & set(shuffle_genes))
else:
raise Exception('Unsupported argument for shuffle_genes')
if verbose: time_print('Shuffling %s genes' % len(shuffle_genes))
gene_permutation = range(len(shuffle_genes))
random.shuffle(gene_permutation)
gene_2_terms = {shuffle_genes[gene_permutation[i]] : gene_2_terms[shuffle_genes[i]] for i in range(len(shuffle_genes))}
## Check that the set of terms is the same according to term_2_terms and gene_2_terms
terms_A = set.union(set(term_2_terms.keys()), *[set(x) for x in term_2_terms.values()])
terms_B = set.union(*[set(x) for x in gene_2_terms.values()])
# print len(terms_A), len(terms_B), len(terms_A & terms_B)
# assert terms_B.issubset(terms_A)
if verbose and len(terms_B - terms_A)>0:
print 'WARNING: There are {} terms that are annotated to genes but not connected to the rest of the ontology'.format(len(terms_B - terms_A))
if verbose and len(terms_A - terms_B)>0:
print 'WARNING: There are {} terms that have no direct gene annotations'.format(len(terms_A - terms_B))
terms = sorted(list(terms_A | terms_B))
genes = sorted(gene_2_terms.keys())
## terms_index[<term_name>] --> index in self.terms
terms_index = {b:a for a,b in enumerate(terms)}
## genes_index[<gene_name>] --> index in self.genes
genes_index = {b:a for a,b in enumerate(genes)}
## Convert gene_2_terms to list term indices rather than term names
for k, v in gene_2_terms.items():
gene_2_terms[k] = [terms_index[x] for x in gene_2_terms[k]]
## Read term-to-term edges
# child_2_parents_indices[<term_name>] --> list of indices of <term_name>'s parent terms
child_2_parents_indices = {r : [terms_index[x] for x in v] for r, v in child_2_parents.items()}
self.term_2_terms = term_2_terms
self.child_2_parents = child_2_parents
self.child_2_parents_indices = child_2_parents_indices
self.gene_2_terms = gene_2_terms
self.terms = terms
self.terms_index = terms_index
self.genes = genes
self.genes_index = genes_index
if verbose: print 'Done constructing ontology'
def collapse_ontology(self, verbose=True, default_relation='default', min_term_size=2):
## Returns a new ontology where redundant and empty terms have been collapsed
g = self.get_igraph().copy()
if verbose: print len(g.vs), 'total nodes'
if verbose: print 'Propagating annotations'
self.propagate_annotations()
# Get term_2_genes
if hasattr(self, 'term_2_genes'):
del self.term_2_genes
parity = True
while True:
names_2_idx = {b : a for a, b in enumerate(g.vs['name'])}
term_hash = {names_2_idx[t] : (len(g_list), hash(tuple(g_list))) for t, g_list in self.get_term_2_genes().items() if names_2_idx.has_key(t)}
if verbose: time_print('Identify nodes to collapse')
node_order = g.topological_sorting(mode='out')
small_terms = [v for v in node_order if term_hash[v][0]<min_term_size]
same_as_all_parents = [v for v in node_order \
if (len(g.neighbors(v, mode='out'))>0 and all(term_hash[v]==term_hash[y] for y in g.neighbors(v, mode='out')))]
same_as_all_children = [v for v in node_order \
if (len(g.neighbors(v, mode='in'))>0 and all(term_hash[v]==term_hash[y] for y in g.neighbors(v, mode='in')))]
if verbose: time_print('%s empty terms, %s (%s) terms that are redundant with all their parents (children)' % \
(len(small_terms), len(same_as_all_parents), len(same_as_all_children)))
to_delete = list(set(small_terms) | set(same_as_all_children if parity else same_as_all_parents))
if verbose: time_print('Collapsing %s empty terms and %s terms that redundant with its %s' % \
(len(small_terms),
len(same_as_all_children) if parity else len(same_as_all_parents),
'children' if parity else 'parents'))
parity = not parity
if len(to_delete)==0:
break
else:
for v in to_delete:
g = collapse_node(g, v, use_v_name=False, verbose=False, fast_collapse=True, delete=False)
g.delete_vertices(to_delete)
g.es(relation_eq=None)['relation'] = default_relation
remaining_terms = set([self.terms_index[x] for x in g.vs['name']])
return Ontology(g,
mapping_file=[(gene, self.terms[t]) for gene, t_list in self.gene_2_terms.items() for t in t_list if t in remaining_terms],
combined_file=False, parent_child=False)
def delete_terms(self, terms_to_delete):
print 'Returning new ontology with %s terms removed' % len(terms_to_delete)
from tempfile import NamedTemporaryFile
ontology_file = NamedTemporaryFile()
ontology_file.write(
'\n'.join([t1+'\t'+t2+'\t'+self.relation_dict[(t2,t1)] for t1,t2_list in self.term_2_terms.items() for t2 in t2_list if (t1 not in terms_to_delete and t2 not in terms_to_delete)]) + '\n' +
'\n'.join([g + '\t' + self.terms[t] + '\tgene' for g, t_list in self.gene_2_terms.items() for t in t_list if self.terms[t] not in terms_to_delete]) + '\n')
ontology_file.flush()
return Ontology(ontology_file.name, combined_file=True, parent_child=False)
def delete_genes(self, genes_to_delete, collapse=True, default_relation='default'):
if hasattr(self, 'relation_dict') and len(self.relation_dict)>0:
ontology_table = [(c, p, self.relation_dict.get((c,p), default_relation)) for p, c_list in self.term_2_terms.items() for c in c_list]
else:
ontology_table = [(c, p) for p, c_list in self.term_2_terms.items() for c in c_list]
if isinstance(genes_to_delete, set):
genes_to_delete = set(genes_to_delete)
mapping_table = [(g, self.terms[t]) for g, t_list in self.gene_2_terms.items() if g not in genes_to_delete for t in t_list]
ont = Ontology(ontology_table, mapping_table, parent_child=False, combined_file=False)
if collapse:
return ont.collapse_ontology()
else:
return ont
def write_ontology(self, output, parent_child=True, encoding=None, default_relation=u'default'):
assert parent_child
if encoding:
import codecs
f = codecs.open(output, 'w', encoding=encoding)
else:
f = open(output, 'w')
if hasattr(self, 'relation_dict'):
f.write(u'\n'.join([u'%s\t%s\t%s' % (p,c, self.relation_dict.get((c, p), default_relation)) for p, c_list in self.term_2_terms.items() for c in c_list]))
f.write(u'\n')
f.write(u'\n'.join([u'%s\t%s\tgene' % (self.terms[t], g) for g, t_list in self.gene_2_terms.items() for t in t_list]))
else:
f.write(u'\n'.join([u'%s\t%s\t%s' % (p,c, default_relation) for p, c_list in self.term_2_terms.items() for c in c_list]))
f.write(u'\n')
f.write(u'\n'.join([u'%s\t%s\tgene' % (self.terms[t], g) for g, t_list in self.gene_2_terms.items() for t in t_list]))
f.write(u'\n')
f.close()
def copy(self):
if hasattr(self, 'relation_dict'):
ontology_file = [(c,p,self.relation_dict.get((c,p))) for p, c_list in self.term_2_terms.items() for c in c_list]
else:
ontology_file = [(c,p) for p, c_list in self.term_2_terms.items() for c in c_list]
return Ontology(ontology_file=ontology_file,
mapping_file=[(g,self.terms[t]) for g, t_list in self.gene_2_terms.items() for t in t_list],
combined_file=False, parent_child=False)
def transitive_closure(self, g=None):
relations = ['is_a', 'regulates', 'positively_regulates', 'negatively_regulates', 'has_part', 'part_of', 'gene']
relations_index = {b : a for a, b in enumerate(relations)}
go_reasoning = dict()
for r in relations:
go_reasoning[('is_a', r)] = r
go_reasoning[(r, 'is_a')] = r
if r != 'has_part':
go_reasoning[(r, 'part_of')] = 'part_of'
for r in ['regulates', 'positively_regulates', 'negatively_regulates']:
go_reasoning[(r, 'part_of')] = 'regulates'
go_reasoning[('has_part', 'has_part')] = 'has_part'
if g is None:
g = self.get_igraph()
# Sort from leaves up
for i in g.topological_sorting(mode='out'):
# Get parents
for j in g.neighbors(i, mode='out'):
# Infer GO relations of new edges
base_relation = g.es[g.get_eid(i, j)]['relation']
# Iterate over grandparents
for p in g.neighbors(j, mode='out'):
r = go_reasoning.get((base_relation, g.es[g.get_eid(j, p)]['relation']), None)
# If a relation can't be inferred, then don't add a new edge
if r is not None:
if -1 != g.get_eid(i, p, error=False):
# Edge already exists, so take the higher-ranked relation
e = g.es[g.get_eid(i, p)]
e['relation'] = r if relations_index[r] > relations_index[e['relation']] else e['relation']
else:
# Add new edge with relation
g.add_edge(i, p, relation=r)
return g
## Update term_2_terms , child_2_parents, child_2_parents_indices
def semantic_similarity(self, genes_subset=None, term_sizes='subset', between_terms=False, output='Resnik'):
"""Computes the semantic similarity between pair of genes in
<genes_subset>. Similarity s(g1,g2) is defined as
-log_2(|T_sca| / |T_root|) where |T| is the number of genes in
<genes_subset> that are under term T. T_sca is the "smallest
common ancestor", the common ancestral term with the smallest
term size. T_root is the root term of the ontology.
genes_subset : The set of genes, over which pairs the
similarity will be calculated. If <term_sizes>='subset', then
term sizes will be recalculated according to only these genes,
rather than all genes in the ontology
between_terms : if True, then output similarity between all
terms
output: type of semantic similarity
"""
# If no genes are specified, then compute the similarity between all pairs of genes
if genes_subset is None: genes_subset = self.genes
# genes_subset = genes_subset[:5]
time_print('Calculating term sizes')
if term_sizes=='all_genes':
term_sizes = np.array(self.get_term_sizes())
elif term_sizes=='subset':
# Recompute term sizes, with respect to the intersection of genes
term_2_genes = self.get_term_2_genes()
genes_subset_set = set(genes_subset)
term_sizes = np.array([len(set([self.genes[x] for x in term_2_genes[t]]) & genes_subset_set) for t in self.terms])
else:
raise Exception()
if output=='Resnik':
graph = self.get_igraph().copy()
sca = get_smallest_ancestor(graph, term_sizes)
ss_terms = -1 * np.log2(term_sizes[sca] / float(term_sizes.max()))
if between_terms:
return ss_terms
else:
# For convenience below, set the similarity between a term and itself to be 0
ss_terms[[np.arange(ss_terms.shape[0]), np.arange(ss_terms.shape[0])]] = 0
idx_list = [self.gene_2_terms[g] for g in genes_subset]
ss = [ss_terms[idx1, :][:, idx2].max() for idx1, idx2 in combinations(idx_list, 2)]
return ss
# # Add nodes in the igraph object to represent genes
# graph.add_vertices(self.genes)
# graph.add_edges([(g, t) for g, t_list in self.gene_2_terms.items() for t in t_list])
# assert graph.vs[-len(self.genes):]['name'] == self.genes
# sca = get_smallest_ancestor(graph, term_sizes)
# # Offset the indices
# idx = [len(self.terms) + self.genes_index[g] for g in genes_subset]
# ss = (-1 * np.log2(term_sizes / float(term_sizes.max())))[sca[idx, :][:, idx][np.triu_indices(len(idx), k=1)]]
# return ss
elif output=='sca_list':
## For each pair of gene, return a list of the smallest
## common ancestors (sca). There may be more than one sca with the same size.
gene_2_terms_numpy = {g : np.array(t_list) for g, t_list in self.gene_2_terms.items()}
common_ancestors = [np.intersect1d(gene_2_terms_numpy[g1], gene_2_terms_numpy[g2], assume_unique=True) \
for g1, g2 in combinations(genes_subset, 2)]
assert all(x.size > 0 for x in common_ancestors)
min_size = [term_sizes[x].min() for x in common_ancestors]
sca_list = [x[term_sizes[x]==m] for x, m in zip(common_ancestors, min_size)]
# Dict: (g1,g2) gene pairs --> list of term indices
return {(g1,g2) : x for (g1, g2), x in zip(combinations(genes_subset, 2), sca_list)}
def get_term_2_genes(self, verbose=True):
if not hasattr(self, 'term_2_genes'):
if verbose: print 'Calculating term_2_genes'
self.term_2_genes = {self.terms[c]: [self.genes_index[x[0]] for x in d] \
for c, d in itertools.groupby(sorted([(a,t) for a, terms in self.gene_2_terms.items() for t in terms],
key=lambda x:x[1]),
key=lambda x:x[1])}
for t in self.terms:
if not self.term_2_genes.has_key(t): self.term_2_genes[t] = []
return self.term_2_genes
def get_term_sizes(self):
"Returns an array of term sizes in the same order as self.terms"
if not hasattr(self, 'term_sizes'):
from collections import Counter
tmp = Counter([x for y in self.gene_2_terms.values() for x in y])
self.term_sizes = [tmp[x] for x in range(len(self.terms))]
# self.term_sizes = np.vstack([np.bincount(x, minlength=len(self.terms)) for x in self.gene_2_terms.values()]).sum(0)
# self.term_sizes = np.bincount(np.concatenate([np.array(x) for x in self.gene_2_terms.values()]))
# self.term_sizes = np.bincount(reduce(lambda a,b : a+b, self.gene_2_terms.values()))
return self.term_sizes
def get_term_descriptions(self,f=os.path.join(os.getenv('HOME'),'GI/data/GO/filtered-GO/goID_2_name.tab'), quote=':'):
if not hasattr(self, 'term_description'):
self.term_description = {x.split('\t')[0].replace(quote, '_'):x.split('\t')[1] for x in open(f).read().splitlines()}
for t in self.terms:
if not self.term_description.has_key(t): self.term_description[t] = 'NA'
return self.term_description
def get_term_namespace(self,f=os.path.join(os.getenv('HOME'),'GI/data/GO/filtered-GO/goID_2_namespace.tab'), quote=':'):
if not hasattr(self, 'term_namespace'):
self.term_namespace = {x.split('\t')[0].replace(quote, '_'):x.split('\t')[1] for x in open(f).read().splitlines()}
for t in self.terms:
if not self.term_namespace.has_key(t): self.term_namespace[t] = 'NA'
return self.term_namespace
# def get_leaves(self):
# # Find leaves
# # Probably to debug
# leaves = [t for t in terms if not self.term_2_terms.has_key(t)]
# return(leaves)
def restrict_genes(self, subset):
assert set(subset).issubset(set(self.gene_2_terms.keys()))
self.genes = sorted(subset)
self.gene_2_terms = {g : self.gene_2_terms[g] for g in self.genes}
self.genes_index = {b:a for a,b in enumerate(self.genes)}
def subset_terms(self, subset_type, threshold=None, size=None):
"""Returns the indices of a subset of terms"""
assert (subset_type not in ['gt', 'gte', 'lt', 'lte', 'eq']) or (threshold is not None)
assert (subset_type not in ['random']) or (size is not None)
if subset_type=='gt':
term_indices = [i for i, x in enumerate(self.get_term_sizes()) if x > threshold]
if subset_type=='gte':
term_indices = [i for i, x in enumerate(self.get_term_sizes()) if x >= threshold]
if subset_type=='lt':
term_indices = [i for i, x in enumerate(self.get_term_sizes()) if x < threshold]
if subset_type=='lte':
term_indices = [i for i, x in enumerate(self.get_term_sizes()) if x <= threshold]
if subset_type=='eq':
term_indices = [i for i, x in enumerate(self.get_term_sizes()) if x == threshold]
if subset_type=='random':
term_indices = random.sample(range(len(self.terms)), size)
return term_indices
def shuffle(self):
pass
## OLD CODE: PROBABLY BUGGY
# # Convert gene_2_terms to term_2_genes
# logger.debug('Converting gene_2_terms to term_2_genes')
# genes_index = {b:a for a,b in enumerate(mapped_genes)}
# term_2_genes = {a:[genes_index[q[0]] for q in b] for a, b in
# itertools.groupby(sorted([(g, t) for g in
# gene_2_terms.keys() for t in gene_2_terms[g]],
# key=lambda x:x[1]), key=lambda x: x[1])}
# for t in range(len(terms)):
# if not term_2_genes.has_key(t): term_2_genes[t] = []
# if 'shuffle_gene_degree' in shuffle_options:
# # For each gene separately, shuffle the terms that it is annotated to
# logger.debug('Shuffling annotations while preserving gene degree')
# indices = range(len(terms))
# gene_2_terms = {k: tuple(random.sample(indices, len(v))) for k, v in gene_2_terms.items()}
# elif 'shuffle_term_degree' in shuffle_options:
# # For each term separately, shuffle the genes that it is annotated to
# # -- Only shuffle to other genes that have at least one annotation
# logger.debug('Shuffling annotations while preserving term degree')
# genes_index = {b:a for a,b in enumerate(mapped_genes)}
# indices = range(len(mapped_genes))
# term_2_genes = {key: tuple(random.sample(indices, len(set([genes_index[a[0]] for a in group])))) for key, group in \
# itertools.groupby(mapping, key=lambda a:a[1])}
# # Convert term_2_genes to gene_2_terms
# mapping = [(mapped_genes[g], k) for k, v in term_2_genes.items() for g in v]
# gene_2_terms = {key: tuple(set([terms_index[a[1]] for a in group])) for key, group in \
# itertools.groupby(mapping, key=lambda a:a[0])}
# if 'shuffle_gene_names' in shuffle_options:
# logger.debug('Shuffling gene names in gene-term relations')
# shuffled_genes = mapped_genes[:]
# random.shuffle(shuffled_genes)
# gene_permutation = dict(zip(mapped_genes, shuffled_genes))
# mapping = [(gene_permutation[x], y) for x,y in mapping]
# else: logger.debug('Not shuffling gene names in gene-term relations')
# if 'shuffle_all_degree' in shuffle_options:
# gene_column = [x[0] for x in mapping]
# term_column = [x[1] for x in mapping]
# random.shuffle(term_column)
# mapping = zip(gene_column, term_column)
@classmethod
def get_ann_count(cls, ann_defn):
## OLD CODE : PROBABLY BUGGY
return {x.split('\t')[0] : int(x.split('\t')[1]) for x in open(ann_defn).read().splitlines()}
# if os.path.isfile(ann_defn):
# logger.debug('Reading annotation frequencies from %s', ann_defn)
# ann_count = {x.split('\t')[0] : int(x.split('\t')[1]) for x in open(ann_defn).read().splitlines()}
# ann_count = {key: (ann_count[key] if ann_count.has_key(key) else 0) for key in genes}
# elif ann_defn=='precollapse':
# logger.debug('Using precollapse annotation frequencies')
# # Annotation count of genes before collapsing and before propagation
# precollapse_ann_count = {x.split('\t')[0] : int(x.split('\t')[1]) for x in
# open(os.path.join(os.path.dirname(ontology_prefix), 'precollapse.annotation_count')).read().splitlines()}
# # Restrict keys (because <precollapse_ann_count> may
# # describe more genes than <genes>) and fill in keys in
# # <genes> not in <precollapse_ann_count>
# precollapse_ann_count = {key: (precollapse_ann_count[key] if precollapse_ann_count.has_key(key) else 0) for key in genes}
# # Check that genes are annotated to 0 terms in
# # precollapse_ann_count (pre-collapse) if and only if they are
# # annotated to 0 terms in gene_2_terms (post-collapse)
# # -- Note: this won't hold true if terms are restricted to leaves
# if (not use_leaves) and len(set(['shuffle_term_degree']) & set(shuffle_options))==0:
# assert set(g for g, k in gene_2_terms.items() if len(k)==0) == set(g for g,k in precollapse_ann_count.items() if k==0)
# # Check that dictionary is over same set of genes
# assert set(precollapse_ann_count.keys()) == set(genes)
# ann_count = precollapse_ann_count
# else:
# raise Exception('Unhandled ann_defn: %s' % ann_defn)
# @classmethod
# def get_ontology_prefix(cls, **kwargs):
# ## OLD!
# ontology_prefix = os.path.join(kwargs['NNstructures'],
# 'start_%s' % kwargs['start_ontology'],
# 'evidence_%s' % kwargs['evidence'],
# 'propagate_%s' % kwargs['propagate'],
# 'ontology_%s' % kwargs['ontology'],
# '%s.propagated' % kwargs['collapse_scheme'])
# return ontology_prefix
@classmethod
def create_ontology(cls, folder, start_ontology, evidence, ontology, collapse_scheme, genes=None):
## IRRELVANT TO SAL
ontology = Ontology(Ontology.get_ontology_prefix(NNstructures=folder,
start_ontology=start_ontology,
evidence=evidence,
propagate='forward',
ontology=ontology,
collapse_scheme=collapse_scheme),
genes,
'precollapse',
ontology,
False,
shuffle_options=[])
return ontology
@classmethod
def to_remove(self, gset_list, ann_thresh, logger=None):
## Apply annotation threshold
assert isinstance(ann_thresh, int)
if ann_thresh > 0:
to_remove = [any(self.ann_count[g] < ann_thresh for g in gset) for gset in gset_list]
else:
to_remove = [False for gset in gset_list]
logger.debug('Applying annotation threshold of %s: removing %s (out of %s) genes and %s (out of %s) gsets',
ann_thresh,
sum(x < ann_thresh for x in self.ann_count.values()),
len(self.ann_count.keys()),
sum(to_remove),
len(to_remove))
return to_remove
def get_igraph(self):
"""The vertices in the resulting graph will be in the same order as
self.terms
"""
if hasattr(self, 'relation_dict') and len(self.relation_dict)>0:
edge_attrs = {'relation': [self.relation_dict[(c, p)] for p, children in self.term_2_terms.items() for c in children]}
else:
edge_attrs = {}
self.graph = igraph.Graph(n=len(self.terms),
edges=[(self.terms_index[c], self.terms_index[p]) for p, children in self.term_2_terms.items() for c in children],
directed=True,
vertex_attrs={'name':self.terms},
edge_attrs=edge_attrs)
# if not hasattr(self, 'graph'):
# time_print('Reading into igraph')
# import igraph
# graph = igraph.Graph.Read_Ncol(self.ontology_file)
# time_print('Permuting vertices')
# # Permute vertices of graph to be in the same order as self.terms
# from code.utilities import permute_x_to_y
# graph = graph.permute_vertices(permute_x_to_y(self.terms, graph.vs['name']))
# assert(graph.vs['name'] == self.terms)
# ontology_table = load_table_as_list(self.ontology_file)
# if all([len(x)==3 for x in ontology_table]):
# # Add edge relations, if they exist
# graph.es[graph.get_eids([(a,b) for a, b, r in ontology_table])]['relation'] = [r for a, b, r in ontology_table]
# time_print('Done reading into igraph')
# self.graph = graph
return self.graph
def get_common_ancestors(self, x, y):
"""
Returns list of indices of the common ancestors between terms with index a and b.
Ancestors are sorted by term size.
"""
d = self.get_connectivity_matrix()
return sorted(np.intersect1d(d[x,:].nonzero()[0], d[y,:].nonzero()[0], assume_unique=True),
key=lambda a: self.term_sizes[a])
def get_lca_matrix(self):
"""
Compute least common ancestor matrix.
lca[a,b] = index of least common ancestor of terms a and b
"""
d = self.get_connectivity_matrix()
time_print('Constructing LCA matrix')
lca_matrix = np.zeros(d.shape, dtype=np.int32)
lca_matrix.fill(-1)
# Topological sorting, going bottom-up the tree
for i in self.get_igraph().topological_sorting(mode='out'):
# Note: includes self as a child
children_list = np.where(d[:,i] == 1)[0]
# For those descendants without a computed LCA yet, set their LCA to this term
lca_sub = lca_matrix[children_list.reshape(-1,1), children_list]
lca_sub[lca_sub == -1] = i
lca_matrix[children_list.reshape(-1,1), children_list] = lca_sub
# Check symmetry
assert (lca_matrix.T == lca_matrix).all()
assert (-1 == lca_matrix).sum() == 0
time_print('Done constructing LCA matrix')
self.lca_matrix = lca_matrix
return self.lca_matrix
def get_smallest_ancestor(self):
d = self.get_connectivity_matrix()
time_print('Constructing smallest ancestor matrix')
ancestor_matrix = np.zeros(d.shape, dtype=np.int32)
ancestor_matrix.fill(-1)
#for i in self.graph.topological_sorting(mode='out')
# Traverse nodes, from smallest to largest terms
for i in map(lambda a: a[0], sorted(enumerate(self.get_term_sizes()), key=lambda a: a[1])):
# Note: includes self as a child
children_list = np.where(d[:,i] == 1)[0]
# For those descendants without a computed LCA yet, set their LCA to this term
lca_sub = ancestor_matrix[children_list.reshape(-1,1), children_list]
lca_sub[lca_sub == -1] = i
ancestor_matrix[children_list.reshape(-1,1), children_list] = lca_sub
# Check symmetry
assert (ancestor_matrix.T == ancestor_matrix).all()
assert (-1 == ancestor_matrix).sum() == 0
time_print('Done constructing ancestor matrix')
self.ancestor_matrix = ancestor_matrix
return self.ancestor_matrix
def get_shortest_paths(self, sparse=False, chunk_size=500):
"""
d[a,b] = length of the shortest path from a to b, bottom-up the ontology
"""
graph = self.get_igraph()
if sparse:
# i, j, lengths = zip(*[(i, v) for i, vpath in enumerate(graph.get_shortest_paths(graph.vs, graph.vs, mode='out', output='vpath')) for v in vpath])
# return scipy.sparse.vstack([scipy.sparse.csr_matrix(graph.shortest_paths(v, graph.vs, mode='out')) for v in graph.vs])
print len(graph.vs), chunk_size
return scipy.sparse.vstack([scipy.sparse.csr_matrix(graph.shortest_paths(graph.vs[x[0]:x[1]], graph.vs, mode='out')) \
for x in split_indices_chunk(len(graph.vs), chunk_size)])