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   124746 use strict;
  1         3  
  1         23  
4 1     1   4 use warnings;
  1         2  
  1         21  
5              
6 1     1   5 use base qw(App::RecordStream::Operation);
  1         1  
  1         473  
7              
8             sub init {
9 10     10 0 83 my $self = shift;
10 10         14 my $args = shift;
11              
12 10         24 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         49 "passthru" => \($self->{PASSTHRU}),
20             };
21              
22 10         40 $self->parse_options($args, $spec);
23              
24             die "--passthru is incompatible with --oneline and --width\n\n"
25 10 50 33     8119 if $self->{PASSTHRU} and ($self->{ONELINE} or $self->{WIDTH});
      66        
26              
27 10   100     42 $self->{WIDTH} ||= 60;
28              
29 10         24 $self->{KEYS}{id} = $id;
30 10         16 $self->{KEYS}{desc} = $desc;
31 10         71 $self->{KEYS}{seq} = $seq;
32             }
33              
34             sub accept_record {
35 50     50 0 5989 my $self = shift;
36 50         67 my $record = shift;
37              
38 125         4113 my %props = map {; "-$_" => ${$record->guess_key_from_spec($self->{KEYS}{$_})} }
  125         265  
39 150         285 grep { $self->{KEYS}{$_} ne 'NONE' }
40 50         73 keys %{$self->{KEYS}};
  50         124  
41              
42 50 100 100     2276 if (not $self->{PASSTHRU} and defined $props{'-seq'}) {
43 21         82 $props{'-seq'} =~ s/\s+//g; # fixme
44              
45 21 100       60 if ($self->{ONELINE}) {
    50          
46 3         7 $props{'-seq'} =~ s/[\n\r]//g;
47             } elsif ($self->{WIDTH}) {
48 18         25 my $width = $self->{WIDTH} + 0;
49 18         111 $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     115 unless defined $props{'-id'} or $self->{PASSTHRU};
57              
58             my $fasta = sprintf ">%s\n%s",
59 53         92 join(" ", map { s/[\n\r]//g; $_ }
  53         209  
60 100         200 grep { defined }
61             @props{'-id', '-desc'}),
62 50   100     91 $props{'-seq'} || "";
63              
64 50         83 chomp $fasta;
65 50         141 $self->push_line($fasta);
66              
67 50         593 return 1;
68             }
69              
70             sub add_help_types {
71 10     10 0 14005 my $self = shift;
72 10         52 $self->use_help_type('keyspecs');
73 10         101 $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;