Source

orange-bioinformatics / _bioinformatics / obiGO.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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
"""obiGO is a Gene Ontology (GO) Handling Library.

"""

from __future__ import absolute_import

from urllib import urlretrieve
from gzip import GzipFile
import tarfile
import gzip
import shutil
import sys, os
import re, cPickle
from datetime import datetime
from collections import defaultdict

from Orange.orng import orngEnviron

from . import obiProb, obiGene

try:
    from Orange.orng import orngServerFiles
    default_database_path = os.path.join(orngServerFiles.localpath(), "GO")
except Exception:
    default_database_path = os.curdir

_CVS_REVISION_RE = re.compile(r"^(rev)?\d+\.\d+$")

evidenceTypes = {
##Experimental
    'EXP': 'Inferred from Experiment',
    'IDA': 'Inferred from Direct Assay',
    'IPI': 'Inferred from Physical Interaction', ## [with <database:protein_name>]',
    'IMP': 'Inferred from Mutant Phenotype',
    'IGI': 'Inferred from Genetic Interaction', ## [with <database:gene_symbol[allele_symbol]>]',
    'IEP': 'Inferred from Expression Pattern',
##Computational Analysis Evidence Codes
    'ISS': 'Inferred from Sequence Similarity', ## [with <database:sequence_id>] ',
    'ISA': 'Inferred from Sequence Alignment',
    'ISO': 'Inferred from Sequence Orthology',
    'ISM': 'Inferred from Sequence Model',
    'IGC': 'Inferred from Genomic Context',
    'RCA': 'Inferred from Reviewed Computational Analysis',
##Author Statement Evidence Codes
    'TAS': 'Traceable author statement',
    'NAS': 'Non-traceable author statement',
##Curatorial Statement Evidence Codes
    'IC': 'Inferred by curator',
    'ND': 'No biological data available',
##Computationally-assigned Evidence Codes
    'IEA': 'Inferred from electronic annotation', ## [to <database:id>]',
##Obsolete Evidence Codes
    'NR': 'Not Recorded(Obsolete)'
}
##evidenceDict={"IMP":1, "IGI":2, "IPI":4, "ISS":8, "IDA":16, "IEP":32, "IEA":64,
##              "TAS":128, "NAS":256, "ND":512, "IC":1024, "RCA":2048, "IGC":4096, "RCA":8192, "NR":16384}

evidenceDict=defaultdict(int, [(e, 2**i) for i, e in enumerate(evidenceTypes.keys())])

evidenceTypesOrdered = [
'EXP',
'IDA',
'IPI',
'IMP',
'IGI',
'IEP',
##Computational Analysis Evidence Codes
'ISS',
'ISA',
'ISO',
'ISM',
'IGC',
'RCA',
##Author Statement Evidence Codes
'TAS',
'NAS',
##Curatorial Statement Evidence Codes
'IC',
'ND',
##Computationally-assigned Evidence Codes
'IEA',
##Obsolete Evidence Codes
'NR'
]

multiplicitySet=set(["alt_id","is_a","subset","synonym","related_synonym",
                     "exact_synonym","broad_synonym","narrow_synonym",
                     "xref_analog","xref_unknown","relationship"])

multipleTagSet = multiplicitySet

annotationFields=["DB","DB_Object_ID","DB_Object_Symbol","Qualifier","GO_ID",
                  "DB_Reference","Evidence_Code","With_From","Aspect",
                  "DB_Object_Name","DB_Object_Synonym","DB_Object_Type",
                  "Taxon","Date","Assigned_by"]

annotationFieldsDict={"DB":0,
                      "DB_Object_ID":1,
                      "DB_Object_Symbol":2,
                      "Qualifier":3,
                      "GO_ID":4,
                      "GO ID":4,
                      "GOID":4,
                      "DB_Reference":5,
                      "DB:Reference":5,
                      "Evidence_Code":6,
                      "Evidence Code":6,
                      "Evidence_code":6, #compatibility with  older revisions
                      "With_or_From":7,
                      "With (or) From":7,
                      "Aspect":8,
                      "DB_Object_Name":9,
                      "DB_Object_Synonym":10,
                      "DB_Object_Type":11,
                      "taxon":12,
                      "Date":13,
                      "Assigned_by":14}    

builtinOBOObjects = ["""
[Typedef]
id: is_a
name: is_a
range: OBO:TERM_OR_TYPE
domain: OBO:TERM_OR_TYPE
definition: The basic subclassing relationship [OBO:defs]"""
,
"""[Typedef]
id: disjoint_from
name: disjoint_from
range: OBO:TERM
domain: OBO:TERM
definition: Indicates that two classes are disjoint [OBO:defs]"""
,
"""[Typedef]
id: instance_of
name: instance_of
range: OBO:TERM
domain: OBO:INSTANCE
definition: Indicates the type of an instance [OBO:defs]"""
,
"""[Typedef]
id: inverse_of
name: inverse_of
range: OBO:TYPE
domain: OBO:TYPE
definition: Indicates that one relationship type is the inverse of another [OBO:defs]"""
,
"""[Typedef]
id: union_of
name: union_of
range: OBO:TERM
domain: OBO:TERM
definition: Indicates that a term is the union of several others [OBO:defs]"""
,
"""[Typedef]
id: intersection_of
name: intersection_of
range: OBO:TERM
domain: OBO:TERM
definition: Indicates that a term is the intersection of several others [OBO:defs]"""]

class OBOObject(object):
    """ Represents a generic OBO object (e.g. Term, Typedef, Instance, ...)
    Example:
    >>> OBOObject(r"[Term]\nid: FOO:001\nname: bar", ontology)
    """
    _INTERN_TAGS = ["id", "name", "namespace", "alt_id", "is_a"]
    def __init__(self, stanza=None, ontology=None):
        self.ontology = ontology
        self._lines = []
        self.values = {}
        self.related = set()
        self.relatedTo = set()
        if stanza:
            self.ParseStanza(stanza)

    def ParseStanza(self, stanza):
        intern_tags = set(self._INTERN_TAGS)
        for line in stanza.splitlines():
            if ":" not in line:
                continue
            tag, rest = line.split(":", 1)
            value, modifiers, comment = "", "", ""
            if "!" in rest:
                rest, comment = rest.split("!")
            if "{" in rest:
                value, modifiers = rest.split("{", 1)
                modifiers = modifiers.strip("}")
            else:
                value = rest
            tag = intern(tag)
            value = value.strip()
            comment = comment.strip()
            if tag in intern_tags:
                value, comment = intern(value), intern(comment)
            self._lines.append((tag, value, modifiers, comment))
            if tag in multipleTagSet:
                self.values.setdefault(tag, []).append(value)
            else:
                self.values[tag] = value
        self.related = set(self.GetRelatedObjects())
        self.__dict__.update(self.values)
        if "def" in self.__dict__:
            self.__dict__["def_"] = self.__dict__["def"]
        

    def GetRelatedObjects(self):
        """ Return a list of tuple pairs where the first element is relationship
        typeId and the second id of object to whom the relationship applys to.
        """
        ##TODO: add other defined Typedef ids
        typeIds = [intern("is_a")]
        result = [(typeId, id) for typeId in typeIds for id in self.values.get(typeId, [])] 
        result = result + [tuple(map(intern, r.split(None, 1))) for r in self.values.get("relationship", [])]
        return result

    def __repr__(self):
        """ Return a string representation of the object in OBO format
        """
        repr = "[%s]\n" % type(self).__name__
        for tag, value, modifiers, comment in self._lines:
            repr = repr + tag + ": " + value
            if modifiers:
                repr = repr + "{ " + modifiers + " }"
            if comment:
                repr = repr + " ! " + comment
            repr = repr + "\n"
        return repr

    def __str__(self):
        """ Return the OBO object id entry
        """
        return "%s: %s" % (self.id, self.name)

    def __iter__(self):
        """ Iterates over sub terms
        """
        for typeId, id in self.relatedTo:
            yield (typeId, self.ontology[id])
        
class Term(OBOObject):
    pass

class Typedef(OBOObject):
    pass

class Instance(OBOObject):
    pass
        
class Ontology(object):
    """ Ontology is the main class representing a gene ontology.
    
    Example::
        >>> ontology = Ontology("my_gene_ontology.obo")
        
    """
    version = 1
    def __init__(self, file=None, progressCallback=None, rev=None):
        """ Initialize the ontology from file (if `None` the default gene
        ontology will be loaded). The optional `progressCallback` will be
        called with a single argument to report on the progress.
        
        """
        self.terms = {}
        self.typedefs = {}
        self.instances = {}
        self.slimsSubset = set()
        if isinstance(file, basestring):
            self.ParseFile(file, progressCallback)
            
        elif rev is not None:
            if not _CVS_REVISION_RE.match(rev):
                raise ValueError("Invalid revision format.")
            if rev.startswith("rev"):
                rev = rev[3:]
            pc = lambda v: progressCallback(v/2.0) \
                 if progressCallback else None
            filename = os.path.join(default_database_path, "gene_ontology_edit@rev%s.obo" % rev)
            if not os.path.exists(filename):
                self.DownloadOntologyAtRev(rev, filename, pc)
            self.ParseFile(filename, lambda v: progressCallback(v/2.0 + 50) \
                                     if progressCallback else None)
        else:
            fool = self.Load(progressCallback)
            self.__dict__ = fool.__dict__ ## A fool and his attributes are soon parted

    @classmethod
    def Load(cls, progressCallback=None):
        """ A class method that tries to load the ontology file from
        default_database_path. It looks for a filename starting with
        'gene_ontology'. If not found it will download it.
        """
        filename = os.path.join(default_database_path, "gene_ontology_edit.obo.tar.gz")
        if not os.path.isfile(filename) and not os.path.isdir(filename):
            from Orange.orng import orngServerFiles
            orngServerFiles.download("GO", "gene_ontology_edit.obo.tar.gz")
        try:
            return cls(filename, progressCallback=progressCallback)
        except (IOError, OSError), ex:
            print ex
            raise Exception, "Could not locate ontology file"
        
    def ParseFile(self, file, progressCallback=None):
        """ Parse the file. file can be a filename string or an open filelike
        object. The optional progressCallback will be called with a single
        argument to report on the progress.
        """
        if type(file) == str:
            if os.path.isfile(file) and tarfile.is_tarfile(file):
                f = tarfile.open(file).extractfile("gene_ontology_edit.obo")
            elif os.path.isfile(file):
                f = open(file)
            else:
                f = open(os.path.join(file, "gene_ontology_edit.obo"))
        else:
            f = file
        
        data = f.readlines()
        data = "".join([line for line in data if not line.startswith("!")])
        self.header = data[: data.index("[Term]")]
        c=re.compile("\[.+?\].*?\n\n", re.DOTALL)
        data=c.findall(data)

        from Orange.orng import orngMisc
        milestones = orngMisc.progressBarMilestones(len(data), 90)
        for i, block in enumerate(builtinOBOObjects + data):
            if block.startswith("[Term]"):
                term = Term(block, self)
                self.terms[term.id] = term
            elif block.startswith("[Typedef]"):
                typedef = Typedef(block, self)
                self.typedefs[typedef.id] = typedef
            elif block.startswith("[Instance]"):
                instance = Instance(block, self)
                self.instances[instance.id] = instance
            if progressCallback and i in milestones:
                progressCallback(90.0*i/len(data))
        
        self.aliasMapper = {}
        self.reverseAliasMapper = defaultdict(set)
        milestones = orngMisc.progressBarMilestones(len(self.terms), 10)
        for i, (id, term) in enumerate(self.terms.iteritems()):
            for typeId, parent in term.related:
                self.terms[parent].relatedTo.add((typeId, id))
            try:
                self.aliasMapper.update([(alt_id, id) for alt_id in term.alt_id])
                self.reverseAliasMapper[id].union_update(term.alt_id)
            except AttributeError:
                pass
            if progressCallback and i in milestones:
                progressCallback(90.0 + 10.0*i/len(self.terms))

    def GetDefinedSlimsSubsets(self):
        """ Return a list of defined subsets.
        """
        return [line.split()[1] for line in self.header.splitlines() if line.startswith("subsetdef:")]

    def SetSlimsSubset(self, subset):
        """ Set the slims term subset to subset. If subset is a string it
        must equal one of the defined subsetdef.
        """
        if type(subset) == str:
            self.slimsSubset = [id for id, term in self.terms.items() if subset in getattr(term, "subset", set())]
        else:
            self.slimsSubset = set(subset)

    def GetSlimsSubset(self, subset):
        return [id for id, term in self.terms.items() if subset in getattr(term, "subset", set())]

    def GetSlimTerms(self, termId):
        """ Return a list of slim terms for termId.
        """
        queue = set([termId])
        visited = set()
        slims = set()
        while queue:
            term = queue.pop()
            visited.add(term)
            if term in self.slimsSubset:
                slims.add(term)
            else:
                queue.update(set(id for typeId, id in self[term].related) - visited)
        return slims

    def ExtractSuperGraph(self, terms):
        """ Return all super terms of terms up to the most general one.
        """
        terms = [terms] if type(terms) == str else terms
        visited = set()
        queue = set(terms)
        while queue:
            term = queue.pop()
            visited.add(term)
            queue.update(set(id for typeId, id in self[term].related) - visited)
        return visited

    def ExtractSubGraph(self, terms):
        """ Return all sub terms of terms.
        """
        terms = [terms] if type(terms) == str else terms
        visited = set()
        queue = set(terms)
        while queue:
            term = queue.pop()
            visited.add(term)
            queue.update(set(id for typeId, id in self[term].relatedTo) - visited)
        return visited

    def GetTermDepth(self, term, cache_={}):
        """ Return the minimum depth of a term (length of the shortest
        path to this term from the top level term).
        """
        if term not in cache_:
            cache_[term] = min([self.GetTermDepth(parent) + 1 for typeId, parent in self[term].related] or [1])
        return cache_[term]

    def __getitem__(self, id):
        """ Return object with id (same as ontology.terms[id]
        """
        return self.terms.get(id, self.terms.get(self.aliasMapper.get(id, id)))

    def __iter__(self):
        """ Iterate over all ids in ontology
        """
        return iter(self.terms)

    def __len__(self):
        """ Return number of objects in ontology
        """
        return len(self.terms)

    def __contains__(self, id):
        return id in self.terms or id in self.aliasMapper

    @staticmethod
    def DownloadOntology(file, progressCallback=None):
        tFile = tarfile.open(file, "w:gz") if isinstance(file, basestring) else file
        tmpDir = os.path.join(orngEnviron.bufferDir, "tmp_go/")
        try:
            os.mkdir(tmpDir)
        except Exception:
            pass
        urlretrieve("http://www.geneontology.org/ontology/gene_ontology_edit.obo", 
                    os.path.join(tmpDir, "gene_ontology_edit.obo"),
                    progressCallback and __progressCallbackWrapper(progressCallback))
        tFile.add(os.path.join(tmpDir, "gene_ontology_edit.obo"),
                  "gene_ontology_edit.obo")
        tFile.close()
        os.remove(os.path.join(tmpDir, "gene_ontology_edit.obo"))

    @staticmethod
    def DownloadOntologyAtRev(rev, filename=None, progressCallback=None):
        import shutil
        import urllib2
        url = "http://cvsweb.geneontology.org/cgi-bin/cvsweb.cgi/~checkout~/go/ontology/gene_ontology_edit.obo?rev=%s" % rev
        url += ";content-type=text%2Fplain"
        if filename is None:
            filename = os.path.join(default_database_path, "gene_ontology_edit@rev%s.obo" % rev)
        r = urllib2.urlopen(url)
        
        with open(filename + ".part", "wb") as f:
            shutil.copyfileobj(r, f)
            
        os.rename(filename + ".part", filename)
        
        
_re_obj_name_ = re.compile("([a-zA-z0-9-_]+)")

class AnnotationRecord(object):
    """Holds the data for an annotation record read from the annotation file.
    Fields can be accessed with the names: DB, DB_Object_ID, DB_Object_Symbol,
    Qualifier, GO_ID, DB_Reference, Evidence_code, With_or_From, Aspect,
    DB_Object_Name, DB_Object_Synonym, DB_Object_Type, taxon, Date,
    Assigned_by (e.g. rec.GO_ID) or by supplying the original name of the
    field (see http://geneontology.org/GO.annotation.shtml#file) to the get
    method (e.g. rec.get("GO ID")) The object also provides the following
    data members for quicker access: geneName, GOId, evidence, aspect and
    alias(a list of aliases)
    
    """
    __slots__ = annotationFields + ["geneName", "GOId", "evidence",
                                    "aspect", "alias", "additionalAliases"]
    def __init__(self, fullText):
        """\
        :param fulText: A single line from the annotation file.
        
        """
        for slot, val in zip(annotationFields, fullText.split("\t")):
            setattr(self, slot, intern(val))
        self.geneName = self.DB_Object_Symbol
        self.GOId = self.GO_ID
        self.evidence = self.Evidence_Code
        self.aspect = self.Aspect
        self.alias = list(map(intern, self.DB_Object_Synonym.split("|")))

        self.additionalAliases = []
        if ":" in self.DB_Object_Name:
            self.additionalAliases = [] #_re_obj_name_.findall(self.DB_Object_Name.split(":")[0])

    def __getattr__(self, name):
        if name in annotationFieldsDict:
            return getattr(self, self.__slots__[annotationFieldsDict[name]])
        else:
            raise AttributeError(name)


class Annotations(object):
    """Annotations object holds the annotations.
    """
    version = 2
    def __init__(self, file=None, ontology=None, genematcher=None, progressCallback=None, rev=None):
        """Initialize the annotations from file by calling ParseFile on it.
        The ontology must be an instance of Ontology class.
        The optional progressCallback will be called with a single argument
        to report on the progress.
        
        """
        self.file = file
        self.ontology = ontology
        self.allAnnotations = defaultdict(list)
        self.geneAnnotations = defaultdict(list)
        self.termAnnotations = defaultdict(list)
        self._geneNames = None
        self._geneNamesDict = None
        self._aliasMapper = None
        self.additionalAliases = {}
        self.annotations = []
        self.header = ""
        self.genematcher = genematcher
        self.taxid = None
        if type(file) in [list, set, dict, Annotations]:
            for ann in file:
                self.AddAnnotation(ann)
            if type(file, Annotations):
                taxid = file.taxid
        elif isinstance(file, basestring) and os.path.exists(file):
            self.ParseFile(file, progressCallback)
            try:
                self.taxid = to_taxid(os.path.basename(file).split(".")[1]).pop()
            except IOError:
                pass
        elif file is not None:
            # Organism code
            if rev is not None:
                if not _CVS_REVISION_RE.match(rev):
                    raise ValueError("Invalid revision format")
                
                if rev.startswith("rev"):
                    rev = rev[3:]
                code = self.organism_name_search(file)
                filename = os.path.join(default_database_path,
                                        "gene_association.%s@rev%s.tar.gz" % (code, rev))
                
                if not os.path.exists(filename):
                    self.DownloadAnnotationsAtRev(code, rev, filename,
                                                  progressCallback
                                                  )
                    
                self.ParseFile(filename, progressCallback)
                self.taxid = to_taxid(code).pop()
            else:
                a = self.Load(file, ontology, genematcher, progressCallback)
                self.__dict__ = a.__dict__
                self.taxid = to_taxid(organism_name_search(file)).pop()
        if not self.genematcher and self.taxid:
            from . import obiGene
            self.genematcher = obiGene.matcher([obiGene.GMGO(self.taxid)] + \
                                               ([obiGene.GMDicty(), [obiGene.GMGO(self.taxid),
                                                                     obiGene.GMDicty()]] \
                                                if self.taxid == "352472"  else []))
        if self.genematcher:
            self.genematcher.set_targets(self.geneNames)
        
    @classmethod
    def organism_name_search(cls, org):
        ids = to_taxid(org) 
        from . import obiTaxonomy as tax
        if not ids:
            ids = [org] if org in Taxonomy().common_org_map.keys() + Taxonomy().code_map.keys() else []
        if not ids:
            ids = tax.to_taxid(org, mapTo=Taxonomy().keys())
        if not ids:
            ids = tax.search(org, exact=True)
            ids = set(ids).intersection(Taxonomy().keys())
        if not ids:
            ids = tax.search(org)
            ids = set(ids).intersection(Taxonomy().keys())
        codes = set([from_taxid(id) for id in ids])
        if len(codes) > 1:
            raise tax.MultipleSpeciesException, ", ".join(["%s: %s" % \
                            (str(from_taxid(id)), tax.name(id)) for id in ids])
        elif len(codes) == 0:
            raise tax.UnknownSpeciesIdentifier, org
        return codes.pop()

    @classmethod
    def organism_version(cls, name):
        name = organism_name_search(name)
        orngServerFiles.localpath_download("GO", "gene_association.%s.tar.gz" % name)
        return ("v%i." % cls.version) + orngServerFiles.info("GO",
                        "gene_association.%s.tar.gz" % name)["datetime"]

    def SetOntology(self, ontology):
        """ Set the ontology to use in the annotations mapping.
        """
        self.allAnnotations = defaultdict(list)
        self._ontology = ontology

    def GetOntology(self):
        return self._ontology

    ontology = property(GetOntology, SetOntology,
                        doc="Ontology object for annotations")
    
    @classmethod
    def Load(cls, org, ontology=None, genematcher=None, progressCallback=None):
        """A class method that tries to load the association file for the
        given organism from default_database_path.
        """
        from Orange.orng import orngServerFiles
        code = organism_name_search(org)
        
        file = "gene_association.%s.tar.gz" % code

        path = os.path.join(orngServerFiles.localpath("GO"), file)
        if not os.path.exists(path):
            orngServerFiles.download("GO", file)
        return cls(path, ontology=ontology, genematcher=genematcher, progressCallback=progressCallback)
    
    def ParseFile(self, file, progressCallback=None):
        """ Parse and load the annotations from file. Report progress
        with progressCallback.
        File can be:
            - a tarball containing the association file named gene_association
            - a directory name containing the association file named gene_association
            - a path to the actual association file
            - an open file-like object of the association file
        """
        if type(file) == str:
            if os.path.isfile(file) and tarfile.is_tarfile(file):
                f = tarfile.open(file).extractfile("gene_association")
            elif os.path.isfile(file) and file.endswith(".gz"):
                f = gzip.open(file) 
            elif os.path.isfile(file):
                f = open(file)
            else:
                f = open(os.path.join(file, "gene_association"))
        else:
            f = file
        lines = [line for line in f.read().splitlines() if line.strip()]
        from Orange.orng import orngMisc
        milestones = orngMisc.progressBarMilestones(len(lines), 100)
        for i,line in enumerate(lines):
            if line.startswith("!"):
                self.header = self.header + line + "\n"
                continue
            
            a=AnnotationRecord(line)
            self.AddAnnotation(a)
#            self.annotations.append(a)
            if progressCallback and i in milestones:
                progressCallback(100.0*i/len(lines))

    def AddAnnotation(self, a):
        """ Add a single `AnotationRecord` instance to this `Annotations`
        object.
        """
        if not isinstance(a, AnnotationRecord):
            a = AnnotationRecord(a)
        if not a.geneName or not a.GOId or a.Qualifier == "NOT":
            return
        
        self.geneAnnotations[a.geneName].append(a)
        self.annotations.append(a)
        self.termAnnotations[a.GOId].append(a)
        self.allAnnotations = defaultdict(list)
        
        self._geneNames = None
        self._geneNamesDict = None
        self._aliasMapper = None

    @property
    def geneNamesDict(self):
        if getattr(self, "_geneNamesDict", None) is None:
            self._geneNamesDict = defaultdict(set)
            for alias, name in self.aliasMapper.iteritems():
                self._geneNamesDict[name].add(alias)
        return self._geneNamesDict

    @property
    def geneNames(self):
        if getattr(self, "_geneNames", None) is None:
            self._geneNames = set([ann.geneName for ann in self.annotations])
        return self._geneNames

    @property
    def aliasMapper(self):
        if getattr(self, "_aliasMapper", None) is None:
            self._aliasMapper = {}
            for ann in self.annotations:
                self._aliasMapper.update([(alias, ann.geneName) for alias in ann.alias + \
                                          [ann.geneName, ann.DB_Object_ID]])
        return self._aliasMapper

    def GetGeneNamesTranslator(self, genes):
        """ Return a dictionary mapping canonical names (DB_Object_Symbol)
        to `genes`.
         
        """
        def alias(gene):
            if self.genematcher:
                return self.genematcher.umatch(gene)
            else:
                return gene if gene in self.geneNames else \
                        self.aliasMapper.get(gene,
                             self.additionalAliases.get(gene, None))
                        
        return dict([(alias(gene), gene) for gene in genes if alias(gene)])

    def _CollectAnnotations(self, id, visited):
        """ Recursive function collects and caches all annotations for id
        """
        if id not in self.allAnnotations and id not in visited:
            if id in self.ontology.reverseAliasMapper:
                annotations = [self.termAnnotations.get(alt_id, []) \
                               for alt_id in self.ontology.reverseAliasMapper[id]] + \
                                             [self.termAnnotations[id]]
            else:
                ## annotations for this term alone
                annotations = [self.termAnnotations[id]] 
            visited.add(id)
            for typeId, child in self.ontology[id].relatedTo:
                aa = self._CollectAnnotations(child, visited)
                if type(aa) == set:
                    ## if it was already reduced in GetAllAnnotations
                    annotations.append(aa)
                else:
                    annotations.extend(aa)
            self.allAnnotations[id] = annotations
        return self.allAnnotations[id]

    def GetAllAnnotations(self, id):
        """ Return a set of all annotations (instances if `AnnotationRectord`)
        for GO term `id` and all it's subterms.
        
        :param id: GO term id
        :type id: str
        
        """
        visited = set()
        id = self.ontology.aliasMapper.get(id, id)
        if id not in self.allAnnotations or type(self.allAnnotations[id]) == list:
            annot_set = set()
            for annots in self._CollectAnnotations(id, set()):
                annot_set.update(annots)
            self.allAnnotations[id] = annot_set
        return self.allAnnotations[id]

    def GetAllGenes(self, id, evidenceCodes = None):
        """ Return a list of genes annotated by specified `evidenceCodes`
        to GO term 'id' and all it's subterms."
        
        :param id: GO term id
        :type id: str
        
        :param evidneceCodes: List of evidence codes to consider when
                              matching annotations to terms.
        :type evidenceCodes: list-of-strings
        """
        evidenceCodes = set(evidenceCodes or evidenceDict.keys())
        annotations = self.GetAllAnnotations(id)
        return list(set([ann.geneName for ann in annotations if ann.Evidence_Code in evidenceCodes]))

    def GetEnrichedTerms(self, genes, reference=None, evidenceCodes=None, 
                         slimsOnly=False, aspect=None, prob=obiProb.Binomial(),
                         useFDR=True, progressCallback=None):
        """ Return a dictionary of enriched terms, with tuples of
        (list_of_genes, p_value, reference_count) for items and term
        ids as keys. P-Values are FDR adjusted if useFDR is True (default).
        
        :param genes: List of genes
        :param reference: list of genes (if None all genes included in the
                          annotations will be used).
        :param evidenceCodes: List of evidence codes to consider.
        :param slimsOnly: If `True` return only slim terms
        :param aspect: Which aspects to use. Use all by default. "P", "F", "C"
            or a set containing these elements.
        """
        revGenesDict = self.GetGeneNamesTranslator(genes)
        genes = set(revGenesDict.keys())
        if reference:
            refGenesDict = self.GetGeneNamesTranslator(reference)
            reference = set(refGenesDict.keys())
        else:
            reference = self.geneNames

        if aspect == None:
            aspects_set = set(["P", "C", "F"])
        else:
            aspects_set = set([aspect]) if isinstance(aspect, basestring) else aspect

        evidenceCodes = set(evidenceCodes or evidenceDict.keys())
        annotations = [ann for gene in genes for ann in self.geneAnnotations[gene] \
                       if ann.Evidence_Code in evidenceCodes and ann.Aspect in aspects_set]
        refAnnotations = set([ann for gene in reference for ann in self.geneAnnotations[gene] \
                              if ann.Evidence_Code in evidenceCodes and ann.Aspect in aspects_set])
        annotationsDict = defaultdict(set)
        for ann in annotations:
            annotationsDict[ann.GO_ID].add(ann)
            
        if slimsOnly and not self.ontology.slimsSubset:
            import warnings
            warnings.warn("Unspecified slims subset in the ontology! Using 'goslim_generic' subset", UserWarning)
            self.ontology.SetSlimsSubset("goslim_generic")
            
        terms = self.ontology.ExtractSuperGraph(annotationsDict.keys())
        res = {}
        from Orange.orng import orngMisc
        milestones = orngMisc.progressBarMilestones(len(terms), 100)
        for i, term in enumerate(terms):
            if slimsOnly and term not in self.ontology.slimsSubset:
                continue
            allAnnotations = self.GetAllAnnotations(term).intersection(refAnnotations)
##            allAnnotations.intersection_update(refAnnotations)
            allAnnotatedGenes = set([ann.geneName for ann in allAnnotations])
            mappedGenes = genes.intersection(allAnnotatedGenes)
##            if not mappedGenes:
##                print >> sys.stderr, term, sorted(genes)
##                print >> sys.stderr, sorted(allAnnotatedGenes)
##                return
            if len(reference) > len(allAnnotatedGenes):
                mappedReferenceGenes = reference.intersection(allAnnotatedGenes)
            else:
                mappedReferenceGenes = allAnnotatedGenes.intersection(reference)
            res[term] = ([revGenesDict[g] for g in mappedGenes],
                         prob.p_value(len(mappedGenes), len(reference),
                                      len(mappedReferenceGenes), len(genes)),
                         len(mappedReferenceGenes))
            if progressCallback and i in milestones:
                progressCallback(100.0 * i / len(terms))
        if useFDR:
            res = sorted(res.items(), key = lambda (_1, (_2, p, _3)): p)
            res = dict([(id, (genes, p, ref)) \
                        for (id, (genes, _, ref)), p in zip(res, obiProb.FDR([p for _, (_, p, _) in res]))])
        return res

    def GetAnnotatedTerms(self, genes, directAnnotationOnly=False, evidenceCodes=None, progressCallback=None):
        """ Return all terms that are annotated by genes with evidenceCodes.
        """
        genes = [genes] if type(genes) == str else genes
        revGenesDict = self.GetGeneNamesTranslator(genes)
        genes = set(revGenesDict.keys())
        evidenceCodes = set(evidenceCodes or evidenceDict.keys())
        annotations = [ann for gene in genes for ann in self.geneAnnotations[gene] \
                       if ann.Evidence_Code in evidenceCodes]
        dd = defaultdict(set)
        for ann in annotations:
            dd[ann.GO_ID].add(revGenesDict.get(ann.geneName, ann.geneName))
        if not directAnnotationOnly:
            terms = self.ontology.ExtractSuperGraph(dd.keys())
            for i, term in enumerate(terms):
                termAnnots = self.GetAllAnnotations(term).intersection(annotations)
##                termAnnots.intersection_update(annotations)
                dd[term].update([revGenesDict.get(ann.geneName, ann.geneName) for ann in termAnnots])
        return dict(dd)

    def DrawEnrichmentGraph(self, terms, clusterSize, refSize=None, file="graph.png", width=None, height=None, precison=3):
        refSize = len(self.geneNames) if refSize == None else refSize
        sortedterms = sorted(terms.items(), key=lambda term:term[1][1])
        fdr = dict(zip([t[0] for t in sortedterms], obiProb.FDR([t[1][1] for t in sortedterms])))
        termsList = [(term, (float(len(terms[term][0]))/clusterSize) / (float(terms[term][2])/refSize),
                      len(terms[term][0]), terms[term][2], terms[term][1],
                      fdr[term], terms[term][0]) for term in terms]
                          
        drawEnrichmentGraph(termsList, file, width, height, ontology=self.ontology, precison=precison)

    def __add__(self, iterable):
        """ Return a new Annotations object with combined annotations
        """
        return Annotations([a for a in self] + [a for a in iterable], ontology=self.ontology)

    def __iadd__(self, iterable):
        """ Add annotations to this instance
        """
        self.extend(iterable)
        return self

    def __contains__(self, item):
        return item in self.annotations
            
    def __iter__(self):
        """ Iterate over all AnnotationRecord objects in annotations
        """
        return iter(self.annotations)

    def __len__(self):
        """ Return the number of annotations
        """
        return len(self.annotations)

    def __getitem__(self, index):
        """ Return the i-th annotation record
        """
        return self.annotations[index]

    def __getslice__(self, *args):
        return self.annotations.__getslice__(*args)

    def add(self, line):
        """ Add one annotation
        """
        self.AddAnnotation(line)

    def append(self, line):
        """ Add one annotation
        """
        self.AddAnnotation(line)

    def extend(self, lines):
        """ Add multiple annotations
        """
        for line in lines:
            self.AddAnnotation(line)

    def RemapGenes(self, map):
        """ 
        """
        from copy import copy
        for gene in map:
            annotations = self.geneAnnotations[gene]
            for ann in annotations:
                for name in map[gene]:
                    ann1 = copy(ann)
                    ann1.geneName = name
                    self.add(ann1)
        self.genematcher = obiGene.GMDirect()
        try:
            del self._geneNames
        except Exception:
            pass
        self.genematcher.set_targets(self.geneNames)
    
    @staticmethod
    def DownloadAnnotations(org, file, progressCallback=None):
        tFile = tarfile.open(file, "w:gz") if type(file) == str else file
        tmpDir = os.path.join(orngEnviron.bufferDir, "tmp_go/")
        try:
            os.mkdir(tmpDir)
        except Exception:
            pass
        fileName = "gene_association." + org + ".gz"
        urlretrieve("http://www.geneontology.org/gene-associations/" + fileName,
                    os.path.join(tmpDir, fileName),
                    progressCallback and __progressCallbackWraper(progressCallback))
        gzFile = GzipFile(os.path.join(tmpDir, fileName), "r")
        file = open(os.path.join(tmpDir, "gene_association." + org), "w")
        file.writelines(gzFile.readlines())
        file.flush()
        file.close()
##        tFile = tarfile.open(os.path.join(tmpDir, "gene_association." + org + ".tar.gz"), "w:gz")
        tFile.add(os.path.join(tmpDir, "gene_association." + org), "gene_association")
        annotation = Annotations(os.path.join(tmpDir, "gene_association." + org),
                    genematcher=obiGene.GMDirect(), progressCallback=progressCallback)
        cPickle.dump(annotation.geneNames, open(os.path.join(tmpDir, "gene_names.pickle"), "wb"))
        tFile.add(os.path.join(tmpDir, "gene_names.pickle"), "gene_names.pickle")
        tFile.close()
        os.remove(os.path.join(tmpDir, "gene_association." + org))
        os.remove(os.path.join(tmpDir, "gene_names.pickle"))
        
    @staticmethod
    def DownloadAnnotationsAtRev(org, rev, filename=None, progressCallback=None):
        import urllib2
        import shutil
        if filename is None:
            filename = os.path.join(default_database_path,
                                    "gene_association.%s@rev%s.tar.gz" % (code, rev))
        url = "http://cvsweb.geneontology.org/cgi-bin/cvsweb.cgi/~checkout~/go/gene-associations/gene_association.%s.gz?rev=%s" % (org, rev)
        url += ";content-type=application%2Fx-gzip"
        r = urllib2.urlopen(url)
        
        with open(filename + ".part", "wb") as f:
            shutil.copyfileobj(r, f)
        
        os.rename(filename + ".part", filename)

from .obiTaxonomy import pickled_cache

@pickled_cache(None, [("GO", "taxonomy.pickle"), ("Taxonomy", "ncbi_taxonomy.tar.gz")])
def organism_name_search(name):
    return Annotations.organism_name_search(name)

def filterByPValue(terms, maxPValue=0.1):
    """ Filters the terms by the p-value. Asumes terms is a dict with
    the same structure as returned from GetEnrichedTerms.
    
    """
    return dict(filter(lambda (k,e): e[1]<=maxPValue, terms.items()))

def filterByFrequency(terms, minF=2):
    """ Filters the terms by the cluster frequency. Asumes terms is
    a dict with the same structure as returned from GetEnrichedTerms.
    
    """
    return dict(filter(lambda (k,e): len(e[0])>=minF, terms.items()))

def filterByRefFrequency(terms, minF=4):
    """ Filters the terms by the reference frequency. Asumes terms is
    a dict with the same structure as returned from GetEnrichedTerms.
    
    """
    return dict(filter(lambda (k,e): e[2]>=minF, terms.items()))

##def drawEnrichmentGraph(termsList, clusterSize, refSize, filename="graph.png", width=None, height=None):
##    if type(termsList) == dict:
##        termsList = [(term, (float(len(termsList[term][0]))/clusterSize) / (float(termsList[term][2])/refSize),
##                      len(termsList[term][0]), termsList[term][2], termsList[term][1], 1.0, termsList[term][0]) for term in termsList]
##                     
##                     
##                             
##    drawEnrichmentGraph_tostreamMk2(termsList, open(filename, "wb"), width, height)

def drawEnrichmentGraph_tostream(GOTerms, clusterSize, refSize, fh, width=None, height=None):
    def getParents(term):
        parents = extractGODAG([term])
        parents = filter(lambda t: t.id in GOTerms and t.id!=term, parents)
        c = []
        map(c.extend, [getParents(t.id) for t in parents])
        parents = filter(lambda t: t not in c, parents)
        return parents
    parents = dict([(term, getParents(term)) for term in GOTerms])
    #print "Parentes", parents
    def getChildren(term):
        return filter(lambda t: term in [p.id for p in parents[t]], GOTerms.keys())
    topLevelTerms = filter(lambda t: not parents[t], parents.keys())
    #print "Top level terms", topLevelTerms
    termsList=[]
    def collect(term, parent):
        termsList.append(
            ((float(len(GOTerms[term][0]))/clusterSize) / (float(GOTerms[term][2])/refSize),
            len(GOTerms[term][0]),
            GOTerms[term][2],
            "%.4f" % GOTerms[term][1],
            loadedGO.termDict[term].name,
            loadedGO.termDict[term].id,
            ", ".join(GOTerms[term][0]),
            parent)
            )
##        print float(len(GOTerms[term][0])), float(GOTerms[term][2]), clusterSize, refSize
        parent = len(termsList)-1
        for c in getChildren(term):
            collect(c, parent)
                         
    for topTerm in topLevelTerms:
        collect(topTerm, None)

    drawEnrichmentGraphPIL_tostream(termsList, fh, width, height)

def drawEnrichmentGraph(enriched, file="graph.png", width=None, height=None, header=None, ontology = None, precison=3):
    file = open(file, "wb") if type(file) == str else file
    drawEnrichmentGraph_tostreamMk2(enriched, file,  width, height, header, ontology, precison)
    
def drawEnrichmentGraph_tostreamMk2(enriched, fh, width, height, header=None, ontology = None, precison=4):
    ontology = ontology if ontology else Ontology()
    header = header if header else ["List", "Total", "p-value", "FDR", "Names", "Genes"]
    GOTerms = dict([(t[0], t) for t in enriched if t[0] in ontology])
    def getParents(term):
        parents = ontology.ExtractSuperGraph([term])
        parents = [id for id in parents if id in GOTerms and id != term]
        c = reduce(set.union, [set(ontology.ExtractSuperGraph([id])) - set([id]) for id in parents], set())
        parents = [t for t in parents if t not in c]
        return parents
    parents = dict([(term, getParents(term)) for term in GOTerms])
    #print "Parentes", parents
    def getChildren(term):
        return [id for id in GOTerms if term in parents[id]]
    topLevelTerms = [id for id in parents if not parents[id]]
    #print "Top level terms", topLevelTerms
    termsList=[]
    fmt = "%" + ".%if" % precison
    def collect(term, parent):
##        termsList.append(
##            ((float(len(GOTerms[term][0]))/clusterSize) / (float(GOTerms[term][2])/refSize),
##            len(GOTerms[term][0]),
##            GOTerms[term][2],
##            "%.4f" % GOTerms[term][1],
##            loadedGO.termDict[term].name,
##            loadedGO.termDict[term].id,
##            ", ".join(GOTerms[term][0]),
##            parent)
##            )
        termsList.append(GOTerms[term][1:4] + \
                         (fmt % GOTerms[term][4],
                          fmt % GOTerms[term][5],
                          ontology[term].name,
                          ", ".join(GOTerms[term][6])) + (parent,))
##        print float(len(GOTerms[term][0])), float(GOTerms[term][2]), clusterSize, refSize
        parent = len(termsList)-1
        for c in getChildren(term):
            collect(c, parent)
                         
    for topTerm in topLevelTerms:
        collect(topTerm, None)
    for entry in enriched:
        if entry[0] not in ontology:
            termsList.append(entry[1:4] + \
                             (fmt % entry[4],
                              fmt % entry[5],
                              entry[0],
                              ", ".join(entry[6])) + (None,))

    drawEnrichmentGraphPIL_tostream(termsList, header, fh, width, height)
##    drawEnrichmentGraphPylab_tostream(termsList, header, fh, width, height)
    
def drawEnrichmentGraphPIL_tostream(termsList, headers, fh, width=None, height=None):
    from PIL import Image, ImageDraw, ImageFont
    backgroundColor = (255, 255, 255)
    textColor = (0, 0, 0)
    graphColor = (0, 0, 255)
    fontSize = height==None and 12 or (height-60)/len(termsList)
    font = ImageFont.load_default()
    try:
        font = ImageFont.truetype("arial.ttf", fontSize)
    except:
        pass
    getMaxTextHeightHint = lambda l: max([font.getsize(t)[1] for t in l])
    getMaxTextWidthHint = lambda l: max([font.getsize(t)[0] for t in l])
    maxFoldWidth = width!=None and min(150, width/6) or 150
    maxFoldEnrichment = max([t[0] for t in termsList])
    foldNormalizationFactor = float(maxFoldWidth)/maxFoldEnrichment
    foldWidths = [int(foldNormalizationFactor*term[0]) for term in termsList]
    treeStep = 10
    treeWidth = {}
    for i, term in enumerate(termsList):
        treeWidth[i] = (term[-1]==None and 1 or treeWidth[term[-1]]+1)
    treeStep = width!=None and min(treeStep, width/(6*max(treeWidth.values())) or 2) or treeStep
    treeWidth = [w*treeStep + foldWidths[i] for i, w in treeWidth.items()]
    treeWidth = max(treeWidth) - maxFoldWidth
    verticalMargin = 10
    horizontalMargin = 10
##    print verticalMargin, maxFoldWidth, treeWidth
##    treeWidth = 100
    firstColumnStart = verticalMargin + maxFoldWidth + treeWidth + 10
    secondColumnStart = firstColumnStart + getMaxTextWidthHint([str(t[1]) for t in termsList]+[headers[0]]) + 2
    thirdColumnStart = secondColumnStart + getMaxTextWidthHint([str(t[2]) for t in termsList]+[headers[1]]) + 2
    fourthColumnStart = thirdColumnStart + getMaxTextWidthHint([str(t[3]) for t in termsList]+[headers[2]]) + 2
    fifthColumnStart = fourthColumnStart + getMaxTextWidthHint([str(t[4]) for t in termsList]+[headers[3]]) + 4
##    maxAnnotationTextWidth = width==None and getMaxTextWidthHint([str(t[4]) for t in termsList]+["Annotation"]) or (width - fourthColumnStart - verticalMargin) * 2 / 3
    maxAnnotationTextWidth = width==None and getMaxTextWidthHint([str(t[5]) for t in termsList]+[headers[4]]) or max((width - fifthColumnStart - verticalMargin) * 2 / 3, getMaxTextWidthHint([t[5] for t in termsList]+[headers[4]]))
    sixthColumnStart  = fifthColumnStart + maxAnnotationTextWidth + 4
    maxGenesTextWidth = width==None and getMaxTextWidthHint([str(t[6]) for t in termsList]+[headers[5]]) or (width - fifthColumnStart - verticalMargin) / 3
    
    legendHeight = font.getsize("1234567890")[1]*2
    termHeight = font.getsize("A")[1]
##    print fourthColumnStart, maxAnnotationTextWidth, verticalMargin
    width = sixthColumnStart + maxGenesTextWidth + verticalMargin
    height = len(termsList)*termHeight+2*(legendHeight+horizontalMargin)

    image = Image.new("RGB", (width, height), backgroundColor)
    draw = ImageDraw.Draw(image)

    def truncText(text, maxWidth, append=""):
        #print getMaxTextWidthHint([text]), maxAnnotationTextWidth
        if getMaxTextWidthHint([text])>maxWidth:
            while getMaxTextWidthHint([text+"..."+append])>maxWidth and text:
                text = text[:-1]
            if text:
                text = text+"..."+append
            else:
                text = append
        return text
    currentY = horizontalMargin + legendHeight
    connectAtX = {}
    for i, term in enumerate(termsList):
        draw.line([(verticalMargin, currentY+termHeight/2), (verticalMargin + foldWidths[i], currentY+termHeight/2)], width=termHeight-2, fill=graphColor)
        draw.text((firstColumnStart, currentY), str(term[1]), font=font, fill=textColor)
        draw.text((secondColumnStart, currentY), str(term[2]), font=font, fill=textColor)
        draw.text((thirdColumnStart, currentY), str(term[3]), font=font, fill=textColor)
        draw.text((fourthColumnStart, currentY), str(term[4]), font=font, fill=textColor)
##        annotText = width!=None and truncText(str(term[5]), maxAnnotationTextWidth, str(term[5])) or str(term[4])
        annotText = width!=None and truncText(str(term[5]), maxAnnotationTextWidth)
        draw.text((fifthColumnStart, currentY), annotText, font=font, fill=textColor)
        genesText = width!=None and truncText(str(term[6]), maxGenesTextWidth) or str(term[6])
        draw.text((sixthColumnStart, currentY), genesText, font=font, fill=textColor)
        lineEnd = term[-1]==None and firstColumnStart-10 or connectAtX[term[-1]]
        draw.line([(verticalMargin+foldWidths[i]+1, currentY+termHeight/2), (lineEnd, currentY+termHeight/2)], width=1, fill=textColor)
        if term[-1]!=None:
            draw.line([(lineEnd, currentY+termHeight/2), (lineEnd, currentY+termHeight/2 - termHeight*(i-term[-1]))], width=1, fill=textColor)
        connectAtX[i] = lineEnd - treeStep
        currentY+=termHeight

    currentY = horizontalMargin
    draw.text((firstColumnStart, currentY), headers[0], font=font, fill=textColor)
    draw.text((secondColumnStart, currentY), headers[1], font=font, fill=textColor)
    draw.text((thirdColumnStart, currentY), headers[2], font=font, fill=textColor)
    draw.text((fourthColumnStart, currentY), headers[3], font=font, fill=textColor)
    draw.text((fifthColumnStart, currentY), headers[4], font=font, fill=textColor)
    draw.text((sixthColumnStart, currentY), headers[5], font=font, fill=textColor)

    horizontalMargin = 0
    #draw.line([(verticalMargin, height - horizontalMargin - legendHeight), (verticalMargin + maxFoldWidth, height - horizontalMargin - legendHeight)], width=1, fill=textColor)
    draw.line([(verticalMargin, horizontalMargin + legendHeight), (verticalMargin + maxFoldWidth, horizontalMargin + legendHeight)], width=1, fill=textColor)
    maxLabelWidth = getMaxTextWidthHint([" "+str(i) for i in range(int(maxFoldEnrichment+1))])
    numOfLegendLabels = max(int(maxFoldWidth/maxLabelWidth), 2)
    for i in range(numOfLegendLabels+1):
        #draw.line([(verticalMargin + i*maxFoldWidth/10, height - horizontalMargin - legendHeight/2), (verticalMargin + i*maxFoldWidth/10, height - horizontalMargin - legendHeight)], width=1, fill=textColor)
        #draw.text((verticalMargin + i*maxFoldWidth/10 - font.getsize(str(i))[0]/2, height - horizontalMargin - legendHeight/2), str(i), font=font, fill=textColor)

        label = str(int(i*maxFoldEnrichment/numOfLegendLabels))
        draw.line([(verticalMargin + i*maxFoldWidth/numOfLegendLabels, horizontalMargin + legendHeight/2), (verticalMargin + i*maxFoldWidth/numOfLegendLabels, horizontalMargin + legendHeight)], width=1, fill=textColor)
        draw.text((verticalMargin + i*maxFoldWidth/numOfLegendLabels - font.getsize(label)[0]/2, horizontalMargin), label, font=font, fill=textColor)
        
    image.save(fh)

def drawEnrichmentGraphPylab_tostream(termsList, headers, fh, width=None, height=None, show=True):
    from matplotlib import pyplot as plt
    from matplotlib.patches import Rectangle
    
    maxFoldWidth = width!=None and min(150, width/6) or 150
    maxFoldEnrichment = max([t[0] for t in termsList])
    foldNormalizationFactor = float(maxFoldWidth)/maxFoldEnrichment
##    foldWidths = [int(foldNormalizationFactor*term[0]) for term in termsList]
    foldWidths = [term[0] for term in termsList]
    treeStep = maxFoldEnrichment*0.05
    treeWidth = {}

    for i, term in enumerate(termsList):
        treeWidth[i] = (term[-1]==None and treeStep or treeWidth[term[-1]] + treeStep)
    maxTreeWidth = max(treeWidth)

    connectAt = {}
    cellText = []
    axes1 = plt.axes([0.1, 0.1, 0.2, 0.8])
    for i, line in enumerate(termsList):
        enrichment, n, m, p_val, fdr_val, name, genes, parent = line
        r = Rectangle((0, len(termsList) - i - 0.4), enrichment, 0.8)
        plt.gca().add_patch(r)
        plt.plot([enrichment, connectAt.get(parent, maxFoldEnrichment + maxTreeWidth)], [len(termsList) - i, len(termsList) - i], color="black")
        connectAt[i] = connectAt.get(parent, maxFoldEnrichment + maxTreeWidth) - treeStep
        if parent != None:
            plt.plot([connectAt.get(parent)]*2, [len(termsList) - i, len(termsList) - parent], color="black")
        cellText.append((str(n), str(m), p_val, fdr_val, name, genes))

##    from Orange.orng.orngClustering import TableTextLayout
##    text = TableTextLayout((maxFoldEnrichment*1.1, len(termsList)), cellText)
    from Orange.orng.orngClustering import TablePlot
    if True:
        axes2 = plt.axes([0.3, 0.1, 0.6, 0.8], sharey=axes1)
        axes2.set_axis_off()
        table = TablePlot((0, len(termsList)), axes=plt.gca())
        for i, line in enumerate(cellText):
            for j, text in enumerate(line):
                table.add_cell(i, j,width=len(text), height=1, text=text, loc="left", edgecolor="w", facecolor="w")

        table.set_figure(plt.gcf())
        plt.gca().add_artist(table)
        plt.gca()._set_artist_props(table)
##    plt.text(3, 3, "\n".join(["\t".join(text) for text in cellText]))

##    table = plt.table(cellText=cellText, colLabels=headers, loc="right")
##    table.set_transform(plt.gca().transData)
##    
##    table.set_xy(20,20)
    plt.show()
    
class Taxonomy(object):
    """Maps NCBI taxonomy ids to coresponding GO organism codes
    """
    common_org_map = {"297284":"9913", "30523":"9913", # Bos taurus
                      "5782":"352472", "44689":"352472", "366501":"352472", # Dictyostelium discoideum
                      "83333": "562", # Escherichia coli
                      "52545":"4530", "4532":"4530", "65489":"4530", "4533":"4530", "77588":"4530", "29689":"4530",
                      "4538":"4530", "40148":"4530", "29690":"4530", "110450":"4530", "4534":"4530", "83309":"4530",
                      "4528":"4530", "127571":"4530", "40149":"4530", "83307":"4530", "63629":"4530", "4536": "4530",
                      "4535":"4530", "4537":"4530", "65491":"4530", "83308":"4530", "4529":"4530", "4530":"4530",
                      "39946":"4530", "39947":"4530", "110451":"4530", "364100":"4530", "364099":"4530", "4539":"4530",
                      }
    code_map = {"3702":"tair",  # Arabidopsis thaliana
                "9913":"goa_cow", # Bos taurus
                "6239":"wb", # Caenorhabditis elegans
                "3055":None, # Chlamydomonas reinhardtii
                "7955":"zfin", # Danio rerio (zebrafish)
                "352472":"dictyBase", # Dictyostelium discoideum
                "7227": "fb", # Drosophila melanogaster
                "562": "ecocyc", # Escherichia coli
                "11103": None, # Hepatitis C virus
                "9606": "goa_human",  # Homo sapiens
                "10090": "mgi", # Mus musculus
                "2104": None,  # Mycoplasma pneumoniae
                "4530": "gramene_oryza",  # Oryza sativa
                "5833": "GeneDB_Pfalciparum",  # Plasmodium falciparum
                "4754": None,  # Pneumocystis carinii
                "10116": "rgd", # Rattus norvegicus
                "4932": "sgd",  # Saccharomyces cerevisiae
                "4896": "GeneDB_Spombe", # Schizosaccharomyces pombe
                "31033": None, # Takifugu rubripes
                "8355": None,  # Xenopus laevis
                "4577": None # Zea mays
                }
    version = 1
    __shared_state = {"tax": None}
    def __init__(self):
        self.__dict__ = self.__shared_state
        if not self.tax:
            from Orange.orng import orngServerFiles
            path = orngServerFiles.localpath_download("GO", "taxonomy.pickle")
            if os.path.isfile(path):
                self.tax = cPickle.load(open(path, "rb"))
            else:
                orngServerFiles.download("GO", "taxonomy.pickle")
                self.tax = cPickle.load(open(path, "rb"))
                
    def __getitem__(self, key):
        key = self.common_org_map.get(key, key)
        return self.code_map[key]
    
    def keys(self):
        return list(set(self.common_org_map.keys() + self.code_map.keys()))
    
#    @classmethod
#    def get_taxonomy(cls):
#        import urllib2 as url
#        import sgmllib
#        organisms
#        class MyParser(sgmllib.SGMLParser):
#            inTable = False
#            def start_table(self, attributes):
#                self.inTable = dict(attributes).get("summary", False)
#            def end_table(self):
#                self.inTable = False
#            def start
    
def from_taxid(id):
    """ Return a set of GO organism codes that correspond to NCBI taxonomy id
    """
    return Taxonomy()[id]

def to_taxid(db_code):
    """ Return a set of NCBI taxonomy ids from db_code GO organism annotations
    """
    r = [key for key, val in Taxonomy().code_map.items() if db_code == val]
    return set(r)
    

class __progressCallbackWrapper:
    def __init__(self, callback):
        self.callback = callback
    def __call__(self, bCount, bSize, fSize):
        fSize = 10000000 if fSize == -1 else fSize
        self.callback(100*bCount*bSize/fSize)
        
from .obiGenomicsUpdate import Update as UpdateBase

import urllib2

class Update(UpdateBase):
    def __init__(self, local_database_path=None, progressCallback=None):
        UpdateBase.__init__(self, local_database_path or getDataDir(), progressCallback)
    def CheckModified(self, addr, date=None):
        return date < self.GetLastModified(addr) if date else True
        
    def CheckModifiedOrg(self, org):
        return self.CheckModified("http://www.geneontology.org/gene-associations/gene_association." + org + ".gz", self.LastModifiedOrg(org))
    
    def LastModifiedOrg(self, org):
        return self.shelve.get((Update.UpdateAnnotation, (org,)), None)

    def GetLastModified(self, addr):
        stream = urllib2.urlopen(addr)
        return datetime.strptime(stream.headers.get("Last-Modified"), "%a, %d %b %Y %H:%M:%S %Z")
##        return stream.headers.get("Last-Modified")

    def GetAvailableOrganisms(self):
        source = urllib2.urlopen("http://www.geneontology.org/gene-associations/").read()
        return [s.split(".")[1] for s in sorted(set(re.findall("gene_association\.[a-zA-z0-9_]+?\.gz", source)))]

    def GetDownloadedOrganisms(self):
        return [name.split(".")[1] for name in os.listdir(self.local_database_path) if name.startswith("gene_association")]

    def IsUpdatable(self, func, args):
        if func == Update.UpdateOntology:
            return self.CheckModified("http://www.geneontology.org/ontology/gene_ontology.obo", self.shelve.get((Update.UpdateOntology, ()), None))
        elif func == Update.UpdateAnnotation:
            return self.CheckModifiedOrg(args[0])
            
    def GetDownloadable(self):
        orgs = set(self.GetAvailableOrganisms()) - set(self.GetDownloadedOrganisms())
        ret = []
        if (Update.UpdateOntology, ()) not in self.shelve:
            ret.append((Update.UpdateOntology, ()))
        if orgs:
            ret.extend([(Update.UpdateAnnotation, (org,)) for org in orgs])
        return ret

    def UpdateOntology(self):
        Ontology.DownloadOntology(os.path.join(self.local_database_path, "gene_ontology_edit.obo.tar.gz"), self.progressCallback)
        self._update(Update.UpdateOntology, (), self.GetLastModified("http://www.geneontology.org/ontology/gene_ontology.obo"))

    def UpdateAnnotation(self, org):
        Annotations.DownloadAnnotations(org, os.path.join(self.local_database_path, "gene_association." + org + ".tar.gz"), self.progressCallback)
        self._update(Update.UpdateAnnotation, (org,), self.GetLastModified("http://www.geneontology.org/gene-associations/gene_association." + org + ".gz"))
        
    def UpdateTaxonomy(self, org):
        exclude = ["goa_uniprot", "goa_pdb", "GeneDB_tsetse", "reactome", "goa_zebrafish", "goa_rat", "goa_mouse"]

        orgs = self.GetAvailableOrganisms()
        tax = defaultdict(set)

        for org in orgs:
            if org in exclude:
                continue
            try:
                a = obiGO.Annotations(os.path.join(self.local_database_path, "gene_association." + org + ".tar.gz"))
                taxons = set(ann.taxon for ann in a.annotations)
                for taxId in [t.split(":")[-1] for t in taxons if "|" not in t]: ## exclude taxons with cardinality 2
                    tax[taxId].add(org)
            except Exception, ex:
                print ex
                
        cPickle.dump(dict(tax), open(os.path.join(path, "taxonomy.pickle"), "wb"))
            

def _test1():
##    Ontology.DownloadOntology("ontology_arch.tar.gz")
##    Annotations.DownloadAnnotations("sgd", "annotations_arch.tar.gz")
    def _print(f):
        print f
    o = Ontology("ontology_arch.tar.gz")
    a = Annotations("annotations_arch.tar.gz", ontology=o)
    
    a.GetEnrichedTerms(sorted(a.geneNames)[:100])#, progressCallback=_print)
##    profile.runctx("a.GetEnrichedTerms(sorted(a.geneNames)[:100])", {"a":a}, {})
    a.GetEnrichedTerms(sorted(a.geneNames)[:100])#, progressCallback=_print)
    d1 = a.GetEnrichedTerms(sorted(a.geneNames)[:1000])#, progressCallback=_print)
    
##    print a.GetEnrichedTerms(sorted(a.geneNames)[:100])#, progressCallback=_print)

def _test2():
    o = Ontology()
    a = Annotations("human", ontology=o)
    clusterGenes = sorted(a.geneNames)[:100]
    for i in range(10):
        terms = a.GetEnrichedTerms(sorted(a.geneNames)[:200], aspect=["P"])
        a.GetEnrichedTerms(sorted(a.geneNames)[:200], aspect=["C"])
        a.GetEnrichedTerms(sorted(a.geneNames)[:200], aspect=["F"])
        print i
#    a.DrawEnrichmentGraph(filterByPValue(terms), len(clusterGenes), len(a.geneNames))
              
##    drawEnrichmentGraph([("bal", 1.0, 5, 6, 0.1, 0.4, ["vv"]),
##                        ("GO:0019079", 0.5, 5, 6, 0.1, 0.4, ["cc", "bb"]),
##                        ("GO:0022415", 0.4, 5, 7, 0.11, 0.4, ["cc1", "bb"])], open("graph.png", "wb"), None, None)

def _test3():
    o = Ontology()
    a = Annotations("sgd", ontology=o)
##    a = Annotations(list(a)[3:len(a)/3], ontology=o)
    clusterGenes = sorted(a.geneNames)[:1] + sorted(a.geneNames)[-1:]
##    clusterGenes = [g + "_" + str(i%5) for g in sorted(a.geneNames)[:2]]
    exonMap = dict([(gene, [gene+"_E%i" %i for i in range(10)]) for gene in a.geneNames])
    a.RemapGenes(exonMap)
##    o.reverseAliasMapper = o.aliasMapper = {}
    terms = a.GetEnrichedTerms(exonMap.values()[0][:2] + exonMap.values()[-1][2:])
##    terms = a.GetEnrichedTerms(clusterGenes)
    print terms
##    a.DrawEnrichmentGraph(filterByPValue(terms), len(clusterGenes), len(a.geneNames))
    a.DrawEnrichmentGraph(filterByPValue(terms, maxPValue=0.1), len(clusterGenes), len(a.geneNames))
    
if __name__ == "__main__":
    _test2()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.