]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_perl/Maasha/Filesys.pm
fixed bug in plot_scores
[biopieces.git] / code_perl / Maasha / Filesys.pm
index cb27c95ff62d4544f6c0c607d085e76ce5aca875..5c26fdd777471965af6d60c1f9e90a2b44ee5927 100644 (file)
@@ -29,9 +29,13 @@ package Maasha::Filesys;
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 
+use warnings;
 use strict;
 use IO::File;
+use Storable;
+use Data::Dumper;
 use Maasha::Common;
+use Digest::MD5;
 
 use Exporter;
 
@@ -66,6 +70,46 @@ sub file_read_open
 }
 
 
+sub files_read_open
+{
+    # Martin A. Hansen, May 2009.
+
+    # Cats a number of files and returns a filehandle.
+
+    my ( $files,   # full path to file
+       ) = @_;
+
+    # returns filehandle
+
+    my ( $file, $fh, $type, %type_hash, $file_string );
+
+    foreach $file ( @{ $files } )
+    {
+        Maasha::Common::error( qq(No such file: $file) ) if not -f $file;
+    
+        $type = `file $file`;
+
+        if ( $type =~ /gzip compressed/ ) {
+            $type_hash{ 'gzip' } = 1;
+        } else {
+            $type_hash{ 'ascii' } = 1;
+        }
+    }
+
+    Maasha::Common::error( qq(Mixture of zipped and unzipped files) ) if scalar keys %type_hash > 1;
+
+    $file_string = join " ", @{ $files };
+
+    if ( $type =~ /gzip compressed/ ) {
+        $fh = new IO::File "zcat $file_string|" or Maasha::Common::error( qq(Could not open pipe: $!) );
+    } else {
+        $fh = new IO::File "cat $file_string|" or Maasha::Common::error( qq(Could not open pipe: $!) );
+    }
+
+    return $fh;
+}
+
+
 sub file_write_open
 {
     # Martin A. Hansen, January 2004.
@@ -162,6 +206,38 @@ sub file_read
 }
 
 
+sub file_store
+{
+    # Martin A. Hansen, December 2004.
+
+    # writes a data structure to file.
+
+    my ( $path,      # full path to file
+         $data,      # data structure
+       ) = @_;
+    
+    Storable::store( $data, $path ) or Maasha::Common::error( qq(Could not write-open file "$path": $!) );
+}
+
+
+sub file_retrieve
+{
+    # Martin A. Hansen, December 2004.
+
+    # retrieves hash data structure
+    # (this routines needs to test if its a hash, array or else)
+
+    my ( $path,   # full path to data file
+       ) = @_;
+
+    my ( $data );
+
+    $data = Storable::retrieve( $path ) or Maasha::Common::error( qq(Could not read-open file "$path": $!) );
+
+    return wantarray ? %{ $data } : $data;
+}
+
+
 sub file_copy
 {
     # Martin A. Hansen, November 2008.
@@ -228,12 +304,37 @@ sub file_size
 
     # returns integer
 
-    my $file_size = ( stat ( $path ) )[ 7 ];
+    my $file_size = -s $path;
 
     return $file_size;
 }
 
 
+sub file_md5
+{
+    # Martin A. Hansen, December 2009.
+
+    # Get an MD5 sum for a given file.
+
+    my ( $file,   # file path
+       ) = @_;
+
+    # Returns a string.
+
+    my ( $fh, $md5 );
+
+    $fh = file_read_open( $file );
+
+    $md5 = Digest::MD5->new;
+
+    $md5->addfile( $fh );
+
+    close $fh;
+
+    return $md5->hexdigest;
+}
+
+
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DIRECTORIES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 
@@ -246,13 +347,15 @@ sub dir_create
     my ( $path,   # full path to dir
        ) = @_;
 
-    # Returns nothing.
+    # Returns created path.
 
     if ( -d $path ) {
-        Maasha::Common::error( qq(Directory already exists "$path": $!) );
+        Maasha::Common::error( qq(Directory already exists "$path") );
     } else {
         mkdir $path or Maasha::Common::error( qq(Could not create directory "$path": $!) );
     }
+
+    return $path;
 }
 
 
@@ -265,11 +368,13 @@ sub dir_create_if_not_exists
     my ( $path,   # full path to dir
        ) = @_;
 
-    # Returns nothing.
+    # Returns path.
 
     if ( not -d $path ) {
         mkdir $path or Maasha::Common::error( qq(Could not create directory "$path": $!) );
     }
+
+    return $path;
 }
 
 
@@ -308,10 +413,39 @@ sub ls_dirs
 
     close $dh;
 
+    @dirs = sort @dirs;
+
     return wantarray ? @dirs : \@dirs;
 }
 
 
+sub ls_dirs_base
+{
+    # Martin A. Hansen, November 2009.
+
+    # Returns all directory basenames execpt . and ..
+    # from a given directory.
+
+    my ( $path,
+       ) = @_;
+
+    # Returns a list.
+
+    my ( @dirs, $dir, @list );
+
+    @dirs = Maasha::Filesys::ls_dirs( $path );
+
+    foreach $dir ( @dirs )
+    {
+        next if $dir =~ /\/\.\.?$/;
+
+        push @list, ( split "/", $dir )[ -1 ];
+    }
+
+    return wantarray ? @list : \@list;
+}
+
+
 sub ls_files
 {
     # Martin A. Hansen, June 2007.