blib/lib/Bioinfo/Fasta.pm untested

filter: Statementstmt 0.0% 0 / 46 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 83 CC 7 SCAR 20.8
CC
7
Cov
0%
fa2hs
20.0
new
6.0
get_id_seq
6.0
CRAP 8.0
1 package Bioinfo::Fasta;
2 use strict;
3 use warnings;
4 use Exporter 'import';
5 our @EXPORT_OK = qw(fa2hs);
6  
7 our $VERSION = '0.1.14'; # VERSION:
8 # ABSTRACT: get one or more sequences from a FASTA file quickly.
9  
10  
11 sub new {
12 my $class = shift;
13 my $self = ref $_[0] ? $_[0] : {@_};
14 $self->{file} || die `file parameter is not exist`;
15 bless $self, $class;
16 }
17  
18  
19 sub fa2hs {
20 my $fa = ref $_[0] ? $_[0]->{file} : $_[0];
21 open my $file, "<", "$fa" or die "Can not open $fa $!";
22 my (%hs, $name);
23 while (my $line = <$file>) {
24 chomp($line);
25 #if ($line =~/^>(.+?)\s*/) {
26 #$name = $1;
27 if ($line =~/^>/) {
28 $name = $line =~/^>(.+?)\s+/ ? $1 : $line;
29 $name =~s/>//;
30 } else {
31 $hs{$name} .= $line;
32 }
33 }
34 close($file);
35 return \%hs;
36 }
37  
38 sub _initialize {
39 my $self = shift;
40 $self->{id2seq} = $self->fa2hs();
41 }
42  
43  
44 sub get_id_seq {
45 my ($self, $id) = @_;
46 $self->_initialize() unless (exists $self->{id2seq});
47 return ">$id\n" . $self->{id2seq}->{$id} . "\n";
48 }
49  
50  
51 sub get_seq {
52 my ($self, $id) = @_;
53 $self->_initialize() unless (exists $self->{id2seq});
54 return $self->{id2seq}{$id};
55 }
56  
57  
58 sub get_seqs_batch {
59 my ($self, $id_file, $outfile) = @_;
60 open my $IN, "<", $id_file or die "Can not open $id_file $!";
61 my @ids = <$IN>;
62 chomp @ids;
63 close($IN);
64 open my $OUT, ">", $outfile or die "Can not open $outfile $!";
65 for my $id (@ids) {
66 print $OUT $self->get_id_seq($id);
67 }
68 close($OUT);
69 }
70  
71 # may be use later
72 sub rename_seqs {
73
74 }
75  
76 1;
77  
78 __END__
79  
80 =pod
81  
82 =encoding UTF-8
83  
84 =head1 NAME
85  
86 Bioinfo::Fasta - get one or more sequences from a FASTA file quickly.
87  
88 =head1 VERSION
89  
90 version 0.1.14
91  
92 =head1 SYNOPSIS
93  
94 # use it in object-oriented way;
95
96 use Bioinfo::Fasta;
97 my $obj = Bioinfo::Fasta->new(file => "test.fa");
98 my $hs = $obj->fa2hs; # get a HashRef coverted from the test.fa
99 my $seq = $obj->get_seq("seq_id"); # get the sequence of "seq_id"(only the sequence)
100 my $seq_fa = $obj->get_id_seq("seq_id"); # get the sequence of "seq_id"(in the FASTA format)
101 $obj->get_seqs_batch('id_list.txt', 'output.fa'); # extract specified sequence to output file
102  
103 # use it in Common mode;
104  
105 use Bioinfo::Fasta "fa2hs";
106 my $hs = fa2hs("in.fa");
107  
108 =head1 DESCRIPTION
109  
110 Currently, there do have some modules that can operate the FASTA file such as Bio::SeqIO,
111 But it only provide some basic operation to obtain the information about sequence. In my daily work,
112 I still have to write some repetitive code. So this module is write to perform a deeper wrapper for operating FASTA file
113 Notice: this module is not suitable for the FASTA file that is extremble big.
114  
115 =head1 METHODS
116  
117 =head2 fa2hs
118  
119 Convert a fasta file into a HashRef variable. If the C<file> parameter have been passed
120 into during the process of new, here needs no parameter, otherwise you have to provide
121 the path of a fasta file.
122  
123 =head2 get_id_seq
124  
125 get a sequence by a id in FASTA format
126  
127 =head2 get_seq
128  
129 get a sequence by a id
130  
131 =head2 get_seqs_batch
132  
133 extract specified gene list from input file
134  
135 =head1 AUTHOR
136  
137 Yan Xueqing <yanxueqing621@163.com>
138  
139 =head1 COPYRIGHT AND LICENSE
140  
141 This software is copyright (c) 2017 by Yan Xueqing.
142  
143 This is free software; you can redistribute it and/or modify it under
144 the same terms as the Perl 5 programming language system itself.
145  
146 =cut