File Coverage

blib/lib/App/RecordStream/Operation/fromfasta.pm
Criterion Covered Total %
statement 37 41 90.2
branch 8 8 100.0
condition n/a
subroutine 7 8 87.5
pod 0 5 0.0
total 52 62 83.8


line stmt bran cond sub pod time code
1             package App::RecordStream::Operation::fromfasta;
2              
3 1     1   140408 use strict;
  1         3  
  1         25  
4 1     1   4 use warnings;
  1         2  
  1         30  
5              
6 1     1   4 use base qw(App::RecordStream::Operation);
  1         2  
  1         363  
7              
8             sub init {
9 3     3 0 9283 my $self = shift;
10 3         5 my $args = shift;
11              
12 3         5 my $oneline = 0;
13 3         7 my %options = (
14             "oneline" => \$oneline,
15             );
16              
17 3         13 $self->parse_options($args, \%options);
18              
19 3         1209 $self->{ONELINE} = $oneline;
20             }
21              
22             sub accept_line {
23 55     55 0 1675 my $self = shift;
24 55         74 my $line = shift;
25              
26 55 100       141 if ($line =~ /^>\s*(.*?)\s*$/) {
27 25         49 my $name = $1;
28 25         73 my ($id, $desc) = split /\h/, $name, 2;
29 25         61 $self->push_accumulated_record;
30 25         55 $self->{RECORD} = App::RecordStream::Record->new(
31             name => $name,
32             id => $id,
33             description => $desc,
34             _filename => $self->get_current_filename,
35             );
36             } else {
37 30 100       75 $self->{SEQUENCE} .= $line . ($self->{ONELINE} ? "" : "\n");
38             }
39              
40 55         336 return 1;
41             }
42              
43             sub push_accumulated_record {
44 28     28 0 35 my $self = shift;
45 28 100       57 if ( my $record = delete $self->{RECORD} ) {
46 25         34 my $seq = delete $self->{SEQUENCE};
47 25 100       51 chomp $seq if defined $seq;
48 25         60 $record->set( sequence => $seq );
49              
50 25         166 my $filename = $self->get_current_filename;
51 25         99 $self->update_current_filename( delete $record->{_filename} );
52 25         172 $self->push_record( $record );
53 25         1256 $self->update_current_filename( $filename );
54 25         147 return 1;
55             } else {
56 3         6 return 0;
57             }
58             }
59              
60             sub stream_done {
61 3     3 0 33 my $self = shift;
62 3         5 $self->push_accumulated_record;
63             }
64              
65             sub usage {
66 0     0 0   my $self = shift;
67              
68 0           my $options = [
69             [ 'oneline', 'Strip any newlines from the sequence so it is one long line'],
70             ];
71              
72 0           my $args_string = $self->options_string($options);
73              
74 0           return <
75             Usage: recs-fromfasta []
76             __FORMAT_TEXT__
77             Each sequence from the FASTA input files (or stdin) produces an output
78             record with the keys name and sequence. Each sequence name is also split into
79             id and description on the first whitespace, if any.
80             __FORMAT_TEXT__
81              
82             Arguments:
83             $args_string
84              
85             Examples:
86             Parse a FASTA file into records, stripping newlines in the sequence
87             recs-fromfasta --oneline < example.fasta
88             USAGE
89             }
90              
91             1;