Changeset 1765:b497b961c944 in orange-bioinformatics for _bioinformatics/obiDictyMutants.py


Ignore:
Timestamp:
04/26/13 11:49:51 (12 months ago)
Author:
Flashpoint <vid.flashpoint@…>
Branch:
default
Message:

Added obiDictyMutants.py

File:
1 edited

Legend:

Unmodified
Added
Removed
  • _bioinformatics/obiDictyMutants.py

    r1763 r1765  
    77from Orange.orng import orngServerFiles 
    88 
    9 class dicty_mutant(object): 
     9class DictyMutant(object): 
    1010 
    1111    def __init__(self, mutant_line): 
     
    1717 
    1818 
    19 class dicty_mutants(object): 
     19class DictyMutants(object): 
    2020    VERSION=1 
    2121    DEFAULT_DATABASE_PATH = orngServerFiles.localpath("DictyMutants") #use a default folder for storing the genesets 
     
    2525 
    2626        if not os.path.exists(self.local_database_path): 
    27             download_from_dictybase(self, self.local_database_path) 
     27            self.download_from_dictybase(self.local_database_path) 
    2828 
    29         filename = os.path.join(cls.local_database_path, "DictyMutants") 
     29        filename = os.path.join(self.local_database_path, "DictyMutants") 
    3030        self.load(filename) 
    3131 
     
    4141        stream = urllib2.urlopen("http://dictybase.org/db/cgi-bin/dictyBase/download/download.pl?area=mutant_phenotypes&ID=all-mutants.txt") 
    4242 
    43         if not os.path.exists(filename): 
    44             with open(filename, "wb") as file: 
    45                 shutil.copyfileobj(stream, file) 
    46         else: 
    47             toremove = False 
    48             with open(temp_file, "wb") as file: 
    49                 shutil.copyfileobj(stream, file) 
    50             current = open(filename, "rb") 
    51             temporary = open(temp_file, "rb") 
    52             if current.read() == temporary.read(): 
    53                 toremove = True 
     43        with open(temp_file, "wb") as file: 
     44            shutil.copyfileobj(stream, file) 
     45 
     46        if os.path.exists(filename): 
     47            current = open(filename, "rb").read() 
     48            temporary = open(temp_file, "rb").read() 
    5449            current.close() 
    5550            temporary.close() 
     51            if current == temporary: 
     52                os.remove(temp_file) 
     53                return False 
    5654 
    57             if toremove: 
    58                 os.remove(temp_file) 
    59             else: 
    60                 os.rename(temp_file, filename) 
     55        os.rename(temp_file, filename) 
     56        return True 
     57 
     58    @classmethod 
     59    def get_instance(cls): 
     60        if not hasattr(cls, "_shared_dict"): 
     61            dicty = DictyMutants() 
     62            cls._shared_dict = dicty.__dict__ 
     63        instance = DictyMutants.__new__(DictyMutants) 
     64        instance.__dict__ = cls._shared_dict 
     65        return instance 
    6166 
    6267    def load(self, filename): 
     
    6469        header = file.readline().rstrip() 
    6570        lines = file.read().splitlines() 
    66         self._dicty_mutants = dict([(dicty_mutant(line), line) for line in lines if line]) 
     71        self._dicty_mutants = dict([(DictyMutant(line).id, DictyMutant(line)) for line in lines if line]) 
    6772 
    6873    def mutants(self): 
    69         return self._dicty_mutants.keys() 
     74        return self._dicty_mutants.values() 
    7075 
    7176    def genes(self): 
    72         return sorted(set(reduce(list.__add__, [self.mutant_genes(mutant) for mutant in self.mutants()], []))) 
     77        return sorted(set(reduce(list.__add__, [self.mutant_genes(mutant.id) for mutant.id in self.mutants()], []))) 
    7378 
    7479    def mutant_genes(self, mutant): 
    75         return dicty_mutant(self._dicty_mutants[mutant]).genes 
     80        return DictyMutant(self._dicty_mutants[mutant]).genes 
    7681 
    7782    def gene_mutants(self): 
     
    8590    """ Return all mutant objects 
    8691    """ 
    87     return dicty_mutants.get_instance().mutants() 
     92    return DictyMutants.get_instance().mutants() 
    8893 
    8994def genes(): 
    9095    """ Return a set of all genes referenced in dictybase 
    9196    """ 
    92     return dicty_mutants.get_instance().genes() 
     97    return DictyMutants.get_instance().genes() 
    9398 
    9499def mutant_genes(mutant): 
    95100    """ Return a set of all genes referenced by a mutant in dictybase 
    96101    """ 
    97     return dicty_mutants.get_instance().mutant_genes(mutant) 
     102    return DictyMutants.get_instance().mutant_genes(mutant) 
    98103 
    99104def gene_mutants(): 
    100105    """ Return a dictionary {gene: set(mutant_objects for mutant), ...} 
    101106    """ 
    102     return dicty_mutants.get_instance().gene_mutants() 
     107    return DictyMutants.get_instance().gene_mutants() 
    103108 
    104  
     109if  __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()) 
Note: See TracChangeset for help on using the changeset viewer.