Changes in [1775:023037d1e29a:1779:736a0fc7fccf] in orange-bioinformatics


Ignore:
Location:
_bioinformatics
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • _bioinformatics/obiDictyMutants.py

    r1765 r1778  
    22import urllib2 
    33import shutil 
     4import cPickle as pickle 
    45 
    56from collections import defaultdict 
     
    1112    def __init__(self, mutant_line): 
    1213        mutant = mutant_line.split("\t") 
    13         self.id = mutant[0] 
     14        self.name = mutant[0] 
    1415        self.descriptor = set(mutant[1].split("/")) 
    1516        self.genes = mutant[2].split(" | ") 
    1617        self.phenotypes = mutant[3].split(" | ") 
    17  
    18  
     18        self.null = False 
     19        self.overexp = False 
     20        self.multiple = False 
     21        self.develop = False 
     22        self.other = False 
     23  
    1924class DictyMutants(object): 
    2025    VERSION=1 
     
    2328    def __init__(self, local_database_path=None): 
    2429        self.local_database_path = local_database_path if local_database_path is not None else self.DEFAULT_DATABASE_PATH 
     30        self.local_pickle_path = os.path.join(self.local_database_path, "Mutants.pkl") 
    2531 
    2632        if not os.path.exists(self.local_database_path): 
    27             self.download_from_dictybase(self.local_database_path) 
    28  
    29         filename = os.path.join(self.local_database_path, "DictyMutants") 
    30         self.load(filename) 
    31  
    32     @classmethod 
    33     def download_from_dictybase(cls, local_database_path=None): 
    34         cls.local_database_path = local_database_path if local_database_path is not None else cls.DEFAULT_DATABASE_PATH 
    35  
    36         if not os.path.exists(cls.local_database_path): 
    37             os.mkdir(cls.local_database_path) 
    38  
    39         filename = os.path.join(cls.local_database_path, "DictyMutants") 
    40         temp_file = os.path.join(cls.local_database_path, "DictyMutantsTemp") 
    41         stream = urllib2.urlopen("http://dictybase.org/db/cgi-bin/dictyBase/download/download.pl?area=mutant_phenotypes&ID=all-mutants.txt") 
    42  
     33            os.mkdir(self.local_database_path) 
     34             
     35        if not os.path.exists(self.local_pickle_path): 
     36            self.prepare_mutants() 
     37        else: 
     38            self._mutants = pickle.load(open(self.local_pickle_path, "rb")) 
     39  
     40    def update_file(self, name): 
     41        url = "http://dictybase.org/db/cgi-bin/dictyBase/download/download.pl?area=mutant_phenotypes&ID=" 
     42        filename = os.path.join(self.local_database_path, name) 
     43        temp_file = os.path.join(self.local_database_path, name + "_temp") 
     44        stream = urllib2.urlopen(url + name) 
     45     
    4346        with open(temp_file, "wb") as file: 
    4447            shutil.copyfileobj(stream, file) 
     48     
     49        os.rename(temp_file, filename) 
     50        return filename 
     51     
     52    def load_mutants(self, file): 
     53        data = open(file) 
     54        data_header = data.readline() 
     55        data = data.read() 
     56        return data.splitlines() 
     57        
     58    def prepare_mutants(self):    
     59        all_mutants = self.load_mutants(self.update_file("all-mutants.txt")) 
     60        null_mutants = self.load_mutants(self.update_file("null-mutants.txt")) 
     61        overexp_mutants = self.load_mutants(self.update_file("overexpression-mutants.txt")) 
     62        multiple_mutants = self.load_mutants(self.update_file("multiple-mutants.txt")) 
     63        develop_mutants = self.load_mutants(self.update_file("developmental-mutants.txt")) 
     64        other_mutants = self.load_mutants(self.update_file("other-mutants.txt")) 
     65    
     66        _mutants = [DictyMutant(mutant) for mutant in all_mutants] 
     67         
     68        the_nulls = set([DictyMutant(line).name for line in null_mutants]) 
     69        the_overexps = set([DictyMutant(line).name for line in overexp_mutants]) 
     70        the_multiples = set([DictyMutant(line).name for line in multiple_mutants]) 
     71        the_develops = set([DictyMutant(line).name for line in develop_mutants]) 
     72        the_others = set([DictyMutant(line).name for line in other_mutants]) 
    4573 
    46         if os.path.exists(filename): 
    47             current = open(filename, "rb").read() 
    48             temporary = open(temp_file, "rb").read() 
    49             current.close() 
    50             temporary.close() 
    51             if current == temporary: 
    52                 os.remove(temp_file) 
    53                 return False 
    54  
    55         os.rename(temp_file, filename) 
    56         return True 
     74        for mutant in _mutants: 
     75            if mutant.name in the_nulls: mutant.null = True 
     76            if mutant.name in the_overexps: mutant.overexp = True  
     77            if mutant.name in the_multiples: mutant.multiple = True 
     78            if mutant.name in the_develops: mutant.develop = True 
     79            if mutant.name in the_others: mutant.other = True 
     80        
     81        self._mutants = {x: x for x in _mutants} 
     82        pickle.dump(self._mutants, open(self.local_pickle_path, "wb"), -1) 
    5783 
    5884    @classmethod 
     
    6591        return instance 
    6692 
    67     def load(self, filename): 
    68         file = open(filename, "rb") 
    69         header = file.readline().rstrip() 
    70         lines = file.read().splitlines() 
    71         self._dicty_mutants = dict([(DictyMutant(line).id, DictyMutant(line)) for line in lines if line]) 
    72  
    7393    def mutants(self): 
    74         return self._dicty_mutants.values() 
     94        return self._mutants.keys() 
    7595 
    7696    def genes(self): 
    77         return sorted(set(reduce(list.__add__, [self.mutant_genes(mutant.id) for mutant.id in self.mutants()], []))) 
     97        return sorted(set(reduce(list.__add__, [self.mutant_genes(mutant) for mutant in self.mutants()], []))) 
    7898 
    7999    def mutant_genes(self, mutant): 
    80         return DictyMutant(self._dicty_mutants[mutant]).genes 
     100        return self._mutants[mutant].genes 
    81101 
    82102    def gene_mutants(self): 
     
    108128 
    109129if  __name__  == "__main__": 
    110     """ 
    111     Test whether the file contains only unique entries 
    112     """ 
    113     entries = [ entry.id for entry in mutants() ] 
    114     print len(set(entries)), len(entries) 
    115     #print(mutants()) 
    116     #print(genes()) 
     130    print(mutants())     
  • _bioinformatics/obiGeneSets.py

    r1761 r1778  
    7575    Return gene sets from OMIM (Online Mendelian Inheritance in Man) diseses 
    7676    """ 
    77     from . import obiOMIM    # The link here leads to a redirection... should replace it with the new URL 
     77    from . import obiOMIM     
    7878    genesets = [GeneSet(id=disease.id, name=disease.name, genes=obiOMIM.disease_genes(disease), hierarchy=("OMIM",), organism="9606", 
    79                     link=("http://www.ncbi.nlm.nih.gov/entrez/dispomim.cgi?id=" % disease.id if disease.id else None)) \ 
     79                    link=("http://www.omim.org/entry/%s" % disease.id if disease.id else None)) \ 
    8080                    for disease in obiOMIM.diseases()] 
    8181    return GeneSets(genesets) 
Note: See TracChangeset for help on using the changeset viewer.