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   196148 use strict;
  1         4  
  1         39  
4 1     1   9 use warnings;
  1         4  
  1         47  
5              
6 1     1   8 use base qw(App::RecordStream::Operation);
  1         4  
  1         619  
7              
8             sub init {
9 3     3 0 17323 my $self = shift;
10 3         8 my $args = shift;
11              
12 3         6 my $oneline = 0;
13 3         13 my %options = (
14             "oneline" => \$oneline,
15             );
16              
17 3         22 $self->parse_options($args, \%options);
18              
19 3         1783 $self->{ONELINE} = $oneline;
20             }
21              
22             sub accept_line {
23 55     55 0 2647 my $self = shift;
24 55         95 my $line = shift;
25              
26 55 100       191 if ($line =~ /^>\s*(.*?)\s*$/) {
27 25         65 my $name = $1;
28 25         84 my ($id, $desc) = split /\h/, $name, 2;
29 25         80 $self->push_accumulated_record;
30 25         81 $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       94 $self->{SEQUENCE} .= $line . ($self->{ONELINE} ? "" : "\n");
38             }
39              
40 55         460 return 1;
41             }
42              
43             sub push_accumulated_record {
44 28     28 0 49 my $self = shift;
45 28 100       77 if ( my $record = delete $self->{RECORD} ) {
46 25         51 my $seq = delete $self->{SEQUENCE};
47 25 100       73 chomp $seq if defined $seq;
48 25         92 $record->set( sequence => $seq );
49              
50 25         222 my $filename = $self->get_current_filename;
51 25         140 $self->update_current_filename( delete $record->{_filename} );
52 25         230 $self->push_record( $record );
53 25         1868 $self->update_current_filename( $filename );
54 25         191 return 1;
55             } else {
56 3         8 return 0;
57             }
58             }
59              
60             sub stream_done {
61 3     3 0 49 my $self = shift;
62 3         11 $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;