File Coverage

blib/lib/Bio/ViennaNGS/Fasta.pm
Criterion Covered Total %
statement 21 61 34.4
branch 0 22 0.0
condition 0 3 0.0
subroutine 7 12 58.3
pod 1 2 50.0
total 29 100 29.0


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