Repository 'hmdb_ms_search'
hg clone https://toolshed.g2.bx.psu.edu/repos/fgiacomoni/hmdb_ms_search

Changeset 20:b5a1d5e43685 (2019-01-23)
Previous changeset 19:f8c8a990688a (2019-01-23) Next changeset 21:63ba1cb240b7 (2019-02-14)
Commit message:
Master branch Updating - - Fxx
modified:
lib/hmdb.pm
wsdl_hmdb.pl
b
diff -r f8c8a990688a -r b5a1d5e43685 lib/hmdb.pm
--- a/lib/hmdb.pm Wed Jan 23 03:35:34 2019 -0500
+++ b/lib/hmdb.pm Wed Jan 23 07:49:34 2019 -0500
[
b'@@ -7,9 +7,10 @@\n \r\n use LWP::Simple;\r\n use LWP::UserAgent;\r\n+use WWW::Mechanize qw();\r\n use URI::URL;\r\n use SOAP::Lite;\r\n-use Encode;\r\n+use Encode qw(encode_utf8);\r\n use HTML::Template ;\r\n use XML::Twig ;\r\n use Text::CSV ;\r\n@@ -143,9 +144,9 @@\n }\r\n ## END of SUB\r\n \r\n-=head2 METHOD test_matches_from_hmdb_ua\r\n+=head2 METHOD test_matches_from_hmdb_ua DEPRECATED\r\n \r\n-\t## Description : test a single query with tests parameters on hmdb - get the status of the complete server infra.\r\n+\t## Description : [DEPRECATED]test a single query with tests parameters on hmdb - get the status of the complete server infra.\r\n \t## Input : none\r\n \t## Output : $status_line\r\n \t## Usage : my ( $status_line ) = test_matches_from_hmdb_ua( ) ;\r\n@@ -159,24 +160,97 @@\n     my @page = () ;\r\n \r\n \tmy $ua = new LWP::UserAgent;\r\n-\t$ua->agent("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36");\r\n+\t$ua->agent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:64.0) Gecko/20100101 Firefox/64.0");\r\n+\t    \r\n+\tmy $url = \'http://specdb.wishartlab.com/ms/search.csv\';\r\n+\tmy $header = [\'Connection\' => \'keep-alive\', \'Content-Type\' => \'application/x-www-form-urlencoded\', \'Referer\' => \'http://www.hmdb.ca/spectra/ms/search\', \'Accept\' => \'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\'];\r\n \t \r\n-\tmy $req = HTTP::Request->new(\r\n-\t    POST => \'http://specdb.wishartlab.com/ms/search.csv\');\r\n+\tmy $req = HTTP::Request->new(\'POST\', $url, $header);\r\n+#\tprint Dumper $req ;\r\n \t\r\n-\t$req->content_type(\'application/x-www-form-urlencoded\');\r\n+\t## Query format for HMDB 4.0\r\n+\t#"utf8=\xe2\x9c\x93&authenticity_token=K1Ys7oyMKmVNm9n8p0jiTxBlh4G4OO0cqKZYnQKDCw0pM6zmm/CiBxv+/cXhuRsVFV98LLeAMJRN5dCyhIWlAA==&query_masses=175.01 238.19 420.16 780.32 956.25 1100.45&ms_search_ion_mode=positive&adduct_type[]=Unknown&tolerance=0.05&tolerance_units=Da&commit=Search"\r\n+\t\r\n \t$req->content(\'utf8=TRUE&mode=positive&query_masses=420.159317&tolerance=0.000001&database=HMDB&commit=Download Results As CSV\');\r\n-\t \r\n+#\tprint Dumper $req ;\r\n \tmy $res = $ua->request($req);\r\n-#\tprint $res->as_string;\r\n+\t\r\n+\tprint $res->as_string;\r\n \tmy $status_line = $res->status_line ;\r\n \t($status_line) = ($status_line =~ /(\\d+)/);\r\n-\t\r\n-\t\r\n+\r\n \treturn (\\$status_line) ;\r\n }\r\n ## END of SUB\r\n \r\n+=head2 METHOD testMatchesFromHmdbWithUA\r\n+\r\n+\t## Description : test a single query with tests parameters on hmdb - get the status of the complete server infra.\r\n+\t## Input : none\r\n+\t## Output : $status_line\r\n+\t## Usage : my ( $status_line ) = testMatchesFromHmdbWithUA( ) ;\r\n+\t\r\n+=cut\r\n+## START of SUB\r\n+sub testMatchesFromHmdbWithUA {\r\n+\t## Retrieve Values\r\n+    my $self = shift ;\r\n+    \r\n+    my @page = () ;\r\n+    #based on https://stackoverflow.com/questions/17732916/perl-post-automation-and\r\n+\r\n+\tmy $mech = WWW::Mechanize->new(\r\n+\t\tagent => \'wonderbot for W4M 1.01\',\r\n+\t\tautocheck => 1,\t\r\n+\t);\r\n+\t\r\n+\tmy $statusGetLine = 0 ;\r\n+\tmy $statusPostLine = 0 ;\r\n+\t\r\n+\t#receiving cookies and authentication token (CFRS)\r\n+\tmy $reqInit = $mech->get("http://www.hmdb.ca/spectra/ms/search");\r\n+\t$statusGetLine = $mech->status() ;\r\n+\t\r\n+\tif ($statusGetLine == 200 ) {\r\n+\t\tdie \'no CSRF_REQUEST_TOKEN_VALUE in page found\'\r\n+\t\tunless ($reqInit->decoded_content =~ /\\"csrf-token\\"\\s+content=\\"(.*)\\"/) ;\r\n+\t\tmy $csrftoken = $1;\r\n+#\t\tprint "\\nTOKEN: $csrftoken\\n" ;\r\n+\t\t$mech->add_header("X-CSRFToken", $csrftoken);\r\n+\t\t$mech->add_header(\'Connection\', \'keep-alive\');\r\n+\t\t$mech->add_header(\'Content-Type\', \'application/x-www-form-urlencoded\');\r\n+\t\t$mech->add_header(\'Referer\', \'http://www.hmdb.ca/spectra/ms/search\');\r\n+\t\t$mech->add_header(\'Accept\', \'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\');\r\n+\t}\r\n+\t\r\n+\t## POST test\r\n+\tmy $top = 1 ;\r\n+\twhile ($statusPostLine != 200 ) {\r\n+\t\t\r\n+\t\t# Fix a limit at 3 tries...\r\n+\t\tif ($top < 4) {\r\n+\t\t\tprint "\\tTesting HMDB server connexion ($top time(s) )...\\n" ;\r\n+\t\t\t$mech->post(\r\n+\t\t\t\t"http://specdb.wishartlab.com/ms/search.csv",\r\n+\t\t    \tContent => \'utf8=TR'..b'rom_hmdb( $mass, $delta, $mode ) ;\r\n@@ -227,18 +302,29 @@\n     my @page = () ;\r\n \r\n \tmy $ua = LWP::UserAgent->new( keep_alive => 10 );\r\n-\t$ua->agent("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36");\r\n+\t$ua->agent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:64.0) Gecko/20100101 Firefox/64.0");\r\n \t$ua->timeout(2400) ;\r\n+\t\r\n+\t# Cookies\r\n+#\tmy $cookie = new HTTP::Cookies( ignore_discard => 1 );\r\n+#\t$ua->cookie_jar( $cookie );\r\n \t \r\n-\tmy $req = HTTP::Request->new(\r\n-\t    POST => \'http://specdb.wishartlab.com/ms/search.csv\');\r\n+#\tmy $req = HTTP::Request->new(\r\n+#\t    POST => \'http://specdb.wishartlab.com/ms/search.csv\');\r\n+\r\n+\tmy $url = \'http://specdb.wishartlab.com/ms/search.csv\';\r\n+\tmy $header = [\'Content-Type\' => \'application/x-www-form-urlencoded\', \'Referer\' => \'http://www.hmdb.ca/spectra/ms/search\', \'Accept\' => \'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\'];\r\n+\tmy $data = {mode => $mode, query_masses => $masses, \'tolerance\' => $delta, database => \'HMDB\', commit => \'Download Results As CSV\'};\r\n+\tmy $encoded_data = encode_utf8($data);\r\n+\t \r\n+\tmy $req = HTTP::Request->new(\'POST\', $url, $header, $encoded_data);\r\n \t\r\n-\t$req->content_type(\'application/x-www-form-urlencoded\');\r\n+#\t$req->content_type(\'application/x-www-form-urlencoded\');\r\n \t$req->content(\'utf8=TRUE&mode=\'.$mode.\'&query_masses=\'.$masses.\'&tolerance=\'.$delta.\'&database=HMDB&commit=Download Results As CSV\');\r\n #\tprint Dumper $req ; \r\n \tmy $res = $ua->request($req);\r\n \tmy $status_line = undef ;\r\n-#\tprint $res->as_string;\r\n+\tprint $res->as_string;\r\n \tif ($res->is_success) {\r\n \t     @page = split ( /\\n/, $res->decoded_content ) ;\r\n \t     $status_line = \'OK\' ;\r\n@@ -253,6 +339,73 @@\n }\r\n ## END of SUB\r\n \r\n+=head2 METHOD getMatchesFromHmdbWithUA\r\n+\r\n+\t## Description : HMDB querying via an user agent with parameters : mz, delta and molecular species (neutral, pos, neg)\r\n+\t## Input : $mass, $delta, $mode\r\n+\t## Output : $results\r\n+\t## Usage : my ( $results ) = getMatchesFromHmdbWithUA( $mass, $delta, $mode ) ;\r\n+\t\r\n+=cut\r\n+## START of SUB\r\n+sub getMatchesFromHmdbWithUA {\r\n+\t## Retrieve Values\r\n+    my $self = shift ;\r\n+    my ( $masses, $delta, $mode ) = @_ ;\r\n+    \r\n+    my @page = () ;\r\n+    \r\n+    #based on https://stackoverflow.com/questions/17732916/perl-post-automation-and\r\n+\r\n+\tmy $mech = WWW::Mechanize->new(\r\n+\t\tagent => \'wonderbot for W4M 1.01\',\r\n+\t\tautocheck => 1,\r\n+\t\ttimeout => 2400,\r\n+\t);\r\n+\t\r\n+\tmy $statusGetLine = 0 ;\r\n+\tmy $statusPostLine = 0 ;\r\n+\t\r\n+\t#receiving cookies and authentication token (CFRS)\r\n+\tmy $reqInit = $mech->get("http://www.hmdb.ca/spectra/ms/search");\r\n+\t$statusGetLine = $mech->status() ;\r\n+\t\r\n+\tif ($statusGetLine == 200 ) {\r\n+\t\tdie \'no CSRF_REQUEST_TOKEN_VALUE in page found\'\r\n+\t\tunless ($reqInit->decoded_content =~ /\\"csrf-token\\"\\s+content=\\"(.*)\\"/) ;\r\n+\t\tmy $csrftoken = $1;\r\n+#\t\tprint "\\nTOKEN: $csrftoken\\n" ;\r\n+\t\t$mech->add_header("X-CSRFToken", $csrftoken);\r\n+\t\t$mech->add_header(\'Connection\', \'keep-alive\');\r\n+\t\t$mech->add_header(\'Content-Type\', \'application/x-www-form-urlencoded\');\r\n+\t\t$mech->add_header(\'Referer\', \'http://www.hmdb.ca/spectra/ms/search\');\r\n+\t\t$mech->add_header(\'Accept\', \'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\');\r\n+\t}\r\n+\t\r\n+\tmy $res = $mech->post(\r\n+\t\t"http://specdb.wishartlab.com/ms/search.csv",\r\n+    \tContent => \'utf8=TRUE&mode=\'\r\n+    \t\t.$mode.\'&query_masses=\'\r\n+    \t\t.$masses.\'&tolerance=\'\r\n+    \t\t.$delta.\'&database=HMDB&commit=Download Results As CSV\'\r\n+\t);\r\n+\t\r\n+\tif ($mech->success) {\r\n+\t\t@page = split ( /\\n/, $res->decoded_content ) ;\r\n+\t\t$statusPostLine = \'OK\' ;\r\n+\t}\r\n+\telse {\r\n+\t\t$statusPostLine = $mech->status() ;\r\n+\t\twarn "\\t[HMDB service issue !! the server returned a $statusPostLine HTTP error]" ;\r\n+\t}\r\n+\t\r\n+#\tprint Dumper $res->decoded_content ;\r\n+\r\n+\treturn (\\@page, $statusPostLine) ;\r\n+}\r\n+## END of SUB\r\n+\r\n+\r\n =head2 METHOD parse_hmdb_csv_results\r\n \r\n \t## Description : parse the csv results and get data\r\n'
b
diff -r f8c8a990688a -r b5a1d5e43685 wsdl_hmdb.pl
--- a/wsdl_hmdb.pl Wed Jan 23 03:35:34 2019 -0500
+++ b/wsdl_hmdb.pl Wed Jan 23 07:49:34 2019 -0500
b
@@ -130,7 +130,7 @@
  print "\t and ".scalar(@$masses)." masses are submitted as ".scalar(@$submasses)." queries to HMDB \n\n" if ($VERBOSE>1) ;
 
  ## get the hmdb server status by a test query - continuous queries or kill script.
- $status = $oHmdb->test_matches_from_hmdb_ua() ;
+ $status = $oHmdb->testMatchesFromHmdbWithUA() ;
  $oHmdb->check_state_from_hmdb_ua($status) ; ## can kill the script execution
 
  my $cluster = 1 ;
@@ -140,9 +140,9 @@
 
  my ( $hmdb_masses, $nb_masses_to_submit ) = $oHmdb->prepare_multi_masses_query($mzs) ;
 
- print "\t Submission of m/z cluster ".sprintf '%04s',$cluster."" if ($VERBOSE>1) ;
+ print "\n\tSubmission of m/z cluster ".sprintf '%04s',$cluster."" if ($VERBOSE>1) ;
 
- ($hmdb_pages, $status) = $oHmdb->get_matches_from_hmdb_ua($hmdb_masses, $delta, $molecular_species) ;
+ ($hmdb_pages, $status) = $oHmdb->getMatchesFromHmdbWithUA($hmdb_masses, $delta, $molecular_species) ;
  print "...HMDB reply results with status: $status\n" if ($VERBOSE>1) ;
 
 # print Dumper $hmdb_pages ;