source: orange-bioinformatics/_bioinformatics/obiOMIM.py @ 1794:3a530b619d7f

Revision 1794:3a530b619d7f, 3.6 KB checked in by Flashpoint <vid.flashpoint@…>, 11 months ago (diff)

Added Cytobands genesets and modified obiOMIM.py. Both need some extra work.

Line 
1import sys, os
2import urllib2
3import shutil
4import re
5
6from collections import defaultdict
7
8from Orange.orng import orngServerFiles
9
10class disease(object):
11    """ A class representing a disease in the OMIM database
12    """
13    regex = re.compile(r'(?P<name>.*?),? (?P<id>[0-9]{3,6} )?(?P<m1>\([123?]\) )?(?P<m2>\([123?]\) )? *$')
14    __slots__ = ["name", "id", "mapping"]
15    def __init__(self, morbidmap_line):
16        string = morbidmap_line.split("|", 1)[0]
17        match = self.regex.match(string)
18#        print string
19#        print match.groups()
20        self.name, self.id, self.mapping = [s.strip() if s else s for s in match.groups()[:3]]
21        if match.group("m2"):
22            self.mapping += " " + match.group("m2").strip()
23       
24class OMIM(object):
25    VERSION = 1
26    DEFAULT_DATABASE_PATH = orngServerFiles.localpath("OMIM")
27    def __init__(self, local_database_path=None):
28        self.local_database_path = local_database_path if local_database_path is not None else self.DEFAULT_DATABASE_PATH
29       
30        if not os.path.exists(self.local_database_path):
31            os.makedirs(self.local_database_path)
32           
33        filename = os.path.join(self.local_database_path, "morbidmap")
34        if not os.path.exists(filename):
35            stream = urllib2.urlopen("ftp://ftp.ncbi.nih.gov/repository/OMIM/ARCHIVE/morbidmap")
36            with open(filename, "wb") as file:
37                shutil.copyfileobj(stream, file, length=10)
38           
39           
40        self.load(filename)
41   
42    @classmethod
43    def download_from_NCBI(cls, file=None):
44        data = urllib2.urlopen("ftp://ftp.ncbi.nih.gov/repository/OMIM/ARCHIVE/morbidmap").read()
45        if file is None:
46            if not os.path.exists(cls.DEFAULT_DATABASE_PATH):
47                os.mkdir(cls.DEFAULT_DATABASE_PATH)
48            file = open(os.path.join(cls.DEFAULT_DATABASE_PATH, "morbidmap"), "wb")
49        elif isinstance(file, basestring):
50            file = open(file, "wb")
51        file.write(data)
52        file.close()
53       
54    @classmethod
55    def get_instance(cls):
56        if not hasattr(cls, "_shared_dict"):
57            omim = OMIM()
58            cls._shared_dict = omim.__dict__
59        instance = OMIM.__new__(OMIM)
60        instance.__dict__ = cls._shared_dict
61        return instance
62   
63    def load(self, filename):
64        file = open(filename, "rb")
65        lines = file.read().splitlines()
66        self._disease_dict = dict([(disease(line), line) for line in lines if line])
67       
68    def diseases(self):
69        return self._disease_dict.keys()
70   
71    def genes(self):
72        return sorted(set(reduce(list.__add__, [self.disease_genes(disease) for disease in self.diseases()], [])))
73   
74    def disease_genes(self, disease):
75        return self._disease_dict[disease].split("|")[1].split(", ")
76   
77    def gene_diseases(self):
78        d = defaultdict(set)
79        for disease, genes in [(disease, self.disease_genes(disease)) for disease in self.diseases()]:
80            for gene in genes:
81                d[gene].add(disease)
82        return d
83   
84def diseases():
85    """ Return all disease objects
86    """
87    return OMIM.get_instance().diseases()
88       
89def genes():
90    """ Return a set of all genes referenced in OMIM
91    """
92    return OMIM.get_instance().genes()
93
94def disease_genes(disease):
95    """ Return a set of all genes referenced by disease in OMIM
96    """
97    return OMIM.get_instance().disease_genes(disease)
98
99def gene_diseases():
100    """ Return a dictionary {gene: set(disease_objects for gene), ...}
101    """
102    return OMIM.get_instance().gene_diseases()
103
104if __name__ == "__main__":
105    print genes()
Note: See TracBrowser for help on using the repository browser.