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 );
142 $time_stamp = Maasha::Common::time_stamp();
143 $user = Maasha::Common::get_user();
144 $script = Maasha::Common::get_scriptname();
146 $fh = Maasha::Filesys::file_append_open( "$ENV{ 'BP_LOG' }/biopieces.log" );
148 print $fh "$time_stamp\t$user\t$script ", join( " ", @ARGV ), "\n";
158 # Martin A. Hansen, July 2007.
160 # Opens a stream to STDIN or a file,
162 my ( $file, # file - OPTIONAL
165 # Returns filehandle.
169 if ( not -t STDIN ) {
170 $fh = Maasha::Filesys::stdin_read();
171 } elsif ( not $file ) {
172 # Maasha::Common::error( qq(no data stream) );
174 $fh = Maasha::Filesys::file_read_open( $file );
183 # Martin A. Hansen, August 2007.
185 # Opens a stream to STDOUT or a file.
187 my ( $path, # path - OPTIONAL
188 $gzip, # compress data - OPTIONAL
191 # Returns filehandle.
196 $fh = Maasha::Filesys::file_write_open( $path, $gzip );
198 $fh = Maasha::Filesys::stdout_write();
207 # Martin A. Hansen, May 2009.
209 # Close stream if open.
211 my ( $fh, # filehandle
216 close $fh if defined $fh;
222 # Martin A. Hansen, July 2007.
224 # Reads one record at a time and converts that record
225 # to a Perl data structure (a hash) which is returned.
227 my ( $fh, # handle to stream
232 my ( $block, @lines, $line, $key, $value, %record );
234 return if not defined $fh;
236 local $/ = "\n---\n";
240 return if not defined $block;
244 @lines = split "\n", $block;
246 foreach $line ( @lines )
248 ( $key, $value ) = split ": ", $line, 2;
250 $record{ $key } = $value;
253 return wantarray ? %record : \%record;
259 # Martin A. Hansen, July 2007.
261 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
263 my ( $data, # data structure
264 $fh, # file handle - OPTIONAL
269 if ( scalar keys %{ $data } )
273 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
278 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
289 # Martin A. Hansen, May 2009
291 # Parses and checks options for Biopieces.
293 # First the argument list is checked for duplicates and then
294 # options are parsed from ARGV after which it is checked if
295 # the Biopieces usage information should be printed. Finally,
296 # all options from ARGV are checked according to the argument list.
298 my ( $arg_list, # data structure with argument description
303 my ( $arg, @list, $options );
305 # ---- Adding the mandatory arguments to the arg_list ----
307 push @{ $arg_list }, (
308 { long => 'help', short => '?', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
309 { long => 'stream_in', short => 'I', type => 'file!', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
310 { long => 'stream_out', short => 'O', type => 'file', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
311 { long => 'verbose', short => 'v', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
314 check_duplicates_args( $arg_list );
316 # ---- Compiling options list ----
318 foreach $arg ( @{ $arg_list } )
320 if ( $arg->{ 'type' } eq 'flag' ) {
321 push @list, "$arg->{ 'long' }|$arg->{ 'short' }";
323 push @list, "$arg->{ 'long' }|$arg->{ 'short' }=s";
327 # ---- Parsing options from @ARGV ----
331 Getopt::Long::GetOptions( $options, @list );
333 # print Dumper( $options );
335 check_print_usage( $options );
337 # ---- Expanding and checking options ----
339 foreach $arg ( @{ $arg_list } )
341 check_mandatory( $arg, $options );
342 set_default( $arg, $options );
343 check_uint( $arg, $options );
344 check_int( $arg, $options );
345 set_list( $arg, $options );
346 check_dir( $arg, $options );
347 check_file( $arg, $options );
348 set_files( $arg, $options );
349 check_files( $arg, $options );
350 check_allowed( $arg, $options );
351 check_disallowed( $arg, $options );
354 # print Dumper( $options );
356 # return wantarray ? $options : %{ $options }; # WTF! Someone changed the behaviour of wantarray???
362 sub check_duplicates_args
364 # Martin A. Hansen, May 2009
366 # Check if there are duplicate long or short arguments,
367 # and raise an error if so.
369 my ( $arg_list, # List of argument hashrefs,
374 my ( $arg, %check_hash );
376 foreach $arg ( @{ $arg_list } )
378 Maasha::Common::error( qq(Duplicate long argument: $arg->{ 'long' }) ) if exists $check_hash{ $arg->{ 'long' } };
379 Maasha::Common::error( qq(Duplicate short argument: $arg->{ 'short' }) ) if exists $check_hash{ $arg->{ 'short' } };
381 $check_hash{ $arg->{ 'long' } } = 1;
382 $check_hash{ $arg->{ 'short' } } = 1;
387 sub check_print_usage
389 # Martin A. Hansen, May 2009.
391 # Check if we need to print usage and print usage
392 # and exit if that is the case.
394 my ( $options, # option hash
399 my ( %options, $help, $script, $wiki );
401 %options = %{ $options };
402 $help = $options{ 'help' };
403 delete $options{ 'help' };
405 $script = Maasha::Common::get_scriptname();
407 if ( $script ne 'print_wiki' )
409 if ( $help or -t STDIN )
411 if ( not ( exists $options{ 'stream_in' } or $options{ 'data_in' } ) )
413 if ( scalar keys %options == 0 )
415 $wiki = $ENV{ 'BP_DIR' } . "/bp_usage/$script.wiki";
418 `print_wiki --data_in=$wiki --help`;
419 } elsif ( $script =~ /^(list_biopieces|list_genomes|list_mysql_databases|biostat)$/ ) {
422 `print_wiki --data_in=$wiki`;
435 # Martin A. Hansen, May 2009.
437 # Check if mandatory arguments are set and raises an error if not.
440 $options, # options hash
445 if ( $arg->{ 'mandatory' } eq 'yes' and not defined $options->{ $arg->{ 'long' } } ) {
446 Maasha::Common::error( qq(Argument --$arg->{ 'long' } is mandatory) );
453 # Martin A. Hansen, May 2009.
455 # Set default values in option hash.
458 $options, # options hash
463 if ( not defined $options->{ $arg->{ 'long' } } ) {
464 $options->{ $arg->{ 'long' } } = $arg->{ 'default' }
471 # Martin A. Hansen, May 2009.
473 # Check if value to argument is an unsigned integer and
474 # raises an error if not.
477 $options, # options hash
482 if ( $arg->{ 'type' } eq 'uint' and defined $options->{ $arg->{ 'long' } } )
484 if ( $options->{ $arg->{ 'long' } } !~ /^\d+$/ ) {
485 Maasha::Common::error( qq(Argument --$arg->{ 'long' } must be an unsigned integer - not $options->{ $arg->{ 'long' } }) );
493 # Martin A. Hansen, May 2009.
495 # Check if value to argument is an integer and
496 # raises an error if not.
499 $options, # options hash
504 if ( $arg->{ 'type' } eq 'int' and defined $options{ $arg->{ 'long' } } )
506 if ( $options->{ $arg->{ 'long' } } !~ /^-?\d+$/ ) {
507 Maasha::Common::error( qq(Argument --$arg->{ 'long' } must be an integer - not $options->{ $arg->{ 'long' } }) );
515 # Martin A. Hansen, May 2009.
517 # Splits an argument of type 'list' into a list that is put
518 # in the options hash.
521 $options, # options hash
526 if ( $arg->{ 'type' } eq 'list' and defined $options->{ $arg->{ 'long' } } ) {
527 $options->{ $arg->{ 'long' } } = [ split /,/, $options->{ $arg->{ 'long' } } ];
534 # Martin A. Hansen, May 2009.
536 # Check if an argument of type 'dir!' truly is a directory and
537 # raises an error if not.
540 $options, # options hash
545 if ( $arg->{ 'type' } eq 'dir!' and defined $options->{ $arg->{ 'long' } } )
547 if ( not -d $options->{ $arg->{ 'long' } } ) {
548 Maasha::Common::error( qq(No such directory: "$options->{ $arg->{ 'long' } }") );
556 # Martin A. Hansen, May 2009.
558 # Check if an argument of type 'file!' truly is a file and
559 # raises an error if not.
562 $options, # options hash
567 if ( $arg->{ 'type' } eq 'file!' and defined $options->{ $arg->{ 'long' } } )
569 if ( not -f $options->{ $arg->{ 'long' } } ) {
570 Maasha::Common::error( qq(No such file: "$options->{ $arg->{ 'long' } }") );
578 # Martin A. Hansen, May 2009.
580 # Split the argument to 'files' into a list that is put on the options hash.
583 $options, # options hash
588 if ( $arg->{ 'type' } eq 'files' and defined $options->{ $arg->{ 'long' } } ) {
589 $options->{ $arg->{ 'long' } } = [ split /,/, $options->{ $arg->{ 'long' } } ];
596 # Martin A. Hansen, May 2009.
598 # Split the argument to 'files!' and check if each file do exists before adding
599 # the file list to the options hash.
602 $options, # options hash
607 my ( $elem, @files );
609 if ( $arg->{ 'type' } eq 'files!' and defined $options->{ $arg->{ 'long' } } )
611 foreach $elem ( split /,/, $options->{ $arg->{ 'long' } } )
615 } elsif ( $elem =~ /\*/ ) {
616 push @files, glob( $elem );
620 if ( scalar @files == 0 ) {
621 Maasha::Common::error( qq(Argument to --$arg->{ 'long' } must be a valid file or fileglob expression - not $options->{ $arg->{ 'long' } }) );
624 $options->{ $arg->{ 'long' } } = [ @files ];
631 # Martin A. Hansen, May 2009.
633 # Check if all values to all arguement are allowed and raise an
637 $options, # options hash
644 if ( defined $arg->{ 'allowed' } and defined $options->{ $arg->{ 'long' } } )
646 map { $val_hash{ $_ } = 1 } split /,/, $arg->{ 'allowed' };
648 if ( $arg->{ 'type' } =~ /^(list|files|files!)$/ )
650 foreach $elem ( @{ $options->{ $arg->{ 'long' } } } )
652 if ( not exists $val_hash{ $elem } ) {
653 Maasha::Common::error( qq(Argument to --$arg->{ 'long' } $elem is not allowed) );
659 if ( not exists $val_hash{ $options->{ $arg->{ 'long' } } } ) {
660 Maasha::Common::error( qq(Argument to --$arg->{ 'long' } $options->{ $arg->{ 'long' } } is not allowed) );
669 # Martin A. Hansen, May 2009.
671 # Check if any values to all arguemnts are disallowed and raise an error if so.
674 $options, # options hash
679 my ( $val, %val_hash );
681 if ( defined $arg->{ 'disallowed' } and defined $options->{ $arg->{ 'long' } } )
683 foreach $val ( split /,/, $arg->{ 'disallowed' } )
685 if ( $options->{ $arg->{ 'long' } } eq $val ) {
686 Maasha::Common::error( qq(Argument to --$arg->{ 'long' } $val is disallowed) );
693 # marked for deletion - obsolete?
696 # # Martin A. Hansen, November 2007.
698 # # Extracts files from an explicit GetOpt::Long argument
699 # # allowing for the use of glob. E.g.
700 # # --data_in=test.fna
701 # # --data_in=test.fna,test2.fna
703 # # --data_in=test.fna,/dir/*.fna
705 # my ( $option, # option from GetOpt::Long
710 # my ( $elem, @files );
712 # foreach $elem ( split ",", $option )
715 # push @files, $elem;
716 # } elsif ( $elem =~ /\*/ ) {
717 # push @files, glob( $elem );
721 # return wantarray ? @files : \@files;
727 # Martin A. Hansen, April 2008.
729 # Removes temporary directory and exits gracefully.
730 # This subroutine is meant to be run always as the last
731 # thing even if a script is dies or is interrupted
734 my ( $sig, # signal from the %SIG
737 # print STDERR "signal->$sig<-\n";
739 my $script = Maasha::Common::get_scriptname();
745 if ( $sig =~ /MAASHA_ERROR/ )
747 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
748 status_log( "ERROR" );
750 elsif ( $sig eq "INT" )
752 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
753 status_log( "INTERRUPTED" );
755 elsif ( $sig eq "TERM" )
757 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
758 status_log( "TERMINATED" );
762 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
763 status_log( "DIED" );
774 # Martin A. Hansen, July 2008.
776 # Cleans out any unused temporary files and directories in BP_TMP.
780 my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
782 $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
784 $curr_pid = Maasha::Common::get_processid();
786 @dirs = Maasha::Filesys::ls_dirs( $tmpdir );
788 foreach $dir ( @dirs )
790 if ( $dir =~ /^$tmpdir\/(.+)_(\d+)_(\d+)_bp_tmp$/ )
796 # next if $user eq "maasha"; # DEBUG
798 if ( $user eq Maasha::Common::get_user() )
800 if ( not Maasha::Common::process_running( $pid ) )
802 # print STDERR "Removing stale dir: $dir\n";
803 Maasha::Filesys::dir_remove( $dir );
805 elsif ( $pid == $curr_pid )
807 # print STDERR "Removing current dir: $dir\n";
808 Maasha::Filesys::dir_remove( $dir );
818 # Martin A. Hansen, April 2008.
820 # Create a temporary directory based on
821 # $ENV{ 'BP_TMP' } and sessionid. The directory
822 # name is written to the status file.
826 my ( $user, $sid, $pid, $script, $path, $file, $fh, $line );
828 Maasha::Common::error( qq(no BP_TMP set in %ENV) ) if not -d $ENV{ 'BP_TMP' };
830 $user = Maasha::Common::get_user();
831 $sid = Maasha::Common::get_sessionid();
832 $pid = Maasha::Common::get_processid();
833 $script = Maasha::Common::get_scriptname();
835 $path = "$ENV{ 'BP_TMP' }/" . join( "_", $user, $sid, $pid, "bp_tmp" );
836 $file = "$ENV{ 'BP_TMP' }/" . join( ".", $user, $script, $pid ) . ".status";
838 $fh = Maasha::Filesys::file_read_open( $file );
843 $fh = Maasha::Filesys::file_write_open( $file );
844 print $fh "$line;$path\n";
847 Maasha::Filesys::dir_create( $path );
855 # Martin A. Hansen, July 2009.
857 # Read Biopiece configuration info from .biopiecesrc.
858 # and returns the value of a given key.
860 my ( $key, # configuration key
865 my ( $file, $fh, $record );
867 $file = "$ENV{ 'HOME' }/.biopiecesrc";
869 return undef if not -f $file;
871 $fh = Maasha::Filesys::file_read_open( $file );
872 $record = get_record( $fh );
875 if ( exists $record->{ $key } ) {
876 return $record->{ $key };
889 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<