File Coverage

blib/lib/App/Rangeops/Command/circos.pm
Criterion Covered Total %
statement 65 69 94.2
branch 12 20 60.0
condition n/a
subroutine 11 11 100.0
pod 5 5 100.0
total 93 105 88.5


line stmt bran cond sub pod time code
1             package App::Rangeops::Command::circos;
2 9     9   68063 use strict;
  9         111  
  9         249  
3 9     9   43 use warnings;
  9         18  
  9         252  
4 9     9   3150 use autodie;
  9         103451  
  9         38  
5              
6 9     9   55545 use App::Rangeops -command;
  9         21  
  9         107  
7 9     9   7740 use App::Rangeops::Common;
  9         31  
  9         388  
8              
9 9     9   82 use constant abstract => 'range links to circos links or highlight file';
  9         19  
  9         6210  
10              
11             sub opt_spec {
12             return (
13 3     3 1 17 [ "outfile|o=s", "Output filename. [stdout] for screen." ],
14             [ "highlight|l", "Create highlights instead of links", ],
15             );
16             }
17              
18             sub usage_desc {
19 3     3 1 29259 return "rangeops circos [options] ";
20             }
21              
22             sub description {
23 1     1 1 643 my $desc;
24 1         4 $desc .= ucfirst(abstract) . ".\n";
25 1         3 $desc .= "\tIt's assumed that all ranges in input files are valid.\n";
26 1         3 return $desc;
27             }
28              
29             sub validate_args {
30 2     2 1 1716 my ( $self, $opt, $args ) = @_;
31              
32 2 50       5 if ( !@{$args} ) {
  2         7  
33 0         0 $self->usage_error("This command need one or more input files.");
34             }
35 2         5 for ( @{$args} ) {
  2         5  
36 2 50       9 next if lc $_ eq "stdin";
37 2 50       10 if ( !Path::Tiny::path($_)->is_file ) {
38 0         0 $self->usage_error("The input file [$_] doesn't exist.");
39             }
40             }
41              
42 2 50       173 if ( !exists $opt->{outfile} ) {
43             $opt->{outfile}
44 0         0 = Path::Tiny::path( $args->[0] )->absolute . ".links.txt";
45             }
46             }
47              
48             sub execute {
49 2     2 1 12 my ( $self, $opt, $args ) = @_;
50              
51             #----------------------------#
52             # Loading
53             #----------------------------#
54 2         4 my @lines;
55 2         5 for my $file ( @{$args} ) {
  2         5  
56 2         15 for my $line ( App::RL::Common::read_lines($file) ) {
57 12         2568 my @parts = split /\t/, $line;
58 12         27 my @colors = reverse map {"paired-12-qual-$_"} ( 1 .. 12 );
  144         318  
59 12         27 my $color_idx = 0;
60              
61 12 100       34 if ( defined $opt->{highlight} ) {
62              
63 6         60 for my $part (@parts) {
64 14         45 my $info = App::RL::Common::decode_header($part);
65 14 50       1942 next unless App::RL::Common::info_is_valid($info);
66             my $str = join( " ",
67             $info->{chr}, $info->{start}, $info->{end},
68 14         422 "fill_color=" . $colors[$color_idx] );
69 14         271 push @lines, $str;
70             }
71              
72             # rotate color
73 6         11 $color_idx++;
74 6 50       27 $color_idx = 0 if $color_idx > 11;
75             }
76             else {
77 6         17 for ( my $i = 0; $i <= $#parts; $i++ ) {
78 14         49 PAIR: for ( my $j = $i + 1; $j <= $#parts; $j++ ) {
79 10         16 my @fields;
80 10         17 for ( $i, $j ) {
81 20         248 my $info
82             = App::RL::Common::decode_header( $parts[$_] );
83             next PAIR
84 20 50       2485 unless App::RL::Common::info_is_valid($info);
85              
86             push @fields,
87             (
88             $info->{chr},
89             $info->{strand} eq "+"
90             ? ( $info->{start}, $info->{end} )
91             : ( $info->{end}, $info->{start} )
92 20 100       506 );
93             }
94 10         214 my $str = join( " ", @fields );
95 10         48 push @lines, $str;
96             }
97             }
98             }
99             }
100             }
101 2         15 @lines = List::MoreUtils::PP::uniq(@lines);
102              
103             #----------------------------#
104             # Output
105             #----------------------------#
106 2         83 my $out_fh;
107 2 50       8 if ( lc( $opt->{outfile} ) eq "stdout" ) {
108 2         7 $out_fh = \*STDOUT;
109             }
110             else {
111 0         0 open $out_fh, ">", $opt->{outfile};
112             }
113              
114 2         6 print {$out_fh} "$_\n" for @lines;
  24         273  
115              
116 2         25 close $out_fh;
117             }
118              
119             1;