Accessing a Mascot Server using http
import msparser
import sys
import getopt
import ntpath
import time
def ms_range(start, stop, step=1):
i = start
while i <= stop:
yield i
i += step
msurl = ''
mscmd = ''
msmgf = ''
mstaskID = ''
msaccession = ''
msdatabase = ''
msusername = ''
mspassword = ''
usage = """
Usage http_client.py --url=<URL> --cmd=<command> [options]
--help Print this message
--url Mascot Server URL of the form
http://your-server/mascot/cgi/
--cmd One of the following
--cmd=search to submit a Mascot search
--cmd=getresults to download the search results file
--cmd=getseq to save protein sequence to XML file
--mgf Path to an MGF peak list file
Required if --cmd=search
--taskID Mascot task ID for the search
Required if --cmd=getresults
--accession Accession string for protein
Required if --cmd=getseq
--database Mascot database name
Required if --cmd=getseq
--username Mascot Server user name
May be required if Mascot Security is enabled
--password Mascot Server password
May be required if Mascot Security is enabled
"""
if len(sys.argv) < 2 :
print(usage)
sys.exit(1)
try:
opts, args = getopt.getopt(sys.argv[1:],"",["help","url=","cmd=","mgf=","taskID=","accession=","database=","username=","password="])
except getopt.GetoptError:
sys.exit(1)
for opt, arg in opts:
if opt == '--help':
print(usage)
sys.exit(2)
elif opt in ("--url"):
msurl = arg
elif opt in ("--cmd"):
mscmd = arg
elif opt in ("--mgf"):
msmgf = arg
elif opt in ("--taskID"):
mstaskID = arg
elif opt in ("--accession"):
msaccession = arg
elif opt in ("--database"):
msdatabase = arg
elif opt in ("--username"):
msusername = arg
elif opt in ("--password"):
mspassword = arg
if not msurl:
print(usage)
sys.exit(1)
elif mscmd == "search":
if not msmgf:
print(usage)
sys.exit(1)
elif mscmd == "getresults":
if not mstaskID:
print(usage)
sys.exit(1)
elif mscmd == "getseq":
if not msaccession:
print(usage)
sys.exit(1)
elif not msdatabase:
print(usage)
sys.exit(1)
objSettings = msparser.ms_connection_settings()
objSettings.setUserAgent("PythonScriptTest/1.0 " + objSettings.getUserAgent())
objSettings.setProxyServerType(msparser.ms_connection_settings.PROXY_TYPE_AUTO)
objHttpClient = msparser.ms_http_client(msurl, objSettings)
if not objHttpClient.isValid():
print("Connection to server %s is not valid" % msurl)
showErrorsAndExit(objHttpClient)
objHttpClient.getErrorHandler().setLoggingFile("log.txt", msparser.ms_errs.sev_debug3)
objSession = msparser.ms_http_client_session()
loginReturnCode = objHttpClient.userLogin(msusername, mspassword, objSession)
if loginReturnCode != msparser.ms_http_client.L_SUCCESS and loginReturnCode != msparser.ms_http_client.L_SECURITYDISABLED:
print("Failed to login to %s as user %s. Return code id %d" % (msurl, msusername, loginReturnCode))
sys.exit(1)
else:
print("Logged in to %s with session: %s" % (msurl, objSession.sessionId()))
def doGetSeq():
print("Get Sequence")
accessions = msparser.VectorString()
frames = msparser.vectori()
accessions.append(msaccession)
localFileName = "sequence.xml"
if objSession.getSequenceFile(msdatabase, accessions, frames, localFileName):
print("Successfully saved sequence to %s" % localFileName)
else:
print("Failed to get sequence or failed to save it to %s" % localFileName)
showErrorsAndExit(objSession)
def doGetResults():
print("Get Results")
objSearch = msparser.ms_http_client_search(objSession, mstaskID)
if not objSearch.isValid():
print("Search is not valid")
showErrorsAndExit(objSearch)
ok, remoteFileName = objSearch.getResultsFileName()
print(remoteFileName)
if ok and remoteFileName:
print("Found filename for results file")
else:
print("Unable to find filename for results file")
sys.exit(1)
localFileName = ntpath.basename(remoteFileName)
print("Downloading remote results file %s to %s" % (remoteFileName, localFileName))
objProgress = msparser.ms_http_helper_progress()
if objSearch.downloadResultsFile(localFileName, objProgress):
print("Downloaded remote file to %s" % localFileName)
else:
print("Failed to download remote file to %s" % localFileName)
return False
return True
def doSearch():
print("Searching" )
httpHeader = "Content-Type: multipart/mixed; boundary=---------FormBoundary4C9ByKKVofH"
prologue = """-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="FORMVER"
1.01
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="SEARCH"
MIS
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="COM"
Python script test
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="DB"
SwissProt
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="CLE"
Trypsin/P
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="PFA"
1
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="QUANTITATION"
None
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="TAXONOMY"
. . . . . . . . . . . . . . . . Homo sapiens (human)
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="MODS"
Carbamidomethyl (C)
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="IT_MODS"
Oxidation (M)
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="TOL"
10
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="TOLU"
ppm
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="PEP_ISOTOPE_ERROR"
1
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="ITOL"
0.1
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="ITOLU"
Da
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="CHARGE"
2+
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="MASS"
Monoisotopic
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="FORMAT"
Mascot generic
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="INSTRUMENT"
ESI-TRAP
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="REPORT"
AUTO
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="USERNAME"
Mascot Parser Test
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="USEREMAIL"
dcreasy@matrixscience.com
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="FILE"; filename="test.mgf"
"""
epilogue = "-----------FormBoundary4C9ByKKVofH--"
objSearch = msparser.ms_http_client_search(objSession, mstaskID)
if not objSearch.isValid():
showErrorsAndExit(objSearch)
print("Submitting search")
objProgress = msparser.ms_http_helper_progress()
if objSession.submitSearch(objSearch, httpHeader, prologue, msmgf, epilogue, objProgress):
print("Search submitted. Task ID = %s" % objSearch.searchTaskId())
success = False
returnValue = 0
returnCode = msparser.ms_http_client_search.SS_UNKNOWN
complete = False
while not complete:
success, returnCode, returnValue = objSearch.getStatus()
if not success:
print("Unable to call getStatus()")
sys.exit(1)
if returnCode == msparser.ms_http_client_search.SS_UNKNOWN:
print("Unkown search id")
complete = False
elif returnCode == msparser.ms_http_client_search.SS_ASSIGNED:
print("Search not started")
complete = False
elif returnCode == msparser.ms_http_client_search.SS_QUEUED:
print("Search Queued")
complete = False
elif returnCode == msparser.ms_http_client_search.SS_RUNNING:
print("Search running: %s complete" % returnValue)
complete = False
elif returnCode == msparser.ms_http_client_search.SS_COMPLETE:
print("Search Complete")
complete = True
elif returnCode == msparser.ms_http_client_search.SS_ERROR:
print("Search Error : %s " % returnValue)
complete = True
elif returnCode == msparser.ms_http_client_search.SS_SEARCH_CONTROL_ERROR:
print("Search control error: %s " % returnValue)
complete = True
time.sleep(1)
ok, remoteFileName = objSearch.getResultsFileName()
if not ok:
print("Unable to get filename for results file")
showErrorsAndExit(objHttpClient)
else:
print("Search result report can be viewed at this URL:")
print(objHttpClient.baseUrl() + "master_results_2.pl?file=" + remoteFileName)
else:
print("Failed to submit search")
showErrorsAndExit(objSession)
def showErrorsAndExit(ms_errs):
print("Error: %s" % ms_errs.getLastErrorString())
errs = ms_errs.getErrorHandler()
for i in ms_range(1, errs.getNumberOfErrors()):
print("Error Number: " + str(errs.getErrorNumber(i)) + " (" + str(errs.getErrorRepeats(i + 1)) + " times :")
sys.exit(1)
if mscmd == "search":
doSearch()
if mscmd == "getresults":
doGetResults()
if mscmd == "getseq":
doGetSeq()
objSession.logout()