]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_perl/Maasha/Filesys.pm
fixed bug in Fasta.pm
[biopieces.git] / code_perl / Maasha / Filesys.pm
index cb27c95ff62d4544f6c0c607d085e76ce5aca875..b545609787529c3f991efe13853fd8bd49c68d43 100644 (file)
@@ -31,6 +31,7 @@ package Maasha::Filesys;
 
 use strict;
 use IO::File;
+use Storable;
 use Maasha::Common;
 
 use Exporter;
@@ -66,6 +67,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 +203,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.