File Coverage

blib/lib/App/RL/Common.pm
Criterion Covered Total %
statement 123 130 94.6
branch 31 44 70.4
condition 5 12 41.6
subroutine 20 20 100.0
pod 0 8 0.0
total 179 214 83.6


line stmt bran cond sub pod time code
1             package App::RL::Common;
2 15     15   14614 use strict;
  15         31  
  15         408  
3 15     15   83 use warnings;
  15         31  
  15         394  
4 15     15   946 use autodie;
  15         24833  
  15         82  
5              
6 15     15   78714 use 5.010001;
  15         104  
7              
8 15     15   114 use Carp qw();
  15         32  
  15         271  
9 15     15   8615 use IO::Zlib;
  15         774017  
  15         96  
10 15     15   807 use List::Util qw();
  15         34  
  15         245  
11 15     15   9599 use Path::Tiny qw();
  15         119100  
  15         396  
12 15     15   6528 use Set::Scalar;
  15         123117  
  15         600  
13 15     15   6016 use Tie::IxHash;
  15         31781  
  15         397  
14 15     15   5665 use YAML::Syck qw();
  15         21604  
  15         319  
15              
16 15     15   6589 use AlignDB::IntSpan;
  15         77227  
  15         16282  
17              
18             # The only entrance for AlignDB::IntSpan or AlignDB::IntSpanXS
19             #@returns AlignDB::IntSpan
20             sub new_set {
21 341     341 0 1217 return AlignDB::IntSpan->new;
22             }
23              
24             sub read_lines {
25 13     13 0 34 my $fn = shift;
26              
27 13         37 my @lines;
28              
29 13 50       59 if ( lc $fn eq "stdin" ) {
30 0         0 while () {
31 0         0 chomp;
32 0         0 push @lines, $_;
33             }
34             }
35             else {
36 13         78 open my $fh, "<", $fn;
37 13         12816 while (<$fh>) {
38 140         340 chomp;
39 140         430 push @lines, $_;
40             }
41 13         64 close $fh;
42             }
43              
44 13         6441 return @lines;
45             }
46              
47             sub read_sizes {
48 9     9 0 26 my $fn = shift;
49 9         27 my $remove_chr = shift;
50              
51 9         44 my @lines = read_lines($fn);
52 9         53 my %length_of;
53 9         31 for (@lines) {
54 122         338 my ( $key, $value ) = split /\t/;
55 122 50       265 $key =~ s/chr0?//i if $remove_chr;
56 122         315 $length_of{$key} = $value;
57             }
58              
59 9         48 return \%length_of;
60             }
61              
62             sub read_names {
63 1     1 0 4 my $fn = shift;
64              
65 1         6 my @lines = read_lines($fn);
66              
67 1         9 return \@lines;
68             }
69              
70             sub runlist2set {
71 57     57 0 5991 my $runlist_of = shift;
72 57         128 my $remove_chr = shift;
73              
74 57         111 my $set_of = {};
75              
76 57         133 for my $chr ( sort keys %{$runlist_of} ) {
  57         309  
77 225         434 my $new_chr = $chr;
78 225 50       605 $new_chr =~ s/chr0?//i if $remove_chr;
79 225         611 my $set = new_set();
80 225         3073 $set->add( $runlist_of->{$chr} );
81 225         952296 $set_of->{$new_chr} = $set;
82             }
83              
84 57         246 return $set_of;
85             }
86              
87             sub decode_header {
88 39     39 0 4473 my $header = shift;
89              
90 39         211 tie my %info, "Tie::IxHash";
91              
92             # S288.chrI(+):27070-29557|species=S288C
93 39         691 my $head_qr = qr{
94             (?:(?P[\w_]+)\.)?
95             (?P[\w-]+)
96             (?:\((?P.+)\))?
97             [\:] # spacer
98             (?P\d+)
99             [\_\-]? # spacer
100             (?P\d+)?
101             }xi;
102              
103 39         346 $header =~ $head_qr;
104 39         142 my $chr_name = $2;
105 39         91 my $chr_start = $4;
106 39         77 my $chr_end = $5;
107              
108 39 100 66     198 if ( defined $chr_name and defined $chr_start ) {
109 36 100       96 if ( !defined $chr_end ) {
110 1         3 $chr_end = $chr_start;
111             }
112             %info = (
113 36         282 name => $1,
114             chr => $chr_name,
115             strand => $3,
116             start => $chr_start,
117             end => $chr_end,
118             );
119 36 100       2830 if ( defined $info{strand} ) {
120 16 50       141 if ( $info{strand} eq '1' ) {
    50          
121 0         0 $info{strand} = '+';
122             }
123             elsif ( $info{strand} eq '-1' ) {
124 0         0 $info{strand} = '-';
125             }
126             }
127             }
128             else {
129 3         9 $header =~ /^(\S+)/;
130 3         7 my $chr = $1;
131 3         15 %info = (
132             name => undef,
133             chr => $chr,
134             strand => undef,
135             start => undef,
136             end => undef,
137             );
138             }
139              
140             # additional keys
141 39 100       633 if ( $header =~ /\|(.+)/ ) {
142 1         6 my @parts = grep {defined} split /;/, $1;
  1         5  
143 1         3 for my $part (@parts) {
144 1         4 my ( $key, $value ) = split /=/, $part;
145 1 50 33     12 if ( defined $key and defined $value ) {
146 1         5 $info{$key} = $value;
147             }
148             }
149             }
150              
151 39         162 return \%info;
152             }
153              
154             sub info_is_valid {
155 42     42 0 88 my $info = shift;
156              
157 42 50       138 if ( ref $info eq "HASH" ) {
158 42 50 33     153 if ( exists $info->{chr} and exists $info->{start} ) {
159 42 50 33     413 if ( defined $info->{chr} and defined $info->{start} ) {
160 42         626 return 1;
161             }
162             }
163             }
164              
165 0         0 return 0;
166             }
167              
168             sub encode_header {
169 19     19 0 31388 my $info = shift;
170 19         48 my $only_essential = shift;
171              
172 19         33 my $header;
173 19 100       113 if ( defined $info->{name} ) {
    50          
174 7 50       46 if ( defined $info->{chr} ) {
175 7         39 $header .= $info->{name};
176 7         38 $header .= "." . $info->{chr};
177             }
178             else {
179 0         0 $header .= $info->{name};
180             }
181             }
182             elsif ( defined $info->{chr} ) {
183 12         152 $header .= $info->{chr};
184             }
185              
186 19 100       152 if ( defined $info->{strand} ) {
187 10         61 $header .= "(" . $info->{strand} . ")";
188             }
189 19 50       121 if ( defined $info->{start} ) {
190 19         114 $header .= ":" . $info->{start};
191 19 100       108 if ( $info->{end} != $info->{start} ) {
192 17         165 $header .= "-" . $info->{end};
193             }
194             }
195              
196             # additional keys
197 19 50       131 if ( !$only_essential ) {
198 19         46 my %essential = map { $_ => 1 } qw{name chr strand start end seq full_seq};
  133         291  
199 19         44 my @parts;
200 19         32 for my $key ( sort keys %{$info} ) {
  19         124  
201 92 100       623 if ( !$essential{$key} ) {
202 2         7 push @parts, $key . "=" . $info->{$key};
203             }
204             }
205 19 100       80 if (@parts) {
206 2         5 my $additional = join ";", @parts;
207 2         8 $header .= "|" . $additional;
208             }
209             }
210              
211 19         108 return $header;
212             }
213              
214             1;