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 16     16   15636 use strict;
  16         37  
  16         409  
3 16     16   77 use warnings;
  16         34  
  16         404  
4 16     16   975 use autodie;
  16         25314  
  16         77  
5              
6 16     16   80785 use 5.010001;
  16         110  
7              
8 16     16   100 use Carp qw();
  16         28  
  16         286  
9 16     16   8380 use IO::Zlib;
  16         843498  
  16         111  
10 16     16   833 use List::Util qw();
  16         44  
  16         279  
11 16     16   10990 use Path::Tiny qw();
  16         134029  
  16         426  
12 16     16   7278 use Set::Scalar;
  16         134943  
  16         656  
13 16     16   6934 use Tie::IxHash;
  16         35694  
  16         449  
14 16     16   6561 use YAML::Syck qw();
  16         23627  
  16         381  
15              
16 16     16   7645 use AlignDB::IntSpan;
  16         83371  
  16         17263  
17              
18             # The only entrance for AlignDB::IntSpan or AlignDB::IntSpanXS
19             #@returns AlignDB::IntSpan
20             sub new_set {
21 357     357 0 1171 return AlignDB::IntSpan->new;
22             }
23              
24             sub read_lines {
25 13     13 0 33 my $fn = shift;
26              
27 13         26 my @lines;
28              
29 13 50       52 if ( lc $fn eq "stdin" ) {
30 0         0 while () {
31 0         0 chomp;
32 0         0 push @lines, $_;
33             }
34             }
35             else {
36 13         71 open my $fh, "<", $fn;
37 13         10927 while (<$fh>) {
38 140         228 chomp;
39 140         361 push @lines, $_;
40             }
41 13         57 close $fh;
42             }
43              
44 13         5248 return @lines;
45             }
46              
47             sub read_sizes {
48 9     9 0 33 my $fn = shift;
49 9         27 my $remove_chr = shift;
50              
51 9         36 my @lines = read_lines($fn);
52 9         25 my %length_of;
53 9         27 for (@lines) {
54 122         260 my ( $key, $value ) = split /\t/;
55 122 50       252 $key =~ s/chr0?//i if $remove_chr;
56 122         265 $length_of{$key} = $value;
57             }
58              
59 9         43 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         4 return \@lines;
68             }
69              
70             sub runlist2set {
71 58     58 0 5893 my $runlist_of = shift;
72 58         131 my $remove_chr = shift;
73              
74 58         107 my $set_of = {};
75              
76 58         100 for my $chr ( sort keys %{$runlist_of} ) {
  58         257  
77 241         482 my $new_chr = $chr;
78 241 50       587 $new_chr =~ s/chr0?//i if $remove_chr;
79 241         585 my $set = new_set();
80 241         3089 $set->add( $runlist_of->{$chr} );
81 241         949649 $set_of->{$new_chr} = $set;
82             }
83              
84 58         246 return $set_of;
85             }
86              
87             sub decode_header {
88 39     39 0 2703 my $header = shift;
89              
90 39         176 tie my %info, "Tie::IxHash";
91              
92             # S288.chrI(+):27070-29557|species=S288C
93 39         612 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         311 $header =~ $head_qr;
104 39         98 my $chr_name = $2;
105 39         73 my $chr_start = $4;
106 39         79 my $chr_end = $5;
107              
108 39 100 66     198 if ( defined $chr_name and defined $chr_start ) {
109 36 100       89 if ( !defined $chr_end ) {
110 1         3 $chr_end = $chr_start;
111             }
112             %info = (
113 36         237 name => $1,
114             chr => $chr_name,
115             strand => $3,
116             start => $chr_start,
117             end => $chr_end,
118             );
119 36 100       2750 if ( defined $info{strand} ) {
120 16 50       133 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         10 $header =~ /^(\S+)/;
130 3         6 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       663 if ( $header =~ /\|(.+)/ ) {
142 1         8 my @parts = grep {defined} split /;/, $1;
  1         5  
143 1         5 for my $part (@parts) {
144 1         11 my ( $key, $value ) = split /=/, $part;
145 1 50 33     7 if ( defined $key and defined $value ) {
146 1         6 $info{$key} = $value;
147             }
148             }
149             }
150              
151 39         147 return \%info;
152             }
153              
154             sub info_is_valid {
155 42     42 0 84 my $info = shift;
156              
157 42 50       123 if ( ref $info eq "HASH" ) {
158 42 50 33     144 if ( exists $info->{chr} and exists $info->{start} ) {
159 42 50 33     375 if ( defined $info->{chr} and defined $info->{start} ) {
160 42         600 return 1;
161             }
162             }
163             }
164              
165 0         0 return 0;
166             }
167              
168             sub encode_header {
169 19     19 0 20881 my $info = shift;
170 19         42 my $only_essential = shift;
171              
172 19         31 my $header;
173 19 100       96 if ( defined $info->{name} ) {
    50          
174 7 50       47 if ( defined $info->{chr} ) {
175 7         36 $header .= $info->{name};
176 7         52 $header .= "." . $info->{chr};
177             }
178             else {
179 0         0 $header .= $info->{name};
180             }
181             }
182             elsif ( defined $info->{chr} ) {
183 12         145 $header .= $info->{chr};
184             }
185              
186 19 100       123 if ( defined $info->{strand} ) {
187 10         51 $header .= "(" . $info->{strand} . ")";
188             }
189 19 50       115 if ( defined $info->{start} ) {
190 19         107 $header .= ":" . $info->{start};
191 19 100       99 if ( $info->{end} != $info->{start} ) {
192 17         145 $header .= "-" . $info->{end};
193             }
194             }
195              
196             # additional keys
197 19 50       121 if ( !$only_essential ) {
198 19         39 my %essential = map { $_ => 1 } qw{name chr strand start end seq full_seq};
  133         266  
199 19         38 my @parts;
200 19         27 for my $key ( sort keys %{$info} ) {
  19         75  
201 92 100       569 if ( !$essential{$key} ) {
202 2         6 push @parts, $key . "=" . $info->{$key};
203             }
204             }
205 19 100       76 if (@parts) {
206 2         5 my $additional = join ";", @parts;
207 2         8 $header .= "|" . $additional;
208             }
209             }
210              
211 19         95 return $header;
212             }
213              
214             1;