source: orange-bioinformatics/_bioinformatics/obiDictyMutants.py @ 1792:89d57e897273

Revision 1792:89d57e897273, 8.0 KB checked in by Flashpoint <vid.flashpoint@…>, 11 months ago (diff)

Added a documentation entry for obiDictyMutants.py

Line 
1"""
2==============================================================================
3DictyMutants - An interface to Dictyostelium discoideum mutants from Dictybase
4==============================================================================
5
6:mod:`DictyMutants` is a python module for accessing Dictyostelium discoideum
7mutant collections from the `Dictybase <http://www.dictybase.org/>`_ website.
8
9The mutants are presented as `DictyMutant` objects with their respective name,
10strain descriptor, associated genes and associated phenotypes.
11
12>>> from Orange.bio.obiDictyMutants import *
13>>> # Create a set of all mutant objects
14>>> dicty_mutants = mutants()
15>>> # List a set of all genes referenced by a single mutant
16>>> print mutant_genes(dicty_mutants[0])
17['cbfA']
18>>> # List a set of all phenotypes referenced by a single mutant
19>>> print mutant_phenotypes(dicty_mutants[0])
20['aberrant protein localization']
21>>> # List all genes or all phenotypes referenced on Dictybase
22>>> print genes()
23>>> print phenotypes()
24>>> # Display a dictionary {phenotypes: set(mutant_objects)}
25>>> print phenotype_mutants()
26>>> # Display a dictionary {genes: set(mutant_objects)}
27>>> print gene_mutants()
28"""
29
30import os
31import urllib2
32import shutil
33import pickle
34
35from collections import defaultdict
36
37from Orange.orng import orngServerFiles
38
39from Orange.utils.serverfiles import localpath_download
40
41domain = "dictybase"
42pickle_file = "mutants.pkl"
43tags = ["Dictyostelium discoideum", "mutant", "dictyBase", "phenotype"]
44
45class DictyMutant(object):
46    """
47    A class representing a single Dictyostelium discoideum mutant
48    from Dictybase
49   
50    :param mutant_entry: A single mutant entry from
51        dictybase's `all curated mutants file <http://dictybase.org/db/cgi-bin/dictyBase/download/download.pl?area=mutant_phenotypes&ID=all-mutants.txt>`_ (updated monthly)
52    :type mutant_entry: str
53
54    :ivar DictyMutant.name: dictyBase ID for a mutant
55    :ivar DictyMutant.descriptor: dictyBase strain descriptor of a mutant
56    :ivar DictyMutant.genes: all of the mutant's associated genes
57    :ivar DictyMutant.phenotypes: all of the mutant's associated phenotypes
58
59    """
60    def __init__(self, mutant_entry):
61        mutant = mutant_entry.split("\t")
62        self.name = mutant[0]
63        self.descriptor = mutant[1]
64        self.genes = mutant[2].split(" | ")
65        self.phenotypes = mutant[3].split(" | ")
66        self.null = False
67        self.overexp = False
68        self.multiple = False
69        self.develop = False
70        self.other = False
71 
72class DictyMutants(object):
73    """
74    A class representing the collection of all Dictybase mutants as
75    a dictionary of `DictyMutant` objects
76   
77    :param local_database_path: A user defined path for storing dicty mutants objects in a file. If `None` then a default database path is used.
78   
79    """
80   
81    VERSION=1
82    DEFAULT_DATABASE_PATH = orngServerFiles.localpath("DictyMutants") #use a default local folder for storing the genesets
83   
84    def __init__(self, local_database_path=None):
85        self.local_database_path = local_database_path if local_database_path is not None else self.DEFAULT_DATABASE_PATH
86       
87        if not os.path.exists(self.local_database_path):
88            os.mkdir(self.local_database_path)
89           
90        self._mutants = pickle.load(open(localpath_download(domain, pickle_file), "rb"))
91             
92    def update_file(self, name):
93        url = "http://dictybase.org/db/cgi-bin/dictyBase/download/download.pl?area=mutant_phenotypes&ID="
94        filename = os.path.join(self.local_database_path, name)
95        temp_file = os.path.join(self.local_database_path, name + "_temp")
96        stream = urllib2.urlopen(url + name)
97   
98        with open(temp_file, "wb") as file:
99            shutil.copyfileobj(stream, file)
100   
101        os.rename(temp_file, filename)
102        return filename
103   
104    def load_mutants(self, file):
105        data = open(file)
106        data_header = data.readline()
107        data = data.read()
108        return data.splitlines()
109                 
110    def download_mutants(self):   
111        all_mutants = self.load_mutants(self.update_file("all-mutants.txt"))
112        null_mutants = self.load_mutants(self.update_file("null-mutants.txt"))
113        overexp_mutants = self.load_mutants(self.update_file("overexpression-mutants.txt"))
114        multiple_mutants = self.load_mutants(self.update_file("multiple-mutants.txt"))
115        develop_mutants = self.load_mutants(self.update_file("developmental-mutants.txt"))
116        other_mutants = self.load_mutants(self.update_file("other-mutants.txt"))
117   
118        _mutants = [DictyMutant(mutant) for mutant in all_mutants]
119       
120        the_nulls = set([DictyMutant(line).name for line in null_mutants])
121        the_overexps = set([DictyMutant(line).name for line in overexp_mutants])
122        the_multiples = set([DictyMutant(line).name for line in multiple_mutants])
123        the_develops = set([DictyMutant(line).name for line in develop_mutants])
124        the_others = set([DictyMutant(line).name for line in other_mutants])
125
126        for mutant in _mutants:
127            if mutant.name in the_nulls: mutant.null = True
128            if mutant.name in the_overexps: mutant.overexp = True 
129            if mutant.name in the_multiples: mutant.multiple = True
130            if mutant.name in the_develops: mutant.develop = True
131            if mutant.name in the_others: mutant.other = True
132       
133        final_mutants = {x: x for x in _mutants}
134        return final_mutants
135
136    def pickle_data(self):
137        return pickle.dumps(self.download_mutants(), -1)
138
139    @classmethod
140    def get_instance(cls):
141        if not hasattr(cls, "_shared_dict"):
142            dicty = DictyMutants()
143            cls._shared_dict = dicty.__dict__
144        instance = DictyMutants.__new__(DictyMutants)
145        instance.__dict__ = cls._shared_dict
146        return instance
147
148    def mutants(self):
149        return self._mutants.keys()
150
151    def genes(self):
152        return sorted(set(reduce(list.__add__, [self.mutant_genes(mutant) for mutant in self.mutants()], [])))
153
154    def phenotypes(self):
155        return sorted(set(reduce(list.__add__, [self.mutant_phenotypes(mutant) for mutant in self.mutants()], [])))
156
157    def mutant_genes(self, mutant):
158        return self._mutants[mutant].genes
159   
160    def mutant_phenotypes(self, mutant):
161        return self._mutants[mutant].phenotypes
162
163    def gene_mutants(self):
164        dgm = defaultdict(set)
165        for mutant, genes in [(mutant, self.mutant_genes(mutant)) for mutant in self.mutants()]:
166            for gene in genes:
167                dgm[gene].add(mutant)
168        return dgm
169
170    def phenotype_mutants(self):
171        dpm = defaultdict(set)
172        for mutant, phenotypes in [(mutant, self.mutant_phenotypes(mutant)) for mutant in self.mutants()]:
173            for phenotype in phenotypes:
174                dpm[phenotype].add(mutant)
175        return dpm
176
177def mutants():
178    """ Return all mutant objects
179    """
180    return DictyMutants.get_instance().mutants()
181
182def genes():
183    """ Return a set of all genes referenced in dictybase
184    """
185    return DictyMutants.get_instance().genes()
186
187def phenotypes():
188    """ Return a set of all phenotypes referenced in dictybase
189    """
190    return DictyMutants.get_instance().phenotypes()
191
192def mutant_genes(mutant):
193    """ Return a set of all genes referenced by a mutant in dictybase
194    """
195    return DictyMutants.get_instance().mutant_genes(mutant)
196
197def mutant_phenotypes(mutant):   
198    """ Return a set of all phenotypes referenced by a mutant in dictybase
199    """
200    return DictyMutants.get_instance().mutant_phenotypes(mutant)
201
202def gene_mutants():
203    """ Return a dictionary {gene: set(mutant_objects for mutant), ...}
204    """
205    return DictyMutants.get_instance().gene_mutants()
206
207def phenotype_mutants():
208    """ Return a dictionary {phenotype: set(mutant_objects for mutant), ...}
209    """
210    return DictyMutants.get_instance().phenotype_mutants()
211
212def download_mutants():
213    return DictyMutants.get_instance().pickle_data()
214
215if  __name__  == "__main__":
216    dicty_mutants = mutants()
217    print mutant_phenotypes(dicty_mutants[0])
218#    print(phenotypes())#_mutants())   
Note: See TracBrowser for help on using the repository browser.