Skip to content

Instantly share code, notes, and snippets.

@subhacom
Last active November 4, 2015 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save subhacom/af8f26964bcfffdd2096 to your computer and use it in GitHub Desktop.
Save subhacom/af8f26964bcfffdd2096 to your computer and use it in GitHub Desktop.
This is a Python implementation to query the webservice of biomodels database (https://www.ebi.ac.uk/biomodels-main/) I originally wrote for the MOOSE gui (https://github.com/BhallaLab/moose-gui) back in 2010. This gist is updated to work with Python3 and PyQt4 and provides a client class as well as a graphical widget for downloading models.
# biomodelsclient.py ---
#
# Filename: biomodelsclient.py
# Description:
# Author: Subhasis Ray (original client & widget),
# GV Harsharani (pickled cache).
# Maintainer:
# Created: Tue Mar 2 07:57:39 2010 (+0530)
# Version:
# Last-Updated: Wed Nov 04 15:02:32 2015 (EDT)
# By: Subhasis Ray
# Update #:
# URL:
# Keywords:
# Compatibility:
#
#
# Commentary:
#
# This is a client for Biomodels database SOAP service.
# It imitates the SOAP client written in JAVA
# availabele at biomodels website.
#
# It provides a client class as well as a graphical widget
# wrapping the same to query and download models from the
# biomodels database.
#
# Requirements:
# Python3
# The client requires suds (`pip install suds-jurko`).
# The GUI uses PyQt4.
#
# Credits: I think the pickling was added by GV Harsharani
# at NCBS. - Subhasis
# Change log:
#
#
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street, Fifth
# Floor, Boston, MA 02110-1301, USA.
#
#
# Code:
from suds.client import Client
from suds.transport.http import HttpTransport as SudsHttpTransport
import os
import pickle
BIOMODELS_WSDL_URI = 'http://www.ebi.ac.uk/biomodels-main/services/BioModelsWebServices?wsdl'
proxyOpts = dict()
for k,v in list({'http':'http_proxy','https':'https_proxy'}.items()):
if v in os.environ:
httpProxy = os.environ[v].replace('http://', '')
proxyOpts[k] = httpProxy[0:httpProxy.rfind('/',0,len(httpProxy))]
elif v.upper() in os.environ:
HttpProxy = os.environ[v.upper()].replace('http://', '')
proxyOpts[k] = HttpProxy[0:HttpProxy.rfind('/',0,len(HttpProxy))]
class BioModelsClient(Client):
def __init__(self, WSDL_URI=BIOMODELS_WSDL_URI):
"""Initialize the client with the available queries listed in
the WSDL file. All the queries can be executed using the following syntax:
client.service.queryToBeExecuted()
"""
try:
Client.__init__(self, WSDL_URI,proxy=proxyOpts)
#Client.__init__(self, WSDL_URI,transport=HttpTransport())
except Exception as e:
print(e)
from PyQt4.Qt import Qt
from PyQt4 import QtCore, QtGui
class BioModelsClientWidget(QtGui.QDialog):
"""This is a widget with a Biomodels Client. It provides simple
access to the biomodel queries and gives the user a view of the
results"""
COMBO_ITEM_QUERY_MAP = [('All Curated Model Ids', 'getAllCuratedModelsId'),
('All Model Ids', 'getAllModelsId'),
('All Non-curated Model Ids', 'getAllNonCuratedModelsId'),
('Model Name By Id', 'getModelNameById'),
('Model Ids by ChEBI', 'getModelsIdByChEBI'),
('Model Ids by ChEBI Id', 'getModelsIdByChEBIId'),
('Model Ids by Gene Ontology Term', 'getModelsIdByGO'),
('Model Ids by Gene Ontology Id', 'getModelsIdByGOId'),
('Model Ids by Name', 'getModelsIdByName'),
('Model Ids by Author/Modeler', 'getModelsIdByPerson'),
('Model Ids by Publication', 'getModelsIdByPublication'),
('Model Ids by Taxonomy', 'getModelsIdByTaxonomy'),]
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setWindowTitle('Connect to BioModels')
self.client = BioModelsClient()
self.queryPanel = QtGui.QWidget(self)
self.queryModelLabel = QtGui.QLabel('Get ', self.queryPanel)
self.queryModelCombo = QtGui.QComboBox(self.queryPanel)
self.queryLineEdit = QtGui.QLineEdit(self.queryPanel)
self.goButton = QtGui.QPushButton('Go', self.queryPanel)
self.filePath = ''
layout = QtGui.QHBoxLayout(self.queryPanel)
layout.addWidget(self.queryModelLabel)
layout.addWidget(self.queryModelCombo)
layout.addWidget(self.queryLineEdit)
layout.addWidget(self.goButton)
self.queryPanel.setLayout(layout)
for entry in BioModelsClientWidget.COMBO_ITEM_QUERY_MAP:
self.queryModelCombo.addItem(self.tr(entry[0]), self.tr(entry[1]))
self.resultsPanel = QtGui.QTableWidget(self)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.queryPanel)
layout.addWidget(self.resultsPanel)
self.queryPanel1 = QtGui.QWidget(self)
self.importButton = QtGui.QPushButton('Import',self.queryPanel1)
self.importButton.setEnabled(False)
self.closeButton = QtGui.QPushButton('Close',self.queryPanel1)
hbox = QtGui.QHBoxLayout(self.queryPanel1)
hbox.addStretch(1)
hbox.addWidget(self.importButton)
hbox.addWidget(self.closeButton)
layout.addWidget(self.queryPanel1)
self.setLayout(layout)
self.setupActions()
self.client.set_options(proxy=proxyOpts)
# TODO:
# proxy = [ can be set using set_option(proxy={'http':'proxyhost:port', ...}) function
def setupActions(self):
self.connect(self.queryLineEdit, QtCore.SIGNAL('returnPressed()'), self.runQuery)
self.connect(self.goButton, QtCore.SIGNAL('clicked()'), self.runQuery)
self.connect(self.closeButton, QtCore.SIGNAL('clicked()'),self.close)
self.connect(self.importButton,QtCore.SIGNAL('clicked()'),self.downloadModel)
self.connect(self.resultsPanel, QtCore.SIGNAL('cellClicked(int,int)'),self.enableimportButton)
def closeWidget(self):
self.close()
def downloadModel(self):
"""Download the selected model"""
""" If user select multi row, only data from currentRow is downloaded and loaded into moose """
selectedRow = self.resultsPanel.currentRow()
modelId = self.resultsPanel.item(selectedRow, 0).text()
modelSBML = str(self.client.service.getModelSBMLById(modelId)).encode("utf-8")
self.filePath = str(modelId)+'.xml'
f = open(str(self.filePath), 'w')
f.write(modelSBML)
self.close()
type_sbml = 'SBML'
filters = {'SBML(*.xml)': type_sbml}
filepath,filter_ = QtGui.QFileDialog.getSaveFileNameAndFilter(None,'Save File',modelId,';;'.join(filters))
if filepath:
if str(filepath).rfind('.') != -1:
filepath = filepath[:str(filepath).rfind('.')]
if str(filter_).rfind('.') != -1:
extension = filter_[str(filter_).rfind('.'):len(filter_)-1]
self.filePath = str(filepath+extension)
if filters[str(filter_)] == 'SBML':
f = open(str(self.filePath), 'w')
f.write(modelSBML)
f.close()
def getTargetPath(self):
return str(self.filePath)
def runQuery(self):
print('Running query .....')
#self.resultsPanel.cellClicked.connect(self.enableDownload)
progressDialog = QtGui.QProgressDialog()
progressDialog.setLabelText('Retrieving data from BioModels Database')
progressDialog.setModal(True)
progressDialog.setVisible(True)
progressDialog.setWindowTitle("BioModels Database")
index = self.queryModelCombo.currentIndex()
query = str(self.queryModelCombo.itemData(index))
argument = self.queryLineEdit.text().strip()
function = eval('self.client.service.' + str(query))
if index > 2:
result = function(str(argument))
else:
result = function()
self.resultsPanel.clear()
row = 0
column = 0
self.resultsPanel.setColumnCount(2)
self.resultsPanel.setHorizontalHeaderItem(column, QtGui.QTableWidgetItem('Id'))
self.resultsPanel.setHorizontalHeaderItem(column + 1, QtGui.QTableWidgetItem('Name'))
self.resultsPanel.setRowCount(0)
display = True
totalRows = 0
if type(result) is type(''):
self.resultsPanel.insertRow(row)
item = QtGui.QTableWidgetItem(argument)
item.setFlags(item.flags() & ~Qt.ItemIsEditable)
self.resultsPanel.setItem(row, column, item)
item = QtGui.QTableWidgetItem(result)
item.setFlags(item.flags() & ~Qt.ItemIsEditable)
self.resultsPanel.setItem(row, column + 1, item)
elif type(result) is type([]):
totalRows = len(result)
updatepickleFile = False
filename = str(query)
filePath = filename+'.pkl'
#print filePath
pickleResult = {}
if os.path.isfile(filePath):
pickleFile = open(filePath,'rb')
pickleResult = pickle.load(pickleFile)
#If there is update from BioModels,then pickle file is updated.
r = 0
if progressDialog:
progressDialog.setMaximum(totalRows)
for item in result:
if item not in pickleResult:
if progressDialog:
progressDialog.setValue(r)
if progressDialog.wasCanceled():
return 0
name = str(self.client.service.getModelNameById(item)).encode("utf-8")
r = r+1
pickleResult.update({item:name})
updatepickleFile = True
if updatepickleFile:
output = open(filename+'.pkl','wb')
pickle.dump(pickleResult,output)
output.close()
if not argument.isEmpty():
try:
name = pickleResult[str(argument)]
pickleResult = {}
pickleResult[argument] = name;
except KeyError as e:
print('A KeyError - "%s"' % str(e) ,' not found in ',filename)
QtGui.QMessageBox.critical(None, "BioModels Database"," The Id "+ str(e) +" not found in "+ filename,QtGui.QMessageBox.Ok | QtGui.QMessageBox.Default,QtGui.QMessageBox.NoButton)
display = False
if display:
for value,name in list(pickleResult.items()):
self.resultsPanel.insertRow(row)
item = QtGui.QTableWidgetItem(self.tr(value))
item.setFlags(item.flags() & ~Qt.ItemIsEditable)
#item.setData(Qt.DisplayRole, QtCore.QVariant(value))
self.resultsPanel.setItem(row, column, item)
#name = self.client.service.getModelNameById(value)
item = QtGui.QTableWidgetItem(name)
item.setFlags(item.flags() & ~Qt.ItemIsEditable)
self.resultsPanel.setItem(row, column + 1, item)
row = row + 1
if progressDialog:
progressDialog.close()
#self.importButton.setEnabled(True)
print('Finished running query')
def enableimportButton(self):
self.importButton.setEnabled(True)
if __name__ == '__main__':
client = BioModelsClient()
#print dir(client)
#print client.helloBioModels()
app = QtGui.QApplication([])
clientWidget = BioModelsClientWidget()
clientWidget.show()
#clientWidget.exec_()
app.exec_()
#
# biomodelsclient.py ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment