Documentation, examples, tutorials and more

<<

NAME

Smash::Utils::MatrixIO - Utilities for I/O of matrices stored in hashes.

SYNOPSIS

        use Smash::Utils::MatrixIO qw(:all);
        my $hash = read_R_matrix("matrix_file.txt");
        print "Matrix[$a,$b] = ".$hash->{$a}->{$b};
        write_R_matrix("new_matrix_file.txt", $hash);

DESCRIPTION

Smash:Utils::MatrixIO provides functions to read and write matrices stored as hashes.

FUNCTIONS

Matrix/hash read/write methods.

read_R_matrix

read an R format matrix file into a hash

write_R_matrix

write a hash into an R format matrix file

read_phylip_matrix

read a phylip format matrix file into a hash

write_phylip_matrix

write a hash into a phylip format matrix file

read_three_column_matrix

reads a three column text file representing a matrix into a hash. e.g.,

        #sample   species   abundance
        JP-AD-1   Bifidobacterium_longum   0.1234
        JP-AD-1   Bacteroides_caccae       0.0546
will set

        $hash->{JP-AD-1}->{Bifidobacterium_longum} = 0.1234;
        $hash->{JP-AD-1}->{Bacteroides_caccae}     = 0.0546;
write_three_column_matrix

writes a matrix in three columns as shown above.

read_two_column_hash

reads a two column text file representing a simple hash.

write_two_column_hash

writes a simple hash as a two column text file.

Multilevel hash read/write and manipulation methods

read_multi_column_matrix

reads a multiple column text file into a multi-level hash/matrix. For example, a line such as:

        Root    Bacteria        Bacteroidetes   Bacteroides     1
will execute something to the effect of:

        $h->{Root}->{Bacteria}->{Bacteroidetes}->{Bacteroides} = 1;
write_multi_column_matrix

writes a multi-level hash as multi-column text file as shown above.

print_hash($FH, $hash)

writes a multi-level hash to $FH as a multi-column text file.

count_hash

count the total number of elements (leaf nodes if you will) in the multi-level hash.

sum_hash

sums all the numbers at the leaf nodes.

average_hash

gets the average of all the numbers at the leaf nodes.

max_hash

gets the maximum value stored at the leaf nodes.

min_hash

gets the minimum value stored at the leaf nodes.

2-D Matrix manipulation methods

transpose_matrix($m)

returns the transpose matrix of $m

t($m)

short-cut to transpose_matrix($m)

multiply_matrices($a, $b)

returns the matrix product of $a and $b.

scalar_multiply_matrix($h, $scalar)

multiplies the matrix (passed by ref) by the given scalar

merge_matrices($a, $b)

merges the matrices $a and $b. Rows and columns in either one are present in the merged matrix.

zero_fill_matrix($m)

fill the undefined cells with 0.

zero_strip_matrix($m)

removes all cells of the matrix with value 0. If a full row (or column) has 0's all over and they were all removed, the row (or column) will also be removed.

filter_matrix($m, $min_value)

removes all cells of the matrix BELOW $min_value. If a full row (or column) has been removed, the row (or column) will also be removed.

normalize_cols_by_sum($m)

normalizes each value by the column sum.

normalize_rows_by_sum($m)

normalizes each value by the row sum.

merge_matrices($a, $b, ...)

merges all the matrices and returns a new matrix with values from all.

Utility methods

get_column_labels

to_string

typeof

<<