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;
59 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
64 my ( $time_stamp, $script, $user, $pid, $file, $fh );
66 $time_stamp = Maasha::Common::time_stamp();
67 $user = Maasha::Common::get_user();
68 $script = Maasha::Common::get_scriptname();
69 $pid = Maasha::Common::get_processid();
71 $file = "$ENV{ 'BP_TMP' }/" . join( ".", $user, $script, $pid ) . ".status";
72 $fh = Maasha::Filesys::file_write_open( $file );
74 print $fh join( ";", $time_stamp, join( " ", @ARGV ) ) . "\n";
82 # Martin A. Hansen, June 2009.
84 # Retrieves initial status information written with status_set and uses this
85 # to write a status entry to the log file.
87 my ( $status, # status - OPTIONAL
92 my ( $time0, $time1, $script, $user, $pid, $file, $fh, $elap, $fh_global, $fh_local, $line, $args, $tmp_dir );
96 $time1 = Maasha::Common::time_stamp();
97 $user = Maasha::Common::get_user();
98 $script = Maasha::Common::get_scriptname();
99 $pid = Maasha::Common::get_processid();
101 $file = "$ENV{ 'BP_TMP' }/" . join( ".", $user, $script, $pid ) . ".status";
103 return if not -f $file;
105 $fh = Maasha::Filesys::file_read_open( $file );
112 ( $time0, $args, $tmp_dir ) = split /;/, $line;
114 Maasha::Filesys::dir_remove( $tmp_dir ) if defined $tmp_dir;
116 $elap = Maasha::Common::time_stamp_diff( $time0, $time1 );
118 $fh_global = Maasha::Filesys::file_append_open( "$ENV{ 'BP_LOG' }/biopieces.log" );
119 $fh_local = Maasha::Filesys::file_append_open( "$ENV{ 'HOME' }/.biopieces.log" );
121 print $fh_global join( "\t", $time0, $time1, $elap, $user, $status, "$script $args" ) . "\n";
122 print $fh_local join( "\t", $time0, $time1, $elap, $user, $status, "$script $args" ) . "\n";
124 $fh_global->autoflush( 1 );
125 $fh_local->autoflush( 1 );
134 # Martin A. Hansen, January 2008.
136 # Log messages to logfile.
140 my ( $time_stamp, $user, $script, $fh_global, $fh_local );
142 $time_stamp = Maasha::Common::time_stamp();
143 $user = Maasha::Common::get_user();
144 $script = Maasha::Common::get_scriptname();
146 $fh_global = Maasha::Filesys::file_append_open( "$ENV{ 'BP_LOG' }/biopieces.log" );
147 $fh_local = Maasha::Filesys::file_append_open( "$ENV{ 'HOME' }/.biopieces.log" );
149 print $fh_global "$time_stamp\t$user\t$script ", join( " ", @ARGV ), "\n";
150 print $fh_local "$time_stamp\t$user\t$script ", join( " ", @ARGV ), "\n";
152 $fh_global->autoflush( 1 );
153 $fh_local->autoflush( 1 );
162 # Martin A. Hansen, July 2007.
164 # Opens a stream to STDIN or a file,
166 my ( $file, # file - OPTIONAL
169 # Returns filehandle.
173 if ( not -t STDIN ) {
174 $fh = Maasha::Filesys::stdin_read();
175 } elsif ( not $file ) {
176 # Maasha::Common::error( qq(no data stream) );
178 $fh = Maasha::Filesys::file_read_open( $file );
187 # Martin A. Hansen, August 2007.
189 # Opens a stream to STDOUT or a file.
191 my ( $path, # path - OPTIONAL
192 $gzip, # compress data - OPTIONAL
195 # Returns filehandle.
200 $fh = Maasha::Filesys::file_write_open( $path, $gzip );
202 $fh = Maasha::Filesys::stdout_write();
211 # Martin A. Hansen, May 2009.
213 # Close stream if open.
215 my ( $fh, # filehandle
220 close $fh if defined $fh;
226 # Martin A. Hansen, July 2007.
228 # Reads one record at a time and converts that record
229 # to a Perl data structure (a hash) which is returned.
231 my ( $fh, # handle to stream
236 my ( $block, @lines, $line, $key, $value, %record );
238 return if not defined $fh;
240 local $/ = "\n---\n";
244 return if not defined $block;
248 @lines = split "\n", $block;
250 foreach $line ( @lines )
252 ( $key, $value ) = split ": ", $line, 2;
254 $record{ $key } = $value;
257 return wantarray ? %record : \%record;
263 # Martin A. Hansen, July 2007.
265 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
267 my ( $data, # data structure
268 $fh, # file handle - OPTIONAL
273 if ( scalar keys %{ $data } )
277 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
282 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
293 # Martin A. Hansen, May 2009
295 # Parses and checks options for Biopieces.
297 # First the argument list is checked for duplicates and then
298 # options are parsed from ARGV after which it is checked if
299 # the Biopieces usage information should be printed. Finally,
300 # all options from ARGV are checked according to the argument list.
302 my ( $arg_list, # data structure with argument description
307 my ( $arg, @list, $options );
309 # ---- Adding the mandatory arguments to the arg_list ----
311 push @{ $arg_list }, (
312 { long => 'help', short => '?', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
313 { long => 'stream_in', short => 'I', type => 'file!', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
314 { long => 'stream_out', short => 'O', type => 'file', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
315 { long => 'verbose', short => 'v', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
318 check_duplicates_args( $arg_list );
320 # ---- Compiling options list ----
322 foreach $arg ( @{ $arg_list } )
324 if ( $arg->{ 'type' } eq 'flag' ) {
325 push @list, "$arg->{ 'long' }|$arg->{ 'short' }";
327 push @list, "$arg->{ 'long' }|$arg->{ 'short' }=s";
331 # ---- Parsing options from @ARGV ----
335 Getopt::Long::GetOptions( $options, @list );
337 # print Dumper( $options );
339 check_print_usage( $options );
341 # ---- Expanding and checking options ----
343 foreach $arg ( @{ $arg_list } )
345 check_mandatory( $arg, $options );
346 set_default( $arg, $options );
347 check_uint( $arg, $options );
348 check_int( $arg, $options );
349 set_list( $arg, $options );
350 check_dir( $arg, $options );
351 check_file( $arg, $options );
352 set_files( $arg, $options );
353 check_files( $arg, $options );
354 check_allowed( $arg, $options );
355 check_disallowed( $arg, $options );
358 # print Dumper( $options );
360 # return wantarray ? $options : %{ $options }; # WTF! Someone changed the behaviour of wantarray???
366 sub check_duplicates_args
368 # Martin A. Hansen, May 2009
370 # Check if there are duplicate long or short arguments,
371 # and raise an error if so.
373 my ( $arg_list, # List of argument hashrefs,
378 my ( $arg, %check_hash );
380 foreach $arg ( @{ $arg_list } )
382 Maasha::Common::error( qq(Duplicate long argument: $arg->{ 'long' }) ) if exists $check_hash{ $arg->{ 'long' } };
383 Maasha::Common::error( qq(Duplicate short argument: $arg->{ 'short' }) ) if exists $check_hash{ $arg->{ 'short' } };
385 $check_hash{ $arg->{ 'long' } } = 1;
386 $check_hash{ $arg->{ 'short' } } = 1;
391 sub check_print_usage
393 # Martin A. Hansen, May 2009.
395 # Check if we need to print usage and print usage
396 # and exit if that is the case.
398 my ( $options, # option hash
403 my ( %options, $help, $script, $wiki );
405 %options = %{ $options };
406 $help = $options{ 'help' };
407 delete $options{ 'help' };
409 $script = Maasha::Common::get_scriptname();
411 if ( $script ne 'print_wiki' )
413 if ( $help or -t STDIN )
415 if ( not ( exists $options{ 'stream_in' } or $options{ 'data_in' } ) )
417 if ( scalar keys %options == 0 )
419 $wiki = $ENV{ 'BP_DIR' } . "/bp_usage/$script.wiki";
422 `print_wiki --data_in=$wiki --help`;
423 } elsif ( $script =~ /^(list_biopieces|list_genomes|list_mysql_databases|biostat)$/ ) {
426 `print_wiki --data_in=$wiki`;
439 # Martin A. Hansen, May 2009.
441 # Check if mandatory arguments are set and raises an error if not.
444 $options, # options hash
449 if ( $arg->{ 'mandatory' } eq 'yes' and not defined $options->{ $arg->{ 'long' } } ) {
450 Maasha::Common::error( qq(Argument --$arg->{ 'long' } is mandatory) );
457 # Martin A. Hansen, May 2009.
459 # Set default values in option hash.
462 $options, # options hash
467 if ( not defined $options->{ $arg->{ 'long' } } ) {
468 $options->{ $arg->{ 'long' } } = $arg->{ 'default' }
475 # Martin A. Hansen, May 2009.
477 # Check if value to argument is an unsigned integer and
478 # raises an error if not.
481 $options, # options hash
486 if ( $arg->{ 'type' } eq 'uint' and defined $options->{ $arg->{ 'long' } } )
488 if ( $options->{ $arg->{ 'long' } } !~ /^\d+$/ ) {
489 Maasha::Common::error( qq(Argument --$arg->{ 'long' } must be an unsigned integer - not $options->{ $arg->{ 'long' } }) );
497 # Martin A. Hansen, May 2009.
499 # Check if value to argument is an integer and
500 # raises an error if not.
503 $options, # options hash
508 if ( $arg->{ 'type' } eq 'int' and defined $options{ $arg->{ 'long' } } )
510 if ( $options->{ $arg->{ 'long' } } !~ /^-?\d+$/ ) {
511 Maasha::Common::error( qq(Argument --$arg->{ 'long' } must be an integer - not $options->{ $arg->{ 'long' } }) );
519 # Martin A. Hansen, May 2009.
521 # Splits an argument of type 'list' into a list that is put
522 # in the options hash.
525 $options, # options hash
530 if ( $arg->{ 'type' } eq 'list' and defined $options->{ $arg->{ 'long' } } ) {
531 $options->{ $arg->{ 'long' } } = [ split /,/, $options->{ $arg->{ 'long' } } ];
538 # Martin A. Hansen, May 2009.
540 # Check if an argument of type 'dir!' truly is a directory and
541 # raises an error if not.
544 $options, # options hash
549 if ( $arg->{ 'type' } eq 'dir!' and defined $options->{ $arg->{ 'long' } } )
551 if ( not -d $options->{ $arg->{ 'long' } } ) {
552 Maasha::Common::error( qq(No such directory: "$options->{ $arg->{ 'long' } }") );
560 # Martin A. Hansen, May 2009.
562 # Check if an argument of type 'file!' truly is a file and
563 # raises an error if not.
566 $options, # options hash
571 if ( $arg->{ 'type' } eq 'file!' and defined $options->{ $arg->{ 'long' } } )
573 if ( not -f $options->{ $arg->{ 'long' } } ) {
574 Maasha::Common::error( qq(No such file: "$options->{ $arg->{ 'long' } }") );
582 # Martin A. Hansen, May 2009.
584 # Split the argument to 'files' into a list that is put on the options hash.
587 $options, # options hash
592 if ( $arg->{ 'type' } eq 'files' and defined $options->{ $arg->{ 'long' } } ) {
593 $options->{ $arg->{ 'long' } } = [ split /,/, $options->{ $arg->{ 'long' } } ];
600 # Martin A. Hansen, May 2009.
602 # Split the argument to 'files!' and check if each file do exists before adding
603 # the file list to the options hash.
606 $options, # options hash
611 my ( $elem, @files );
613 if ( $arg->{ 'type' } eq 'files!' and defined $options->{ $arg->{ 'long' } } )
615 foreach $elem ( split /,/, $options->{ $arg->{ 'long' } } )
619 } elsif ( $elem =~ /\*/ ) {
620 push @files, glob( $elem );
624 if ( scalar @files == 0 ) {
625 Maasha::Common::error( qq(Argument to --$arg->{ 'long' } must be a valid file or fileglob expression - not $options->{ $arg->{ 'long' } }) );
628 $options->{ $arg->{ 'long' } } = [ @files ];
635 # Martin A. Hansen, May 2009.
637 # Check if all values to all arguement are allowed and raise an
641 $options, # options hash
648 if ( defined $arg->{ 'allowed' } and defined $options->{ $arg->{ 'long' } } )
650 map { $val_hash{ $_ } = 1 } split /,/, $arg->{ 'allowed' };
652 if ( $arg->{ 'type' } =~ /^(list|files|files!)$/ )
654 foreach $elem ( @{ $options->{ $arg->{ 'long' } } } )
656 if ( not exists $val_hash{ $elem } ) {
657 Maasha::Common::error( qq(Argument to --$arg->{ 'long' } $elem is not allowed) );
663 if ( not exists $val_hash{ $options->{ $arg->{ 'long' } } } ) {
664 Maasha::Common::error( qq(Argument to --$arg->{ 'long' } $options->{ $arg->{ 'long' } } is not allowed) );
673 # Martin A. Hansen, May 2009.
675 # Check if any values to all arguemnts are disallowed and raise an error if so.
678 $options, # options hash
683 my ( $val, %val_hash );
685 if ( defined $arg->{ 'disallowed' } and defined $options->{ $arg->{ 'long' } } )
687 foreach $val ( split /,/, $arg->{ 'disallowed' } )
689 if ( $options->{ $arg->{ 'long' } } eq $val ) {
690 Maasha::Common::error( qq(Argument to --$arg->{ 'long' } $val is disallowed) );
697 # marked for deletion - obsolete?
700 # # Martin A. Hansen, November 2007.
702 # # Extracts files from an explicit GetOpt::Long argument
703 # # allowing for the use of glob. E.g.
704 # # --data_in=test.fna
705 # # --data_in=test.fna,test2.fna
707 # # --data_in=test.fna,/dir/*.fna
709 # my ( $option, # option from GetOpt::Long
714 # my ( $elem, @files );
716 # foreach $elem ( split ",", $option )
719 # push @files, $elem;
720 # } elsif ( $elem =~ /\*/ ) {
721 # push @files, glob( $elem );
725 # return wantarray ? @files : \@files;
731 # Martin A. Hansen, April 2008.
733 # Removes temporary directory and exits gracefully.
734 # This subroutine is meant to be run always as the last
735 # thing even if a script is dies or is interrupted
738 my ( $sig, # signal from the %SIG
741 # print STDERR "signal->$sig<-\n";
743 my $script = Maasha::Common::get_scriptname();
749 if ( $sig =~ /MAASHA_ERROR/ )
751 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
752 status_log( "ERROR" );
754 elsif ( $sig eq "INT" )
756 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
757 status_log( "INTERRUPTED" );
759 elsif ( $sig eq "TERM" )
761 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
762 status_log( "TERMINATED" );
766 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
767 status_log( "DIED" );
778 # Martin A. Hansen, July 2008.
780 # Cleans out any unused temporary files and directories in BP_TMP.
784 my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
786 $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
788 $curr_pid = Maasha::Common::get_processid();
790 @dirs = Maasha::Filesys::ls_dirs( $tmpdir );
792 foreach $dir ( @dirs )
794 if ( $dir =~ /^$tmpdir\/(.+)_(\d+)_(\d+)_bp_tmp$/ )
800 # next if $user eq "maasha"; # DEBUG
802 if ( $user eq Maasha::Common::get_user() )
804 if ( not Maasha::Common::process_running( $pid ) )
806 # print STDERR "Removing stale dir: $dir\n";
807 Maasha::Filesys::dir_remove( $dir );
809 elsif ( $pid == $curr_pid )
811 # print STDERR "Removing current dir: $dir\n";
812 Maasha::Filesys::dir_remove( $dir );
822 # Martin A. Hansen, April 2008.
824 # Create a temporary directory based on
825 # $ENV{ 'BP_TMP' } and sessionid. The directory
826 # name is written to the status file.
830 my ( $user, $sid, $pid, $script, $path, $file, $fh, $line );
832 Maasha::Common::error( qq(no BP_TMP set in %ENV) ) if not -d $ENV{ 'BP_TMP' };
834 $user = Maasha::Common::get_user();
835 $sid = Maasha::Common::get_sessionid();
836 $pid = Maasha::Common::get_processid();
837 $script = Maasha::Common::get_scriptname();
839 $path = "$ENV{ 'BP_TMP' }/" . join( "_", $user, $sid, $pid, "bp_tmp" );
840 $file = "$ENV{ 'BP_TMP' }/" . join( ".", $user, $script, $pid ) . ".status";
842 $fh = Maasha::Filesys::file_read_open( $file );
847 $fh = Maasha::Filesys::file_write_open( $file );
848 print $fh "$line;$path\n";
851 Maasha::Filesys::dir_create( $path );
859 # Martin A. Hansen, July 2009.
861 # Read Biopiece configuration info from .biopiecesrc.
862 # and returns the value of a given key.
864 my ( $key, # configuration key
869 my ( $file, $fh, $record );
871 $file = "$ENV{ 'HOME' }/.biopiecesrc";
873 return undef if not -f $file;
875 $fh = Maasha::Filesys::file_read_open( $file );
876 $record = get_record( $fh );
879 if ( exists $record->{ $key } ) {
880 return $record->{ $key };
893 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<