]> git.donarmstrong.com Git - biopieces.git/commitdiff
fixed cleanup of temporary files
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Thu, 24 Jul 2008 06:27:53 +0000 (06:27 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Thu, 24 Jul 2008 06:27:53 +0000 (06:27 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@189 74ccb610-7750-0410-82ae-013aeee3265d

code_perl/Maasha/Biopieces.pm
code_perl/Maasha/Common.pm
code_perl/Maasha/Match.pm
code_perl/Maasha/UCSC.pm

index 001538882ab1c0459d5654d01fad2d45bc645523..d2d83ad322da5be26891c1adbcb5e212afc013f7 100644 (file)
@@ -161,6 +161,8 @@ sub run_script
 
     $options = get_options( $script );
 
+    $options->{ "SCRIPT" } = $script;
+
     if ( $script ne "list_biopieces" and $script ne "list_genomes" ) {
         $script = "print_usage" if ( -t STDIN and keys %{ $options } <= 1 or $options->{ 'help' } );
     }
@@ -261,9 +263,6 @@ sub run_script
 
     close $in if defined $in;
     close $out;
-
-    # unset status   - missing
-    # write log file - missing
 }
 
 
@@ -932,14 +931,12 @@ sub get_options
     );
 
 #    print STDERR Dumper( \@options );
-
+    
     GetOptions(
         \%options,
         @options,
     );
 
-    $options{ "script" } = $script;
-
 #    print STDERR Dumper( \%options );
 
     if ( -t STDIN && scalar( keys %options ) == 1 or $options{ "help" } ) {
@@ -1057,7 +1054,7 @@ sub script_print_usage
     if ( $options->{ 'data_in' } ) {
         $file = $options->{ 'data_in' };
     } else {
-        $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'script' }, ".wiki";
+        $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'SCRIPT' }, ".wiki";
     }
 
     $wiki = Maasha::Gwiki::gwiki_read( $file );
@@ -1103,6 +1100,7 @@ sub script_list_biopieces
             @{ $wiki } = grep { $_->[ 0 ]->{ 'FORMAT' }  =~ /paragraph/ } @{ $wiki };
 
             $synopsis = $wiki->[ 0 ]->[ 0 ]->{ 'TEXT' };
+            $synopsis =~ s/!(\w)/$1/g;
 
             printf( "%-30s%s\n", $program, $synopsis );
         }
@@ -3606,7 +3604,7 @@ sub script_soap_seq
 
     close $fh_out;
 
-    Maasha::Common::run( "soap", "-r 2 -a $tmp_in -v $options->{ 'mismatches' } -g $options->{ 'gap_size'} -p $options->{ 'cpus' } -d $options->{ 'in_file' } -o $tmp_out > /dev/null 2>&1", 1 );
+    Maasha::Common::run( "soap", "-r 2 -a $tmp_in -v $options->{ 'mismatches' } -g $options->{ 'gap_size' } -p $options->{ 'cpus' } -d $options->{ 'in_file' } -o $tmp_out > /dev/null 2>&1", 1 );
 
     unlink $tmp_in;
 
@@ -3625,7 +3623,7 @@ sub script_soap_seq
         $record->{ "SCORE" }      = $fields[ 3 ];
         $record->{ "STRAND" }     = $fields[ 6 ];
         $record->{ "S_ID" }       = $fields[ 7 ];
-        $record->{ "S_BEG" }      = $fields[ 8 ] - 1; # soap is one based
+        $record->{ "S_BEG" }      = $fields[ 8 ] - 1; # soap is 1-based
         $record->{ "S_END" }      = $fields[ 8 ] + $fields[ 5 ] - 2;
 
         put_record( $record, $out );
@@ -5995,77 +5993,6 @@ sub put_record
 }
 
 
-sub get_status
-{
-    # Martin A. Hansen, July 2008.
-
-    # Gets the first record in the stream, the status record,
-    # which is returned. An error is raised if no status record
-    # was found.
-
-    my ( $fh,   # handle to stream
-       ) = @_;
-    
-    # Returns a hash.
-
-    my ( $record );
-
-    $record = get_record( $fh );
-
-    Maasha::Common::error( "No status record found in stream." ) if not $record->{ "REC_TYPE" } eq "STATUS";
-
-    return wantarray ? %{ $record } : $record;
-}
-
-
-sub put_status
-{
-    # Martin A. Hansen, July 2008.
-
-    # Output the status record to STDOUT.
-
-    # Should it ever go to file?
-
-    my ( $record,   # status record.
-         $fh,       # handle to file or stream - OPTIONAL
-       ) = @_;
-
-    # Returns nothing.
-
-    put_record( $record, $fh );
-}
-
-
-sub check_status
-{
-    # Martin A. Hansen, July 2008.
-
-}
-
-
-sub status_new
-{
-    # Martin A. Hansen, July 2008.
-
-    # Create a new status record which
-    # is returned.
-
-    my ( $fisk
-    
-       );
-
-    my ( $record );
-    return wantarray ? %{ $record } : $record;
-}
-
-
-sub status_update
-{
-    # Martin A. Hansen, July 2008.
-
-}
-
-
 sub getopt_files
 {
     # Martin A. Hansen, November 2007.
@@ -6127,22 +6054,55 @@ sub sig_handler
             print STDERR "\nProgram '$script' died->$sig"                       . "  -  Please wait for temporary data to be removed\n";
         }
 
-        # This is a really bad solution, potentially, anyone can include this module and set
-        # the BP_TMP to point at any dir and thus take out the machine !!!
-
-        Maasha::Common::dir_remove( $BP_TMP );
+        clean_tmp();
     }
 
     exit( 0 );
 }
 
 
-END
+sub clean_tmp
 {
-    # This is a really bad solution, potentially, anyone can include this module and set
-    # the BP_TMP to point at any dir and thus take out the machine !!!
+    # Martin A. Hansen, July 2008.
 
-    Maasha::Common::dir_remove( $BP_TMP );
+    # Cleans out any unused temporary files and direcotries in BP_TMP.
+
+    # Returns nothing.
+
+    my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
+
+    $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
+
+    $curr_pid = Maasha::Common::get_processid();
+
+    @dirs = Maasha::Common::ls_dirs( $tmpdir );
+
+    foreach $dir ( @dirs )
+    {
+        if ( $dir =~ /(.+)_(\d+)_(\d+)_bp_tmp/ )
+        {
+            $user = $1;
+            $sid  = $2;
+            $pid  = $3;
+
+            if ( not Maasha::Common::process_running( $pid ) )
+            {
+                # print STDERR "Removing stale dir: $dir\n";
+                Maasha::Common::dir_remove( $dir );
+            }
+            elsif ( $pid == $curr_pid )
+            {
+                # print STDERR "Removing current dir: $dir\n";
+                Maasha::Common::dir_remove( $dir );
+            }
+        }
+    }
+}
+
+
+END
+{
+    clean_tmp();
 }
 
 
index 4da7483e2ef6e0e444f6fa38c52e8e1bb10953e6..60d9bc5efdfee701dee85f1dfcf716818e0d85f2 100644 (file)
@@ -467,6 +467,30 @@ sub read_args
 }
 
 
+sub get_time
+{
+    # Martin A. Hansen, July 2008.
+
+    # Get current time as a number.
+
+    # Returns a number.
+
+    return time;
+}
+
+
+sub get_processid
+{
+    # Martin A. Hansen, July 2008.
+
+    # Get process id for current process.
+
+    # Returns a number.
+
+    return $$;
+}
+
+
 sub get_sessionid
 {
     # Martin A. Hansen, April 2008.
@@ -475,7 +499,19 @@ sub get_sessionid
 
     # Returns a number
 
-    return time . $$;
+    return get_time . get_processid;
+}
+
+
+sub get_user
+{
+    # Martin A. Hansen, July 2008.
+
+    # Return the user name of the current user.
+
+    # Returns a string.
+
+    return $ENV{ 'USER' };
 }
 
 
@@ -486,18 +522,19 @@ sub get_tmpdir
     # Create a temporary directory based on
     # $ENV{ 'BP_TMP' } and sessionid.
 
+    # this thing is a really bad solution and needs to be removed.
+
     # Returns a path.
 
-    my ( $user, $sid, $path );
+    my ( $user, $sid, $pid, $path );
 
     Maasha::Common::error( qq(no BP_TMP set in %ENV) ) if not -d $ENV{ 'BP_TMP' };
 
-    $user = $ENV{ 'USER' };
-    $user =~ s/\.//g;
-
+    $user = Maasha::Common::get_user();
     $sid  = Maasha::Common::get_sessionid();
+    $pid  = Maasha::Common::get_processid();
 
-    $path = "$ENV{ 'BP_TMP' }/$user\_$sid";
+    $path = "$ENV{ 'BP_TMP' }/" . join( "_", $user, $sid, $pid, "bp_tmp" );
     
     Maasha::Common::dir_create( $path );
 
@@ -655,6 +692,31 @@ sub time_stamp
 }
 
 
+sub process_running
+{
+    # Martin A. Hansen, July 2008.
+
+    # Given a process ID check if it is running
+    # on the system. Return 1 if the process is
+    # running else 0.
+
+    my ( $pid,   # process ID to check.
+       ) = @_;
+
+    # Returns boolean
+
+    my ( @ps_table );
+    
+    @ps_table = run_and_return( "ps", " a" );
+
+    if ( grep /^\s*$pid\s+/, @ps_table ) {
+        return 1;
+    } else {
+        return 0;
+    }
+}
+
+
 sub wrap_line
 {
     # Martin A. Hansen, May 2005
index 5b0bd0907a6cc6f49d089b0986fb75659cb481a8..5acbdfee9a667fdb597120ef62cc4065c69e9a6b 100644 (file)
@@ -34,6 +34,7 @@ use Storable qw( dclone );
 use Maasha::Common;
 use Maasha::Fasta;
 use Maasha::Seq;
+use Maasha::Biopieces;
 use vars qw ( @ISA @EXPORT );
 
 use constant {
@@ -146,7 +147,7 @@ sub match_vmatch
 
     # Returns a string.
 
-    my ( $query_file, $result_file, @result_files, $fh_in, $fh_out, $line, @fields, $i, $record, $vmatch_args, @index_names, @seq_names, $count_list );
+    my ( $query_file, $result_file, @result_files, $fh_in, $fh_out, $line, @fields, $i, $record, $vmatch_args, @index_names, @seq_names, $count_list, $entry );
 
     $query_file  = "$tmp_dir/query.seq";
     $result_file = "$tmp_dir/vmatch.out";
@@ -155,13 +156,13 @@ sub match_vmatch
 
     foreach $record ( @{ $records } ) 
     {
-        if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
+        if ( $entry = record2fasta( $record ) )
         {
-            next if length $record->{ "SEQ" } < 12; # assuming that the index is created for 12 as minimum length
+            next if length $entry->[ SEQ ] < 12; # assuming that the index is created for 12 as minimum length
 
-            push @seq_names, $record->{ "SEQ_NAME" };
+            push @seq_names, $entry->[ SEQ_NAME ];
 
-            Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out, 80 );
+            Maasha::Fasta::put_entry( $entry, $fh_out, 100 );
         }
     }
 
index dfc5255915804bbe02d4ea2e5a0366258ae247fc..9964d881edfc0cc78c461883f0a833659ca93d8a 100644 (file)
@@ -73,7 +73,7 @@ sub bed_get_entry
 
     $line = <$fh>;
 
-    $line =~ s/(\n|\r)$//g;    # some people have carriage returns in their BED files -> Grrrr
+    $line =~ tr/\n\r//d;    # some people have carriage returns in their BED files -> Grrrr
 
     return if not defined $line;