orange-bioinformatics / _bioinformatics /

Full commit
Flashpoint 12c8aae 
Flashpoint d8f3d4b 

Flashpoint 896de92 
Flashpoint d8f3d4b 

Flashpoint 896de92 
Flashpoint d8f3d4b 
Flashpoint 896de92 
Flashpoint d8f3d4b 
import os
import urllib2
import shutil

from collections import defaultdict

from Orange.orng import orngServerFiles

class dicty_mutant(object):

    def __init__(self, mutant_line):
        mutant = mutant_line.split("\t") = mutant[0]
        self.descriptor = set(mutant[1].split("/"))
        self.genes = mutant[2].split(" | ")
        self.phenotypes = mutant[3].split(" | ")

class dicty_mutants(object):
    DEFAULT_DATABASE_PATH = orngServerFiles.localpath("DictyMutants") #use a default folder for storing the genesets

    def __init__(self, local_database_path=None):
        self.local_database_path = local_database_path if local_database_path is not None else self.DEFAULT_DATABASE_PATH

        if not os.path.exists(self.local_database_path):
            download_from_dictybase(self, self.local_database_path)

        filename = os.path.join(cls.local_database_path, "DictyMutants")

    def download_from_dictybase(cls, local_database_path=None):
        cls.local_database_path = local_database_path if local_database_path is not None else cls.DEFAULT_DATABASE_PATH

        if not os.path.exists(cls.local_database_path):

        filename = os.path.join(cls.local_database_path, "DictyMutants")
        temp_file = os.path.join(cls.local_database_path, "DictyMutantsTemp")
        stream = urllib2.urlopen("")

        if not os.path.exists(filename):
            with open(filename, "wb") as file:
                shutil.copyfileobj(stream, file)
            toremove = False
            with open(temp_file, "wb") as file:
                shutil.copyfileobj(stream, file)
            current = open(filename, "rb")
            temporary = open(temp_file, "rb")
            if ==
                toremove = True

            if toremove:
                os.rename(temp_file, filename)

    def load(self, filename):
        file = open(filename, "rb")
        header = file.readline().rstrip()
        lines =
        self._dicty_mutants = dict([(dicty_mutant(line), line) for line in lines if line])

    def mutants(self):
        return self._dicty_mutants.keys()

    def genes(self):
        return sorted(set(reduce(list.__add__, [self.mutant_genes(mutant) for mutant in self.mutants()], [])))

    def mutant_genes(self, mutant):
        return dicty_mutant(self._dicty_mutants[mutant]).genes

    def gene_mutants(self):
        d = defaultdict(set)
        for mutant, genes in [(mutant, self.mutant_genes(mutant)) for mutant in self.mutants()]:
            for gene in genes:
        return d

def mutants():
    """ Return all mutant objects
    return dicty_mutants.get_instance().mutants()

def genes():
    """ Return a set of all genes referenced in dictybase
    return dicty_mutants.get_instance().genes()

def mutant_genes(mutant):
    """ Return a set of all genes referenced by a mutant in dictybase
    return dicty_mutants.get_instance().mutant_genes(mutant)

def gene_mutants():
    """ Return a dictionary {gene: set(mutant_objects for mutant), ...}
    return dicty_mutants.get_instance().gene_mutants()