]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_perl/Maasha/Common.pm
slight polish of read_fastq
[biopieces.git] / code_perl / Maasha / Common.pm
index 847ff5d3337a8ddd053a3924e7ed89e52e257a5c..0e86c2e431fa4b218899e7739845330d8b2fceea 100644 (file)
@@ -29,6 +29,7 @@ package Maasha::Common;
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 
+use warnings;
 use strict;
 use Carp;
 use Data::Dumper;
@@ -117,6 +118,30 @@ int match_m( char *str, char *substr, size_t str_len, size_t substr_len, size_t
 }
 
 
+void str_analyze_C( const char *string )
+{
+    /* Martin A. Hansen, July 2009 */
+
+    /* Scans a string incrementing the char count in an array. */
+
+    int count[ 256 ] = { 0 };   /* Integer array spanning the ASCII alphabet */
+    int i;
+
+    for ( i = 0; i < strlen( string ); i++ ) {
+        count[ ( int ) string[ i ] ]++;
+    }
+
+    Inline_Stack_Vars;
+    Inline_Stack_Reset;
+
+    for ( i = 0; i < 256; i++ ) {
+        Inline_Stack_Push( sv_2mortal( newSViv( count[ i ] ) ) );
+    }
+
+    Inline_Stack_Done;
+}
+
+
 END_C
 
 
@@ -237,46 +262,6 @@ sub read_open
 }
 
 
-sub read_open_multi
-{
-    # 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 write_open
 {
     # Martin A. Hansen, January 2004.
@@ -334,38 +319,6 @@ sub pipe_open
 }
 
 
-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 read_args
 {
     # Martin A. Hansen, December 2006
@@ -551,9 +504,9 @@ sub get_fields
 
     $line = <$fh>;
 
-    chomp $line;
+    return if not defined $line;
 
-    return if not $line;
+    chomp $line;
 
     $delimiter ||= "\t";
 
@@ -641,6 +594,63 @@ sub time_stamp
 }
 
 
+sub time_stamp_diff
+{
+    # Martin A. Hansen, June 2009.
+
+    # Return the difference between two time stamps in
+    # the time stamp format.
+
+    my ( $t0,   # time stamp 0
+         $t1,   # time stamp 1
+       ) = @_;
+
+    # Returns a time stamp string.
+
+    my ( $year0, $mon0, $day0, $hour0, $min0, $sec0,
+         $year1, $mon1, $day1, $hour1, $min1, $sec1,
+         $year,  $mon,  $day,  $hour,  $min,  $sec );
+
+    $t0 =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/;
+    $year0 = $1;
+    $mon0  = $2;
+    $day0  = $3;
+    $hour0 = $4;
+    $min0  = $5;
+    $sec0  = $6;
+
+    $sec0 += $day0 * 24 * 60 * 60;
+    $sec0 += $hour0 * 60 * 60;;
+    $sec0 += $min0  * 60;
+
+    $t1 =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/;
+    $year1 = $1;
+    $mon1  = $2;
+    $day1  = $3;
+    $hour1 = $4;
+    $min1  = $5;
+    $sec1  = $6;
+
+    $sec1 += $day1 * 24 * 60 * 60;
+    $sec1 += $hour1 * 60 * 60;;
+    $sec1 += $min1  * 60;
+
+    $year = $year1 - $year0;
+    $mon  = $mon1  - $mon0;
+    $day  = $day1  - $day0; 
+
+    $sec  = $sec1 - $sec0;
+
+    $hour = int( $sec / ( 60 * 60 ) );
+    $sec -= $hour * 60 * 60;
+
+    $min  = int( $sec / 60 );
+    $sec -= $min * 60;
+
+    return join( ":", sprintf( "%02d", $hour ), sprintf( "%02d", $min ), sprintf( "%02d", $sec ) );
+}
+
+
 sub process_running
 {
     # Martin A. Hansen, July 2008.
@@ -711,6 +721,29 @@ sub wrap_line
 }
 
 
+sub str_analyze
+{
+    # Martin A. Hansen, July 2009.
+
+    # Analyzes the string composition of a given string.
+
+    my ( $str,   # string to analyze
+       ) = @_;
+
+    # Returns hash
+
+    my ( @composition, %hash, $i );
+
+    @composition = Maasha::Common::str_analyze_C( $str ); 
+
+    for ( $i = 32; $i <= 126; $i++ ) {          # Only include printable chars
+        $hash{ chr $i } = $composition[ $i ]
+    }
+
+    return wantarray ? %hash : \%hash;
+}
+
+
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 1;