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 vars qw( @ISA @EXPORT_OK );
36 @ISA = qw( Exporter );
46 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SIGNAL HANDLER <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
49 $SIG{ '__DIE__' } = \&sig_handler;
50 $SIG{ 'INT' } = \&sig_handler;
51 $SIG{ 'TERM' } = \&sig_handler;
56 # Martin A. Hansen, January 2008.
58 # Log messages to logfile.
62 my ( $time_stamp, $user, $script, $fh_global, $fh_local );
64 $time_stamp = Maasha::Common::time_stamp();
65 $user = Maasha::Common::get_user();
66 $script = Maasha::Common::get_scriptname();
68 $fh_global = Maasha::Common::append_open( "$ENV{ 'BP_LOG' }/biopieces.log" );
69 $fh_local = Maasha::Common::append_open( "$ENV{ 'HOME' }/.biopieces.log" );
71 print $fh_global "$time_stamp\t$user\t$script ", join( " ", @ARGV ), "\n";
72 print $fh_local "$time_stamp\t$user\t$script ", join( " ", @ARGV ), "\n";
74 $fh_global->autoflush( 1 );
75 $fh_local->autoflush( 1 );
84 # Martin A. Hansen, July 2007.
86 # Opens a stream to STDIN or a file,
88 my ( $path, # path - OPTIONAL
96 $fh = Maasha::Common::read_stdin();
97 } elsif ( not $path ) {
98 # Maasha::Common::error( qq(no data stream) );
100 $fh = Maasha::Common::read_open( $path );
103 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
111 # Martin A. Hansen, August 2007.
113 # Opens a stream to STDOUT or a file.
115 my ( $path, # path - OPTIONAL
116 $gzip, # compress data - OPTIONAL
119 # Returns filehandle.
124 $fh = Maasha::Common::write_open( $path, $gzip );
126 $fh = Maasha::Common::write_stdout();
135 # Martin A. Hansen, July 2007.
137 # Reads one record at a time and converts that record
138 # to a Perl data structure (a hash) which is returned.
140 my ( $fh, # handle to stream
145 my ( $block, @lines, $line, $key, $value, %record );
147 local $/ = "\n---\n";
153 return if not defined $block;
155 @lines = split "\n", $block;
157 foreach $line ( @lines )
159 ( $key, $value ) = split ": ", $line, 2;
161 $record{ $key } = $value;
164 return wantarray ? %record : \%record;
170 # Martin A. Hansen, July 2007.
172 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
174 my ( $data, # data structure
175 $fh, # file handle - OPTIONAL
180 if ( scalar keys %{ $data } )
184 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
189 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
200 # Martin A. Hansen, November 2007.
202 # Extracts files from an explicit GetOpt::Long argument
203 # allowing for the use of glob. E.g.
205 # --data_in=test.fna,test2.fna
207 # --data_in=test.fna,/dir/*.fna
209 my ( $option, # option from GetOpt::Long
214 my ( $elem, @files );
216 foreach $elem ( split ",", $option )
220 } elsif ( $elem =~ /\*/ ) {
221 push @files, glob( $elem );
225 return wantarray ? @files : \@files;
231 # Martin A. Hansen, April 2008.
233 # Removes temporary directory and exits gracefully.
234 # This subroutine is meant to be run always as the last
235 # thing even if a script is dies or is interrupted
238 my ( $sig, # signal from the %SIG
241 # print STDERR "signal->$sig<-\n";
249 if ( $sig =~ /MAASHA_ERROR/ ) {
250 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
251 } elsif ( $sig eq "INT" ) {
252 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
253 } elsif ( $sig eq "TERM" ) {
254 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
256 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
268 # Martin A. Hansen, July 2008.
270 # Cleans out any unused temporary files and directories in BP_TMP.
274 my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
276 $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
278 $curr_pid = Maasha::Common::get_processid();
280 @dirs = Maasha::Common::ls_dirs( $tmpdir );
282 foreach $dir ( @dirs )
284 if ( $dir =~ /^$tmpdir\/(.+)_(\d+)_(\d+)_bp_tmp$/ )
290 # next if $user eq "maasha"; # DEBUG
292 if ( $user eq Maasha::Common::get_user() )
294 if ( not Maasha::Common::process_running( $pid ) )
296 # print STDERR "Removing stale dir: $dir\n";
297 Maasha::Common::dir_remove( $dir );
299 elsif ( $pid == $curr_pid )
301 # print STDERR "Removing current dir: $dir\n";
302 Maasha::Common::dir_remove( $dir );
316 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<