Changes in [1771:bf833fa5d2a3:1777:ab15a6b6fbbe] in orange-bioinformatics


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • _bioinformatics/obiDictyMutants.py

    r1765 r1776  
    1111    def __init__(self, mutant_line): 
    1212        mutant = mutant_line.split("\t") 
    13         self.id = mutant[0] 
     13        self.name = mutant[0] 
    1414        self.descriptor = set(mutant[1].split("/")) 
    1515        self.genes = mutant[2].split(" | ") 
    1616        self.phenotypes = mutant[3].split(" | ") 
    17  
    18  
     17        self.null = False 
     18        self.overexp = False 
     19        self.multiple = False 
     20        self.develop = False 
     21        self.other = False 
     22  
    1923class DictyMutants(object): 
    2024    VERSION=1 
     
    2529 
    2630        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  
     31            os.mkdir(self.local_database_path) 
     32             
     33        _mutants = self.prepare_mutants() 
     34  
     35    def update_file(self, name): 
     36        url = "http://dictybase.org/db/cgi-bin/dictyBase/download/download.pl?area=mutant_phenotypes&ID=" 
     37        filename = os.path.join(self.local_database_path, name) 
     38        temp_file = os.path.join(self.local_database_path, name + "_temp") 
     39        stream = urllib2.urlopen(url + name) 
     40     
    4341        with open(temp_file, "wb") as file: 
    4442            shutil.copyfileobj(stream, file) 
     43     
     44        os.rename(temp_file, filename) 
     45        return filename 
     46     
     47    def load_mutants(self, file): 
     48        data = open(file) 
     49        data_header = data.readline() 
     50        data = data.read() 
     51        return data.splitlines() 
     52        
     53    def prepare_mutants(self):    
     54        all_mutants = self.load_mutants(self.update_file("all-mutants.txt")) 
     55        null_mutants = self.load_mutants(self.update_file("null-mutants.txt")) 
     56        overexp_mutants = self.load_mutants(self.update_file("overexpression-mutants.txt")) 
     57        multiple_mutants = self.load_mutants(self.update_file("multiple-mutants.txt")) 
     58        develop_mutants = self.load_mutants(self.update_file("developmental-mutants.txt")) 
     59        other_mutants = self.load_mutants(self.update_file("other-mutants.txt")) 
     60    
     61        _mutants = [DictyMutant(mutant) for mutant in all_mutants] 
     62         
     63        the_nulls = set([DictyMutant(line).name for line in null_mutants]) 
     64        the_overexps = set([DictyMutant(line).name for line in overexp_mutants]) 
     65        the_multiples = set([DictyMutant(line).name for line in multiple_mutants]) 
     66        the_develops = set([DictyMutant(line).name for line in develop_mutants]) 
     67        the_others = set([DictyMutant(line).name for line in other_mutants]) 
    4568 
    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 
     69        for mutant in _mutants: 
     70            if mutant.name in the_nulls: mutant.null = True 
     71            if mutant.name in the_overexps: mutant.overexp = True  
     72            if mutant.name in the_multiples: mutant.multiple = True 
     73            if mutant.name in the_develops: mutant.develop = True 
     74            if mutant.name in the_others: mutant.other = True 
     75        
     76        self._mutants = {x: x for x in _mutants} 
    5777 
    5878    @classmethod 
     
    6585        return instance 
    6686 
    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  
    7387    def mutants(self): 
    74         return self._dicty_mutants.values() 
     88        return self._mutants.keys() 
    7589 
    7690    def genes(self): 
    77         return sorted(set(reduce(list.__add__, [self.mutant_genes(mutant.id) for mutant.id in self.mutants()], []))) 
     91        return sorted(set(reduce(list.__add__, [self.mutant_genes(mutant) for mutant in self.mutants()], []))) 
    7892 
    7993    def mutant_genes(self, mutant): 
    80         return DictyMutant(self._dicty_mutants[mutant]).genes 
     94        return self._mutants[mutant].genes 
    8195 
    8296    def gene_mutants(self): 
     
    108122 
    109123if  __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()) 
     124    print(mutants())     
Note: See TracChangeset for help on using the changeset viewer.