File Coverage

blib/lib/App/RecordStream/Operation/tofasta.pm
Criterion Covered Total %
statement 43 47 91.4
branch 8 10 80.0
condition 13 16 81.2
subroutine 6 7 85.7
pod 0 4 0.0
total 70 84 83.3


line stmt bran cond sub pod time code
1             package App::RecordStream::Operation::tofasta;
2              
3 1     1   124452 use strict;
  1         4  
  1         38  
4 1     1   8 use warnings;
  1         4  
  1         36  
5              
6 1     1   8 use base qw(App::RecordStream::Operation);
  1         4  
  1         829  
7              
8             sub init {
9 10     10 0 91 my $self = shift;
10 10         22 my $args = shift;
11              
12 10         29 my ($id, $desc, $seq) = qw(id description sequence);
13             my $spec = {
14             "id|i=s" => \$id,
15             "description|d=s" => \$desc,
16             "sequence|s=s" => \$seq,
17             "width|w=i" => \($self->{WIDTH}),
18             "oneline" => \($self->{ONELINE}),
19 10         71 "passthru" => \($self->{PASSTHRU}),
20             };
21              
22 10         47 $self->parse_options($args, $spec);
23              
24             die "--passthru is incompatible with --oneline and --width\n\n"
25 10 50 33     10834 if $self->{PASSTHRU} and ($self->{ONELINE} or $self->{WIDTH});
      66        
26              
27 10   100     58 $self->{WIDTH} ||= 60;
28              
29 10         34 $self->{KEYS}{id} = $id;
30 10         26 $self->{KEYS}{desc} = $desc;
31 10         92 $self->{KEYS}{seq} = $seq;
32             }
33              
34             sub accept_record {
35 50     50 0 9584 my $self = shift;
36 50         96 my $record = shift;
37              
38 125         4580 my %props = map {; "-$_" => ${$record->guess_key_from_spec($self->{KEYS}{$_})} }
  125         351  
39 150         332 grep { $self->{KEYS}{$_} ne 'NONE' }
40 50         85 keys %{$self->{KEYS}};
  50         149  
41              
42 50 100 100     3149 if (not $self->{PASSTHRU} and defined $props{'-seq'}) {
43 21         105 $props{'-seq'} =~ s/\s+//g; # fixme
44              
45 21 100       76 if ($self->{ONELINE}) {
    50          
46 3         9 $props{'-seq'} =~ s/[\n\r]//g;
47             } elsif ($self->{WIDTH}) {
48 18         40 my $width = $self->{WIDTH} + 0;
49 18         134 $props{'-seq'} =~ s/(.{$width})/$1\n/g;
50             }
51             }
52              
53             # Retain previous behaviour of preserving a leading space before any
54             # description without --passthru
55             $props{'-id'} = ""
56 50 100 100     163 unless defined $props{'-id'} or $self->{PASSTHRU};
57              
58             my $fasta = sprintf ">%s\n%s",
59 53         112 join(" ", map { s/[\n\r]//g; $_ }
  53         268  
60 100         214 grep { defined }
61             @props{'-id', '-desc'}),
62 50   100     115 $props{'-seq'} || "";
63              
64 50         111 chomp $fasta;
65 50         190 $self->push_line($fasta);
66              
67 50         696 return 1;
68             }
69              
70             sub add_help_types {
71 10     10 0 19575 my $self = shift;
72 10         42 $self->use_help_type('keyspecs');
73 10         87 $self->use_help_type('keys');
74             }
75              
76             sub usage {
77 0     0 0   my $self = shift;
78              
79 0           my $options = [
80             [ 'id|-i ', 'Record field to use for the sequence id' ],
81             [ 'description|-d ', 'Record field to use for the sequence description' ],
82             [ 'sequence|-s ', 'Record field to use for the sequence itself' ],
83             [ 'width|w <#>', 'Format sequence blocks to # characters wide' ],
84             [ 'oneline', 'Format sequences on a single long line' ],
85             [ 'passthru', 'Pass through nucleotides unformatted' ],
86             ];
87              
88 0           my $args_string = $self->options_string($options);
89              
90 0           return <
91             Usage: recs-tofasta [files]
92             __FORMAT_TEXT__
93             Outputs a FASTA-formatted sequence for each record.
94              
95             By default the keys "id", "description", and "sequence" are used to build
96             the FASTA format. These defaults match up with what recs-fromfasta produces.
97             The special key name "NONE" may be used to indicate that no key should be
98             used, disabling the defaults. Note that specifying NONE for --id will cause
99             any --description to appear with a space between it and the line's ">",
100             unless --passthru is also used.
101             __FORMAT_TEXT__
102              
103             Arguments:
104             $args_string
105              
106             Examples:
107             # Remove gaps from a fasta file
108             recs-fromfasta seqs.fa | recs-xform '{{sequence}} =~ s/-//g' | recs-tofasta > seqs-nogaps.fa
109             USAGE
110             }
111              
112             1;