comparison lib/output.pm @ 0:e3d43b8c987b draft

Init repository with last tool-bank-golm-lib_search master version
author fgiacomoni
date Mon, 05 Dec 2016 08:32:04 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e3d43b8c987b
1 package lib::output ;
2
3 use strict;
4 use warnings ;
5 use Exporter ;
6 use Carp ;
7 use HTML::Template ;
8 use JSON ;
9
10 use FindBin ;
11 use lib $FindBin::Bin ;
12 my $binPath = $FindBin::Bin ;
13
14 use Data::Dumper ;
15
16 use vars qw($VERSION @ISA @EXPORT %EXPORT_TAGS);
17
18 our $VERSION = "1.0";
19 our @ISA = qw(Exporter);
20 our @EXPORT = qw(build_json_res_object excel_output write_html_body add_entries_to_tbody_object write_json_skel write_ajax_data_source excel_like_output);
21 our %EXPORT_TAGS = ( ALL => [qw(build_json_res_object excel_output write_html_body add_entries_to_tbody_object write_json_skel write_ajax_data_source excel_like_output)] );
22
23 =head1 NAME
24 My::Module - An example module
25
26 =head1 SYNOPSIS
27
28 use My::Module;
29 my $object = My::Module->new();
30 print $object->as_string;
31
32 =head1 DESCRIPTION
33
34 This module does not really exist, it
35 was made for the sole purpose of
36 demonstrating how POD works.
37
38 =head1 METHODS
39
40 Methods are :
41
42 =head2 METHOD new
43
44 ## Description : new
45 ## Input : $self
46 ## Ouput : bless $self ;
47 ## Usage : new() ;
48
49 =cut
50
51 sub new {
52 ## Variables
53 my $self={};
54 bless($self) ;
55 return $self ;
56 }
57 ### END of SUB
58
59
60
61 =head2 METHOD build_json_res_object
62
63 ## Description : build json from array of hits
64 ## Input : $results
65 ## Output : \@json_results
66 ## Usage : my ( \@json_results ) = build_json_res_object( $results ) ;
67 ## JSON structure: [
68 # {
69 # 'id' : 'int',
70 # 'nb_hits' : int,
71 # 'searchResults' : [
72 # {
73 # 'metaboliteID' : GUID
74 # 'distance_scores' : {
75 # 'EuclideanDistance' : float
76 # 'DotproductDistance' : int
77 # 'HammingDistance' : int
78 # 'JaccardDistance' : int
79 # 's12GowerLegendreDistance' : int
80 # }
81 # 'ri_infos' : {
82 # 'ri' : float
83 # 'riDiscrepancy' : float
84 # }
85 # 'analyte' : {
86 # 'id' : GUID
87 # 'name' : string
88 # }
89 # 'spectrum' : {
90 # 'id' : GUID
91 # 'name' : string
92 # }
93 # },
94 # .
95 # .
96 # .
97 # ]
98 # }
99 # .
100 # .
101 # .
102 # ]
103 =cut
104 ## START of SUB
105 sub build_json_res_object {
106 ## Retrieve Values
107 my $self = shift ;
108 my ( $results ) = @_ ;
109
110 my @json_results ;
111 my @array_results = @$results ;
112
113 my $nb_hits = 0 ;
114 my $i = 0 ;
115 my $spectrumID = 1 ;
116
117
118
119 ## Loop on each spectra
120 foreach my $res (@array_results) {
121
122 if (@$res[0] eq 'no results'){
123
124 my %hit_infos = () ;
125
126 $json_results[$i]{'id'} = $spectrumID++ ;
127 $json_results[$i]{'nb_hits'} = $nb_hits ;
128 $hit_infos{'metaboliteID'} = "" ;
129 $hit_infos{'distance_scores'}{'EuclideanDistance'} = "" ;
130 $hit_infos{'distance_scores'}{'DotproductDistance'} = "" ;
131 $hit_infos{'distance_scores'}{'HammingDistance'} = "" ;
132 $hit_infos{'distance_scores'}{'JaccardDistance'} = "" ;
133 $hit_infos{'distance_scores'}{'s12GowerLegendreDistance'} = "" ;
134 $hit_infos{'ri_infos'}{'ri'} = "" ;
135 $hit_infos{'ri_infos'}{'riDiscrepancy'} = "" ;
136 $hit_infos{'analyte'}{'id'} = "" ;
137 $hit_infos{'analyte'}{'name'} = "" ;
138 $hit_infos{'spectrum'}{'id'} = "" ;
139 $hit_infos{'spectrum'}{'name'} = "no results" ;
140
141 push ( @{ $json_results[$i]{'searchResults'} } , \%hit_infos );
142 }
143 else {
144
145 $nb_hits = scalar @$res;
146
147 $json_results[$i]{'id'} = $spectrumID++ ;
148 $json_results[$i]{'nb_hits'} = $nb_hits ;
149
150 ## Loop on each hit of a spectrum + build json
151 foreach my $href (@$res) {
152
153 if (!defined $href){
154
155 last ;
156 }
157 else {
158 my %hash_res = %$href ;
159 my %hit_infos = () ;
160 # Get rid of false results
161 if ($hash_res{'metaboliteID'} eq '00000000-0000-0000-0000-000000000000') {
162 --$json_results[$i]{'nb_hits'} ;
163 }
164 else {
165 $hit_infos{'metaboliteID'} = $hash_res{'metaboliteID'} ;
166 $hit_infos{'distance_scores'}{'EuclideanDistance'} = $hash_res{'EuclideanDistance'} ;
167 $hit_infos{'distance_scores'}{'DotproductDistance'} = $hash_res{'DotproductDistance'} ;
168 $hit_infos{'distance_scores'}{'HammingDistance'} = $hash_res{'HammingDistance'} ;
169 $hit_infos{'distance_scores'}{'JaccardDistance'} = $hash_res{'JaccardDistance'} ;
170 $hit_infos{'distance_scores'}{'s12GowerLegendreDistance'} = $hash_res{'s12GowerLegendreDistance'} ;
171 $hit_infos{'ri_infos'}{'ri'} = $hash_res{'ri'} ;
172 $hit_infos{'ri_infos'}{'riDiscrepancy'} = $hash_res{'riDiscrepancy'} ;
173 $hit_infos{'analyte'}{'id'} = $hash_res{'analyteID'} ;
174 $hit_infos{'analyte'}{'name'} = $hash_res{'analyteName'} ;
175 $hit_infos{'spectrum'}{'id'} = $hash_res{'spectrumID'} ;
176 $hit_infos{'spectrum'}{'name'} = $hash_res{'spectrumName'} ;
177
178 push ( @{ $json_results[$i]{'searchResults'} } , \%hit_infos );
179 }
180 }
181 }
182 }
183 $i++ ;
184 }
185 return \@json_results ;
186 }
187 ## END of SUB
188
189
190
191 =head2 METHOD add_entries_to_tbody_object
192
193 ## Description : initialize and build the entries object needed by HTML::Template
194 ## Input : $results
195 ## Output : \@tbody_entries
196 ## Usage : my ( $tbody_entries ) = add_entries_to_tbody_object( $results ) ;
197 =cut
198 ## START of SUB
199 sub add_entries_to_tbody_object {
200 ## Retrieve Values
201 my $self = shift ;
202 my ( $results ) = @_ ;
203
204 my @tbody_entries = () ;
205
206 foreach my $href_grp (@$results) {
207
208 foreach my $hit ( @{$href_grp->{'searchResults'}} ){
209 my %grp_res = () ;
210
211 ## Add hyperlinks
212 if ( $hit->{'spectrum'}{'name'} ne 'no results') {
213 %grp_res = (
214 ID => $href_grp->{id} ,
215 ANALYTE_NAME => $hit->{analyte}{name} ,
216 SPECTRUM_NAME => $hit->{spectrum}{name} ,
217 RI => $hit->{ri_infos}{ri} ,
218 RI_DISCREPANCY => $hit->{ri_infos}{riDiscrepancy} ,
219 DOT_PRODUCT_DISTANCE => $hit->{distance_scores}{DotproductDistance} ,
220 EUCLIDEAN_DISTANCE => $hit->{distance_scores}{EuclideanDistance} ,
221 JACCARD_DISTANCE => $hit->{distance_scores}{JaccardDistance} ,
222 HAMMING_DISTANCE => $hit->{distance_scores}{HammingDistance} ,
223 S12_GOWER_LEGENDRE_DISTANCE => $hit->{distance_scores}{s12GowerLegendreDistance} ,
224 SPECTRUM_ID => $hit->{spectrum}{id} ,
225 METABOLITE_ID => $hit->{metaboliteID} ,
226 ANALYTE_ID => $hit->{analyte}{id},
227 ANALYTE_REF => 'http://gmd.mpimp-golm.mpg.de/Analytes/'.$hit->{analyte}{id}.'.aspx',
228 SPECTRUM_REF => 'http://gmd.mpimp-golm.mpg.de/Spectrums/'.$hit->{spectrum}{id}.'.aspx',
229 METABOLITE_REF => 'http://gmd.mpimp-golm.mpg.de/Metabolites/'.$hit->{metaboliteID}.'.aspx',
230 ) ;
231 }
232 else {
233 %grp_res = (
234 ID => $href_grp->{id} ,
235 ANALYTE_NAME => $hit->{analyte}{name} ,
236 SPECTRUM_NAME => $hit->{spectrum}{name} ,
237 RI => $hit->{ri_infos}{ri} ,
238 RI_DISCREPANCY => $hit->{ri_infos}{riDiscrepancy} ,
239 DOT_PRODUCT_DISTANCE => $hit->{distance_scores}{DotproductDistance} ,
240 EUCLIDEAN_DISTANCE => $hit->{distance_scores}{EuclideanDistance} ,
241 JACCARD_DISTANCE => $hit->{distance_scores}{JaccardDistance} ,
242 HAMMING_DISTANCE => $hit->{distance_scores}{HammingDistance} ,
243 S12_GOWER_LEGENDRE_DISTANCE => $hit->{distance_scores}{s12GowerLegendreDistance} ,
244 SPECTRUM_ID => $hit->{spectrum}{id} ,
245 METABOLITE_ID => $hit->{metaboliteID} ,
246 ANALYTE_ID => $hit->{analyte}{id},
247 ANALYTE_REF => 'http://gmd.mpimp-golm.mpg.de/',
248 SPECTRUM_REF => 'http://gmd.mpimp-golm.mpg.de/',
249 METABOLITE_REF => 'http://gmd.mpimp-golm.mpg.de/',
250 ) ;
251 }
252 push (@tbody_entries , \%grp_res) ;
253 }
254 }
255 return \@tbody_entries ;
256 }
257 ## END of SUB
258
259
260
261
262
263
264 =head2 METHOD write_html_body
265
266 ## Description : Write the html output file
267 ## Input : $results, $tbody_entries, $html_file, $html_template
268 ## Output : $html_file
269 ## Usage : $o_output->write_html_body( $results, $tbody_entries, $html_file, $html_template ) ;
270 =cut
271 ## START of SUB
272 sub write_html_body {
273 ## Retrieve Values
274 my $self = shift ;
275 my ( $results, $tbody_entries, $html_file_name, $html_template, $default_entries, $jsons_obj ) = @_ ;
276
277 if (defined $html_file_name){
278
279 open (HTML, '>', $html_file_name) or die "Failed to open filehandle: $!" ;
280
281 if (-e $html_template) {
282
283 my $ohtml = HTML::Template->new(filename => $html_template) ;
284 # $ohtml->param( DATA => $jsons_obj ) ;
285 $ohtml->param( GROUPS => $tbody_entries ) ;
286 $ohtml->param( DEFAULT_ENTRIES => $default_entries ) ;
287 print HTML $ohtml->output ;
288 }
289 else {
290 croak "Problem about your html template: no html template available\n" ;
291 }
292 close (HTML) ;
293 }
294 else {
295 croak "Problem with the html output file: $html_file_name is not defined\n" ;
296 }
297 }
298 ## END of SUB
299
300
301
302 =head2 METHOD excel_output
303
304 ## Description : create an excel XLS output of the results
305 ## Input : $jsons, $excel_file
306 ## Output : excel file
307 ## Usage : $o_output->excel_output( $jsons, $excel_file ) ;
308 =cut
309 ## START of SUB
310 #sub excel_output {
311 # ## Retrieve Values
312 # my $self = shift ;
313 # my ( $excel_file, $jsons ) = @_ ;
314 #
315 # # Create a new workbook and add a worksheet
316 # my $workbook = Excel::Writer::XLSX->new( $excel_file ) ;
317 # my $worksheet = $workbook->add_worksheet() ;
318 #
319 # my $i = 0 ;
320 #
321 # # Create a format for the headings
322 # my $format = $workbook->add_format() ;
323 # $format->set_bold() ;
324 #
325 # $worksheet->write( $i, 0, "Num Spectre" , $format);
326 # $worksheet->write( $i, 1, "Analyte Name" , $format);
327 # $worksheet->write( $i, 2, "Spectrum Name" , $format);
328 # $worksheet->write( $i, 3, "Retention Index" , $format);
329 # $worksheet->write( $i, 4, "RI Discrepancy" , $format);
330 # $worksheet->write( $i, 5, "DotproductDistance" , $format);
331 # $worksheet->write( $i, 6, "EuclideanDistance" , $format);
332 # $worksheet->write( $i, 7, "JaccardDistance" , $format);
333 # $worksheet->write( $i, 8, "HammingDistance" , $format);
334 # $worksheet->write( $i, 9, "s12GowerLegendreDistance" , $format);
335 # $worksheet->write( $i, 10, "Spectrum ID" , $format);
336 # $worksheet->write( $i, 11, "Metabolite ID" , $format);
337 # $worksheet->write( $i, 12, "Analyte ID" , $format);
338 # $i++;
339 #
340 # foreach my $href_grp (@$jsons) {
341 #
342 # foreach my $hit ( @{$href_grp->{'searchResults'}} ){
343 #
344 # $worksheet->write( $i, 0, $href_grp->{id} );
345 # $worksheet->write( $i, 1, $hit->{analyte}{name} );
346 # $worksheet->write( $i, 2, $hit->{spectrum}{name} );
347 # $worksheet->write( $i, 3, $hit->{ri_infos}{ri} );
348 # $worksheet->write( $i, 4, $hit->{ri_infos}{riDiscrepancy} );
349 # $worksheet->write( $i, 5, $hit->{distance_scores}{DotproductDistance} );
350 # $worksheet->write( $i, 6, $hit->{distance_scores}{EuclideanDistance} );
351 # $worksheet->write( $i, 7, $hit->{distance_scores}{JaccardDistance} );
352 # $worksheet->write( $i, 8, $hit->{distance_scores}{HammingDistance} );
353 # $worksheet->write( $i, 9, $hit->{distance_scores}{s12GowerLegendreDistance} );
354 # $worksheet->write( $i, 10, $hit->{spectrum}{id} );
355 # $worksheet->write( $i, 11, $hit->{metaboliteID} );
356 # $worksheet->write( $i, 12, $hit->{analyte}{id});
357 # $i++;
358 # }
359 # }
360 #
361 # $workbook->close();
362 #}
363 ## END of SUB
364
365
366
367
368 =head2 METHOD excel_like_output
369
370 ## Description : create an excel like output of the results
371 ## Input : $jsons, $excel_file
372 ## Output : excel file
373 ## Usage : $o_output->excel_like_output( $jsons, $excel_file ) ;
374 =cut
375 ## START of SUB
376 sub excel_like_output {
377 ## Retrieve Values
378 my $self = shift ;
379 my ( $excel_file, $jsons ) = @_ ;
380
381 open (OUT , ">" , $excel_file) or die "Error at opening file $excel_file" ;
382
383 print OUT "Num Spectre\tAnalyte Name\tSpectrum Name\tRetention Index\tRI Discrepancy\tDotproductDistance\tEuclideanDistance\tJaccardDistance\tHammingDistance\ts12GowerLegendreDistance\tSpectrum ID\tMetabolite ID\tAnalyte ID\n" ;
384
385 foreach my $href_grp (@$jsons) {
386
387 foreach my $hit ( @{$href_grp->{'searchResults'}} ){
388
389 print OUT "$href_grp->{id}\t$hit->{analyte}{name}\t$hit->{spectrum}{name}\t$hit->{ri_infos}{ri}\t$hit->{ri_infos}{riDiscrepancy}\t$hit->{distance_scores}{DotproductDistance}\t$hit->{distance_scores}{EuclideanDistance}\t$hit->{distance_scores}{JaccardDistance}\t$hit->{distance_scores}{HammingDistance}\t$hit->{distance_scores}{s12GowerLegendreDistance}\t$hit->{spectrum}{id}\t$hit->{metaboliteID}\t$hit->{analyte}{id}\n";
390 }
391 }
392 close (OUT) ;
393
394 }
395 ## END of SUB
396
397
398 =head2 METHOD write_json_skel
399
400 ## Description : prepare and write json output file
401 ## Input : $json_file, $scalar
402 ## Output : json file
403 ## Usage : $o_output->write_json_skel( $csv_file, $scalar ) ;
404
405 =cut
406 ## START of SUB
407 sub write_json_skel {
408 ## Retrieve Values
409 my $self = shift ;
410 my ( $json_file, $json_obj ) = @_ ;
411
412 my $utf8_encoded_json_text = encode_json $json_obj ;
413 open(JSON, '>:utf8', $$json_file) or die "Can't create the file $$json_file\n" ;
414 print JSON $utf8_encoded_json_text ;
415 close(JSON) ;
416
417 }
418 ## END of SUB
419
420
421
422 =head2 METHOD write_csv
423
424 ## Description : write csv output file
425 ## Input : $xlsx_file, $csv_file
426 ## Output : csv file
427 ## Usage : $o_output->write_csv( $xlsx_file, $csv_file ) ;
428
429 =cut
430 ## START of SUB
431 sub write_csv {
432 ## Retrieve Values
433 my $self = shift ;
434 my ( $csv_file, $jsons ) = @_ ;
435
436 open (CSV , ">" , $csv_file) or die "Can't create the file $csv_file\n" ;
437
438 print CSV "\"Num Spectre\"\t\"Analyte Name\"\t\"Spectrum Name\"\t\"Retention Index\"\t\"RI Discrepancy\"\t\"DotproductDistance\"\t\"EuclideanDistance\"\t\"JaccardDistance\"\t\"HammingDistance\"\t\"s12GowerLegendreDistance\"\t\"Spectrum ID\"\t\"Metabolite ID\"\t\"Analyte ID\"\n" ;
439
440 foreach my $href_grp (@$jsons) {
441
442 foreach my $hit ( @{$href_grp->{'searchResults'}} ){
443
444 print CSV "\"$href_grp->{id}\"\t\"$hit->{analyte}{name}\"\t\"$hit->{spectrum}{name}\"\t\"$hit->{ri_infos}{ri}\"\t\"$hit->{ri_infos}{riDiscrepancy}\"\t\"$hit->{distance_scores}{DotproductDistance}\"\t\"$hit->{distance_scores}{EuclideanDistance}\"\t\"$hit->{distance_scores}{JaccardDistance}\"\t\"$hit->{distance_scores}{HammingDistance}\"\t\"$hit->{distance_scores}{s12GowerLegendreDistance}\"\t\"$hit->{spectrum}{id}\"\t\"$hit->{metaboliteID}\"\t\"$hit->{analyte}{id}\"\n" ;
445 }
446 }
447 close(CSV) ;
448
449 }
450 ## END of SUB
451
452
453
454
455 =head2 METHOD write_ajax_data_source
456
457 ## Description : write csv output file
458 ## Input : $jsons_obj
459 ## Output :
460 ## Usage : $o_output->write_ajax_data_source( $jsons_obj ) ;
461
462 =cut
463 ## START of SUB
464 sub write_ajax_data_source {
465 ## Retrieve Values
466 my $self = shift ;
467 my ( $jsons_obj ) = @_ ;
468
469 my %ajax = () ;
470 my $i = 0 ;
471
472 #open (AJAX,">ajax.txt") or die "ERROR at opening file" ;
473
474 foreach my $href_grp (@$jsons_obj) {
475
476 foreach my $hit ( @{$href_grp->{'searchResults'}} ){
477
478 push (@{$ajax{ 'data' }[$i]} , $href_grp->{id}) ;
479 push (@{$ajax{ 'data' }[$i]} , $hit->{spectrum}{name}) ;
480 push (@{$ajax{ 'data' }[$i]} , $hit->{analyte}{name}) ;
481 push (@{$ajax{ 'data' }[$i]} , $hit->{ri_infos}{ri}) ;
482 push (@{$ajax{ 'data' }[$i]} , $hit->{ri_infos}{riDiscrepancy}) ;
483 push (@{$ajax{ 'data' }[$i]} , $hit->{distance_scores}{DotproductDistance}) ;
484 push (@{$ajax{ 'data' }[$i]} , $hit->{distance_scores}{EuclideanDistance}) ;
485 push (@{$ajax{ 'data' }[$i]} , $hit->{distance_scores}{JaccardDistance}) ;
486 push (@{$ajax{ 'data' }[$i]} , $hit->{distance_scores}{HammingDistance}) ;
487 push (@{$ajax{ 'data' }[$i]} , $hit->{distance_scores}{s12GowerLegendreDistance}) ;
488 push (@{$ajax{ 'data' }[$i]} , $hit->{spectrum}{id}) ;
489 push (@{$ajax{ 'data' }[$i]} , $hit->{metaboliteID}) ;
490 push (@{$ajax{ 'data' }[$i]} , $hit->{analyte}{ID}) ;
491 $i++ ;
492 }
493 }
494
495 my $ajax = encode_json \%ajax ;
496 return $ajax ;
497 #print AJAX $ajax ;
498
499 }
500 #END of SUB
501
502
503
504 1 ;
505
506
507 __END__
508
509 =head1 SUPPORT
510
511 You can find documentation for this module with the perldoc command.
512
513 perldoc csv.pm
514
515 =head1 Exports
516
517 =over 4
518
519 =item :ALL is get_spectra
520
521 =back
522
523 =head1 AUTHOR
524
525 Gabriel Cretin E<lt>gabriel.cretin@clermont.inra.frE<gt>
526 Franck Giacomoni E<lt>franck.giacomoni@clermont.inra.frE<gt>
527
528 =head1 LICENSE
529
530 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
531
532 =head1 VERSION
533
534 version 1 : ??
535
536 =cut