File Coverage

blib/lib/App/Fasops/Command/replace.pm
Criterion Covered Total %
statement 95 99 95.9
branch 19 24 79.1
condition 6 9 66.6
subroutine 12 12 100.0
pod 6 6 100.0
total 138 150 92.0


line stmt bran cond sub pod time code
1             package App::Fasops::Command::replace;
2 21     21   15554 use strict;
  21         56  
  21         690  
3 21     21   115 use warnings;
  21         46  
  21         561  
4 21     21   111 use autodie;
  21         45  
  21         121  
5              
6 21     21   108732 use App::Fasops -command;
  21         47  
  21         212  
7 21     21   7293 use App::RL::Common;
  21         54  
  21         575  
8 21     21   119 use App::Fasops::Common;
  21         44  
  21         25064  
9              
10             sub abstract {
11 2     2 1 56 return 'replace headers from a blocked fasta';
12             }
13              
14             sub opt_spec {
15 7     7 1 57 return ( [ "outfile|o=s", "Output filename. [stdout] for screen." ], { show_defaults => 1, } );
16             }
17              
18             sub usage_desc {
19 7     7 1 71305 return "fasops replace [options] ";
20             }
21              
22             sub description {
23 1     1 1 692 my $desc;
24 1         5 $desc .= ucfirst(abstract) . ".\n";
25 1         4 $desc .= <<'MARKDOWN';
26              
27             * are paths to axt files, .axt.gz is supported
28             * infile == stdin means reading from STDIN
29              
30             * is a tab-separated file containing more than one fields
31              
32             original_name replace_name more_replace_name
33              
34             * With one field will delete the whole alignment block
35             * With three or more fields will duplicate the whole alignment block
36              
37             MARKDOWN
38              
39 1         3 return $desc;
40             }
41              
42             sub validate_args {
43 6     6 1 4591 my ( $self, $opt, $args ) = @_;
44              
45 6 100       12 if ( @{$args} != 2 ) {
  6         24  
46 2         6 my $message = "This command need two input files.\n\tIt found";
47 2         3 $message .= sprintf " [%s]", $_ for @{$args};
  2         11  
48 2         6 $message .= ".\n";
49 2         14 $self->usage_error($message);
50             }
51 4         10 for ( @{$args} ) {
  4         25  
52 7 50       320 next if lc $_ eq "stdin";
53 7 100       36 if ( !Path::Tiny::path($_)->is_file ) {
54 1         162 $self->usage_error("The input file [$_] doesn't exist.");
55             }
56             }
57              
58 3 50       170 if ( !exists $opt->{outfile} ) {
59 0         0 $opt->{outfile} = Path::Tiny::path( $args->[0] )->absolute . ".fas";
60             }
61             }
62              
63             sub execute {
64 3     3 1 42 my ( $self, $opt, $args ) = @_;
65              
66 3         19 my $replace = App::Fasops::Common::read_replaces( $args->[1] );
67              
68 3         25 my $in_fh;
69 3 50       14 if ( lc $args->[0] eq "stdin" ) {
70 0         0 $in_fh = *STDIN{IO};
71             }
72             else {
73 3         26 $in_fh = IO::Zlib->new( $args->[0], "rb" );
74             }
75              
76 3         5871 my $out_fh;
77 3 50       14 if ( lc( $opt->{outfile} ) eq "stdout" ) {
78 3         7 $out_fh = *STDOUT{IO};
79             }
80             else {
81 0         0 open $out_fh, ">", $opt->{outfile};
82             }
83              
84             {
85 3         11 my $content = ''; # content of one block
  3         6  
86 3         22 while (1) {
87 84 100 66     702 last if $in_fh->eof and $content eq '';
88 81         4256 my $line = '';
89 81 50       310 if ( !$in_fh->eof ) {
90 81         3922 $line = $in_fh->getline;
91             }
92 81 100 66     11279 if ( ( $line eq '' or $line =~ /^\s+$/ ) and $content ne '' ) {
      66        
93 9         36 my $info_of = App::Fasops::Common::parse_block_header($content);
94 9         18 $content = '';
95              
96 9         27 my @ori_names = keys %{$info_of};
  9         45  
97              
98             my @replace_names
99 9         266 = grep { exists $info_of->{$_} } keys %{$replace};
  15         199  
  9         23  
100              
101 9 100       70 if ( @replace_names == 0 ) { # block untouched
    100          
102 5         11 for my $header (@ori_names) {
103 20         84 printf {$out_fh} ">%s\n",
104 20         470 App::RL::Common::encode_header( $info_of->{$header} );
105 20         4088 printf {$out_fh} "%s\n", $info_of->{$header}{seq};
  20         98  
106             }
107 5         233 print {$out_fh} "\n";
  5         16  
108             }
109             elsif ( @replace_names == 1 ) { # each replaces create a new block
110 3         7 my $ori_name = $replace_names[0];
111 3         5 for my $new_name ( @{ $replace->{$ori_name} } ) {
  3         23  
112 3         35 for my $header (@ori_names) {
113 12 100       327 if ( $header eq $ori_name ) {
114 3         21 printf {$out_fh} ">%s\n", $new_name;
  3         26  
115 3         57 printf {$out_fh} "%s\n", $info_of->{$header}{seq};
  3         12  
116             }
117             else {
118 9         42 printf {$out_fh} ">%s\n",
119 9         16 App::RL::Common::encode_header( $info_of->{$header} );
120 9         1844 printf {$out_fh} "%s\n", $info_of->{$header}{seq};
  9         56  
121             }
122             }
123 3         83 print {$out_fh} "\n";
  3         10  
124             }
125             }
126             else {
127 1         267 Carp::carp "Don't support multiply records in one block. @replace_names\n";
128 1         162 for my $header (@ori_names) {
129 4         38 printf {$out_fh} ">%s\n",
130 4         116 App::RL::Common::encode_header( $info_of->{$header} );
131 4         776 printf {$out_fh} "%s\n", $info_of->{$header}{seq};
  4         20  
132             }
133 1         28 print {$out_fh} "\n";
  1         4  
134             }
135              
136             }
137             else {
138 72         175 $content .= $line;
139             }
140             }
141             }
142 3         590 close $out_fh;
143 0           $in_fh->close;
144             }
145              
146             1;