Mercurial > repos > fgiacomoni > lipidmaps_textsearch
comparison lib/operations.pm @ 0:e8bd49794291 draft
Init repository with last lipidmaps_textsearch master version
author | fgiacomoni |
---|---|
date | Tue, 11 Apr 2017 03:47:06 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e8bd49794291 |
---|---|
1 package lib::operations ; | |
2 | |
3 use strict; | |
4 use warnings ; | |
5 use Exporter ; | |
6 use Carp ; | |
7 use Data::Dumper ; | |
8 | |
9 use vars qw($VERSION @ISA @EXPORT %EXPORT_TAGS); | |
10 | |
11 our $VERSION = "1.0"; | |
12 our @ISA = qw(Exporter); | |
13 our @EXPORT = qw( get_factorial truncate_num truncate_nums round_num round_nums manage_mode ); | |
14 our %EXPORT_TAGS = ( ALL => [qw( get_factorial truncate_num truncate_nums round_num round_nums manage_mode )] ); | |
15 | |
16 =head1 NAME | |
17 | |
18 My::operations - An example module | |
19 | |
20 =head1 SYNOPSIS | |
21 | |
22 use My::operations; | |
23 my $object = My::Module->new(); | |
24 print $object->as_string; | |
25 | |
26 =head1 DESCRIPTION | |
27 | |
28 This module clusters several more used maths functions like factorial... | |
29 | |
30 =head1 METHODS | |
31 | |
32 Methods are : | |
33 | |
34 =head2 METHOD new | |
35 | |
36 ## Description : new | |
37 ## Input : $self | |
38 ## Ouput : bless $self ; | |
39 ## Usage : new() ; | |
40 | |
41 =cut | |
42 | |
43 sub new { | |
44 ## Variables | |
45 my $self={}; | |
46 bless($self) ; | |
47 return $self ; | |
48 } | |
49 ### END of SUB | |
50 | |
51 =head2 METHOD get_factorial | |
52 | |
53 ## Description :permet de retourner la factorielle d'un nombre | |
54 ## Input : $indice | |
55 ## Output : $factorial | |
56 ## Usage : my ( var2 ) = get_factorial( var1 ) ; | |
57 | |
58 =cut | |
59 ## START of SUB | |
60 sub get_factorial { | |
61 ## Retrieve Values | |
62 my $self = shift ; | |
63 my ( $ind )= @_ ; # transmission des parametres | |
64 my ( $factorial, $indmun ) = ( 0, 0 ) ; | |
65 | |
66 if ( defined $ind ) { | |
67 | |
68 if ( $ind == 0 ) { | |
69 $factorial = 1 ; | |
70 } | |
71 else { | |
72 $indmun = $ind-1 ; | |
73 $factorial = &get_factorial ( $self, $indmun ) * $ind ; | |
74 } | |
75 } | |
76 else { | |
77 croak "Indice in \"get_factorial sub\" is undef\n" ; | |
78 } | |
79 | |
80 return ($factorial) ; # renvoi de la valeur | |
81 } | |
82 ## END of SUB | |
83 | |
84 =head2 METHOD manage_mode | |
85 | |
86 ## Description : manage mode and apply mass correction (positive/negative/neutral) | |
87 ## Input : $mode, $charge, $electron, $proton, $mass | |
88 ## Output : $exact_mass | |
89 ## Usage : my ( $exact_mass ) = manage_mode( $mode, $charge, $electron, $proton, $mass ) ; | |
90 | |
91 =cut | |
92 ## START of SUB | |
93 sub manage_mode { | |
94 ## Retrieve Values | |
95 my $self = shift ; | |
96 my ( $mode, $charge, $electron, $proton, $mass ) = @_ ; | |
97 my ($exact_mass, $tmp_mass) = ( undef, undef ) ; | |
98 | |
99 ## some explanations : | |
100 # MS in + mode = adds H+ (proton) and molecule is positive : el+ => $charge = "positive" | |
101 # For HR, need to subtrack proton mz and to add electron mz (1 electron per charge) to the input mass which comes neutral! | |
102 | |
103 if ( ( defined $$electron ) and ( defined $$proton ) ) { | |
104 # check mass | |
105 if ( defined $$mass ) { $tmp_mass = $$mass ; $tmp_mass =~ tr/,/./ ; } # manage . and , in case of... | |
106 else { warn "No mass is defined\n" } | |
107 | |
108 # manage charge | |
109 if ( ( !defined $$charge ) || ($$charge < 0) ){ warn "Charge is not defined or value is less than zero\n" ; } | |
110 | |
111 # set neutral mass in function of ms mode | |
112 if($$mode eq 'POS') { $exact_mass = ( $tmp_mass - $$proton + $$electron) * $$charge ; } | |
113 elsif($$mode eq 'NEG') { $exact_mass = ( $tmp_mass + $$proton - $$electron) * $$charge ; } | |
114 elsif($$mode eq "NEU") { $exact_mass = $tmp_mass ; } | |
115 else { warn "This mode doesn't exist : please select positive/negative or neutral mode\n" ; } | |
116 } | |
117 else { | |
118 warn "Missing some parameter values (electron, neutron masses), please check your conf file\n" ; | |
119 } | |
120 # print "$tmp_mass -> $exact_mass ($$mode) \n" ; | |
121 return(\$exact_mass) ; | |
122 } | |
123 ## END of SUB | |
124 | |
125 =head2 METHOD truncate_num | |
126 | |
127 ## Description : truncate a number by the sended decimal | |
128 ## Input : $number, $decimal | |
129 ## Output : $trunk_num | |
130 ## Usage : my ( $trunk_num ) = truncate_num( $number, $decimal ) ; | |
131 | |
132 =cut | |
133 ## START of SUB | |
134 sub truncate_num { | |
135 ## Retrieve Values | |
136 my $self = shift ; | |
137 my ( $number, $decimal ) = @_ ; | |
138 my $trunk_num = 0 ; | |
139 | |
140 if ( ( defined $decimal ) and ( $decimal > 0 ) and ( defined $number ) and ( $number > 0 ) ) { | |
141 $trunk_num = ($number =~ m/(\d+[\.|,]\d{$decimal})/); ## on utilise une tronquature seche 5.3 -> 5 et 5.8 -> 5 | |
142 if($number =~/^\-/) {$trunk_num = -$trunk_num ;} # For neg number | |
143 } | |
144 else { | |
145 croak "Can't trunk any number : missing value or decimal\n" ; | |
146 } | |
147 | |
148 return(\$trunk_num) ; | |
149 } | |
150 ## END of SUB | |
151 | |
152 =head2 METHOD truncate_nums | |
153 | |
154 ## Description : truncate a list of numbers by the sended decimal | |
155 ## Input : $numbers, $decimal | |
156 ## Output : $trunk_nums | |
157 ## Usage : my ( $trunk_nums ) = truncate_nums( $numbers, $decimal ) ; | |
158 | |
159 =cut | |
160 ## START of SUB | |
161 sub truncate_nums { | |
162 ## Retrieve Values | |
163 my $self = shift ; | |
164 my ( $numbers, $decimal ) = @_ ; | |
165 my @trunk_nums = () ; | |
166 | |
167 if ( ( defined $decimal ) and ( $decimal > 0 ) and ( defined $numbers ) and ( scalar(@{$numbers}) > 0 ) ) { | |
168 foreach my $nb ( @{$numbers} ) { | |
169 my ( $trunk_num ) = ( $nb =~ m/(\d+[\.|,]\d{$decimal})/ ); ## on utilise une tronquature seche 5.3 -> 5 et 5.8 -> 5 | |
170 if( $nb =~/^\-/ ) { $trunk_num = -$trunk_num ; } # For neg number | |
171 push ( @trunk_nums, $trunk_num ) ; | |
172 } | |
173 } | |
174 else { | |
175 croak "Can't trunk any number : missing values or decimal\n" ; | |
176 } | |
177 return( \@trunk_nums ) ; | |
178 } | |
179 ## END of SUB | |
180 | |
181 =head2 METHOD round_num | |
182 | |
183 ## Description : round a number by the sended decimal | |
184 ## Input : $number, $decimal | |
185 ## Output : $round_num | |
186 ## Usage : my ( $round_num ) = round_num( $number, $decimal ) ; | |
187 | |
188 =cut | |
189 ## START of SUB | |
190 sub round_num { | |
191 ## Retrieve Values | |
192 my $self = shift ; | |
193 my ( $number, $decimal ) = @_ ; | |
194 my $round_num = 0 ; | |
195 | |
196 if ( ( defined $decimal ) and ( $decimal > 0 ) and ( defined $number ) and ( $number > 0 ) ) { | |
197 $round_num = sprintf("%.".$decimal."f", $number); ## on utilise un arrondit : 5.3 -> 5 et 5.5 -> 6 | |
198 } | |
199 else { | |
200 croak "Can't round any number : missing value or decimal\n" ; | |
201 } | |
202 | |
203 return(\$round_num) ; | |
204 } | |
205 ## END of SUB | |
206 | |
207 =head2 METHOD round_nums | |
208 | |
209 ## Description : round a list of numbers by the sended decimal | |
210 ## Input : $numbers, $decimal | |
211 ## Output : $round_nums | |
212 ## Usage : my ( $round_nums ) = round_nums( $numbers, $decimal ) ; | |
213 | |
214 =cut | |
215 ## START of SUB | |
216 sub round_nums { | |
217 ## Retrieve Values | |
218 my $self = shift ; | |
219 my ( $numbers, $decimal ) = @_ ; | |
220 my @round_nums = () ; | |
221 | |
222 # print Dumper $numbers ; | |
223 | |
224 if ( ( defined $decimal ) and ( $decimal >= 0 ) and ( defined $numbers ) and ( scalar(@{$numbers}) > 0 ) ) { | |
225 foreach my $nb ( @{$numbers} ) { | |
226 if ( ( defined $nb ) ) { | |
227 if ($nb =~ /^\d+\.\d+$/ ) { ## check float | |
228 my $round_num = sprintf("%.".$decimal."f", $nb); ## on utilise un arrondit : 5.3 -> 5 et 5.5 -> 6 mais 5.25 -> 5.2 | |
229 push ( @round_nums, $round_num ) ; | |
230 } | |
231 else { | |
232 warn "This var $nb is not a float\n" ; | |
233 } | |
234 } | |
235 else { | |
236 croak "This number is not defined or is a string\n "; | |
237 } | |
238 | |
239 } | |
240 } | |
241 else { | |
242 croak "Can't round any numbers : missing values or decimal\n" ; | |
243 } | |
244 return( \@round_nums ) ; | |
245 } | |
246 ## END of SUB | |
247 | |
248 =head2 METHOD subtract_num | |
249 | |
250 ## Description : subtracting a number to an other | |
251 ## Input : $number, $number_to_subtr | |
252 ## Output : $value | |
253 ## Usage : my ( $value ) = subtract_num( $number, $number_to_subtr ) ; | |
254 | |
255 =cut | |
256 ## START of SUB | |
257 sub subtract_num { | |
258 ## Retrieve Values | |
259 my $self = shift ; | |
260 my ( $number, $number_to_subtr ) = @_ ; | |
261 my $value = 0 ; | |
262 | |
263 if ( ( defined $number ) and ( defined $number_to_subtr ) ) { | |
264 $value = ($number - $number_to_subtr) ; | |
265 } | |
266 else { | |
267 warn "The \n" ; | |
268 } | |
269 return(\$value) ; | |
270 } | |
271 ## END of SUB | |
272 | |
273 =head2 METHOD subtract_nums | |
274 | |
275 ## Description : subtracting a number to a list of numbers | |
276 ## Input : $numbers, $numbers_to_subtr | |
277 ## Output : $values | |
278 ## Usage : my ( $values ) = subtract_num( $numbers, $numbers_to_subtr ) ; | |
279 | |
280 =cut | |
281 ## START of SUB | |
282 sub subtract_nums { | |
283 ## Retrieve Values | |
284 my $self = shift ; | |
285 my ( $numbers, $number_to_subtr ) = @_ ; | |
286 my @values = () ; | |
287 | |
288 if ( ( defined $numbers ) and ( defined $number_to_subtr ) ) { | |
289 foreach my $num ( @{$numbers} ) { push ( @values, ( $num - $number_to_subtr ) ) ; } | |
290 } | |
291 return(\@values) ; | |
292 } | |
293 ## END of SUB | |
294 | |
295 1 ; | |
296 | |
297 | |
298 __END__ | |
299 | |
300 =head1 SUPPORT | |
301 | |
302 You can find documentation for this module with the perldoc command. | |
303 | |
304 perldoc operations.pm | |
305 | |
306 =head1 Exports | |
307 | |
308 =over 4 | |
309 | |
310 =item :ALL is get_factorial truncate_num truncate_nums round_num round_nums | |
311 | |
312 =back | |
313 | |
314 =head1 AUTHOR | |
315 | |
316 Franck Giacomoni E<lt>franck.giacomoni@clermont.inra.frE<gt> | |
317 | |
318 =head1 LICENSE | |
319 | |
320 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | |
321 | |
322 =head1 VERSION | |
323 | |
324 version 1 : 29 / 04 / 2013 | |
325 | |
326 version 2 : ?? | |
327 | |
328 =cut |