File Coverage

blib/lib/App/RL/Command/span.pm
Criterion Covered Total %
statement 82 88 93.1
branch 22 28 78.5
condition n/a
subroutine 11 11 100.0
pod 5 5 100.0
total 120 132 90.9


line stmt bran cond sub pod time code
1             package App::RL::Command::span;
2 14     14   8323 use strict;
  14         39  
  14         387  
3 14     14   71 use warnings;
  14         27  
  14         316  
4 14     14   70 use autodie;
  14         25  
  14         67  
5              
6 14     14   68978 use App::RL -command;
  14         34  
  14         126  
7 14     14   4410 use App::RL::Common;
  14         32  
  14         344  
8              
9 14     14   70 use constant abstract => 'operate spans in a YAML file';
  14         30  
  14         12472  
10              
11             sub opt_spec {
12             return (
13 8     8 1 59 [ "outfile|o=s", "Output filename. [stdout] for screen." ],
14             [ "op=s", "operations: cover, holes, trim, pad, excise or fill", { default => "cover" } ],
15             [ "number|n=i", "Apply this number to trim, pad, excise or fill", { default => 0 } ],
16             [ "remove|r", "Remove 'chr0' from chromosome names." ],
17             [ "mk", "YAML file contains multiple sets of runlists." ],
18             { show_defaults => 1, }
19             );
20             }
21              
22             sub usage_desc {
23 8     8 1 49925 return "runlist span [options] ";
24             }
25              
26             sub description {
27 1     1 1 1195 my $desc;
28 1         19 $desc .= ucfirst(abstract) . ".\n";
29 1         3 $desc .= "List of operations.\n";
30 1         3 $desc .= " " x 4 . "cover: a single span from min to max;\n";
31 1         3 $desc .= " " x 4 . "holes: all the holes in runlist;\n";
32 1         3 $desc .= " " x 4 . "trim: remove N integers from each end of each span of runlist;\n";
33 1         3 $desc .= " " x 4 . "pad: add N integers from each end of each span of runlist;\n";
34 1         3 $desc .= " " x 4 . "excise: remove all spans smaller than N;\n";
35 1         2 $desc .= " " x 4 . "fill: fill in all holes smaller than N.\n";
36 1         4 return $desc;
37             }
38              
39             sub validate_args {
40 7     7 1 9983 my ( $self, $opt, $args ) = @_;
41              
42 7 100       13 if ( @{$args} != 1 ) {
  7         24  
43 1         2 my $message = "This command need one input file.\n\tIt found";
44 1         2 $message .= sprintf " [%s]", $_ for @{$args};
  1         4  
45 1         3 $message .= ".\n";
46 1         9 $self->usage_error($message);
47             }
48 6         12 for ( @{$args} ) {
  6         14  
49 6 50       19 next if lc $_ eq "stdin";
50 6 100       26 if ( !Path::Tiny::path($_)->is_file ) {
51 1         99 $self->usage_error("The input file [$_] doesn't exist.");
52             }
53             }
54              
55 5 100       300 if ( $opt->{op} =~ /^cover/i ) {
    100          
    100          
    50          
    100          
    50          
56 1         4 $opt->{op} = 'cover';
57             }
58             elsif ( $opt->{op} =~ /^hole/i ) {
59 1         3 $opt->{op} = 'holes';
60             }
61             elsif ( $opt->{op} =~ /^trim/i ) {
62 1         4 $opt->{op} = 'trim';
63             }
64             elsif ( $opt->{op} =~ /^pad/i ) {
65 0         0 $opt->{op} = 'pad';
66             }
67             elsif ( $opt->{op} =~ /^excise/i ) {
68 1         3 $opt->{op} = 'excise';
69             }
70             elsif ( $opt->{op} =~ /^fill/i ) {
71 1         4 $opt->{op} = 'fill';
72             }
73             else {
74 0         0 Carp::confess "[@{[$opt->{op}]}] is invalid\n";
  0         0  
75             }
76              
77 5 50       22 if ( !exists $opt->{outfile} ) {
78 0         0 $opt->{outfile} = $opt->{op} . ".yml";
79             }
80             }
81              
82             sub execute {
83 5     5 1 26 my ( $self, $opt, $args ) = @_;
84              
85             #----------------------------#
86             # Loading
87             #----------------------------#
88 5         15 my $infile; # YAML::Syck::LoadFile handles IO::*
89 5 50       16 if ( lc $args->[0] eq 'stdin' ) {
90 0         0 $infile = *STDIN;
91             }
92             else {
93 5         11 $infile = $args->[0];
94             }
95              
96 5         10 my $s_of = {};
97 5         7 my @keys;
98 5 100       15 if ( $opt->{mk} ) {
99 3         10 my $yml = YAML::Syck::LoadFile($infile);
100 3         433 @keys = sort keys %{$yml};
  3         18  
101              
102 3         9 for my $key (@keys) {
103             $s_of->{$key}
104 15         52 = App::RL::Common::runlist2set( $yml->{$key}, $opt->{remove} );
105             }
106             }
107             else {
108 2         5 @keys = ("__single");
109             $s_of->{__single}
110 2         9 = App::RL::Common::runlist2set( YAML::Syck::LoadFile($infile), $opt->{remove} );
111             }
112              
113             #----------------------------#
114             # Operating
115             #----------------------------#
116 5         14 my $op_result_of = { map { $_ => {} } @keys };
  17         37  
117              
118 5         12 for my $key (@keys) {
119 17         601 my $s = $s_of->{$key};
120              
121 17         24 for my $chr ( keys %{$s} ) {
  17         44  
122 17         28 my $op = $opt->{op};
123 17         55 my $op_set = $s->{$chr}->$op( $opt->{number} );
124 17         14961 $op_result_of->{$key}{$chr} = $op_set->runlist;
125             }
126             }
127              
128             #----------------------------#
129             # Output
130             #----------------------------#
131 5         240 my $out_fh;
132 5 50       17 if ( lc( $opt->{outfile} ) eq "stdout" ) {
133 5         14 $out_fh = *STDOUT;
134             }
135             else {
136 0         0 open $out_fh, ">", $opt->{outfile};
137             }
138              
139 5 100       13 if ( $opt->{mk} ) {
140 3         7 print {$out_fh} YAML::Syck::Dump($op_result_of);
  3         11  
141             }
142             else {
143 2         3 print {$out_fh} YAML::Syck::Dump( $op_result_of->{__single} );
  2         10  
144             }
145              
146 5         349 close $out_fh;
147             }
148              
149             1;