Matrix Science header

http_client.py

Accessing a Mascot Server using http

#!/usr/bin/python
##############################################################################
# file: http_client.py                                                       #
# 'msparser' toolkit example code                                            #
##############################################################################
# COPYRIGHT NOTICE                                                           #
# Copyright 1998-2010 Matrix Science Limited  All Rights Reserved.           #
#                                                                            #
##############################################################################
#     $Source: /vol/cvsroot/parser/examples/test_python/http_client.py,v $   #
#     $Author: neilr $                                                      #
#       $Date: 2016/04/22 14:16:29 $                                         #
#   $Revision: 1.4 $                                                         #
# $NoKeywords::                                                            $ #
##############################################################################

import msparser
import sys
import getopt
import ntpath
import time

# ms_range function has been added to make the range supplied consistent with other languages such as Perl and C#
def ms_range(start, stop, step=1):
    i = start
    while i <= stop:
        yield i
        i += step

# Define Global argument variables
msurl = ''
mscmd = ''
msmgf = ''
mstaskID = ''
msaccession = ''
msdatabase = ''
msusername = ''
mspassword = ''

# Define help text
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 no arguments supplied, print help and exit
if len(sys.argv) < 2 :
    print(usage)
    sys.exit(1)

# Ensure arguments are legal. if not, exit
try:
    opts, args = getopt.getopt(sys.argv[1:],"",["help","url=","cmd=","mgf=","taskID=","accession=","database=","username=","password="])
except getopt.GetoptError:
    sys.exit(1)

# If argument is --help, display help then exit
for opt, arg in opts:
    if opt == '--help':
        print(usage)
        sys.exit(2) 

# Read arguments and assign each option to appropriate variables
    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

# ensure that the correct combinations of arguments are assigned
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)

# Common Code for all cases

# Create connection settings
# Any settings for web server authentication or a proxy server should be set here
objSettings = msparser.ms_connection_settings()
objSettings.setUserAgent("PythonScriptTest/1.0 " + objSettings.getUserAgent())
# Try to auto detect any proxy settings
objSettings.setProxyServerType(msparser.ms_connection_settings.PROXY_TYPE_AUTO)

# Connect to the server
objHttpClient = msparser.ms_http_client(msurl, objSettings)
if not objHttpClient.isValid():
    print("Connection to server %s is not valid" % msurl)
    showErrorsAndExit(objHttpClient)

# Enable Logging, mostly for debugging
objHttpClient.getErrorHandler().setLoggingFile("log.txt", msparser.ms_errs.sev_debug3)

# Create Mascot Security Session
# If security is disabled, the empty username and password return the 'all_secdisabledsession' session
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()
    # Download the complete, uncompressed results file
    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" )
# The input to Mascot is the peak list and search parameters in MIME format
    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--"
    
# Request a Mascot task id for the search
    objSearch = msparser.ms_http_client_search(objSession, mstaskID)
    if not objSearch.isValid():
        showErrorsAndExit(objSearch)
# Submit the search
    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) 
            
# Search complete - get the relative path to the result file
        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()

# And finally logout
objSession.logout()


Copyright © 2016 Matrix Science Ltd.  All Rights Reserved. Generated on Fri Jun 2 2017 01:44:50