1 package Maasha::Biopieces;
4 # Copyright (C) 2007-2009 Martin A. Hansen.
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # http://www.gnu.org/copyleft/gpl.html
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26 # Routines for manipulation, parsing and emitting of human/machine readable biopieces records.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
32 use Getopt::Long qw( :config bundling );
37 use vars qw( @ISA @EXPORT_OK );
41 @ISA = qw( Exporter );
51 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SIGNAL HANDLER <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
54 $SIG{ '__DIE__' } = \&sig_handler;
55 $SIG{ 'INT' } = \&sig_handler;
56 $SIG{ 'TERM' } = \&sig_handler;
61 my ( $time_stamp, $script, $user, $pid, $file, $fh );
63 $time_stamp = Maasha::Common::time_stamp();
64 $user = Maasha::Common::get_user();
65 $script = Maasha::Common::get_scriptname();
66 $pid = Maasha::Common::get_processid();
68 $file = "$ENV{ 'BP_TMP' }/" . join( ".", $user, $script, $pid ) . ".status";
69 $fh = Maasha::Filesys::file_write_open( $file );
71 print $fh join( ";", $time_stamp, join( " ", @ARGV ) ) . "\n";
79 # Martin A. Hansen, June 2009.
81 # Retrieves initial status information written with status_set and uses this
82 # to write a status entry to the log file.
84 my ( $status, # status - OPTIONAL
89 my ( $time0, $time1, $script, $user, $pid, $file, $fh, $elap, $fh_global, $fh_local, $line, $args, $tmp_dir );
93 $time1 = Maasha::Common::time_stamp();
94 $user = Maasha::Common::get_user();
95 $script = Maasha::Common::get_scriptname();
96 $pid = Maasha::Common::get_processid();
98 $file = "$ENV{ 'BP_TMP' }/" . join( ".", $user, $script, $pid ) . ".status";
100 return if not -f $file;
102 $fh = Maasha::Filesys::file_read_open( $file );
109 ( $time0, $args, $tmp_dir ) = split /;/, $line;
111 Maasha::Filesys::dir_remove( $tmp_dir ) if defined $tmp_dir;
113 $elap = Maasha::Common::time_stamp_diff( $time0, $time1 );
115 $fh_global = Maasha::Filesys::file_append_open( "$ENV{ 'BP_LOG' }/biopieces.log" );
116 $fh_local = Maasha::Filesys::file_append_open( "$ENV{ 'HOME' }/.biopieces.log" );
118 print $fh_global join( "\t", $time0, $time1, $elap, $user, $status, "$script $args" ) . "\n";
119 print $fh_local join( "\t", $time0, $time1, $elap, $user, $status, "$script $args" ) . "\n";
121 $fh_global->autoflush( 1 );
122 $fh_local->autoflush( 1 );
131 # Martin A. Hansen, January 2008.
133 # Log messages to logfile.
137 my ( $time_stamp, $user, $script, $fh_global, $fh_local );
139 $time_stamp = Maasha::Common::time_stamp();
140 $user = Maasha::Common::get_user();
141 $script = Maasha::Common::get_scriptname();
143 $fh_global = Maasha::Filesys::file_append_open( "$ENV{ 'BP_LOG' }/biopieces.log" );
144 $fh_local = Maasha::Filesys::file_append_open( "$ENV{ 'HOME' }/.biopieces.log" );
146 print $fh_global "$time_stamp\t$user\t$script ", join( " ", @ARGV ), "\n";
147 print $fh_local "$time_stamp\t$user\t$script ", join( " ", @ARGV ), "\n";
149 $fh_global->autoflush( 1 );
150 $fh_local->autoflush( 1 );
159 # Martin A. Hansen, July 2007.
161 # Opens a stream to STDIN or a file,
163 my ( $file, # file - OPTIONAL
166 # Returns filehandle.
170 if ( not -t STDIN ) {
171 $fh = Maasha::Filesys::stdin_read();
172 } elsif ( not $file ) {
173 # Maasha::Common::error( qq(no data stream) );
175 $fh = Maasha::Filesys::file_read_open( $file );
184 # Martin A. Hansen, August 2007.
186 # Opens a stream to STDOUT or a file.
188 my ( $path, # path - OPTIONAL
189 $gzip, # compress data - OPTIONAL
192 # Returns filehandle.
197 $fh = Maasha::Filesys::file_write_open( $path, $gzip );
199 $fh = Maasha::Filesys::stdout_write();
208 # Martin A. Hansen, May 2009.
210 # Close stream if open.
212 my ( $fh, # filehandle
217 close $fh if defined $fh;
223 # Martin A. Hansen, July 2007.
225 # Reads one record at a time and converts that record
226 # to a Perl data structure (a hash) which is returned.
228 my ( $fh, # handle to stream
233 my ( $block, @lines, $line, $key, $value, %record );
235 return if not defined $fh;
237 local $/ = "\n---\n";
241 return if not defined $block;
245 @lines = split "\n", $block;
247 foreach $line ( @lines )
249 ( $key, $value ) = split ": ", $line, 2;
251 $record{ $key } = $value;
254 return wantarray ? %record : \%record;
260 # Martin A. Hansen, July 2007.
262 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
264 my ( $data, # data structure
265 $fh, # file handle - OPTIONAL
270 if ( scalar keys %{ $data } )
274 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
279 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
290 # Martin A. Hansen, May 2009
292 # Parses and checks options for Biopieces.
294 # First the argument list is checked for duplicates and then
295 # options are parsed from ARGV after which it is checked if
296 # the Biopieces usage information should be printed. Finally,
297 # all options from ARGV are checked according to the argument list.
299 my ( $arg_list, # data structure with argument description
304 my ( $arg, @list, $options );
306 # ---- Adding the mandatory arguments to the arg_list ----
308 push @{ $arg_list }, (
310 { long => 'help', short => '?', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
311 { long => 'stream_in', short => 'I', type => 'file!', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
312 { long => 'stream_out', short => 'O', type => 'file', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
313 { long => 'verbose', short => 'v', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
316 check_duplicates_args( $arg_list );
318 # ---- Compiling options list ----
320 foreach $arg ( @{ $arg_list } )
322 if ( $arg->{ 'type' } eq 'flag' ) {
323 push @list, "$arg->{ 'long' }|$arg->{ 'short' }";
325 push @list, "$arg->{ 'long' }|$arg->{ 'short' }=s";
329 # ---- Parsing options from @ARGV ----
333 Getopt::Long::GetOptions( $options, @list );
335 # print Dumper( $options );
337 check_print_usage( $options );
339 # ---- Expanding and checking options ----
341 foreach $arg ( @{ $arg_list } )
343 check_mandatory( $arg, $options );
344 set_default( $arg, $options );
345 check_uint( $arg, $options );
346 check_int( $arg, $options );
347 set_list( $arg, $options );
348 check_dir( $arg, $options );
349 check_file( $arg, $options );
350 set_files( $arg, $options );
351 check_files( $arg, $options );
352 check_allowed( $arg, $options );
353 check_disallowed( $arg, $options );
356 # print Dumper( $options );
358 # return wantarray ? $options : %{ $options }; # WTF! Someone changed the behaviour of wantarray???
364 sub check_duplicates_args
366 # Martin A. Hansen, May 2009
368 # Check if there are duplicate long or short arguments,
369 # and raise an error if so.
371 my ( $arg_list, # List of argument hashrefs,
376 my ( $arg, %check_hash );
378 foreach $arg ( @{ $arg_list } )
380 Maasha::Common::error( qq(Duplicate long argument: $arg->{ 'long' }) ) if exists $check_hash{ $arg->{ 'long' } };
381 Maasha::Common::error( qq(Duplicate short argument: $arg->{ 'short' }) ) if exists $check_hash{ $arg->{ 'short' } };
383 $check_hash{ $arg->{ 'long' } } = 1;
384 $check_hash{ $arg->{ 'short' } } = 1;
389 sub check_print_usage
391 # Martin A. Hansen, May 2009.
393 # Check if we need to print usage and print usage
394 # and exit if that is the case.
396 my ( $options, # option hash
401 my ( $script, $wiki );
403 $script = Maasha::Common::get_scriptname();
405 if ( $script ne 'print_wiki' )
407 if ( exists $options->{ 'help' } or -t STDIN )
409 if ( not ( exists $options->{ 'stream_in' } or $options->{ 'data_in' } ) )
411 if ( scalar keys %{ $options } <= 1 )
413 $wiki = $ENV{ 'BP_DIR' } . "/bp_usage/$script.wiki";
415 if ( exists $options->{ 'help' } ) {
416 `print_wiki --data_in=$wiki --help`;
417 } elsif ( $script =~ /^(list_biopieces|list_genomes|biostat)$/ ) {
420 `print_wiki --data_in=$wiki`;
433 # Martin A. Hansen, May 2009.
435 # Check if mandatory arguments are set and raises an error if not.
438 $options, # options hash
443 if ( $arg->{ 'mandatory' } eq 'yes' and not defined $options->{ $arg->{ 'long' } } ) {
444 Maasha::Common::error( qq(Argument --$arg->{ 'long' } is mandatory) );
451 # Martin A. Hansen, May 2009.
453 # Set default values in option hash.
456 $options, # options hash
461 if ( not defined $options->{ $arg->{ 'long' } } ) {
462 $options->{ $arg->{ 'long' } } = $arg->{ 'default' }
469 # Martin A. Hansen, May 2009.
471 # Check if value to argument is an unsigned integer and
472 # raises an error if not.
475 $options, # options hash
480 if ( $arg->{ 'type' } eq 'uint' and defined $options->{ $arg->{ 'long' } } )
482 if ( $options->{ $arg->{ 'long' } } !~ /^\d+$/ ) {
483 Maasha::Common::error( qq(Argument --$arg->{ 'long' } must be an unsigned integer - not $options->{ $arg->{ 'long' } }) );
491 # Martin A. Hansen, May 2009.
493 # Check if value to argument is an integer and
494 # raises an error if not.
497 $options, # options hash
502 if ( $arg->{ 'type' } eq 'int' and defined $options{ $arg->{ 'long' } } )
504 if ( $options->{ $arg->{ 'long' } } !~ /^-?\d+$/ ) {
505 Maasha::Common::error( qq(Argument --$arg->{ 'long' } must be an integer - not $options->{ $arg->{ 'long' } }) );
513 # Martin A. Hansen, May 2009.
515 # Splits an argument of type 'list' into a list that is put
516 # in the options hash.
519 $options, # options hash
524 if ( $arg->{ 'type' } eq 'list' and defined $options->{ $arg->{ 'long' } } ) {
525 $options->{ $arg->{ 'long' } } = [ split /,/, $options->{ $arg->{ 'long' } } ];
532 # Martin A. Hansen, May 2009.
534 # Check if an argument of type 'dir!' truly is a directory and
535 # raises an error if not.
538 $options, # options hash
543 if ( $arg->{ 'type' } eq 'dir!' and defined $options->{ $arg->{ 'long' } } )
545 if ( not -d $options->{ $arg->{ 'long' } } ) {
546 Maasha::Common::error( qq(No such directory: "$options->{ $arg->{ 'long' } }") );
554 # Martin A. Hansen, May 2009.
556 # Check if an argument of type 'file!' truly is a file and
557 # raises an error if not.
560 $options, # options hash
565 if ( $arg->{ 'type' } eq 'file!' and defined $options->{ $arg->{ 'long' } } )
567 if ( not -f $options->{ $arg->{ 'long' } } ) {
568 Maasha::Common::error( qq(No such file: "$options->{ $arg->{ 'long' } }") );
576 # Martin A. Hansen, May 2009.
578 # Split the argument to 'files' into a list that is put on the options hash.
581 $options, # options hash
586 if ( $arg->{ 'type' } eq 'files' and defined $options->{ $arg->{ 'long' } } ) {
587 $options->{ $arg->{ 'long' } } = [ split /,/, $options->{ $arg->{ 'long' } } ];
594 # Martin A. Hansen, May 2009.
596 # Split the argument to 'files!' and check if each file do exists before adding
597 # the file list to the options hash.
600 $options, # options hash
605 my ( $elem, @files );
607 if ( $arg->{ 'type' } eq 'files!' and defined $options->{ $arg->{ 'long' } } )
609 foreach $elem ( split /,/, $options->{ $arg->{ 'long' } } )
613 } elsif ( $elem =~ /\*/ ) {
614 push @files, glob( $elem );
618 if ( scalar @files == 0 ) {
619 Maasha::Common::error( qq(Argument to --$arg->{ 'long' } must be a valid file or fileglob expression - not $options->{ $arg->{ 'long' } }) );
622 $options->{ $arg->{ 'long' } } = [ @files ];
629 # Martin A. Hansen, May 2009.
631 # Check if all values to all arguement are allowed and raise an
635 $options, # options hash
642 if ( defined $arg->{ 'allowed' } and defined $options->{ $arg->{ 'long' } } )
644 map { $val_hash{ $_ } = 1 } split /,/, $arg->{ 'allowed' };
646 if ( $arg->{ 'type' } =~ /^(list|files|files!)$/ )
648 foreach $elem ( @{ $options->{ $arg->{ 'long' } } } )
650 if ( not exists $val_hash{ $elem } ) {
651 Maasha::Common::error( qq(Argument to --$arg->{ 'long' } $elem is not allowed) );
657 if ( not exists $val_hash{ $options->{ $arg->{ 'long' } } } ) {
658 Maasha::Common::error( qq(Argument to --$arg->{ 'long' } $options->{ $arg->{ 'long' } } is not allowed) );
667 # Martin A. Hansen, May 2009.
669 # Check if any values to all arguemnts are disallowed and raise an error if so.
672 $options, # options hash
677 my ( $val, %val_hash );
679 if ( defined $arg->{ 'disallowed' } and defined $options->{ $arg->{ 'long' } } )
681 foreach $val ( split /,/, $arg->{ 'disallowed' } )
683 if ( $options->{ $arg->{ 'long' } } eq $val ) {
684 Maasha::Common::error( qq(Argument to --$arg->{ 'long' } $val is disallowed) );
693 # Martin A. Hansen, November 2007.
695 # Extracts files from an explicit GetOpt::Long argument
696 # allowing for the use of glob. E.g.
698 # --data_in=test.fna,test2.fna
700 # --data_in=test.fna,/dir/*.fna
702 my ( $option, # option from GetOpt::Long
707 my ( $elem, @files );
709 foreach $elem ( split ",", $option )
713 } elsif ( $elem =~ /\*/ ) {
714 push @files, glob( $elem );
718 return wantarray ? @files : \@files;
724 # Martin A. Hansen, April 2008.
726 # Removes temporary directory and exits gracefully.
727 # This subroutine is meant to be run always as the last
728 # thing even if a script is dies or is interrupted
731 my ( $sig, # signal from the %SIG
734 # print STDERR "signal->$sig<-\n";
736 my $script = Maasha::Common::get_scriptname();
742 if ( $sig =~ /MAASHA_ERROR/ )
744 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
745 status_log( "ERROR" );
747 elsif ( $sig eq "INT" )
749 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
750 status_log( "INTERUPTED" );
752 elsif ( $sig eq "TERM" )
754 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
755 status_log( "TERMINATED" );
759 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
760 status_log( "DIED" );
771 # Martin A. Hansen, July 2008.
773 # Cleans out any unused temporary files and directories in BP_TMP.
777 my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
779 $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
781 $curr_pid = Maasha::Common::get_processid();
783 @dirs = Maasha::Filesys::ls_dirs( $tmpdir );
785 foreach $dir ( @dirs )
787 if ( $dir =~ /^$tmpdir\/(.+)_(\d+)_(\d+)_bp_tmp$/ )
793 # next if $user eq "maasha"; # DEBUG
795 if ( $user eq Maasha::Common::get_user() )
797 if ( not Maasha::Common::process_running( $pid ) )
799 # print STDERR "Removing stale dir: $dir\n";
800 Maasha::Filesys::dir_remove( $dir );
802 elsif ( $pid == $curr_pid )
804 # print STDERR "Removing current dir: $dir\n";
805 Maasha::Filesys::dir_remove( $dir );
815 # Martin A. Hansen, April 2008.
817 # Create a temporary directory based on
818 # $ENV{ 'BP_TMP' } and sessionid. The directory
819 # name is written to the status file.
823 my ( $user, $sid, $pid, $script, $path, $file, $fh, $line );
825 Maasha::Common::error( qq(no BP_TMP set in %ENV) ) if not -d $ENV{ 'BP_TMP' };
827 $user = Maasha::Common::get_user();
828 $sid = Maasha::Common::get_sessionid();
829 $pid = Maasha::Common::get_processid();
830 $script = Maasha::Common::get_scriptname();
832 $path = "$ENV{ 'BP_TMP' }/" . join( "_", $user, $sid, $pid, "bp_tmp" );
833 $file = "$ENV{ 'BP_TMP' }/" . join( ".", $user, $script, $pid ) . ".status";
835 $fh = Maasha::Filesys::file_read_open( $file );
840 $fh = Maasha::Filesys::file_write_open( $file );
841 print $fh "$line;$path\n";
844 Maasha::Filesys::dir_create( $path );
856 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<