comparison hr2_manager.pl @ 0:86296c048e46 draft

Init repository for [hr2]
author fgiacomoni
date Wed, 05 Jun 2019 09:40:20 -0400
parents
children e2cbcf6fa22e
comparison
equal deleted inserted replaced
-1:000000000000 0:86296c048e46
1 #!perl
2
3 ## script : hr2_manager.pl
4 ## VERSIONS :
5 # - 01/03/2019 : Fix P issue, permit more flexible atom settings and add requirements for conda auto managing.
6
7 #=============================================================================
8 # Included modules and versions
9 #=============================================================================
10 ## Perl modules
11 use strict ;
12 use warnings ;
13 use Carp qw (cluck croak carp) ;
14
15 use Data::Dumper ;
16 use Getopt::Long ;
17 use POSIX ;
18 use FindBin ; ## Allows you to locate the directory of original perl script
19
20 ## Dedicate Perl Modules (Home made...)
21 use lib $FindBin::Bin ;
22 my $binPath = $FindBin::Bin ;
23 use lib::hr qw( :ALL ) ;
24 ## PFEM Perl Modules
25 use lib::conf qw( :ALL ) ;
26 use lib::csv qw( :ALL ) ;
27
28 ## Initialized values
29 use vars qw(%parametre);
30 my $help = undef ;
31 my ( $input_file, $line_header, $col_id, $col_mass ) = ( undef, undef, undef, undef ) ; # manage input option file of masses
32 my ( $mass ) = ( undef ) ; # manage input option masses list
33 my ( $tolerance, $mode, $charge, $has_golden_rules, $atomes, $atomes_basic ) = ( undef, undef, undef, undef, undef, undef ) ; # manage params
34 my ( $output_csv, $output_html ) = ( undef, undef) ; # manage ouputs
35 my $verbose = 2 ;
36
37 #=============================================================================
38 # Manage EXCEPTIONS
39 #=============================================================================
40 &GetOptions ( "h" => \$help, # HELP
41 "input:s" => \$input_file,
42 "colId:i" => \$col_id,
43 "nbHeader:i" => \$line_header,
44 "colmass:i" => \$col_mass,
45 "masse:s" => \$mass,
46 "tolerance:f" => \$tolerance,
47 "mode:s" => \$mode,
48 "charge:i" => \$charge,
49 "regleOr:s" => \$has_golden_rules,
50 "atomes:s" => \$atomes_basic, # [basic atoms like CNOHP]
51 "atomessup:s" => \$atomes,
52 "output1:s" => \$output_csv,
53 "outputView:s" => \$output_html,
54 "verbose:i" => \$verbose,
55 ) ;
56
57 #=============================================================================
58 # EXCEPTIONS
59 #=============================================================================
60 $help and &help ;
61
62 #=============================================================================
63 # MAIN SCRIPT
64 #=============================================================================
65 my %atomsConf = () ;
66 $atomsConf{'C'} = {'min' => 0, 'max' => 0} ;
67 $atomsConf{ 'H'} = {'min' => 0, 'max' => 0} ;
68 $atomsConf{ 'N'} = {'min' => 0, 'max' => 0} ;
69 $atomsConf{ 'O'} = {'min' => 0, 'max' => 0} ;
70 $atomsConf{ 'P'} = {'min' => 0, 'max' => 0} ;
71 $atomsConf{ 'S'} = {'min' => 0, 'max' => 0} ;
72 $atomsConf{ 'F'} = {'min' => 0, 'max' => 0} ;
73 $atomsConf{ 'L'} = {'min' => 0, 'max' => 0} ;
74 $atomsConf{ 'K'} = {'min' => 0, 'max' => 0} ;
75 $atomsConf{ 'B'} = {'min' => 0, 'max' => 0} ;
76 $atomsConf{ 'A'} = {'min' => 0, 'max' => 0} ;
77 $atomsConf{ '1'} = {'min' => 0, 'max' => 0} ;
78
79 ## -------------- Conf file and verbose ------------------------ :
80 my ( $CONF ) = ( undef ) ; ## verbose level is 3 for debugg
81 my $time_start = time ;
82
83 foreach my $conf ( <$binPath/*.cfg> ) {
84 my $oConf = lib::conf::new() ;
85 $CONF = $oConf->as_conf($conf) ;
86 }
87 ## --------------- Global parameters ---------------- :
88 my ( $ids, $masses, $hr_cmds, $results, $parsed_results ) = ( undef, undef, undef, undef, undef ) ;
89 my $complete_rows = undef ;
90 my ($hr_atoms_list, $hr_atoms_and_ranges, $set_tol, ) = (undef, undef, undef, ) ;
91
92 ## Check and manage params
93 my $ohr = lib::hr->new() ;
94
95 ## set tolerance
96 $set_tol = $ohr->manage_tolerance( \$tolerance, \$CONF->{'tolerance'} ) ;
97
98 ## check HR exe envt :
99 my $hr_check = $ohr->check_hr_exe(\$CONF->{'HR2_EXE'}, \$CONF->{'HR2_VERSION'}) ;
100 if (!defined $hr_check ) { croak "No hr exe available (wrong path) or wrong version will be used -- end of script\n" ; }
101
102 ## manage atoms and their ranges
103 #$hr_atoms_list = $ohr->manage_atoms(\$atomes, \$CONF->{'DEFAULT_ATOMS'}) ; ## DEPRECATED
104
105 # manage atoms and their ranges with a hash structure
106 my $atomsCurrentConf = $ohr->manage_atoms_and_ranges(\%atomsConf, $CONF, $atomes_basic, $atomes) ;
107
108 #if ( defined $hr_atoms_list ) {
109 # ## implements range foreach atom
110 # foreach my $atom ( (split(",", $$hr_atoms_list )) ) {
111 # my $range_max = $CONF->{'DEFAULT_MAX'} ; # manage max value in case of
112 # if ( exists $CONF->{$atom} ) { $range_max = $CONF->{$atom} ; }
113 # my $ref_range = $ohr->manage_atom_and_range(\$atom, \$CONF->{'DEFAULT_MIN'}, \$range_max ) ;
114 # $hr_atoms_and_ranges .= $$ref_range ; ## concat ranges
115 # }
116 #}
117 #else { croak "No atom detected with input params\n" ; }
118
119 ## Parsing input file with masses/ids or unik mass :
120 ## manage only one mass
121 if ( ( defined $mass ) and ( $mass ne "" ) and ( $mass > 0 ) ) {
122 $ids = ['mass_01'] ;
123 $masses = [$mass] ;
124
125 } ## END IF
126 ## manage csv file containing list of masses
127 elsif ( ( defined $input_file ) and ( $input_file ne "" ) and ( -e $input_file ) ) {
128
129 ## parse all csv for later : output csv build
130 my $ocsv_input = lib::csv->new() ;
131 my $complete_csv = $ocsv_input->get_csv_object( "\t" ) ;
132 $complete_rows = $ocsv_input->parse_csv_object($complete_csv, \$input_file) ;
133
134 ## parse csv ids and masses
135 my $is_header = undef ;
136 my $ocsv = lib::csv->new() ;
137 my $csv = $ocsv->get_csv_object( "\t" ) ;
138 if ( ( defined $line_header ) and ( $line_header > 0 ) ) { $is_header = 'yes' ; } else{ $is_header = 'no' ; }
139 $masses = $ocsv->get_value_from_csv_multi_header( $csv, $input_file, $col_mass, $is_header, $line_header ) ; ## retrieve mz values on csv
140 $ids = $ocsv->get_value_from_csv_multi_header( $csv, $input_file, $col_id, $is_header, $line_header ) ; ## retrieve ids values on csv
141
142 }
143 else {
144 croak "Can't work with HR2 : missing input file or mass (list of masses, ids)\n" ;
145 } ## end ELSE
146
147 ## check using golden rules
148 if ( $has_golden_rules eq 'NO') { $has_golden_rules = undef ; }
149
150 ## ---------------- launch queries -------------------- :
151
152 ## prepare cmd
153 foreach my $mz (@{ $masses }) {
154 ## computes mass
155 my $ohr_mode = lib::hr->new() ;
156 my ( $exact_mass ) = $ohr_mode->manage_mode( \$mode, \$charge, \$CONF->{'electron'}, \$CONF->{'proton'}, \$mz ) ;
157 print "Current MZ send to HR\n" if $verbose == 3 ;
158 print Dumper $exact_mass if $verbose == 3 ;
159 ## build exe line
160 my $ohr_exe = lib::hr->new() ;
161 my $hr_cmd = $ohr_exe->config_hr_exe( \$CONF->{'HR2_EXE'}, \$tolerance, $exact_mass, \$has_golden_rules, \$atomsCurrentConf ) ;
162 print "$hr_cmd\n" if $verbose == 3 ;
163 push(@{$hr_cmds}, $$hr_cmd) ;
164 }
165
166 ## MultiThreading execution of Hr :
167 my $threads = lib::hr->new() ;
168 my $hr_object = lib::hr->new() ;
169 if ( $hr_object->can('hr_exe') ) {
170 my $method = $hr_object->can('hr_exe') ;
171 $results = $threads->threading_hr_exe( $method, $hr_cmds) ;
172
173 }
174
175 ## MultiThreading parsing of Hr outputs :
176 my $hrres_object = lib::hr->new() ;
177 if ( $hrres_object->can('hr_out_parser') ) {
178 my $method = $hr_object->can('hr_out_parser') ;
179 if ( defined $results ) { $parsed_results = $threads->threading_hr_exe( $method, $results ) ; }
180 }
181
182 ## -------------- Produce HTML/CSV output ------------------ :
183 my $search_condition = 'Mode used: '.$mode.' / Charge: +'.$charge.' / Mass tolerance: '.$$set_tol.' / Composition: '.$atomsCurrentConf ;
184 ## Uses N mz and theirs entries per page (see config file).
185 # how many pages you need with your input mz list?
186 my $nb_pages_for_html_out = ceil( scalar(@{$masses} ) / $CONF->{HTML_ENTRIES_PER_PAGE} ) ;
187
188 if ( ( defined $output_html ) and ( defined $parsed_results ) ) {
189 my $oHtml = lib::hr::new() ;
190 my ($tbody_object) = $oHtml->set_html_tbody_object( $nb_pages_for_html_out, $CONF->{HTML_ENTRIES_PER_PAGE} ) ;
191 ($tbody_object) = $oHtml->add_mz_to_tbody_object($tbody_object, $CONF->{HTML_ENTRIES_PER_PAGE}, $masses, $ids, $parsed_results ) ;
192 ($tbody_object) = $oHtml->add_entries_to_tbody_object($tbody_object, $parsed_results) ;
193 my $html_file = $binPath.'/'.$CONF->{'HTML_TEMPLATE'} ;
194 my $output = $oHtml->write_html_skel(\$output_html, $tbody_object, $nb_pages_for_html_out, $search_condition, $html_file, $CONF->{'JS_GALAXY_PATH'}, $CONF->{'CSS_GALAXY_PATH'}) ;
195
196 } ## END IF
197 else {
198 croak "Can't create a HTML output for HMDB : no result found or your output file is not defined\n" ;
199 }
200
201 if ( ( defined $output_csv ) and ( defined $parsed_results ) ) {
202 # produce a csv based on METLIN format
203 my $ocsv = lib::hr::new() ;
204 if (defined $input_file) {
205 my $lm_matrix = undef ;
206 if ( ( defined $line_header ) and ( $line_header == 1 ) ) { $lm_matrix = $ocsv->set_hr_matrix_object('hr2', $masses, $parsed_results ) ; }
207 elsif ( ( defined $line_header ) and ( $line_header == 0 ) ) { $lm_matrix = $ocsv->set_hr_matrix_object(undef, $masses, $parsed_results ) ; }
208 $lm_matrix = $ocsv->add_hr_matrix_to_input_matrix($complete_rows, $lm_matrix) ;
209 $ocsv->write_csv_skel(\$output_csv, $lm_matrix) ;
210 }
211 elsif (defined $mass) {
212 $ocsv->write_csv_one_mass($masses, $ids, $parsed_results, $output_csv) ;
213 }
214 } ## END IF
215 else {
216 # croak "Can't create a tabular output for HR2 : no result found or your output file is not defined\n" ;
217 }
218
219
220
221 ### VERBOSE OUTPUTs
222 if ( $verbose == 3 ) {
223 print "-- Conf file contains :\n" ;
224 print Dumper $CONF ;
225 print "-- Atoms input list :\n" ;
226 print Dumper $atomes_basic ;
227 print "-- Suppl. atoms input list :\n" ;
228 print Dumper $atomes ;
229 print "-- HR envt ready :\n" ;
230 print Dumper $hr_check ;
231 print "-- Atoms and ranges :\n" ;
232 print Dumper $atomsCurrentConf ;
233 print "-- Tolerance :\n" ;
234 print Dumper $set_tol ;
235 print "-- Complete input file :\n" ;
236 print Dumper $complete_rows ;
237 print "-- Inputs initiales masses :\n" ;
238 print Dumper $masses ;
239 print "-- Inputs initiales ids :\n" ;
240 print Dumper $ids ;
241 print "-- Hr_Cmds :\n" ;
242 print Dumper $hr_cmds ;
243 print "-- Hr_Results :\n" ;
244 # print Dumper $results ;
245 print "-- Hr_parsed Results :\n" ;
246 print Dumper $parsed_results ;
247
248 my $nb_results = scalar (@{$results}) ;
249 print "-- Hr_Results return : $nb_results\n" ;
250 }
251
252 my $time_end = time ;
253 my $seconds = $time_end-$time_start ;
254 print "\n------ Time used in threaded mode by 6 : $seconds seconds --------\n\n" ;
255
256
257
258
259
260
261 #====================================================================================
262 # Help subroutine called with -h option
263 # number of arguments : 0
264 # Argument(s) :
265 # Return : 1
266 #====================================================================================
267 sub help {
268 print STDERR "
269 hr2_manager.pl
270
271 # hr2_manager is a script to elucide chemical formula by their accurate masses. The HiRes program is integrate in this package
272 # Input : a accurate mass or a file of masses
273 # Author : Franck Giacomoni and Marion Landi
274 # Email : fgiacomoni\@clermont.inra.fr or mlandi\@clermont.inra.fr
275 # Version : 1.1
276 # Created : 01/12/2011
277 # Last Update : 09032014
278 USAGE :
279 hr2_manager.pl -h or
280 hr2_manager.pl -input [cvs file of masses] -colId [int] -colmass [int] -nbHeader [int] -tolerance [float] -mode [positive, neutral or negative] -charge [int] -regleOr [yes or no] -atome [P, S, F, Cl, K, B, A, 1 ] -output1 [csv file] -outputView [html file]
281 hr2_manager.pl -masse [double] -tolerance [float] -mode [positive, neutral or negative] -charge [int] -regleOr [yes or no] -atome [P, S, F, Cl, K, B, A, 1 ] -output1 [csv file] -outputView [html file]
282 " ;
283 exit(1);
284 }
285
286 ## END of script - F Giacomoni
287
288 __END__
289
290 =head1 NAME
291
292 hr2_manager.pl -- script for launch / manage hr2 binary
293
294 =head1 USAGE
295
296 hr2_manager.pl -h or
297 hr2_manager.pl -input [cvs file of masses] -colId [int] -colmass [int] -nbHeader [int] -tolerance [float] -mode [positive, neutral or negative] -charge [int] -regleOr [yes or no] -atome [P, S, F, Cl, K, B, A, 1 ] -output1 [csv file] -outputView [html file]
298 hr2_manager.pl -masse [double] -tolerance [float] -mode [positive, neutral or negative] -charge [int] -regleOr [yes or no] -atome [P, S, F, Cl, K, B, A, 1 ] -output1 [csv file] -outputView [html file]
299
300 =head1 SYNOPSIS
301
302 This script manages hr2 binary which elucids raw formula with exact masses.
303
304 =head1 DESCRIPTION
305
306 This main program is a module to elucidate chemical formula with HiRes program. Source is available on Fiehn lab web.
307
308 =over 4
309
310 =item B<function01>
311
312 =item B<function02>
313
314 =back
315
316 =head1 AUTHOR
317
318 Franck Giacomoni E<lt>franck.giacomoni@clermont.inra.frE<gt>
319 Marion Landi E<lt>marion.landi@clermont.inra.frE<gt>
320
321 =head1 LICENSE
322
323 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
324
325 =head1 VERSION
326
327 version 1 : 18/07/2012
328
329 version 2 : 02/10/2013
330
331 version 3 : 20/02/2014
332
333 version 4 : 01/03/2019
334
335 =cut