NAME
BioUtil::Seq - Utilities for sequence
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.0814
EXPORT
FastaReader
read_sequence_from_fasta_file
write_sequence_to_fasta_file
format_seq
validate_sequence
revcom
base_content
dna2peptide
codon2aa
generate_random_seqence
shuffle_sequences
rename_fasta_header
clean_fasta_header
SYNOPSIS
use BioUtil::Seq;
SUBROUTINES/METHODS
FastaReader
FastaReader is a fasta file parser using closure. FastaReader returns an anonymous subroutine, when called, it will return a fasta record which is reference of an array containing fasta header and sequence.
A boolean argument is optional. If set as "true", "return" ("\r") and "new line" ("\n") symbols in sequence will not be trimed.
Example:
# my $next_seq = FastaReader("test.fa", 1);
my $next_seq = FastaReader("test.fa");
while ( my $fa = &$next_seq() ) {
my ( $header, $seq ) = @$fa;
print ">$header\n$seq\n";
}
read_sequence_from_fasta_file
Read all sequences from fasta file.
Example:
my $seqs = read_sequence_from_fasta_file($file);
for my $header (keys %$seqs) {
my $seq = $$seqs{$header};
print ">$header\n$seq\n";
}
write_sequence_to_fasta_file
Example:
my $seq = {"seq1" => "acgagaggag"};
write_sequence_to_fasta_file($seq, "seq.fa");
format_seq
Format sequence to readable text
Example:
my $seq = {"seq1" => "acgagaggag"};
write_sequence_to_fasta_file($seq, "seq.fa");
validate_sequence
Validate a sequence.
Legale symbols:
DNA: ACGTRYSWKMBDHV
RNA: ACGURYSWKMBDHV
Protein: ACDEFGHIKLMNPQRSTVWY
gap and space: - *.
Example:
if (validate_sequence($seq)) {
# do some thing
}
revcom
Reverse complement sequence
my $recom = revcom($seq);
base_content
Example:
my $gc_cotent = base_content('gc', $seq);
dna2peptide
Translate DNA sequence into a peptide
dna2peptide
Translate a DNA 3-character codon to an amino acid
generate_random_seqence
Example:
my @alphabet = qw/a c g t/;
my $seq = generate_random_seqence( \@alphabet, 50 );
shuffle sequences
Example:
shuffle_sequences($file, "$file.shuf.fa");
rename_fasta_header
Rename fasta header with regexp.
Example:
# delete some symbols
my $n = rename_fasta_header('[^a-z\d\s\-\_\(\)\[\]\|]', '', $file, "$file.rename.fa");
print "$n records renamed\n";
clean_fasta_header
Rename given symbols to repalcement string. Because, some symbols in fasta header will cause unexpected result.
Example:
my $file = "test.fa";
my $n = clean_fasta_header($file, "$file.rename.fa");
# replace any symbol in (\/:*?"<>|) with '', i.e. deleting.
# my $n = clean_fasta_header($file, "$file.rename.fa", '', '\/:*?"<>|');
print "$n records renamed\n";