NAME

BioUtil::Util - Utilities for operation on data or file

Some great modules like BioPerl provide many robust solutions. However, it is not easy to install for someone in some platforms. And for some simple task scripts, a lite module may be a good choice. So I reinvented some wheels and added some useful utilities into this module, hoping it would be helpful.

VERSION

Version 2014.0815

EXPORT

get_file_list

extract_parameters_from_string
get_parameters_from_file

get_list_from_file
get_column_data

read_json_file
write_json_file

SYNOPSIS

use BioUtil::Util;

SUBROUTINES/METHODS

get_file_list

Find files/directories with custom filter, max serach depth could be specified.

Example (searching perl scripts)

my $dir   = "~";
my $depth = 2;

my $list = get_file_list(
    $dir,
    sub {
        if ( -d or /^\./i ) {  # ignore configuration file and folders
            return 0;
        }
        if (/\.pm/i or /\.pl/i) {
            return 1;
        }
        return 0;
    },
    $depth
);
print "$_\n" for @$list;

extract_parameters_from_string

Extract parameters from string.

The regular expression is

/([\w\d\_\-\.]+)\s*=\s*([^\=;]*)[\s;]*/

Example:

# bad format, but could also be parsed
# my $s = " s = b; a=test; b_c=12 3; a.b =; b
# = asdf
# sd; ads-f = 12313";

# recommended
my $s = "key1=abcde; key2=123; conf.a=file; conf.b=12; ";

my $pa = extract_parameters_from_string($s);
print "=$_:$$p{$_}=\n" for sort keys %$pa;

get_parameters_from_file

Get parameters from a file. Comments start with # are allowed in file.

Example:

my $pa = get_parameters_from_file("d.txt");
print "$_: $$pa{$_}\n" for sort keys %$pa;

For a file with content:

# cell phone 
apple = 1 # note

nokia = 2 #

output is:

apple: 1
nokia: 2

get_list_from_file

Get list from a file. Comments start with # are allowed in file.

Example:

my $list = get_list_from_file("d.txt");
print "$_\n" for @$list;

For a file with content:

# cell phone 
apple # note

nokia

output is:

apple
nokia

get_column_data

Get one column of a file.

Example:

my $list = get_column_data("d.txt", 2);
print "$_\n" for @$list;

read_json_file

Read json file and decode it into a hash ref.

Example:

my $hashref = read_json_file($file);

write_json_file

Write a hash ref into a file.

Example:

my $hashref = { "a" => 1, "b" => 2 };
write_json_file($hashref, $file);