The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

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.1115

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

    run

    check_positive_integer
    
    filename_prefix
    check_all_files_exist
    check_in_out_dir 
    rm_and_mkdir

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);

run

Run a command

Example:

    run('date');

check_positive_integer

Check Positive Integer

Example:

    rcheck_positive_integer(1);

check_positive_integer

Get filename prefix

Example:

    filename_prefix("test.fa"); # "test"
    filename_prefix("tmp");     # "tmp"

check_all_files_exist

    Check whether all files existed.

check_in_out_dir

    Check in and out directory.

Example:

    check_in_out_dir("~/dir", "~/dir.out");

rm_and_mkdir

    Make a directory, remove it firstly if it exists.

Example:

    rm_and_mkdir("out")