File Coverage

blib/lib/App/Rangeops/Command/sort.pm
Criterion Covered Total %
statement 49 53 92.4
branch 7 12 58.3
condition n/a
subroutine 11 11 100.0
pod 5 5 100.0
total 72 81 88.8


line stmt bran cond sub pod time code
1             package App::Rangeops::Command::sort;
2 9     9   6573 use strict;
  9         18  
  9         290  
3 9     9   52 use warnings;
  9         43  
  9         244  
4 9     9   51 use autodie;
  9         13  
  9         67  
5              
6 9     9   32736 use App::Rangeops -command;
  9         18  
  9         92  
7 9     9   2810 use App::Rangeops::Common;
  9         18  
  9         257  
8              
9 9     9   47 use constant abstract => 'sort links and ranges within links';
  9         11  
  9         5524  
10              
11             sub opt_spec {
12             return (
13 2     2 1 12 [ "outfile|o=s", "Output filename. [stdout] for screen." ],
14             [ "numeric|n", "Sort chromosome names numerically.", ],
15             );
16             }
17              
18             sub usage_desc {
19 2     2 1 30936 return "rangeops sort [options] ";
20             }
21              
22             sub description {
23 1     1 1 584 my $desc;
24 1         3 $desc .= ucfirst(abstract) . ".\n";
25 1         2 return $desc;
26             }
27              
28             sub validate_args {
29 1     1 1 651 my ( $self, $opt, $args ) = @_;
30              
31 1 50       2 if ( !@{$args} ) {
  1         5  
32 0         0 $self->usage_error("This command need one or more input files.");
33             }
34 1         2 for ( @{$args} ) {
  1         2  
35 1 50       3 next if lc $_ eq "stdin";
36 1 50       6 if ( !Path::Tiny::path($_)->is_file ) {
37 0         0 $self->usage_error("The input file [$_] doesn't exist.");
38             }
39             }
40              
41 1 50       115 if ( !exists $opt->{outfile} ) {
42             $opt->{outfile}
43 0         0 = Path::Tiny::path( $args->[0] )->absolute . ".sort.tsv";
44             }
45             }
46              
47             sub execute {
48 1     1 1 5 my ( $self, $opt, $args ) = @_;
49              
50             #----------------------------#
51             # Loading
52             #----------------------------#
53 1         2 my @lines;
54 1         7 for my $file ( @{$args} ) {
  1         4  
55 1         6 for my $line ( App::RL::Common::read_lines($file) ) {
56 29         904 for my $part ( split /\t/, $line ) {
57 87         148 my $info = App::RL::Common::decode_header($part);
58 87 100       6247 next unless App::RL::Common::info_is_valid($info);
59              
60 58         1198 push @lines, $line; # May produce duplicated lines
61             }
62             }
63             }
64 1         29 @lines = List::MoreUtils::PP::uniq(@lines);
65              
66             #----------------------------#
67             # Sort
68             #----------------------------#
69             my @sorted_lines
70 1         109 = @{ App::Rangeops::Common::sort_links( \@lines, $opt->{numeric} ) };
  1         8  
71              
72             #----------------------------#
73             # Output
74             #----------------------------#
75 1         5 my $out_fh;
76 1 50       6 if ( lc( $opt->{outfile} ) eq "stdout" ) {
77 1         3 $out_fh = \*STDOUT;
78             }
79             else {
80 0         0 open $out_fh, ">", $opt->{outfile};
81             }
82              
83 1         3 print {$out_fh} "$_\n" for @sorted_lines;
  15         149  
84              
85 1         27 close $out_fh;
86             }
87              
88             1;