File Coverage

blib/lib/Bio/Polloc/Rule/pattern.pm
Criterion Covered Total %
statement 26 73 35.6
branch 2 30 6.6
condition n/a
subroutine 7 12 58.3
pod 3 3 100.0
total 38 118 32.2


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Bio::Polloc::Rule::pattern - A rule determined by a pattern
4              
5             =head1 AUTHOR - Luis M. Rodriguez-R
6              
7             Email lmrodriguezr at gmail dot com
8              
9             =head1 IMPLEMENTS OR EXTENDS
10              
11             =over
12              
13             =item *
14              
15             L<Bio::Polloc::RuleI>
16              
17             =back
18              
19             =cut
20              
21             package Bio::Polloc::Rule::pattern;
22 1     1   768 use base qw(Bio::Polloc::RuleI);
  1         2  
  1         606  
23 1     1   7 use strict;
  1         3  
  1         32  
24 1     1   1193 use Bio::SeqIO;
  1         65199  
  1         39  
25 1     1   769 use Bio::Polloc::LocusI;
  1         4  
  1         32  
26 1     1   7 use Bio::Polloc::Polloc::IO;
  1         2  
  1         21  
27 1     1   6 use List::Util qw(min max);
  1         2  
  1         969  
28             our $VERSION = 1.0503; # [a-version] from Bio::Polloc::Polloc::Version
29              
30              
31             =head1 PUBLIC METHODS
32              
33             Methods provided by the package
34              
35             =cut
36              
37             =head2 new
38              
39             The basic initialization method
40              
41             =cut
42              
43             sub new {
44 0     0 1 0 my($caller,@args) = @_;
45 0         0 my $self = $caller->SUPER::new(@args);
46 0         0 $self->_initialize(@args);
47 0         0 return $self;
48             }
49              
50             =head2 execute
51              
52             Executes the search and returns the loci.
53              
54             =head3 Arguments
55              
56             The sequence (C<-seq>) as a L<Bio::Seq> or a L<Bio::SeqIO> object.
57              
58             =head3 Returns
59              
60             An array reference populated with L<Bio::Polloc::Locus::pattern> objects.
61              
62             =cut
63              
64             sub execute {
65 0     0 1 0 my($self,@args) = @_;
66 0         0 my($seq) = $self->_rearrange([qw(SEQ)], @args);
67            
68 0 0       0 $self->throw("You must provide a sequence to evaluate the rule", $seq) unless $seq;
69 0 0       0 $self->throw("You must provide an object as sequence", $seq)
70             unless UNIVERSAL::can($seq,'isa');
71            
72             # For Bio::SeqIO objects
73 0 0       0 if($seq->isa('Bio::SeqIO')){
74 0         0 my @feats = ();
75 0         0 while(my $s = $seq->next_seq){
76 0         0 push(@feats, @{$self->execute(-seq=>$s)})
  0         0  
77             }
78 0 0       0 return wantarray ? @feats : \@feats;
79             }
80              
81 0 0       0 $self->throw("Illegal class of sequence '".ref($seq)."'", $seq) unless $seq->isa('Bio::Seq');
82              
83 0         0 my $io = Bio::Polloc::Polloc::IO->new();
84 0         0 my @cmd = ();
85            
86             # fuzznuc
87 0         0 $self->source('fuzznuc');
88 0 0       0 my $fuzznuc = $self->_executable(defined $self->ruleset ? $self->ruleset->value('path') : undef)
    0          
89             or $self->throw("Could not find the fuzznuc binary");
90 0         0 push @cmd, $fuzznuc;
91              
92             # Input sequence
93 0         0 my($seq_fh, $seq_file) = $io->tempfile;
94 0         0 close $seq_fh;
95 0         0 my $seqO = Bio::SeqIO->new(-file=>">$seq_file", -format=>'Fasta');
96 0         0 $seqO->write_seq($seq);
97 0         0 push @cmd, "-sequence", $seq_file;
98              
99             # Pattern
100 0         0 my $pattern = $self->_search_value('pattern');
101 0 0       0 $pattern or $self->throw("You must set the pattern using the -pattern key on value()");
102 0         0 push @cmd, "-pattern", $pattern;
103              
104             # Output file
105 0         0 push @cmd, "-complement", "-auto", "-stdout", "-rformat2", "gff", "2>&1", "|";
106              
107             # Run it;
108 0         0 $self->debug("Running: ".join(" ", @cmd));
109 0         0 my $run = Bio::Polloc::Polloc::IO->new(-file=>join(" ", @cmd));
110 0         0 my @loci = ();
111 0         0 while(my $ln = $run->_readline){
112 0         0 chomp $ln;
113 0 0       0 next if $ln =~ /^#/;
114 0 0       0 next if $ln =~ /^\s*$/;
115 0 0       0 next if $ln =~ /^Error: Unable to read feature tags data.*/; # not really important
116 0         0 my @l = split /\t/, $ln;
117 0         0 my $id = $self->_next_child_id;
118 0 0       0 push @loci, Bio::Polloc::LocusI->new(
119             -type=>$self->type,
120             -rule=>$self, -seq=>$seq,
121             -from=>min($l[3], $l[4]), -to=>max($l[3], $l[4]), # Because of Gff2
122             -strand=>$l[6],
123             -name=>$self->name,
124             -id=>(defined $id ? $id : ""),
125             -pattern=>$pattern,
126             -score=>$l[5]+0);
127             }
128 0         0 $run->close();
129 0         0 return \@loci;
130             }
131              
132             =head2 stringify_value
133              
134             Produces a string with the value of the rule.
135              
136             =cut
137              
138 0     0 1 0 sub stringify_value { return "pattern=>" . (shift->_search_value('pattern')) }
139              
140             =head2 value
141              
142             =head3 Arguments
143              
144             A I<str>, I<hashref> or I<arrayref>. The supported keys are:
145              
146             =over
147              
148             =item -pattern I<str>
149              
150             The pattern to be identified. For example: C<TAT[TA]AC>.
151              
152             =back
153              
154             =head3 Return
155              
156             Value (hashref or undef).
157              
158             =head1 INTERNAL METHODS
159              
160             Methods intended to be used only within the scope of Bio::Polloc::*
161              
162             =head2 _parameters
163              
164             =cut
165              
166 0     0   0 sub _parameters { return [qw(PATTERN)] }
167              
168             =head2 _executable
169              
170             Gets the executable of fuzznuc.
171              
172             =cut
173              
174             sub _executable {
175 1     1   48579 my($self, $path) = @_;
176 1         4 my $name = "fuzznuc";
177 1         2 my $exe;
178 1         3 my $io = "Bio::Polloc::Polloc::IO";
179 1         22 $self->debug("Searching for the $name binary for $^O");
180 1 50       4 if($path){
181 0 0       0 $exe = $io->exists_exe($io->catfile($path, $name)) unless $exe;
182             }
183 1 50       12 $exe = $io->exists_exe($name) unless $exe;
184 1         3 return $exe;
185             }
186              
187             =head2 _initialize
188              
189             =cut
190              
191             sub _initialize {
192 0     0     my($self,@args) = @_;
193 0           $self->type('pattern');
194             }
195              
196             1;