File Coverage

blib/lib/Bio/SeqHash.pm
Criterion Covered Total %
statement 9 38 23.6
branch 0 18 0.0
condition n/a
subroutine 3 10 30.0
pod 4 6 66.6
total 16 72 22.2


line stmt bran cond sub pod time code
1             package Bio::SeqHash;
2 1     1   61106 use strict;
  1         9  
  1         26  
3 1     1   5 use warnings;
  1         1  
  1         31  
4 1     1   3 use Exporter 'import';
  1         2  
  1         444  
5             our @EXPORT_OK = qw(fa2hs);
6              
7             our $VERSION = '0.1.4'; # VERSION:
8             # ABSTRACT: get one or more sequences from a FASTA file quickly.
9              
10              
11             sub new {
12 0     0 0   my $class = shift;
13 0 0         my $self = ref $_[0] ? $_[0] : {@_};
14 0 0         $self->{file} || die `file parameter is not exist`;
15 0           bless $self, $class;
16             }
17              
18              
19             sub fa2hs {
20 0 0   0 1   my $fa = ref $_[0] ? $_[0]->{file} : $_[0];
21 0 0         open my $file, "<", "$fa" or die "Can not open $fa $!";
22 0           my (%hs, $name);
23 0           while (my $line = <$file>) {
24 0           chomp($line);
25 0 0         if ($line =~/^>(\S+)/) {
26 0           $name = $1;
27             } else {
28 0           $hs{$name} .= $line;
29             }
30             }
31 0           close($file);
32 0           return \%hs;
33             }
34              
35             sub _initialize {
36 0     0     my $self = shift;
37 0           $self->{id2seq} = $self->fa2hs();
38             }
39              
40              
41             sub get_id_seq {
42 0     0 1   my ($self, $id) = @_;
43 0 0         $self->_initialize() unless (exists $self->{id2seq});
44 0           return ">$id\n" . $self->{id2seq}->{$id} . "\n";
45             }
46              
47              
48             sub get_seq {
49 0     0 1   my ($self, $id) = @_;
50 0 0         $self->_initialize() unless (exists $self->{id2seq});
51 0           return $self->{id2seq}{$id};
52             }
53              
54              
55             sub get_seqs_batch {
56 0     0 1   my ($self, $id_file, $outfile) = @_;
57 0 0         open my $IN, "<", $id_file or die "Can not open $id_file $!";
58 0           my @ids = <$IN>;
59 0           chomp @ids;
60 0 0         open my $OUT, ">", $outfile or die "Can not open $outfile $!";
61 0           for my $id (@ids) {
62 0           print $OUT $self->get_id_seq($id);
63             }
64             }
65              
66             # may be use later
67       0 0   sub rename_seqs {
68            
69             }
70              
71             1;
72              
73             __END__