mitar avatar mitar committed 61fbdcb Merge

Merge.

Comments (0)

Files changed (6)

_bioinformatics/obiDicty.py

 
         return et
 
+def bufferkeypipax(command, data):
+    """ Do not save password to the buffer! """
+    command = command + " v7" #add version
+    if data != None:
+        data = data.copy()
+        if pipaparpass in data:
+            data.pop(pipaparpass)
+        return command + " " +  urllib.urlencode(sorted(data.items()))
+    else:
+        return command
+
+
+class PIPAx(PIPA):
+    """`PIPAx <http://pipa.biolab.si/?page_id=23>` api interface.
+    """
+
+    API_ADDRESS = "https://pipa.biolab.si/pipax/api.py"
+
+    def __init__(self, address=API_ADDRESS, buffer=None,
+                 username=None, password=None):
+        self.address = address
+        self.db = DBInterface(address)
+        self.buffer = buffer
+        self.username = username
+        self.password = password
+
+    def genomes(self, reload=False, bufver="0"):
+        """Return a list of available genomes as a list of
+        (genome_id, genome_name) tuples.
+
+        >>> pipax.genomes()
+        [('dd', 'Dictyostelium discoideum'), ('dp', 'Dictyostelium purpureum')]
+
+        """
+        data = {"action": "genomes"}
+        res, _ = self.sq("", data=data, reload=reload, bufver=bufver)
+        return [tuple(r) for r in res]
+
+    def mappings(self, reload=False, bufver="0"):
+        """Return a dictionary of {unique_id: dictionary_of_annotations}
+        where the keys for dictionary_of_annotations are
+        ["id", data_id", "data_name", "genomes_id"]
+
+        >>> mappings = pipax.mappings()
+
+        """
+        data = self.add_auth({"action": "mappings"})
+        res, legend = self.sq("", data=data, reload=reload,
+                              bufferkey=bufferkeypipax,
+                              bufver=bufver)
+
+        return dict((sa[0], dict(zip(legend[1:], sa[1:]))) for sa in res)
+
+    def result_types(self, reload=False, bufver="0"):
+        """Return a list of available result type templates.
+        """
+        data = {"action": "results_templates"}
+        res, _ = self.sq("", data=data, reload=reload, bufver=bufver)
+        return sorted(tuple(a) for a in res)
+
+    def results_list(self, rtype, reload=False, bufver="0"):
+        """Return a list of available results for a result type template
+        `rtype` (a key from the `result_types` return value).
+
+        """
+        data = {"action": "results",
+                "results_templates_id": rtype}
+        data = self.add_auth(data)
+        res, legend = self.sq("", data=data, reload=reload,
+                              bufferkey=bufferkeypipax,
+                              bufver=bufver)
+        # index by unique_id (last column)
+        res = splitTableOnColumn(res, -1)
+        return dict((id, dict(zip(legend, line[0]))) \
+                    for id, line in res.items())
+
+    def download_key_function(self):
+        data = self.add_auth({"action": "download",
+                              "ids": "$MULTI$"}
+                                )
+        keynamingfn, _ = self.downloadMulti_bufcommand_replace_multi("",
+                         data=data, chunk=100, bufferkey=bufferkeypipax,
+                         transformfn=None)
+        return keynamingfn
+
+    def get_data(self, ids=None, result_type=None,
+                 exclude_constant_labels=False, average=median,
+                 callback=None, bufver="0", transform=None,
+                 allowed_labels=None):
+        """
+        Get data in a single example table with labels of individual
+        attributes set to annotations for query and post-processing
+        instructions.
+
+        :param ids: If `result_type` is specified then this must
+            be a list of  `unique_id`s or (id, data_id) tuples as
+            returned by `mappings`. Else the `ids` must be a list
+            of `unique_id`s as returned by `results_list`.
+        :type ids: list
+
+        :param result_type: Result template type id as returned by
+             `result_types`. Not specified by default (see `ids`.)
+        :type result_type: str
+
+        :param exclude_constant_labels: If a label has the same value
+            in whole example table, remove it.
+        :type exclude_constant_labels: bool
+
+        :param average: Function used for combining multiple reading of
+            the same spot on a chip. If None, no averaging is done.
+            Function should take a list of floats and return an "averaged"
+            float (default `median`).
+        :type average: function
+
+        """
+
+        def optcb():
+            if callback:
+                callback()
+
+        def argsort(a):
+            sort = sorted([(map(int, item), i) for i, item in enumerate(a)])
+            return [i for _, i in sort]
+
+        cbc = CallBack(len(ids), optcb, callbacks=10)
+
+        if result_type is not None:
+            ids_sort = argsort(ids)
+            res_list = self.results_list(result_type, reload=reload,
+                                         bufver=bufver)
+            # Map (data_id, mapping_id) to unique_id
+            res_types_to_unique_id = \
+                dict(((annot["data_id"], annot["mappings_id"]),
+                      annot["unique_id"]) \
+                     for annot in res_list.values())
+
+            mappings = self.mappings(reload=reload, bufver=bufver)
+
+            def id_map(mappings_unique_id):
+                cbc()
+                if isinstance(mappings_unique_id, tuple):
+                    data_id, mappings_id = mappings_unique_id
+                else:
+                    annot = mappings[mappings_unique_id]
+                    data_id = annot["data_id"]
+                    mappings_id = annot["id"]
+                return res_types_to_unique_id[data_id, mappings_id]
+
+            ids = map(id_map, ids)
+        else:
+            result_type_set = set(id.rsplit("_", 1)[-1] for id in ids)
+            if len(result_type_set) != 1:
+                raise ValueError("""\
+Can only retrieve a single result_template_type at a time"""
+)
+            result_type = result_type_set.pop()
+            res_list = self.results_list(result_type, reload=reload,
+                                         bufver=bufver)
+            sort_keys = [(int(res_list[id]["data_id"]),
+                          int(res_list[id]["mappings_id"]))\
+                         for id in ids]
+            ids_sort = argsort(sort_keys)
+
+        # Sort the ids for use by pipax api
+        ids_sorted = [ids[i] for i in ids_sort]
+
+        read = {}
+        for a, b in res_list.items():
+            read[a] = b.items()
+
+        cbc.end()
+
+        download_func = lambda x: self.download(x, reload=reload,
+                                                bufver=bufver)
+
+        cbc = CallBack(len(ids_sorted) + 3, optcb,
+                       callbacks=99 - 20)
+        et = self.exampleTables(ids_sorted, spotmap={}, callback=cbc,
+                            annots=read,
+                            exclude_constant_labels=exclude_constant_labels,
+                            chipfn=download_func,
+                            allowed_labels=allowed_labels)
+        cbc.end()
+
+        # Restore input ids order.
+        domain = orange.Domain([et.domain[id] for id in ids], None)
+        domain.addmetas(et.domain.getmetas())
+        et = orange.ExampleTable(domain, et)
+
+        cbc = CallBack(2, optcb, callbacks=10)
+
+        #transformation is performed prior to averaging
+        if transform != None:
+            transformValues(et, fn=transform)  # in place transform
+            cbc()
+
+        #if average function is given, use it to join same spotids
+        if average != None:
+            et = averageAttributes(et, fn=average)
+            cbc()
+
+        cbc.end()
+
+        return et
+
+    def download(self, result_ids, reload=False, bufver="0"):
+        """result_ids must be sorted (by `(data_id, mappings_id)`)
+        """
+        data = {"action": "download", "ids": "$MULTI$"}
+        data = self.add_auth(data)
+        antss = self.downloadMulti("", result_ids, data=data, chunk=10,
+                                   bufferkey=bufferkeypipax, bufreload=reload,
+                                   bufver=bufver)
+        for a, legend in antss:
+            yield a
+
+    def downloadMulti(self, command, ids, data=None, chunk=100,
+                      transformfn=None, bufferkey=None, separatefn=None,
+                      bufreload=False, bufver="0"):
+        """
+        Downloads multiple results at once.
+        Results in the same order as in ids.
+
+        Bufferkey transforms command and data into buffer key.
+        bufver is a function returning buffer version for a given id. if
+            a string is given, use it for all ids
+        """
+
+        sids = split(ids, chunk)
+
+        bufverfn = None
+        if isinstance(bufver, basestring):
+            bufverfn = lambda x: bufver
+        else:
+            bufverfn = bufver
+
+        bufcommand, replace_multi = \
+                self.downloadMulti_bufcommand_replace_multi(command,
+                                data=data, chunk=chunk, bufferkey=bufferkey,
+                                transformfn=transformfn
+                                )
+
+        for i, sidp in enumerate(sids):
+
+            buffered = []
+            unbuffered = []
+
+            for a in sidp:
+                if self.inBuffer(bufcommand(a)) == bufverfn(a) and \
+                        bufreload == False:
+                    buffered.append(a)
+                else:
+                    unbuffered.append(a)
+
+            res = []
+            legend = []
+
+            if len(unbuffered) > 0:
+                com1, d1 = replace_multi(command, data, ",".join(unbuffered))
+                # get unbuffered part
+                res, legend = self.sq(com1, data=d1, buffer=False)
+            else:
+                # get legend from buffer also
+                legend = self.fromBuffer(bufcommand(buffered[0]))[0]
+
+            legend = ["gene_id", "value"]
+            genes = nth(res, 0)
+
+            antss = {}
+            for i, cid in enumerate(unbuffered):
+                col = i + 1
+                vals = nth(res, col)
+                antss[cid] = [[a, b] for a, b in zip(genes, vals) if b != "?"]
+
+            #here save buffer
+            for a, b in antss.items():
+                self.toBuffer(bufcommand(a), [legend] + b, bufverfn(a),
+                              autocommit=False)
+            self.bufferCommit()
+
+            #get buffered from the buffer
+            antssb = dict([(b, self.fromBuffer(bufcommand(b))[1:]) \
+                           for b in buffered])
+            antss.update(antssb)
+
+            #put results in order
+            for ci in sidp:
+                yield antss[ci], legend
+
+
 class DictyExpress(DBCommon):
     """
     Type is object id

_bioinformatics/obiDifscale.py

 
 
 def signed_PCA(data):
-    pca = Orange.projection.pca.Pca(data, standardize=False, max_components=1)
+    pca = Orange.projection.linear.PCA(data, standardize=False, max_components=1)
     classifier = lambda X: [x[0].value for x in pca(X)]
     predictions = classifier(data)
     classes = [ex.getclass().value for ex in data]

_bioinformatics/obiGeneAtlas.py

 AtlasExperiment = ExperimentExpression
 ##
 
+CACHE_VERSION = 1
+
+
 def _cache(name="AtlasGeneResult.shelve"):
     """ Return a open cache instance (a shelve object).
     """
             os.makedirs(orngServerFiles.localpath("GeneAtlas"))
         except OSError:
             pass
-    return shelve.open(orngServerFiles.localpath("GeneAtlas", name))
+    cache = shelve.open(orngServerFiles.localpath("GeneAtlas", name))
+    if cache.get(name + "__CACHE_VERSION__", None) == CACHE_VERSION:
+        return cache
+    else:
+        cache = shelve.open(orngServerFiles.localpath("GeneAtlas", name), "n")
+        cache[name + "__CACHE_VERSION__"] = CACHE_VERSION
+        return cache
+
 
 SLEEP_TIME_MULTIPLIER = 3.0
 
             experiments = expression["experiments"]
             result_experiment = []
             for exp in experiments:
-                exp_accession = exp["accession"]
-                updown = exp["expression"]
+                if "accession" in exp:
+                    exp_accession = exp["accession"]
+                elif "experimentAccession" in exp:
+                    exp_accession = exp["experimentAccession"]
+                else:
+                    raise KeyError()
+                if "expression" in exp:
+                    updown = exp["expression"]
+                elif "updn" in exp:
+                    updown = exp["updn"]
+                else:
+                    raise KeyError
                 pval = exp["pvalue"]
                 result_experiment.append(ExperimentExpression(exp_accession, updown, pval))
             result_expressions.append(ExpressionResults(ef, efv, up, down, result_experiment))

_bioinformatics/widgets/OWGeneInfo.py

     def setRoleData(self, role, row, col, data):
         self._roleData[role][row][col] = data
         
-    def data(self, index, role):
+    def data(self, index, role=Qt.DisplayRole):
         row, col = index.row(), index.column()
         return self._roleData[role][row][col]
         
     def parent(self, index):
         return QModelIndex()
     
-    def rowCount(self, index):
+    def rowCount(self, index=QModelIndex()):
         if index.isValid():
             return 0
         else:
             return len(self._data)
         
-    def columnCount(self, index):
+    def columnCount(self, index=QModelIndex()):
         return len(self._header)
 
-    def headerData(self, section, orientation, role):
+    def headerData(self, section, orientation, role=Qt.DisplayRole):
         if role==Qt.DisplayRole:
             return QVariant(self._header[section])
         return QVariant()
         ## A label for dictyExpress link
         self.dictyExpressBox = OWGUI.widgetBox(self.controlArea, "Dicty Express")
         self.linkLabel = OWGUI.widgetLabel(self.dictyExpressBox, "")
-        self.linkLabel.setOpenExternalLinks(True)
+        self.linkLabel.setOpenExternalLinks(False)
+        self.connect(self.linkLabel, SIGNAL("linkActivated(QString)"),
+                     self.onDictyExpressLink)
         self.dictyExpressBox.hide()
         
         OWGUI.rubber(self.controlArea)
     def commit(self):
         if not self.data:
             return
-        
-        mapToSource = self.treeWidget.model().mapToSource
+        model = self.treeWidget.model()
+        mapToSource = model.mapToSource
         selectedIds = [self.cells[mapToSource(index).row()][0] for index in self.treeWidget.selectedIndexes()]
         selectedRows = self.treeWidget.selectedIndexes()
         selectedRows = [mapToSource(index).row() for index in selectedRows]
+        model = model.sourceModel()
         
         selectedGeneids = [self.row2geneinfo[row] for row in selectedRows]
         selectedIds = [self.geneinfo[i][0] for i in selectedGeneids]
         selectedIds = set(selectedIds)
+        gene2row = dict((self.geneinfo[self.row2geneinfo[row]][0], row) \
+                        for row in selectedRows)
         
         if self.useAttr:
             def is_selected(attr):
             attr = self.attributes[self.geneAttr]
             geneinfo = dict(self.geneinfo)
             examples = [ex for ex in self.data if str(ex[attr]) in selectedIds]
+            if True:  # Add gene info
+                domain = orange.Domain(self.data.domain, self.data.domain.classVar)
+                domain.addmetas(self.data.domain.getmetas())
+                n_columns = model.columnCount()
+
+                headers = [str(model.headerData(i, Qt.Horizontal, Qt.DisplayRole).toString()) \
+                           for i in range(n_columns)]
+                new_meta_attrs = [(orange.newmetaid(), orange.StringVariable(name)) \
+                                  for name in headers]
+                domain.addmetas(dict(new_meta_attrs))
+                examples = [orange.Example(domain, ex) for ex in examples]
+                for ex in examples:
+                    for i, (_, meta) in enumerate(new_meta_attrs):
+                        row = gene2row[str(ex[attr])]
+                        ex[meta] = str(model.data(model.index(row, i), Qt.DisplayRole).toString())
+
             if examples:
                 newdata = orange.ExampleTable(examples)
             else:
             return None 
         if show:
             genes = [fix(gene) for gene in genes if fix(gene)]
-            link1 = '<a href="http://www.ailab.si/dictyexpress/run/index.php?gene=%s">Microarray profile</a>' % (" ".join(genes))
-            link2 = '<a href="http://www.ailab.si/dictyexpress/run/index.php?gene=%s&db=rnaseq">RNA-Seq profile</a>' % (" ".join(genes))
+            link1 = '<a href="http://dictyexpress.biolab.si/run/index.php?gene=%s">Microarray profile</a>'
+            link2 = '<a href="http://dictyexpress.biolab.si/run/index.php?gene=%s&db=rnaseq">RNA-Seq profile</a>'
             self.linkLabel.setText(link1 + "<br/>" + link2)
             
             show = any(genes)
             self.dictyExpressBox.show()
         else:
             self.dictyExpressBox.hide()
+
+    def onDictyExpressLink(self, link):
+        if not self.data:
+            return
+
+        selectedIndexes = self.treeWidget.selectedIndexes()
+        if not len(selectedIndexes):
+            QMessageBox.information(self, "No gene ids selected", "Please select some genes and try again.")
+            return
+        model = self.treeWidget.model()
+        mapToSource = model.mapToSource
+        selectedIds = [self.cells[mapToSource(index).row()][0] for index in selectedIndexes]
+        selectedRows = self.treeWidget.selectedIndexes()
+        selectedRows = [mapToSource(index).row() for index in selectedRows]
+        model = model.sourceModel()
+
+        selectedGeneids = [self.row2geneinfo[row] for row in selectedRows]
+        selectedIds = [self.geneinfo[i][0] for i in selectedGeneids]
+        selectedIds = set(selectedIds)
+
+        def fix(ddb):
+            if ddb.startswith("DDB"):
+                if not ddb.startswith("DDB_G"):
+                    ddb = ddb.replace("DDB", "DDB_G")
+                return ddb
+            return None
+
+        genes = [fix(gene) for gene in selectedIds if fix(gene)]
+        url = str(link) % " ".join(genes)
+        QDesktopServices.openUrl(QUrl(url))
             
     def onAltSourceChange(self):
         self.setItems()
         
 if __name__ == "__main__":
     app = QApplication(sys.argv)
-    data = orange.ExampleTable("../../orange/doc/datasets/brown-selected.tab")
+    data = orange.ExampleTable("brown-selected.tab")
     w = OWGeneInfo()
     w.show()
     w.setData(data)

_bioinformatics/widgets/OWPIPAx.py

+"""
+<name>PIPAx</name>
+<description>Access data from PIPA RNA-Seq database.</description>
+<icon>icons/PIPA.png</icon>
+<priority>35</priority>
+"""
+
+from __future__ import absolute_import
+
+import sys, os
+from collections import defaultdict
+import math
+from datetime import date
+
+from Orange.orng import orngEnviron
+from Orange.OrangeWidgetsimport OWGUI
+from Orange.OrangeWidgets.OWWidget import *
+
+from .. import obiDicty
+
+from .OWPIPA import (MyTreeWidgetItem, ListItemDelegate,
+                    SelectionSetsWidget, SortedListWidget)
+
+try:
+    from ast import literal_eval
+except ImportError:
+    # Compatibility with Python 2.5
+    literal_eval = eval
+
+
+def tfloat(s):
+    try:
+        return float(s)
+    except:
+        return None
+
+
+class MyTreeWidgetItem(QTreeWidgetItem):
+
+    def __init__(self, parent, *args):
+        QTreeWidgetItem.__init__(self, parent, *args)
+        self.par = parent
+
+    def __contains__(self, text):
+        return any(text.upper() in str(self.text(i)).upper() \
+                   for i in range(self.columnCount()))
+
+    def __lt__(self, o1):
+        col = self.par.sortColumn()
+        if col in [8, 9]:  # WARNING: hardcoded column numbers
+            return tfloat(self.text(col)) < tfloat(o1.text(col))
+        else:
+            return QTreeWidgetItem.__lt__(self, o1)
+
+
+# set buffer file
+bufferpath = os.path.join(orngEnviron.directoryNames["bufferDir"], "pipax")
+
+try:
+    os.makedirs(bufferpath)
+except:
+    pass
+
+bufferfile = os.path.join(bufferpath, "database.sq3")
+
+
+class SelectionByKey(object):
+    """An object stores item selection by unique key values
+    (works only for row selections in list and table models)
+    Example::
+
+        ## Save selection by unique tuple pairs (DisplayRole of column 1 and 2)
+        selection = SelectionsByKey(itemView.selectionModel().selection(),
+                                    key = (1,2))
+        ## restore selection (Possibly omitting rows not present in the model)
+        selection.select(itemView.selectionModel())
+
+    """
+
+    def __init__(self, itemSelection, name="", key=(0,)):
+        self._key = key
+        self.name = name
+        self._selected_keys = []
+        if itemSelection:
+            self.setSelection(itemSelection)
+
+    def _row_key(self, model, row):
+        def key(row, col):
+            return str(model.data(model.index(row, col),
+                                  Qt.DisplayRole).toString())
+
+        return tuple(key(row, col) for col in self._key)
+
+    def setSelection(self, itemSelection):
+        self._selected_keys = [self._row_key(ind.model(), ind.row()) \
+                               for ind in itemSelection.indexes() \
+                               if ind.column() == 0]
+
+    def select(self, selectionModel):
+        model = selectionModel.model()
+        selectionModel.clear()
+        for i in range(model.rowCount()):
+            if self._row_key(model, i) in self._selected_keys:
+                selectionModel.select(model.index(i, 0),
+                    QItemSelectionModel.Select | QItemSelectionModel.Rows)
+
+    def __len__(self):
+        return len(self._selected_keys)
+
+
+# Mapping from PIPAx.results_list annotation keys to Header names.
+HEADER = [("_cached", ""),
+          ("data_name", "Name"),
+          ("species_name", "Species"),
+          ("strain", "Strain"),
+          ("Experiment", "Experiment"),
+          ("genotype", "Genotype"),
+          ("treatment", "Treatment"),
+          ("growth", "Growth"),
+          ("tp", "Timepoint"),
+          ("replicate", "Replicate"),
+          ("unique_id", "ID"),
+          ("date_rnaseq", "Date RNAseq"),
+          ("adapter_type", "Adapter"),
+          ("experimenter", "Experimenter"),
+          ("band", "Band"),
+          ("polya", "Polya"),
+          ("primer", "Primer"),
+          ("shearing", "Shearing")
+          ]
+
+# Index of unique_id
+ID_INDEX = 10
+
+# Index of 'date_rnaseq'
+DATE_INDEX = 11
+
+SORTING_MODEL_LIST = \
+    ["Strain", "Experiment", "Genotype",
+     "Timepoint", "Growth", "Species",
+     "ID", "Name", "Replicate"]
+
+
+class OWPIPAx(OWWidget):
+    settingsList = ["server", "excludeconstant", "username", "password",
+                    "joinreplicates", "selectionSetsWidget.selections",
+                    "columnsSortingWidget.sortingOrder", "currentSelection",
+                    "log2", "experimentsHeaderState", "rtypei"]
+
+    def __init__(self, parent=None, signalManager=None, name="PIPAx"):
+        OWWidget.__init__(self, parent, signalManager, name)
+        self.outputs = [("Example table", ExampleTable)]
+
+        self.username = ""
+        self.password = ""
+        self.log2 = False
+        self.rtypei = 0
+
+        self.selectedExperiments = []
+        self.buffer = obiDicty.BufferSQLite(bufferfile)
+
+        self.searchString = ""
+        self.excludeconstant = False
+        self.joinreplicates = False
+        self.currentSelection = None
+
+        self.experimentsHeaderState = \
+                dict(((name, False) for _, name in HEADER[:ID_INDEX + 1]))
+
+        self.result_types = []
+        self.mappings = {}
+
+        self.controlArea.setMaximumWidth(250)
+        self.controlArea.setMinimumWidth(250)
+
+        OWGUI.button(self.controlArea, self, "Reload",
+                     callback=self.Reload)
+        OWGUI.button(self.controlArea, self, "Clear cache",
+                     callback=self.clear_cache)
+
+        b = OWGUI.widgetBox(self.controlArea, "Experiment Sets")
+        self.selectionSetsWidget = SelectionSetsWidget(self)
+        self.selectionSetsWidget.setSizePolicy(QSizePolicy.Preferred,
+                                               QSizePolicy.Maximum)
+        b.layout().addWidget(self.selectionSetsWidget)
+
+        OWGUI.separator(self.controlArea)
+
+        b = OWGUI.widgetBox(self.controlArea, "Sort output columns")
+        self.columnsSortingWidget = SortedListWidget(self)
+        self.columnsSortingWidget.setSizePolicy(QSizePolicy.Preferred,
+                                                QSizePolicy.Maximum)
+        b.layout().addWidget(self.columnsSortingWidget)
+        sorting_model = QStringListModel(SORTING_MODEL_LIST)
+        self.columnsSortingWidget.setModel(sorting_model)
+
+        self.columnsSortingWidget.sortingOrder = \
+                ["Strain", "Experiment", "Genotype", "Timepoint"]
+
+        OWGUI.separator(self.controlArea)
+
+        box = OWGUI.widgetBox(self.controlArea, 'Expression Type')
+        self.expressionTypesCB = OWGUI.comboBox(box, self, "rtypei",
+                items=[],
+                callback=self.UpdateResultsList)
+
+        OWGUI.checkBox(self.controlArea, self, "excludeconstant",
+                       "Exclude labels with constant values"
+                       )
+
+        OWGUI.checkBox(self.controlArea, self, "joinreplicates",
+                       "Average replicates (use median)"
+                       )
+
+        OWGUI.checkBox(self.controlArea, self, "log2",
+                       "Logarithmic (base 2) transformation"
+                       )
+
+        self.commit_button = OWGUI.button(self.controlArea, self, "&Commit",
+                                          callback=self.Commit)
+        self.commit_button.setDisabled(True)
+
+        OWGUI.rubber(self.controlArea)
+
+        box = OWGUI.widgetBox(self.controlArea, "Authentication")
+
+        OWGUI.lineEdit(box, self, "username", "Username:",
+                       labelWidth=100,
+                       orientation='horizontal',
+                       callback=self.AuthChanged)
+
+        self.passf = OWGUI.lineEdit(box, self, "password", "Password:",
+                                    labelWidth=100,
+                                    orientation='horizontal',
+                                    callback=self.AuthChanged)
+
+        self.passf.setEchoMode(QLineEdit.Password)
+
+        OWGUI.lineEdit(self.mainArea, self, "searchString", "Search",
+                       callbackOnType=True,
+                       callback=self.SearchUpdate)
+
+        self.headerLabels = [t[1] for t in HEADER]
+
+        self.experimentsWidget = QTreeWidget()
+        self.experimentsWidget.setHeaderLabels(self.headerLabels)
+        self.experimentsWidget.setSelectionMode(QTreeWidget.ExtendedSelection)
+        self.experimentsWidget.setRootIsDecorated(False)
+        self.experimentsWidget.setSortingEnabled(True)
+
+        contextEventFilter = OWGUI.VisibleHeaderSectionContextEventFilter(
+                            self.experimentsWidget, self.experimentsWidget
+                            )
+
+        self.experimentsWidget.header().installEventFilter(contextEventFilter)
+        self.experimentsWidget.setItemDelegateForColumn(0,
+                    OWGUI.IndicatorItemDelegate(self, role=Qt.DisplayRole)
+                    )
+
+        self.experimentsWidget.setAlternatingRowColors(True)
+
+        self.connect(self.experimentsWidget.selectionModel(),
+                 SIGNAL("selectionChanged(QItemSelection, QItemSelection)"),
+                 self.onSelectionChanged)
+
+        self.selectionSetsWidget.setSelectionModel(
+                            self.experimentsWidget.selectionModel()
+                            )
+
+        self.mainArea.layout().addWidget(self.experimentsWidget)
+
+        self.loadSettings()
+
+        self.restoreHeaderState()
+
+        self.connect(self.experimentsWidget.header(),
+                     SIGNAL("geometriesChanged()"),
+                     self.saveHeaderState)
+
+        self.dbc = None
+
+        self.AuthSet()
+
+        QTimer.singleShot(100, self.UpdateExperiments)
+
+        self.resize(800, 600)
+
+    def AuthSet(self):
+        if len(self.username):
+            self.passf.setDisabled(False)
+        else:
+            self.passf.setDisabled(True)
+
+    def AuthChanged(self):
+        self.AuthSet()
+        self.ConnectAndUpdate()
+
+    def ConnectAndUpdate(self):
+        self.Connect()
+        self.UpdateExperiments(reload=True)
+
+    def Connect(self):
+        self.error(1)
+        self.warning(1)
+
+        def en(x):
+            return x if len(x) else None
+
+        self.dbc = obiDicty.PIPAx(buffer=self.buffer,
+                                  username=en(self.username),
+                                  password=self.password)
+
+        #check password
+        if en(self.username) != None:
+            try:
+                self.dbc.mappings(reload=True)
+            except obiDicty.AuthenticationError:
+                self.error(1, "Wrong username or password")
+                self.dbc = None
+            except Exception, ex:
+                print "Error when contacting the PIPA database", ex
+                import traceback
+                print traceback.format_exc()
+                try:  # maybe cached?
+                    self.dbc.mappings()
+                    self.warning(1, "Can not access database - using cached data.")
+                except Exception, ex:
+                    self.dbc = None
+                    self.error(1, "Can not access database.")
+
+    def Reload(self):
+        self.UpdateExperiments(reload=True)
+
+    def clear_cache(self):
+        self.buffer.clear()
+        self.Reload()
+
+    def rtype(self):
+        """Return selected result template type """
+        if self.result_types:
+            return self.result_types[self.rtypei][0]
+        else:
+            return "-1"
+
+    def UpdateExperimentTypes(self):
+        self.expressionTypesCB.clear()
+        items = [desc for _, desc  in self.result_types]
+        self.expressionTypesCB.addItems(items)
+        self.rtypei = max(0, min(self.rtypei, len(self.result_types) - 1))
+
+    def UpdateExperiments(self, reload=False):
+        self.experimentsWidget.clear()
+        self.items = []
+
+        self.progressBarInit()
+
+        if not self.dbc:
+            self.Connect()
+
+        mappings = {}
+        result_types = []
+        sucind = False  # success indicator for database index
+
+        try:
+            mappings = self.dbc.mappings(reload=reload)
+            result_types = self.dbc.result_types(reload=reload)
+            sucind = True
+        except Exception, ex:
+            try:
+                mappings = self.dbc.mappings()
+                result_types = self.dbc.result_types()
+                self.warning(0, "Can not access database - using cached data.")
+                sucind = True
+            except Exception, ex:
+                self.error(0, "Can not access database.")
+
+        if sucind:
+            self.warning(0)
+            self.error(0)
+
+        self.mappings = mappings
+        self.result_types = result_types
+
+        self.UpdateExperimentTypes()
+
+        results_list = {}
+        try:
+            results_list = self.dbc.results_list(self.rtype(), reload=reload)
+        except Exception, ex:
+            try:
+                results_list = self.dbc.results_list(self.rtype())
+            except Exception, ex:
+                self.error(0, "Can not access database.")
+
+        self.results_list = results_list
+        mappings_key_dict = dict(((m["data_id"], m["id"]), key) \
+                                 for key, m in mappings.items())
+
+        def mapping_unique_id(annot):
+            """Map annotations dict from results_list to unique
+            `mappings` ids.
+            """
+            data_id, mappings_id = annot["data_id"], annot["mappings_id"]
+            return mappings_key_dict[data_id, mappings_id]
+
+        elements = []
+        pos = 0
+
+        for r_id, r_annot in self.results_list.items():
+            pos += 1
+            d = defaultdict(lambda: "?", r_annot)
+            row_items = [""] + [d.get(key, "?") for key, _ in HEADER[1:]]
+            date_string = row_items[DATE_INDEX]
+            try:
+                time_dict = literal_eval(date_string)
+            except Exception:
+                time_dict = {}
+
+            if time_dict and "dateUTC" in time_dict and \
+                    "monthUTC" in time_dict and "fullYearUTC" in time_dict:
+                date_rna = date(time_dict["fullYearUTC"],
+                                time_dict["monthUTC"] + 1,  # Why is month 0 based?
+                                time_dict["dateUTC"])
+
+                row_items[DATE_INDEX] = date_rna.strftime("%x")
+
+            row_items[ID_INDEX] = mapping_unique_id(r_annot)
+            elements.append(row_items)
+
+            ci = MyTreeWidgetItem(self.experimentsWidget, row_items)
+
+            self.items.append(ci)
+
+        for i in range(len(self.headerLabels)):
+            self.experimentsWidget.resizeColumnToContents(i)
+
+        # which is the ok buffer version
+        # FIXME: what attribute to use for version?
+        self.wantbufver = \
+            lambda x, ad=self.results_list: \
+                defaultdict(lambda: "?", ad[x])["date"]
+
+        self.wantbufver = lambda x: "0"
+
+        self.UpdateCached()
+
+        self.progressBarFinished()
+
+        if self.currentSelection:
+            self.currentSelection.select(self.experimentsWidget.selectionModel())
+
+        self.handle_commit_button()
+
+    def UpdateResultsList(self, reload=False):
+        results_list = {}
+        try:
+            results_list = self.dbc.results_list(self.rtype(), reload=reload)
+        except Exception:
+            results_list = self.dbc.results_list(self.rtype())
+        self.results_list = results_list
+        self.UpdateCached()
+
+    def UpdateCached(self):
+        if self.wantbufver and self.dbc:
+            fn = self.dbc.download_key_function()
+            result_id_key = dict(((m["data_id"], m["mappings_id"]), key) \
+                                 for key, m in self.results_list.items())
+
+            for item in self.items:
+                c = str(item.text(10))
+                mapping = self.mappings[c]
+                data_id, mappings_id = mapping["data_id"], mapping["id"]
+                r_id = result_id_key[data_id, mappings_id]
+                # Get the buffered version
+                buffered = self.dbc.inBuffer(fn(r_id))
+                value = " " if buffered == self.wantbufver(r_id) else ""
+                item.setData(0, Qt.DisplayRole, QVariant(value))
+
+    def SearchUpdate(self, string=""):
+        for item in self.items:
+            item.setHidden(not all(s in item \
+                                   for s in self.searchString.split())
+                           )
+
+    def Commit(self):
+        if not self.dbc:
+            self.Connect()
+
+        pb = OWGUI.ProgressBar(self, iterations=100)
+
+        table = None
+
+        ids = []
+        for item in self.experimentsWidget.selectedItems():
+            unique_id = str(item.text(10))
+            annots = self.mappings[unique_id]
+            ids.append((annots["data_id"], annots["id"]))
+
+        transfn = None
+        if self.log2:
+            transfn = lambda x: math.log(x + 1.0, 2)
+
+        reverse_header_dict = dict((name, key) for key, name in HEADER)
+
+        hview = self.experimentsWidget.header()
+        shownHeaders = [label for i, label in \
+                        list(enumerate(self.headerLabels))[1:] \
+                        if not hview.isSectionHidden(i)
+                        ]
+
+        allowed_labels = [reverse_header_dict.get(label, label) \
+                          for label in shownHeaders]
+
+        if self.joinreplicates and "id" not in allowed_labels:
+            # need 'id' labels in join_replicates for attribute names
+            allowed_labels.append("id")
+
+        if len(ids):
+            table = self.dbc.get_data(ids=ids, result_type=self.rtype(),
+                          callback=pb.advance,
+                          exclude_constant_labels=self.excludeconstant,
+#                          bufver=self.wantbufver,
+                          transform=transfn,
+                          allowed_labels=allowed_labels)
+
+            if self.joinreplicates:
+                table = obiDicty.join_replicates(table,
+                    ignorenames=["replicate", "data_id", "mappings_id",
+                                 "data_name", "id", "unique_id"],
+                    namefn=None,
+                    avg=obiDicty.median
+                    )
+
+            # Sort attributes
+            sortOrder = self.columnsSortingWidget.sortingOrder
+
+            def sorting_key(attr):
+                atts = attr.attributes
+                return tuple([atts.get(reverse_header_dict[name], "") \
+                              for name in sortOrder])
+
+            attributes = sorted(table.domain.attributes,
+                                key=sorting_key)
+
+            domain = orange.Domain(attributes, table.domain.classVar)
+            domain.addmetas(table.domain.getmetas())
+            table = orange.ExampleTable(domain, table)
+
+            from Orange.orng.orngDataCaching import data_hints
+            data_hints.set_hint(table, "taxid", "352472")
+            data_hints.set_hint(table, "genesinrows", False)
+
+            self.send("Example table", table)
+
+            self.UpdateCached()
+
+        pb.finish()
+
+    def onSelectionChanged(self, selected, deselected):
+        self.handle_commit_button()
+
+    def handle_commit_button(self):
+        self.currentSelection = \
+            SelectionByKey(self.experimentsWidget.selectionModel().selection(),
+                           key=(1, 2, 3, 10))
+        self.commit_button.setDisabled(not len(self.currentSelection))
+
+    def saveHeaderState(self):
+        hview = self.experimentsWidget.header()
+        for i, label in enumerate(self.headerLabels):
+            self.experimentsHeaderState[label] = hview.isSectionHidden(i)
+
+    def restoreHeaderState(self):
+        hview = self.experimentsWidget.header()
+        state = self.experimentsHeaderState
+        for i, label in enumerate(self.headerLabels):
+            hview.setSectionHidden(i, state.get(label, True))
+            self.experimentsWidget.resizeColumnToContents(i)
+
+if __name__ == "__main__":
+    app = QApplication(sys.argv)
+    obiDicty.verbose = True
+    w = OWPIPAx()
+    w.show()
+    app.exec_()
+    w.saveSettings()

_bioinformatics/widgets/OWVulcanoPlot.py

         
     def clear(self):
         self.labels_combo.clear()
-#        self.values_view.clear()
         self._values_model[:] = []
         self.labels = []
         
         """ Return the current label and selected values.
         """
         i = self.labels_combo.currentIndex()
+
+        if i == -1:
+            # When clearing the labels model / combobox
+            return None, None
+
         label, all_values = self.labels[i]
         values = [all_values[i] for i in self.selection_indexes()]
         return label, values
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.