#!/usr/local/bin/perl
##############################################################################
# File: http_client.pl #
# Mascot Parser toolkit example code #
##############################################################################
# COPYRIGHT NOTICE #
# Copyright 1998-2015 Matrix Science Limited All Rights Reserved. #
# #
##############################################################################
# $Source: parser/examples/test_perl/http_client.pl $
# $Author: villek@matrixscience.com $
# $Date: 2018-07-30 16:23:53 +0100 $
# $Revision: 1b450440f9c97e1e41d0fc6016a27d68951d4532 | MSPARSER_REL_3_0_0-2024-09-24-0-g93ebaeb4f4 $
##############################################################################
use strict;
use warnings;
##############################################################################
# required Perl packages
use lib "../bin";
use msparser;
use Getopt::Long;
use File::Basename;
my ($url, $cmd, $mgf, $taskID, $accession, $database, $username, $password);
my %allowedCommands = ("search" => 1, "getresults" => 1, "getseq" => 1);
$username = "";
$password = "";
GetOptions('url=s' => \$url,
'cmd=s' => \$cmd,
'mgf=s' => \$mgf,
'taskID=s' => \$taskID,
'accession=s'=> \$accession,
'database=s' => \$database,
'username=s' => \$username,
'password=s' => \$password);
if ((not defined $url or not defined $cmd or not exists($allowedCommands{$cmd}))
or ($cmd eq 'search' and not defined $mgf)
or ($cmd eq 'getresults' and not defined $taskID)
or ($cmd eq 'getseq' and (not defined $accession or not defined $database))
) {
print <<USAGE;
Usage http_client.pl --url=<URL> --cmd=<command> [options]
--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
Note that the Mascot search parameters are hard coded towards the end
of this script, and may need modifying for different peak lists.
USAGE
exit 1;
}
# Common code for all cases
# Create connection settings.
# Any settings for web server authentication or a proxy server should be set here
my $objSettings = new msparser::ms_connection_settings;
$objSettings->setUserAgent("PerlScriptTest/1.0 " . $objSettings->getUserAgent);
# Connect to the server
my $objHttpClient = new msparser::ms_http_client($url, $objSettings);
unless ($objHttpClient->isValid) {
showErrorsAndExit($objHttpClient);
}
# Enable logging, mostly for debugging
$objHttpClient->getErrorHandler->setLoggingFile("log.txt", $msparser::ms_errs::sev_debug3);
# Create a Mascot security session
# If security is disabled, the empty user name and password return the 'all_secdisabledsession' session
my $objSession = new msparser::ms_http_client_session;
my $loginReturnCode = $objHttpClient->userLogin($username, $password, $objSession);
if ($loginReturnCode != $msparser::ms_http_client::L_SUCCESS and $loginReturnCode != $msparser::ms_http_client::L_SECURITYDISABLED) {
die "Failed to login to $url as user $username. return code is $loginReturnCode\n";
} else {
print "Logged in to $url with session: " . $objSession->sessionId() . "\n";
}
if ($cmd eq 'search') {
doSearch();
}
if ($cmd eq 'getresults') {
doGetResults();
}
if ($cmd eq 'getseq') {
doGetSeq();
}
# and finally logout
$objSession->logout();
sub doSearch {
# The input to Mascot is the peak list and search parameters in MIME format
my $httpHeader = "Content-Type: multipart/mixed; boundary=---------FormBoundary4C9ByKKVofH";
my $prologue = "";
while (<DATA>){
$prologue .= $_;
}
my $epilogue = "-----------FormBoundary4C9ByKKVofH--";
# Request a Mascot task id for the search
my $taskID = "";
my $objSearch = new msparser::ms_http_client_search($objSession, $taskID);
unless ($objSearch->isValid) {
showErrorsAndExit($objSearch);
}
# Submit the search
print "Submitting search\n";
my $objProgress = new msparser::ms_http_helper_progress;
if ($objSession->submitSearch($objSearch, $httpHeader, $prologue, $mgf, $epilogue, $objProgress)) {
print "Search submitted. Task ID = " . $objSearch->searchTaskId . "\n";
my ($success, $returnValue);
my $returnCode = $msparser::ms_http_client_search::SS_UNKNOWN;
my $complete = 0;
while (!$complete) {
($success, $returnCode, $returnValue) = $objSearch->getStatus();
if (!$success) {
print "Unable to call getStatus()\n";
exit 1;
}
if ($returnCode == $msparser::ms_http_client_search::SS_UNKNOWN ) {
print "Unknown search id\n"; $complete = 0;
} elsif ($returnCode == $msparser::ms_http_client_search::SS_ASSIGNED) {
print "Search not started\n"; $complete = 0;
} elsif ($returnCode == $msparser::ms_http_client_search::SS_QUEUED ) {
print "Search queued\n"; $complete = 0;
} elsif ($returnCode == $msparser::ms_http_client_search::SS_RUNNING ) {
print "Seach running: $returnValue\% complete\n";
} elsif ($returnCode == $msparser::ms_http_client_search::SS_COMPLETE) {
print "Search complete\n"; $complete = 1;
} elsif ($returnCode == $msparser::ms_http_client_search::SS_ERROR ) {
print "Search error : $returnValue \n"; $complete = 1;
} elsif ($returnCode == $msparser::ms_http_client_search::SS_SEARCH_CONTROL_ERROR) {
print "Search control error: $returnValue\n"; $complete = 1;
}
sleep 1;
}
# Search complete - get the relative path to the result file
my ($ok, $remoteFileName) = $objSearch->getResultsFileName();
unless ($ok) {
print "Unable to get filename for results file";
showErrorsAndExit($objHttpClient);
}
# Display URL for search result report
print "Search result report can be viewed at this URL:\n"
. $objHttpClient->baseUrl
. "master_results_2.pl?file="
. $remoteFileName
. "\n";
} else {
print "Failed to submit search\n";
showErrorsAndExit($objSession);
}
}
sub doGetResults {
my $objSearch = new msparser::ms_http_client_search($objSession, $taskID);
unless ($objSearch->isValid) {
showErrorsAndExit($objSearch);
}
my ($ok, $remoteFileName) = $objSearch->getResultsFileName();
unless ($ok && $remoteFileName) {
print "Unable to get filename for results file";
exit -1;
}
my ($localFileName, undef, undef) = fileparse($remoteFileName);
print "Downloading remote results file ($remoteFileName) to $localFileName\n";
my $objProgress = new msparser::ms_http_helper_progress;
# Download the complete, uncompressed result file
if ($objSearch->downloadResultsFile($localFileName, $objProgress)) {
print "Downloaded remote file to $localFileName \n";
} else {
print "Failed to downloaded remote file to $localFileName \n";
showErrorsAndExit($objSearch);
}
}
sub doGetSeq {
my $accessions = new msparser::VectorString;
my $frames = new msparser::vectori;
$accessions->push($accession);
my $localFileName = "sequence.xml";
if ($objSession->getSequenceFile($database, $accessions, $frames, $localFileName)) {
print "Successfully saved sequence to $localFileName \n";
} else {
print "Failed to get sequence or failed to save it to $localFileName \n";
showErrorsAndExit($objSession);
}
}
sub showErrorsAndExit {
my ($obj) = @_;
print "Error: ", $obj->getLastErrorString(), "\n";
my $err = $obj->getErrorHandler();
my $i;
for my $i (1 .. $err->getNumberOfErrors) {
print "Error number: ";
print $err->getErrorNumber($i);
print " (";
print $err->getErrorRepeats($i) + 1;
print " times) : ";
print $err->getErrorString($i), "\n";
}
print "\n";
exit -1;
}
__END__
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="FORMVER"
1.01
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="SEARCH"
MIS
-----------FormBoundary4C9ByKKVofH
Content-Disposition: form-data; name="COM"
Perl 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"