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   111712 use strict;
  1         3  
  1         24  
4 1     1   4 use warnings;
  1         11  
  1         26  
5              
6 1     1   5 use base qw(App::RecordStream::Operation);
  1         2  
  1         350  
7              
8             sub init {
9 3     3 0 12118 my $self = shift;
10 3         6 my $args = shift;
11              
12 3         6 my $oneline = 0;
13 3         9 my %options = (
14             "oneline" => \$oneline,
15             );
16              
17 3         15 $self->parse_options($args, \%options);
18              
19 3         1481 $self->{ONELINE} = $oneline;
20             }
21              
22             sub accept_line {
23 55     55 0 2081 my $self = shift;
24 55         94 my $line = shift;
25              
26 55 100       163 if ($line =~ /^>\s*(.*?)\s*$/) {
27 25         53 my $name = $1;
28 25         99 my ($id, $desc) = split /\h/, $name, 2;
29 25         61 $self->push_accumulated_record;
30 25         98 $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       85 $self->{SEQUENCE} .= $line . ($self->{ONELINE} ? "" : "\n");
38             }
39              
40 55         418 return 1;
41             }
42              
43             sub push_accumulated_record {
44 28     28 0 46 my $self = shift;
45 28 100       66 if ( my $record = delete $self->{RECORD} ) {
46 25         44 my $seq = delete $self->{SEQUENCE};
47 25 100       74 chomp $seq if defined $seq;
48 25         71 $record->set( sequence => $seq );
49              
50 25         188 my $filename = $self->get_current_filename;
51 25         117 $self->update_current_filename( delete $record->{_filename} );
52 25         196 $self->push_record( $record );
53 25         1332 $self->update_current_filename( $filename );
54 25         163 return 1;
55             } else {
56 3         8 return 0;
57             }
58             }
59              
60             sub stream_done {
61 3     3 0 36 my $self = shift;
62 3         8 $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;