source: orange-bioinformatics/orangecontrib/bio/widgets/OWDicty.py @ 1874:b3e32cc5cf6f

Revision 1874:b3e32cc5cf6f, 7.3 KB checked in by Ales Erjavec <ales.erjavec@…>, 6 months ago (diff)

Added new style widget meta descriptions.

RevLine 
[432]1"""
[1462]2<name>dictyExpress</name>
[1051]3<description>Access to data in dictyExpress database.</description>
[1726]4<icon>icons/DictyExpress.svg</icon>
[1051]5<priority>40</priority>
[432]6"""
7
[1632]8from __future__ import absolute_import
9
10from collections import defaultdict
[432]11import sys
[435]12
[1874]13import Orange
[1632]14from Orange.orng import orngEnviron
15from Orange.OrangeWidgets import OWGUI
16from Orange.OrangeWidgets.OWWidget import *
[705]17
[1632]18from .. import obiDicty
[443]19
[1874]20NAME = "dictyExpress"
21DESCRIPTION = "Access to data in dictyExpress database."
22ICON = "icons/DictyExpress.svg"
23PRIORITY = 40
24
25
26INPUTS = []
27OUTPUTS = [("Example table", Orange.data.Table)]
28
29REPLACES = ["_bioinformatics.widgets.OWDicty.OWDicty"]
30
31
[435]32class MyTreeWidgetItem(QTreeWidgetItem):
33    def __contains__(self, text):
34        return any(text.upper() in str(self.text(i)).upper() for i in range(self.columnCount()))   
[795]35
36#set buffer file
37bufferpath = os.path.join(orngEnviron.directoryNames["bufferDir"], "dicty")
38try:
39    os.makedirs(bufferpath)
40except:
41    pass
42bufferfile = os.path.join(bufferpath, "database.sq3")
43
[432]44class OWDicty(OWWidget):
[838]45    settingsList = ["serverToken", "platform", "selectedExperiments", "server", "buffertime", "excludeconstant" ]
[1462]46    def __init__(self, parent=None, signalManager=None, name="dictyExpress"):
[432]47        OWWidget.__init__(self, parent, signalManager, name)
[795]48        self.outputs = [("Example table", ExampleTable)]
[432]49        self.serverToken = ""
[1179]50        self.server = obiDicty.defaddress
[432]51
52        self.platform = None
[434]53
[432]54        self.selectedExperiments = []
[795]55        self.buffer = obiDicty.BufferSQLite(bufferfile)
[432]56
57        self.searchString = ""
[838]58        self.excludeconstant = False
[432]59       
[795]60        box = OWGUI.widgetBox(self.controlArea, "Cache")
[796]61        OWGUI.button(box, self, "Clear cache", callback=self.clear_buffer)
[795]62
[838]63        OWGUI.checkBox(self.controlArea, self, "excludeconstant", "Exclude labels with constant values" )
64
[434]65        OWGUI.button(self.controlArea, self, "&Commit", callback=self.Commit)
[443]66        box  = OWGUI.widgetBox(self.controlArea, "Server")
67        OWGUI.lineEdit(box, self, "serverToken","Token", callback=self.Connect)
[434]68        OWGUI.rubber(self.controlArea)
69
[432]70        OWGUI.lineEdit(self.mainArea, self, "searchString", "Search", callbackOnType=True, callback=self.SearchUpdate)
71        self.experimentsWidget = QTreeWidget()
[795]72        self.experimentsWidget.setHeaderLabels(["Strain", "Treatment", "Growth condition", "Platform", "N", "Chips"]) 
[432]73        self.experimentsWidget.setSelectionMode(QTreeWidget.ExtendedSelection)
74        self.experimentsWidget.setRootIsDecorated(False)
75        self.experimentsWidget.setSortingEnabled(True)
76##        self.experimentsWidget.setAlternatingRowColors(True)
[795]77
[432]78        self.mainArea.layout().addWidget(self.experimentsWidget)
79
[434]80        self.loadSettings()
[432]81        self.dbc = None       
82
[831]83        QTimer.singleShot(0, self.UpdateExperiments)       
[432]84
[902]85        self.resize(800, 600)
[432]86
87    def __updateSelectionList(self, oldList, oldSelection, newList):
88        oldList = [oldList[i] for i in oldSelection]
89        return [ i for i, new in enumerate(newList) if new in oldList]
90   
91    def Connect(self):
[831]92
[443]93        address = self.server + "?"
[432]94        if self.serverToken:
95            address += "token="+self.serverToken+"&"
96        try:
[795]97            #obiDicty.verbose = 1
98            self.dbc = obiDicty.DatabaseConnection(address, buffer=self.buffer)
[432]99        except Exception, ex:
100            from traceback import print_exception
101            print_exception(*sys.exc_info())
102            self.error(0, "Error connecting to server" + str(ex))
103            return
104        self.error(0)
105
[795]106    def clear_buffer(self):
[796]107        self.buffer.clear()
[795]108        self.UpdateExperiments()
[435]109
[432]110    def UpdateExperiments(self):
[795]111        self.chipsl = []
[432]112        self.experimentsWidget.clear()
113        self.items = []
[795]114
115        if not self.dbc:
116            self.Connect()
[1179]117
118        """
119        self.progressBarInit()
120
[527]121        strains = self.dbc.annotationOptions("sample")["sample"]
[705]122
[432]123        for i, strain in enumerate(strains):
[705]124            chips = self.dbc.search("norms", sample=strain)
125            annotations = self.dbc.annotations("norms", chips)
126
127            elements = []
128
[795]129            for chip,annot in zip(chips,annotations):
[705]130                d = dict(annot)
[795]131                elements.append(((d.get("treatment", ""), d.get("growthCond", ""), d.get("platform", "")),chip))
[705]132
[795]133            def different_chips(li):
[1179]134                # Returns a map, where keys are different elements in li and values their chip ids
[795]135                dc = defaultdict(list)
136                for a,chip in li:
137                    dc[a].append(chip)
[705]138                return dc
139
[795]140            typeswchips = different_chips(elements) #types with counts
[705]141
[1179]142            for (treatment, cond, platforma),cchips in typeswchips.items():
[795]143                self.chipsl.append(cchips)
144                num = len(cchips)
145                experiment = [strain, treatment, cond, platform, str(num), ','.join(cchips)]
146                self.items.append(MyTreeWidgetItem(self.experimentsWidget, experiment))
[705]147
[432]148            self.progressBarSet((100.0 * i) / len(strains))
[1179]149        """
150
151        annotations = self.dbc.annotations("norms", None)
152
153        elements = []
154
155        for chip,annot in annotations:
156            d = dict(annot)
157            elements.append(((d.get("treatment", ""), d.get("growthCond", ""), d.get("platform", ""), d.get("sample", "")),chip))
158
159        def different_chips(li):
160            # Returns a map, where keys are different elements in li and values their chip ids
161            dc = defaultdict(list)
162            for a,chip in li:
163                dc[a].append(chip)
164            return dc
165
166        typeswchips = different_chips(elements) #types with counts
167
168        for (treatment, cond, platform, strain),cchips in typeswchips.items():
169            self.chipsl.append(cchips)
170            num = len(cchips)
171            experiment = [strain, treatment, cond, platform, str(num), ','.join(cchips)] 
172            self.items.append(MyTreeWidgetItem(self.experimentsWidget, experiment))
173
[902]174        for i in range(5):
175            self.experimentsWidget.resizeColumnToContents(i)
[705]176
[432]177        self.progressBarFinished()
178
179    def SearchUpdate(self, string=""):
180        for item in self.items:
[435]181            item.setHidden(not all(s in item for s in self.searchString.split()))
[443]182
[432]183    def Commit(self):
184        if not self.dbc:
185            self.Connect()
186        allTables = []
[557]187
[434]188        import time
189        start = time.time()
[557]190
191        pb = OWGUI.ProgressBar(self, iterations=1000)
192
[795]193        table = None
194
195        ids = []
[432]196        for item in self.experimentsWidget.selectedItems():
[795]197            ids += str(item.text(5)).split(",")
198
[838]199        table = self.dbc.get_single_data(ids=ids, callback=pb.advance, exclude_constant_labels=self.excludeconstant)
[795]200
[434]201        end = int(time.time()-start)
202       
[557]203        pb.finish()
204
[795]205        #self.send("Example table", None)
[1118]206#        table.taxid = "352472"
207#        table.genesinrows = False
208       
[1632]209        from Orange.orng.orngDataCaching import data_hints
[1118]210        data_hints.set_hint(table, "taxid", "352472", 10.0)
211        data_hints.set_hint(table, "genesinrows", False, 10.0)
[1104]212       
[795]213        self.send("Example table", table)
[432]214
215if __name__ == "__main__":
216    app  = QApplication(sys.argv)
[434]217##    from pywin.debugger import set_trace
218##    set_trace()
[432]219    w = OWDicty()
220    w.show()
221    app.exec_()
222    w.saveSettings()
223           
[503]224       
Note: See TracBrowser for help on using the repository browser.