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   66182 use strict;
  9         14  
  9         314  
3 9     9   38 use warnings;
  9         11  
  9         279  
4 9     9   4491 use autodie;
  9         124494  
  9         43  
5              
6 9     9   42383 use App::Rangeops -command;
  9         18  
  9         122  
7 9     9   8001 use App::Rangeops::Common;
  9         28  
  9         385  
8              
9 9     9   109 use constant abstract => 'range links to circos links or highlight file';
  9         14  
  9         7337  
10              
11             sub opt_spec {
12             return (
13 3     3 1 22 [ "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 36499 return "rangeops circos [options] ";
20             }
21              
22             sub description {
23 1     1 1 555 my $desc;
24 1         2 $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 1834 my ( $self, $opt, $args ) = @_;
31              
32 2 50       4 if ( !@{$args} ) {
  2         21  
33 0         0 $self->usage_error("This command need one or more input files.");
34             }
35 2         4 for ( @{$args} ) {
  2         6  
36 2 50       9 next if lc $_ eq "stdin";
37 2 50       26 if ( !Path::Tiny::path($_)->is_file ) {
38 0         0 $self->usage_error("The input file [$_] doesn't exist.");
39             }
40             }
41              
42 2 50       264 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         3 for my $file ( @{$args} ) {
  2         6  
56 2         13 for my $line ( App::RL::Common::read_lines($file) ) {
57 12         677 my @parts = split /\t/, $line;
58 12         28 my @colors = reverse map {"paired-12-qual-$_"} ( 1 .. 12 );
  144         322  
59 12         28 my $color_idx = 0;
60              
61 12 100       32 if ( defined $opt->{highlight} ) {
62              
63 6         13 for my $part (@parts) {
64 14         47 my $info = App::RL::Common::decode_header($part);
65 14 50       2251 next unless App::RL::Common::info_is_valid($info);
66             my $str = join( " ",
67             $info->{chr}, $info->{start}, $info->{end},
68 14         529 "fill_color=" . $colors[$color_idx] );
69 14         348 push @lines, $str;
70             }
71              
72             # rotate color
73 6         20 $color_idx++;
74 6 50       42 $color_idx = 0 if $color_idx > 11;
75             }
76             else {
77 6         13 for ( my $i = 0; $i <= $#parts; $i++ ) {
78 14         40 PAIR: for ( my $j = $i + 1; $j <= $#parts; $j++ ) {
79 10         8 my @fields;
80 10         16 for ( $i, $j ) {
81 20         200 my $info
82             = App::RL::Common::decode_header( $parts[$_] );
83             next PAIR
84 20 50       1650 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       415 );
93             }
94 10         165 my $str = join( " ", @fields );
95 10         49 push @lines, $str;
96             }
97             }
98             }
99             }
100             }
101 2         16 @lines = List::MoreUtils::PP::uniq(@lines);
102              
103             #----------------------------#
104             # Output
105             #----------------------------#
106 2         102 my $out_fh;
107 2 50       24 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         7 print {$out_fh} "$_\n" for @lines;
  24         321  
115              
116 2         28 close $out_fh;
117             }
118              
119             1;