Commits

Flashpoint  committed d8f3d4b

Added obiDictyMutants.py

  • Participants
  • Parent commits 28ae945

Comments (0)

Files changed (3)

File _bioinformatics/obiDictyMutants.py

+import sys, os
+import urllib2
+import shutil
+import re
+
+from collections import defaultdict
+
+from Orange.orng import orngServerFiles
+
+class dicty_mutant(object):
+
+    def __init__(self, mutant_line):
+        mutant = mutant_line.split("\t")
+        self.id = mutant[0]
+        self.descriptor = set(mutant[1].split("/"))
+        self.genes = mutant[2].split(" | ")
+        self.phenotypes = mutant[3].split(" | ")
+
+
+class dicty_mutants(object):
+    VERSION=1
+    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")
+        self.load(filename)
+
+    @classmethod
+    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):
+            os.mkdir(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("http://dictybase.org/db/cgi-bin/dictyBase/download/download.pl?area=mutant_phenotypes&ID=all-mutants.txt")
+
+        if not os.path.exists(filename):
+            with open(filename, "wb") as file:
+                shutil.copyfileobj(stream, file)
+        else:
+            toremove = False
+            with open(temp_file, "wb") as file:
+                shutil.copyfileobj(stream, file)
+            current = open(filename, "rb")
+            temporary = open(temp_file, "rb")
+            if current.read() == temporary.read():
+                toremove = True
+            current.close()
+            temporary.close()
+
+            if toremove:
+                os.remove(temp_file)
+            else:
+                os.rename(temp_file, filename)
+
+    def load(self, filename):
+        file = open(filename, "rb")
+        header = file.readline().rstrip()
+        lines = file.read().splitlines()
+        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:
+                d[gene].add(mutant)
+        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 OMIM.get_instance().mutant_genes(mutant)
+
+def gene_mutants():
+    """ Return a dictionary {gene: set(disease_objects for gene), ...}
+    """
+    return OMIM.get_instance().gene_diseases()
+
+

File _bioinformatics/obiGeneSets.py

         genes = annotations.GetAllGenes(termn)
         hier = ("GO", term.namespace)
         if len(genes) > 0:
-            gs = GeneSet(id=termn, name=term.name, genes=genes, hierarchy=hier, organism=org, link=link_fmt % termn) 
+            gs = GeneSet(id=termn, name=term.name, genes=genes, hierarchy=hier, organism=org, link=link_fmt % termn)
             genesets.append(gs)
 
     return GeneSets(genesets)
     """
     Returns gene sets from KEGG pathways.
     """
-    
+
     kegg = obiKEGG.KEGGOrganism(org)
 
     genesets = []
 
     return GeneSets(genesets)
 
+def dictyMutantSets():
+    """
+    Return dicty mutant phenotype gene sets from Dictybase
+    """
+    from . import obiDictyMutants
+    genesets = [GeneSet(id=dicty_mutant.id, name=dicty_mutant.descriptor, genes=obiDictyMutants.mutant_genes(mutant), hierarchy=("Dictybase", "mutant_phenotypes"), organism="352472", # 352742 gathered from obiGO.py code_map -> Dicty identifier
+                        link=("http://dictybase.org/db/cgi-bin/dictyBase/SC/scsearch.pl?searchdb=strains&search_term=%s&column=all&B1=Submit" % mutant.id if mutant.id else None)) \
+                        for mutant in obiDictyMutants.mutants()]
+    return GeneSets(genesets)
+
 def omimGeneSets():
     """
     Return gene sets from OMIM (Online Mendelian Inheritance in Man) diseses
     """
-    from . import obiOMIM
+    from . import obiOMIM    # The link here leads to a redirection... should replace it with the new URL
     genesets = [GeneSet(id=disease.id, name=disease.name, genes=obiOMIM.disease_genes(disease), hierarchy=("OMIM",), organism="9606",
                     link=("http://www.ncbi.nlm.nih.gov/entrez/dispomim.cgi?id=" % disease.id if disease.id else None)) \
                     for disease in obiOMIM.diseases()]
     mirnas = obimiRNA.ids(int(org))
     if ontology is None:
         ontology = obiGO.Ontology()
-         
+
     annotations = obiGO.Annotations(org, ontology=ontology)
-    
+
     go_sets = obimiRNA.get_GO(mirnas, annotations, enrichment=enrichment, pval=pval, goSwitch=False)
     print go_sets
-    
+
     go_sets = obimiRNA.filter_GO(go_sets, annotations, treshold=treshold)
-    
+
     link_fmt = "http://amigo.geneontology.org/cgi-bin/amigo/term-details.cgi?term=%s"
     gsets = [GeneSet(id=key, name=ontology[key].name, genes=value, hierarchy=("miRNA", "go_sets",),
                         organism=org, link=link_fmt % key) for key, value in go_sets.items()]
     gset = GeneSets(gsets)
     return gset
 
-
 def loadGMT(contents, name):
     """
     Eech line consists of tab separated elements. First is
     the geneset name, next is it's description.
-    
+
     For now the description is skipped.
     """
 
     pth = local_path()
     gs_files = filter(is_genesets_file, os.listdir(pth))
     return [ filename_parse(fn) + (True,) for fn in gs_files ]
-    
+
 def list_serverfiles_from_flist(flist):
     gs_files = filter(is_genesets_file, flist)
     localfiles = set(orngServerFiles.listfiles(sfdomain))
     flist = map(lambda x: filename(*x[:2]), list_serverfiles_conn(serverfiles_list))
 
     tfname = pickle_temp(flist)
-    
+
     try:
         fn = "index.pck"
         title = "Gene sets: index"
 
     #save to temporary file
     tfname = pickle_temp(genesets)
-    
+
     try:
         taxname = obiTaxonomy.name(org)
         title = "Gene sets: " + ", ".join(hierarchy) + \
     hierd = build_hierarchy_dict(files)
     out = GeneSets()
     for (h, o) in [ files[i] for i in hierd[(hierarchy, organism)]]:
-        fname = orngServerFiles.localpath_download(sfdomain, 
+        fname = orngServerFiles.localpath_download(sfdomain,
             filename(h, o))
         out.update(pickle.load(open(fname, 'r')))
     return out
 
 def upload_genesets(rsf):
     """
-    Builds the default gene sets and 
+    Builds the default gene sets and
     """
     orngServerFiles.update_local_files()
 
     rsf = orngServerFiles.ServerFiles(username=sys.argv[1], password=sys.argv[2])
     upload_genesets(rsf)
     pass
+
+

File _bioinformatics/widgets/OWSetEnrichment.py

             painter.setFont(font)
             painter.drawText(self.viewport().geometry(), Qt.AlignCenter, self._userMessage)
             painter.end()
-            
+
 class MyTreeWidgetItem(QTreeWidgetItem):
     def __lt__(self, other):
         if not self.treeWidget():
         lhs = _toPyObject(self.data(column, Qt.DisplayRole))
         rhs = _toPyObject(other.data(column, Qt.DisplayRole))
         return lhs < rhs
-    
+
 def name_or_none(id):
     """Return organism name for ncbi taxid or None if not found.
     """
         return obiTaxonomy.name(id)
     except obiTaxonomy.UnknownSpeciesIdentifier:
         return None
-            
+
 class OWSetEnrichment(OWWidget):
     settingsList = ["speciesIndex", "genesinrows", "geneattr", "categoriesCheckState"]
     contextHandlers = {"":DomainContextHandler("", ["speciesIndex", "genesinrows", "geneattr", "categoriesCheckState"])}
-    
+
     def __init__(self, parent=None, signalManager=None, name="Gene Set Enrichment Analysis", **kwargs):
         OWWidget.__init__(self, parent, signalManager, name, **kwargs)
         self.inputs = [("Example Table", ExampleTable, self.setData, Default), ("Reference", ExampleTable, self.setReference)]
         self.outputs = [("Selected Examples", ExampleTable)]
-        
+
         self.speciesIndex = 0
         self.genesinrows = False
         self.geneattr = 0
         self.useMaxPValFilter = True
         self.minClusterCount = 0
         self.maxPValue = 0.01
-        
+
         self.useFDR = True
-        
+
         self.categoriesCheckState = {}
-        
+
         self.loadSettings()
-        
+
         if self.signalManager:
             self.signalManager.freeze(self).push()
         QTimer.singleShot(50, self.updateHierarchy)
-        
+
         box = OWGUI.widgetBox(self.controlArea, "Info")
         self.infoBox = OWGUI.widgetLabel(box, "Info")
         self.infoBox.setText("No data on input")
-        
+
         self.speciesComboBox = OWGUI.comboBox(self.controlArea, self,
                       "speciesIndex", "Species",
                       callback=lambda :self.data and self.updateAnnotations(),
                       debuggingEnabled=0)
-        
+
         box = OWGUI.widgetBox(self.controlArea, "Gene names")
         self.geneAttrComboBox = OWGUI.comboBox(box, self, "geneattr",
                                 "Gene attribute",
                                 sendSelectedValue=0,
                                 callback=self.updateAnnotations)
-        
+
         cb = OWGUI.checkBox(box, self, "genesinrows", "Use attribute names",
                             callback=lambda :self.data and self.updateAnnotations(),
                             disables=[(-1, self.geneAttrComboBox)])
         cb.makeConsistent()
-        
+
         OWGUI.button(box, self, "Gene matcher settings",
                      callback=self.updateGeneMatcherSettings,
                      tooltip="Open gene matching settings dialog",
                      debuggingEnabled=0)
-        
+
         self.referenceRadioBox = OWGUI.radioButtonsInBox(self.controlArea,
                     self, "useReferenceData", ["Entire genome", "Reference set (input)"],
                     tooltips=["Use entire genome for reference",
                               "Use genes from Referece Examples input signal as reference"],
                     box="Reference", callback=self.updateAnnotations)
-        
-        box = OWGUI.widgetBox(self.controlArea, "Annotation Summary")
+
+        box = OWGUI.widgetBox(self.controlArea, "Gene Sets")
         self.groupsWidget = QTreeWidget(self)
         self.groupsWidget.setHeaderLabels(["Category"])
         box.layout().addWidget(self.groupsWidget)
         hLayout.setSpacing(10)
         hWidget = OWGUI.widgetBox(self.mainArea, orientation=hLayout)
         sb, sbcb = OWGUI.spin(hWidget, self, "minClusterCount",
-                              0, 100, label="Min. Count",
+                              0, 100, label="Genes",
                               tooltip="Minimum gene count",
                               callback=self.filterAnnotationsChartView,
                               callbackOnReturn=True,
                               checked="useMinCountFilter",
                               checkCallback=self.filterAnnotationsChartView)
-        
+
         dsp, dspcb = OWGUI.doubleSpin(hWidget, self,
                         "maxPValue", 0.0, 1.0, 0.0001,
-                        label="Max. P-Value",
+                        label="P-Value (FDR corrected)",
                         tooltip="Maximum (FDR corrected) P-Value",
                         callback=self.filterAnnotationsChartView,
                         callbackOnReturn=True,
                         checked="useMaxPValFilter",
                         checkCallback=self.filterAnnotationsChartView)
-        
+
         from Orange.OrangeWidgets import OWGUIEx
         self.filterLineEdit = OWGUIEx.QLineEditWithActions(self)
         self.filterLineEdit.setPlaceholderText("Filter ...")
         action = QAction(QIcon(os.path.join(orngEnviron.canvasDir,
                         "icons", "delete_gray.png")), "Clear", self)
-        
+
         self.filterLineEdit.addAction(action, 0, Qt.AlignHCenter)
         self.connect(action, SIGNAL("triggered()"), self.filterLineEdit.clear)
-        
+
         self.filterCompleter = QCompleter(self.filterLineEdit)
         self.filterCompleter.setCaseSensitivity(Qt.CaseInsensitive)
         self.filterLineEdit.setCompleter(self.filterCompleter)
-        
+
         hLayout.addWidget(self.filterLineEdit)
         self.mainArea.layout().addWidget(hWidget)
-        
+
         self.connect(self.filterLineEdit, SIGNAL("textChanged(QString)"),
                      self.filterAnnotationsChartView)
-        
+
         self.annotationsChartView = MyTreeWidget(self)
         self.annotationsChartView.setHeaderLabels(["Category", "Term",
                             "Count", "Reference count", "P-Value", "Enrichment"])
         self.annotationsChartView.viewport().setMouseTracking(True)
 #        self.annotationsChartView.viewport().setAttribute(Qt.WA_Hover)
         self.mainArea.layout().addWidget(self.annotationsChartView)
-        
+
         contextEventFilter = OWGUI.VisibleHeaderSectionContextEventFilter(self.annotationsChartView)
         self.annotationsChartView.header().installEventFilter(contextEventFilter)
-        
+
         self.taxid_list = []
-        
+
         self.connect(self.groupsWidget, SIGNAL("itemClicked(QTreeWidgetItem *, int)"), self.subsetSelectionChanged)
-        
+
         OWGUI.button(self.controlArea, self, "Commit", callback=self.commit)
-        
+
         self.loadedGenematcher = "None"
         self.referenceData = None
         self.data = None
-        
+
         self.treeItems = []
-        
+
         self.resize(1024, 600)
-        
+
         self.connect(self, SIGNAL("widgetStateChanged(QString, int, QString)"), self.onStateChange)
-        
+
         self.updatingAnnotationsFlag = False
-        
+
     def updateHierarchy(self):
         try:
             self.progressBarInit()
                 all, local = obiGeneSets.list_all(), obiGeneSets.list_local()
                 organisms = set(obiTaxonomy.essential_taxids() + [t[1] for t in all])
             self.progressBarFinished()
-            
+
             organism_names = map(name_or_none, organisms)
             organisms = [taxid for taxid, name in zip(organisms, organism_names) \
                          if name is not None]
-            
+
             self.taxid_list = list(organisms)
             self.speciesComboBox.clear()
             self.speciesComboBox.addItems([obiTaxonomy.name(id) for id in self.taxid_list])
             self.genesets = all
         finally:
             if self.signalManager:
-                self.signalManager.freeze(self).pop()
-        
+                self.signalManager.freeze(self).pop() #setFreeze(self.signalManager.freezing - 1)
+
     def setData(self, data=None):
         self.data = data
         self.error(0)
         self.geneAttrComboBox.clear()
         self.groupsWidget.clear()
         self.annotationsChartView.clear()
-        
+
         if not getattr(self,"taxid_list", None):
             QTimer.singleShot(100, lambda data=data: self.setData(data))
-            return 
+            return
         if data:
             self.geneAttrs = [attr for attr in data.domain.variables + data.domain.getmetas().values() \
                               if attr.varType != orange.VarTypes.Continuous]
-            
+
             self.geneAttrComboBox.addItems([attr.name for attr in self.geneAttrs])
             self.geneattr = min(self.geneattr, len(self.geneAttrs) - 1)
-             
+
             taxid = data_hints.get_hint(data, "taxid", "")
             try:
                 self.speciesIndex = self.taxid_list.index(taxid)
             except ValueError, ex:
                 pass
             self.genesinrows = data_hints.get_hint(data, "genesinrows", self.genesinrows)
-            
+
             self.openContext("", data)
-            
+
 #            print self.speciesIndex
-            
+
             self.setHierarchy(self.getHierarchy(taxid=self.taxid_list[self.speciesIndex]))
-            
+
             self.loadedGenematcher = "None"
             self.updateAnnotations()
-            
+
     def setReference(self, data=None):
         self.referenceData = data
         self.referenceRadioBox.setEnabled(bool(data))
-        
+
     def getHierarchy(self, taxid):
         def recursive_dict():
             return defaultdict(recursive_dict)
         collection = recursive_dict()
-        
+
         def collect(col, hier):
             if hier:
                 collect(col[hier[0]], hier[1:])
-                
+
         for hierarchy, t_id, _ in self.genesets:
             collect(collection[t_id], hierarchy)
         return collection[taxid]
-        
+
     def setHierarchy(self, hierarchy):
         self.groupsWidgetItems = {}
         def fill(col, parent, full=()):
                 item.setFlags(item.flags() | Qt.ItemIsUserCheckable | Qt.ItemIsSelectable | Qt.ItemIsEnabled)
                 if value:
                     item.setFlags(item.flags() | Qt.ItemIsTristate)
-                    
+
                 item.setData(0, Qt.CheckStateRole, QVariant(self.categoriesCheckState.get(full_cat, Qt.Checked)))
                 item.setExpanded(True)
                 item.category = full_cat
                 self.groupsWidgetItems[full_cat] = item
                 fill(value, item, full_cat)
-                
+
         fill(hierarchy, self.groupsWidget)
-        
+
 #    def updateCategoryCounts(self):
 #        for cat, item in self.groupWidgetItem:
 #            item.setData(1, QVariant(), Qt.DisplayRole)
-        
+
     def selectedCategories(self):
         taxid = self.taxid_list[self.speciesIndex]
         return [(key, taxid) for key, check in self.getHierarchyCheckState().items() if check == Qt.Checked]
             for i in range(item.childCount()):
                 result.extend(collect(item.child(i), full_cat))
             return result
-            
+
         items = [self.groupsWidget.topLevelItem(i) for i in range(self.groupsWidget.topLevelItemCount())]
         states = reduce(list.__add__, [collect(item) for item in items], [])
         return dict(states)
-            
+
     def subsetSelectionChanged(self, item, column):
         self.categoriesCheckState = self.getHierarchyCheckState()
-        
+
         categories = self.selectedCategories()
         if not set(categories) <= set(self.currentAnnotatedCategories):
             self.updateAnnotations()
         else:
             self.filterAnnotationsChartView()
-        
+
     def updateGeneMatcherSettings(self):
         from .OWGOEnrichmentAnalysis import GeneMatcherDialog
         dialog = GeneMatcherDialog(self, defaults=self.geneMatcherSettings, enabled=[True] * 4, modal=True)
             self.loadedGenematcher = "None"
             if self.data:
                 self.updateAnnotations()
-                
+
     def updateGenematcher(self):
         taxid = self.taxid_list[self.speciesIndex]
         if taxid != self.loadedGenematcher:
 #                self.genematcher.set_targets(self.referenceGenes())
                 self.loadedGenematcher = taxid
             self.progressBarFinished()
-            
+
     def genesFromExampleTable(self, table):
         if self.genesinrows:
             genes = [attr.name for attr in table.domain.attributes]
             geneattr = self.geneAttrs[self.geneattr]
             genes = [str(ex[geneattr]) for ex in table]
         return genes
-    
+
     def clusterGenes(self):
         return self.genesFromExampleTable(self.data)
-    
+
     def referenceGenes(self):
         if self.referenceData and self.useReferenceData:
             return self.genesFromExampleTable(self.referenceData)
             with orngServerFiles.DownloadProgress.setredirect(call.emitProgressChanged):
                 call.__call__()
                 return call.get_result()
-    
+
     def _cached_name_lookup(self, func, cache):
         def f(name, cache=cache):
             if name not in cache:
                 cache[name] = func(name)
             return cache[name]
         return f
-    
+
     def mapGeneNames(self, names, cache=None, passUnknown=False):
         if cache is not None:
             umatch = self._cached_name_lookup(self.genematcher.umatch, cache)
             return [umatch(name) or name for name in names]
 #            return [(mapped_name or name, mapped_name is not None) for mapped_name, name in zip(mapped, names)]
         return [n for n in [umatch(name) for name in names] if n is not None]
-    
+
     def enrichment(self, geneset, cluster, reference, pval=obiProb.Hypergeometric(), cache=None):
         genes = set(self.mapGeneNames(geneset.genes, cache, passUnknown=False))
-        
+
         cmapped = genes.intersection(cluster)
         rmapped = genes.intersection(reference)
         return (cmapped, rmapped, pval.p_value(len(cmapped), len(reference), len(rmapped), len(cluster)), float(len(cmapped)) / (len(cluster) or 1) / (float(len(rmapped) or 1) / (len(reference) or 1))) # TODO: compute all statistics here
-    
+
     def updateAnnotations(self):
         self.updatingAnnotationsFlag = True
         self.annotationsChartView.clear()
         if not self.genesinrows and len(self.geneAttrs) == 0:
             self.error(0, "Input data contains no attributes with gene names")
             return
-        
+
         self.progressBarInit()
         self.updateGenematcher()
         self.currentAnnotatedCategories = categories = self.selectedCategories()
-        
+
         ## Load collections in a worker thread
         call = self.asyncCall(obiGeneSets.collections, categories, name="Loading collections", blocking=True, thread=self.thread())
         call.connect(call, SIGNAL("progressChanged(float)"), self.progressBarSet)
         with orngServerFiles.DownloadProgress.setredirect(call.emitProgressChanged):
             call.__call__()
             collections = list(call.get_result())
-            
+
 #        with orngServerFiles.DownloadProgress.setredirect(self.progressBarSet):
-#            collections = list(obiGeneSets.collections(*categories)) 
+#            collections = list(obiGeneSets.collections(*categories))
         clusterGenes, referenceGenes = self.clusterGenes(), self.referenceGenes()
         cache = {}
 
         self.genematcher.set_targets(referenceGenes)
-        
+
         countAll = len(set(clusterGenes))
         infoText = "%i unique gene names on input\n" % countAll
         referenceGenes = set(self.mapGeneNames(referenceGenes, cache, passUnknown=False))
         self.progressBarSet(2)
         infoText += "%i (%.1f) gene names matched" % (len(clusterGenes), 100.0 * len(clusterGenes) / countAll)
         self.infoBox.setText(infoText)
-        
+
         results = []
         from Orange.orng.orngMisc import progressBarMilestones
-        
+
         milestones = progressBarMilestones(len(collections), 100)
         for i, geneset in enumerate(collections):
             results.append((geneset, self.enrichment(geneset, clusterGenes, referenceGenes, cache=cache)))
             if i in milestones:
                 self.progressBarSet(100.0 * i / len(collections))
-                
+
         if self.useFDR:
             results = sorted(results, key=lambda a:a[1][2])
             pvals = obiProb.FDR([pval for _, (_, _, pval, _) in results])
             results = [(geneset, (cmapped, rmapped, pvals[i], es)) for i, (geneset, (cmapped, rmapped, _, es)) in enumerate(results)]
-        
+
         fmt = lambda score, max_decimals=10: "%%.%if" % min(int(abs(math.log(max(score, 1e-10)))) + 2, max_decimals) if score > math.pow(10, -max_decimals) and score < 1 else "%.1f"
         self.annotationsChartView.clear()
-        
+
         maxCount = max([len(cm) for _, (cm, _, _, _) in results] + [1])
         maxRefCount = max([len(rc) for _, (_, rc, _, _) in results] + [1])
         countSpaces = int(math.ceil(math.log10(maxCount)))
+        #print maxRefCount
         refSpaces = int(math.ceil(math.log(maxRefCount)))
         countFmt = "%"+str(countSpaces) + "s  (%.2f%%)"
         refFmt = "%"+str(refSpaces) + "s  (%.2f%%)"
-            
+
         self.filterCompleter.setModel(None)
         linkFont = QFont(self.annotationsChartView.viewOptions().font)
         linkFont.setUnderline(True)
                 item.setData(2, Qt.DisplayRole, QVariant(countFmt % (len(cmapped), 100.0*len(cmapped)/countAll)))
                 item.setData(2, Qt.ToolTipRole, QVariant(len(cmapped))) # For filtering
                 item.setData(3, Qt.DisplayRole, QVariant(refFmt % (len(rmapped), 100.0*len(rmapped)/len(referenceGenes))))
-                item.setData(4, Qt.DisplayRole, QVariant(p_val))
+                item.setData(4, Qt.DisplayRole, QVariant("%0.2e"% p_val))
+                item.setData(4, 42, QVariant(p_val))
+                #stoplec 4 - zelim sort po p_val
+                item.setData(4, Qt.ToolTipRole, QVariant("%0.10f" % p_val))
                 item.setData(5, Qt.DisplayRole, QVariant(enrichment))
                 item.setData(5, Qt.ToolTipRole, QVariant("%.3f" % enrichment))
                 item.geneset= geneset
                     item.setToolTip(1, geneset.link)
                     item.setFont(1, linkFont)
                     item.setForeground(1, QColor(Qt.blue))
-                    
+
         if not self.treeItems:
             self.warning(0, "No enriched sets found.")
         else:
             self.warning(0)
-                
+
         replace = lambda s:s.replace(",", " ").replace("(", " ").replace(")", " ")
         self._completerModel = completerModel = QStringListModel(sorted(reduce(set.union, [[geneset.name] + replace(geneset.name).split() for geneset, (c, _, _, _) in results if c], set())))
         self.filterCompleter.setModel(completerModel)
-        
+
         self.annotationsChartView.setItemDelegateForColumn(5, BarItemDelegate(self, scale=(0.0, max(t[1][3] for t in results))))
         self.annotationsChartView.setItemDelegateForColumn(1, LinkStyledItemDelegate(self.annotationsChartView))
-                
+
         for i in range(self.annotationsChartView.columnCount()):
             self.annotationsChartView.resizeColumnToContents(i)
-            
+
         self.annotationsChartView.setColumnWidth(1, min(self.annotationsChartView.columnWidth(1), 300))
         self.progressBarFinished()
         QTimer.singleShot(10, self.filterAnnotationsChartView)
         self.updatingAnnotationsFlag = False
-    
+
     def filterAnnotationsChartView(self, filterString=""):
         if self.updatingAnnotationsFlag:
             return
         itemsHidden = []
         for item in self.treeItems:
             item_cat = str(item.data(0, Qt.EditRole).toString())
-            count, pval = _toPyObject(item.data(2, Qt.ToolTipRole)), _toPyObject(item.data(4, Qt.DisplayRole))
+            count, pval = _toPyObject(item.data(2, Qt.ToolTipRole)), _toPyObject(item.data(4, 42))
             geneset = item.geneset.name.lower()
             hidden = item_cat not in categories or (self.useMinCountFilter and count < self.minClusterCount) or \
                      (self.useMaxPValFilter and pval > self.maxPValue) or filterString not in geneset
             item.setHidden(hidden)
             itemsHidden.append(hidden)
-            
+
         if self.treeItems and all(itemsHidden):
             self.information(0, "All sets were filtered out.")
         else:
             self.information(0)
-            
-        
+
+
     def commit(self):
         selected = self.annotationsChartView.selectedItems()
         genesets = [item.geneset for item in selected]
             data = orange.ExampleTable(newdomain, self.data)
         else:
             geneattr = self.geneAttrs[self.geneattr]
-            selected = [1 if self.genematcher.umatch(str(ex[geneattr])) in mappedNames else 0 for ex in self.data]                
+            selected = [1 if self.genematcher.umatch(str(ex[geneattr])) in mappedNames else 0 for ex in self.data]
             data = self.data.select(selected)
-            
+
 #            if self.appendAnnotations:
 #                meta = orange.StringVariable("Annotations")
 #                data.domain.addmeta(orange.newmetaid(), meta)
 #                    geneattr = self.geneAttrs[self.geneattr]
 #                    gene = str(ex[geneattr])
 #                    annotations = getgene
-        
+
         self.send("Selected Examples", data)
-        
+
     def sendReport(self):
         self.reportSettings("Settings", [("Organism", obiTaxonomy.name(self.taxid_list[self.speciesIndex]))])
         self.reportSettings("Filter", [("Min cluster size", self.minClusterCount if self.useMinCountFilter else 0),
                                        ("Max p-value", self.maxPValue if self.useMaxPValFilter else 1.0)])
-    
+
         self.reportSubsection("Annotations")
         self.reportRaw(reportItemView(self.annotationsChartView))
-        
+
     def onStateChange(self, stateType, id, text):
         if stateType == "Warning" or stateType == "Info":
             self.annotationsChartView._userMessage = text
             self.annotationsChartView.viewport().update()
-        
-        
+
+
 def reportItemView(view):
     model = view.model()
     return reportItemModel(view, model)
-    
+
 def reportItemModel(view, model, index=QModelIndex()):
     if not index.isValid() or model.hasChildren(index):
         columnCount, rowCount = model.columnCount(index), model.rowCount(index)
         variant = model.data(index, Qt.DisplayRole)
         return str(variant.toString()) if variant.isValid() else ""
 
-    
+
+
 if __name__ == "__main__":
     import cProfile
-    
+
     app = QApplication(sys.argv)
     w = OWSetEnrichment()
     w.updateHierarchy()
-    data = orange.ExampleTable("brown-selected")
+    data = orange.ExampleTable("yeast-class-RPR.tab")
 #    data = orange.ExampleTable("../human")
 #    print cProfile.runctx("w.setData(data)", globals(), locals())
     w.setData(data)
     w.show()
     app.exec_()
     w.saveSettings()
-    
+
+