comparison lib/hmdb.pm @ 14:625fa968d99a draft

Master branch Updating - - Fxx
author fgiacomoni
date Thu, 10 Jan 2019 09:10:14 -0500
parents c32280662103
children beb9619263b0
comparison
equal deleted inserted replaced
13:3587cb923890 14:625fa968d99a
193 193
194 if (!defined $$status) { 194 if (!defined $$status) {
195 croak "No http status is defined for the distant server" ; 195 croak "No http status is defined for the distant server" ;
196 } 196 }
197 else { 197 else {
198 unless ( $$status == 200 ) { 198 unless ( $$status == 200 ) {
199 if ( $$status == 504 ) { croak "Gateway Timeout: The HMDB server was acting as a gateway or proxy and did not receive a timely response from the upstream server" ; } 199 if ( $$status == 504 ) { croak "Gateway Timeout: The HMDB server was acting as a gateway or proxy and did not receive a timely response from the upstream server" ; }
200 if ( $$status == 500 ) { croak "Internal Server Error: The HMDB server returns an unexpected internal server error" ; }
200 else { 201 else {
201 ## None supported http code error ## 202 ## None supported http code error ##
203 croak "Internal Server Error $$status..." ;
202 } 204 }
203 } 205 }
206 if ( $$status == 200 ) { print "The HMDB server returns that your request was fulfilled" ; }
204 } 207 }
205 208
206 return (1) ; 209 return (1) ;
207 } 210 }
208 ## END of SUB 211 ## END of SUB
230 my $req = HTTP::Request->new( 233 my $req = HTTP::Request->new(
231 POST => 'http://specdb.wishartlab.com/ms/search.csv'); 234 POST => 'http://specdb.wishartlab.com/ms/search.csv');
232 235
233 $req->content_type('application/x-www-form-urlencoded'); 236 $req->content_type('application/x-www-form-urlencoded');
234 $req->content('utf8=TRUE&mode='.$mode.'&query_masses='.$masses.'&tolerance='.$delta.'&database=HMDB&commit=Download Results As CSV'); 237 $req->content('utf8=TRUE&mode='.$mode.'&query_masses='.$masses.'&tolerance='.$delta.'&database=HMDB&commit=Download Results As CSV');
235 238 # print Dumper $req ;
236 my $res = $ua->request($req); 239 my $res = $ua->request($req);
237 # print $res->as_string; 240 # print $res->as_string;
238 if ($res->is_success) { 241 if ($res->is_success) {
239 @page = split ( /\n/, $res->decoded_content ) ; 242 @page = split ( /\n/, $res->decoded_content ) ;
240 } else { 243 } else {
267 270
268 my %result_by_entry = () ; 271 my %result_by_entry = () ;
269 my %features = () ; 272 my %features = () ;
270 273
271 # print Dumper $csv ; 274 # print Dumper $csv ;
275 # print Dumper $masses ;
276 # print Dumper $max_query ;
272 277
273 foreach my $line (@{$csv}) { 278 foreach my $line (@{$csv}) {
274 ## HMDB csv output - [DEPRECATED]...
275 # if ($line !~ /query_mass,compound_id,formula,compound_mass,adduct,adduct_type,adduct_mass,delta/) {
276 # my @entry = split(/,/, $line) ;
277 #
278 # if ( !exists $result_by_entry{$entry[0]} ) { $result_by_entry{$entry[0]} = [] ; }
279 #
280 # $features{ENTRY_ENTRY_ID} = $entry[1] ;
281 # $features{ENTRY_FORMULA} = $entry[2] ;
282 # $features{ENTRY_CPD_MZ} = $entry[3] ;
283 # $features{ENTRY_ADDUCT} = $entry[4] ;
284 # $features{ENTRY_ADDUCT_TYPE} = $entry[5] ;
285 # $features{ENTRY_ADDUCT_MZ} = $entry[6] ;
286 # $features{ENTRY_DELTA} = $entry[7] ;
287 #
288 # my %temp = %features ;
289 #
290 # push (@{$result_by_entry{$entry[0]} }, \%temp) ;
291 # }
292 ## NEW HMDB format V4.0 - dec2017 279 ## NEW HMDB format V4.0 - dec2017
293 if ($line !~ /query_mass,compound_id,compound_name,kegg_id,formula,monoisotopic_mass,adduct,adduct_type,adduct_m\/z,delta\(ppm\)/) { 280 if ($line !~ /query_mass,compound_id,compound_name,kegg_id,formula,monoisotopic_mass,adduct,adduct_type,adduct_m\/z,delta\(ppm\)/) {
294 281
295 if ( $line =~ /(\d+\.\d+),(\w+),(.*),([\w|n\/a]+)\s*,(\w+),(\d+\.\d+),(.*),(\+|\-),(\d+\.\d+),(\d+)/ ) { 282 if ( $line =~ /(\d+\.\d+),(\w+),(.*),([\w|n\/a]+)\s*,(\w+),(\d+\.\d+),(.*),(\+|\-),(\d+\.\d+),(\d+)/ ) {
296 my @entry = ("$1","$2","$3","$4","$5","$6","$7","$8","$9","$10") ; 283 my @entry = ("$1","$2","$3","$4","$5","$6","$7","$8","$9","$10") ;
339 # push (@results, $result_by_entry{$_}) ; 326 # push (@results, $result_by_entry{$_}) ;
340 } 327 }
341 else {push (@results, [] ) ;} ; 328 else {push (@results, [] ) ;} ;
342 329
343 } 330 }
331
344 return(\@results) ; 332 return(\@results) ;
345 } 333 }
346 ## END of SUB 334 ## END of SUB
347 335
348 =head2 METHOD parse_hmdb_page_results 336 =head2 METHOD parse_hmdb_page_results
1028 sub write_csv_skel { 1016 sub write_csv_skel {
1029 ## Retrieve Values 1017 ## Retrieve Values
1030 my $self = shift ; 1018 my $self = shift ;
1031 my ( $csv_file, $rows ) = @_ ; 1019 my ( $csv_file, $rows ) = @_ ;
1032 1020
1033 my $ocsv = lib::csv::new( {is_binary => 1 , quote_binary => 0, quote_char => undef }) ; 1021 # my $ocsv = lib::csv::new( {is_binary => 1 , quote_binary => 0, quote_char => undef }) ;
1022 my $ocsv = lib::csv::new() ;
1034 my $csv = $ocsv->get_csv_object("\t") ; 1023 my $csv = $ocsv->get_csv_object("\t") ;
1035 $ocsv->write_csv_from_arrays($csv, $$csv_file, $rows) ; 1024 $ocsv->write_csv_from_arrays($csv, $$csv_file, $rows) ;
1036 1025
1037 return($csv_file) ; 1026 return($csv_file) ;
1038 } 1027 }
1051 ## Retrieve Values 1040 ## Retrieve Values
1052 my $self = shift ; 1041 my $self = shift ;
1053 my ( $masses, $ids, $results, $file, ) = @_ ; 1042 my ( $masses, $ids, $results, $file, ) = @_ ;
1054 1043
1055 open(CSV, '>:utf8', "$file") or die "Cant' create the file $file\n" ; 1044 open(CSV, '>:utf8', "$file") or die "Cant' create the file $file\n" ;
1056 print CSV "ID\tQuery(Da)\tDelta\tMetabolite_Name\tCpd_MW(Da)\tFormula\tAdduct\tAdduct_MW(Da)\tHMDB_ID\n" ; 1045 print CSV "ID\tQuery(Da)\tDelta(ppm)\tMetabolite_Name\tCpd_MW(Da)\tFormula\tAdduct\tAdduct_MW(Da)\tHMDB_ID\n" ;
1057 1046
1058 my $i = 0 ; 1047 my $i = 0 ;
1059 1048
1060 foreach my $id (@{$ids}) { 1049 foreach my $id (@{$ids}) {
1061 my $mass = undef ; 1050 my $mass = undef ;