File Coverage

blib/lib/Bio/ViennaNGS/Fasta.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             # -*-CPerl-*-
2             # Last changed Time-stamp: <2014-12-20 00:32:02 mtw>
3              
4             package Bio::ViennaNGS::Fasta;
5              
6 1     1   927 use 5.12.0;
  1         2  
  1         34  
7 1     1   3 use version; our $VERSION = qv('0.12_07');
  1         1  
  1         4  
8 1     1   826 use Bio::Perl 1.00690001;
  0            
  0            
9             use Bio::DB::Fasta;
10             use Moose;
11             use Carp;
12             use Data::Dumper;
13             use namespace::autoclean;
14              
15             has 'fa' => (
16             is => 'rw',
17             isa => 'Str',
18             required => 1,
19             predicate => 'has_fa',
20             );
21              
22             has 'fastadb' => (
23             is => 'rw',
24             isa => 'Bio::DB::Fasta',
25             builder => '_get_fastadb',
26             predicate => 'has_db',
27             lazy => 1,
28             );
29              
30             has 'fastaids' => (
31             is => 'ro',
32             isa => 'ArrayRef',
33             builder => '_get_fastaids',
34             predicate => 'has_ids',
35             lazy => 1,
36             );
37              
38             has 'primaryseq' => (
39             is => 'ro',
40             isa => 'HashRef',
41             builder => '_get_primaryseq',
42             lazy => 1,
43             );
44              
45             before 'primaryseq' => sub{
46             my $self = shift;
47             $self->fastaids;
48             };
49              
50             before 'fastaids' => sub{
51             my $self = shift;
52             $self->fastadb;
53             };
54              
55             sub _get_fastadb {
56             my $self = shift;
57             my $this_function = (caller(0))[3];
58             confess "ERROR [$this_function] Fasta input not available"
59             unless (-f $self->fa);
60             my $db = Bio::DB::Fasta->new($self->fa) or croak $!;
61             return $db;
62             }
63              
64             sub _get_fastaids {
65             my $self = shift;
66             my $this_function = (caller(0))[3];
67             confess "ERROR [$this_function] Attribute 'fastadb' not found $!"
68             unless ($self->has_db);
69             my $db = $self->fastadb or croak $!;
70             my @ids = $db->ids or croak $!;
71             return \@ids;
72             }
73              
74             sub _get_primaryseq {
75             my $self = shift;
76             my $this_function = (caller(0))[3];
77             confess "ERROR [$this_function] Attribute 'fastaids' not found $!"
78             unless ($self->has_ids);
79             my %fobj = ();
80             my $db = $self->fastadb or croak $!;
81             foreach my $id (@{$self->fastaids}) {
82             $fobj{$id} = $db->get_Seq_by_id($id); # Bio::PrimarySeq::Fasta object
83             }
84             return \%fobj;
85             }
86              
87             # stranded_subsequence ($id,$start,$stop,$strand)
88             # retrieve RNA/DNA sequence from a Bio::PrimarySeqI /
89             # Bio::PrimarySeq::Fasta object
90             sub stranded_subsequence {
91             my ($self,$id,$start,$end,$strand) = @_;
92             my ($this_function,$seq,$rc,$p,$obj);
93             $this_function = (caller(0))[3];
94             my @dummy = $self->fastaids;
95             confess "ERROR [$this_function] Attribute 'fastaids' not found $!"
96             unless ($self->has_ids);
97             $p = $self->primaryseq; # Hash of Bio::PrimarySeq::Fasta objects
98             confess "ERROR [$this_function] Fasta ID $id not found in hash $!"
99             unless (exists $$p{$id});
100             $obj = $$p{$id};
101             $seq = $obj->subseq($start => $end);
102             if ($strand eq '-1' || $strand eq '-') {
103             $rc = revcom($seq);
104             $seq = $rc->seq();
105             }
106             # print "id:$id\nstart:$start\nend:$end\n";
107             return $seq;
108             }
109              
110             sub has_sequid {
111             my ($self,$id) = @_;
112             my $ids = $self->fastaids;
113             #$i = grep{$_ eq $id}@{$ids} ? 1 : 0;
114             for my $j (@$ids){
115             if ($id eq $j){return 1;}
116             else {return 0;}
117             }
118             return -1;
119             }
120              
121             __PACKAGE__->meta->make_immutable;
122              
123             1;
124             __END__
125              
126              
127             =head1 NAME
128              
129             Bio::ViennaNGS::Fasta - Moose wrapper for Bio::DB::Fasta
130              
131             =head1 SYNOPSIS
132              
133             use Bio::ViennaNGS::Fasta;
134              
135             my $f = Bio::ViennaNGS::Fasta->new( fa => "data/foo.fa", );
136              
137             # get all FASTA IDs
138             my @ids = $f->fastaids;
139              
140             # get a reference to a hash of Bio::PrimarySeq::Fasta objects whose
141             # keys are the Fasta IDs
142             my $ps = $f->primaryseq;
143              
144             # get the strand-specific genomic sequence for a certain Fasta ID
145             my $id = "chr1";
146             my $start = 287;
147             my $end = 1289;
148             my $strand = "+";
149             my $seq = $foo->stranded_subsequence($id,$start,$end,$strand);
150              
151             =head1 DESCRIPTION
152              
153             This module provides a L<Moose> interface to L<Bio::DB::Fasta>.
154              
155             =head1 METHODS
156              
157             =over
158              
159             =item stranded_subsequence
160              
161             Title : stranded_subsequence
162             Usage : $obj->stranded_subsequence($id,$start,$end,$strand)
163             Function: Returns the DNA/RNA sequence for C<$id> from
164             C<$start> to C<$end>.
165             Args : C<$id> is the Fasta ID (a L<Bio::PrimarySeq::Fasta> object).
166             C<$start> and C<$end> should be self-explnatory, C<$strand>
167             is 1 or -1 for [+] or [-] strand, respectively
168             Returns : A string
169              
170             =back
171              
172             =head1 DEPENDENCIES
173              
174             =over 5
175              
176             =item L<Bio::Perl> >= 1.00690001
177              
178             =item L<Bio::DB::Fasta>
179              
180             =item L<Moose>
181              
182             =item L<Carp>
183              
184             =item L<namespace::autoclean>
185              
186             =back
187              
188             =head1 SEE ALSO
189              
190             =over 2
191              
192             =item L<Bio::ViennaNGS>
193              
194             =item L<Bio::DB::Fasta>
195              
196             =back
197              
198             =head1 AUTHOR
199              
200             Michael T. Wolfinger, E<lt>michael@wolfinger.euE<gt>
201              
202             =head1 COPYRIGHT AND LICENSE
203              
204             Copyright (C) 2014 by Michael T. Wolfinger
205              
206             This library is free software; you can redistribute it and/or modify
207             it under the same terms as Perl itself, either Perl version 5.16.3 or,
208             at your option, any later version of Perl 5 you may have available.
209              
210             This software is distributed in the hope that it will be useful, but
211             WITHOUT ANY WARRANTY; without even the implied warranty of
212             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
213              
214             =cut