lib/Bio/ViennaNGS/Util.pm

filter: Statementstmt 20.3% 45 / 221 Branchbran 0.0% 0 / 110 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 61.9% 13 / 21 Podpod 100% 8 / 8 totaltotal 18.0% 66 / 365 CC 63 SCAR 50.4
CC
63
Cov
13%
bed_or_bam2bw
420.0
sortbed
156.0
bed2bigBed
110.0
CRAP 154.7
« lib/Bio/ViennaNGS/UCSC.pm ↑ Summary lib/Bio/ViennaNGS/FeatureIO.pm »
1 # -*-CPerl-*-
2 # Last changed Time-stamp: <2017-06-10 19:12:00 michl>
3  
4 package Bio::ViennaNGS::Util;
5  
6 1 ▶use Bio::ViennaNGS;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Basename qw(fileparse basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use IPC::Cmd qw(can_run run);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Path::Class qw(dir file);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Path qw(make_path remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Math::Round;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Env qw(HOME);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use version; our $VERSION = version->declare("$Bio::ViennaNGS::VERSION");
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 our @ISA = qw(Exporter);
21 1 our @EXPORT = ();
22  
23 1 our @EXPORT_OK = qw ( bed_or_bam2bw sortbed bed2bigBed unique_array
24 kmer_enrichment fetch_chrom_sizes mkdircheck rmdircheck);
25  
26  
27 #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^#
28 #^^^^^^^^^^ Variables ^^^^^^^^^^^#
29 #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^#
30  
31 1 my %unique = ();
32  
33 #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^#
34 #^^^^^^^^^^^ Subroutines ^^^^^^^^^^#
35 #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^#
36  
37 0 ▶sub bed_or_bam2bw {
Subroutine bed_or_bam2bw: not called CC=20 SCAR=60.4
Pod documented
38 0 my ($type,$infile,$chromsizes,$strand,$dest,$want_norm,$size,$scale,$log) = @_;
39 0 my ($fn_bg_tmp,$fn_bg,$fn_bw,$fn,$bn,$path,$ext,$cmd);
40 0 my @processed_files = ();
41 0 my $factor = 1.;
42 0 my $this_function = (caller(0))[3];
43  
44 0 ▶ croak "ERROR [$this_function] \$type is '$type', however it is expected to be either 'bam' or 'bed'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "bam" or $type eq "bed" 0 0 0
Condition0%0/3
$type eq "bam" or $type eq "bed"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "bam" or $type eq "bed"
$type eq "bam" $type eq "bed"
Truth table0%0/3
A or B
A= $type eq "bam"B= $type eq "bed"
ABresult
1X1
011
000
45 unless ($type eq "bam") || ($type eq "bed");
46  
47 0 ▶ my $genomeCoverageBed = can_run('genomeCoverageBed') or
Branch: 1 branch
BranchTrueFalseTotal
my $genomeCoverageBed = can_run("genomeCoverageBed") or croak("ERROR [$this_function] genomeCoverageBed utility not found") 0 0 0
48 croak "ERROR [$this_function] genomeCoverageBed utility not found";
49 0 ▶ my $bedGraphToBigWig = can_run('bedGraphToBigWig') or
Branch: 1 branch
BranchTrueFalseTotal
my $bedGraphToBigWig = can_run("bedGraphToBigWig") or croak("ERROR [$this_function] bedGraphToBigWig utility not found") 0 0 0
50 croak "ERROR [$this_function] bedGraphToBigWig utility not found";
51 0 ▶ my $awk = can_run('awk') or
Branch: 1 branch
BranchTrueFalseTotal
my $awk = can_run("awk") or croak("ERROR [$this_function] awk utility not found") 0 0 0
52 croak "ERROR [$this_function] awk utility not found";
53  
54 0 ▶ if(defined $log){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $log) 0 0 0
55 0 ▶ open(LOG, ">>", $log) or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
open LOG, ">>", $log or croak($!) 0 0 0
56 0 print LOG "LOG [$this_function] \$infile: $infile\n";
57 0 print LOG "LOG [$this_function] \$dest: $dest\n";
58 0 print LOG "LOG [$this_function] \$chromsizes: $chromsizes\n";
59 }
60  
61 0 ▶ croak "ERROR [$this_function] Cannot find $infile\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -e $infile 0 0 0
62 unless (-e $infile);
63 0 ▶ croak "ERROR [$this_function] $dest does not exist\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dest 0 0 0
64 unless (-d $dest);
65 0 ▶ croak "ERROR [$this_function] Cannot find $chromsizes\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -e $chromsizes 0 0 0
66 unless (-e $chromsizes);
67  
68 0 ▶ if ($want_norm == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($want_norm == 1) 0 0 0
69 0 $factor = $scale/$size;
70 0 ▶ print LOG "LOG [$this_function] normalization: $factor = ($scale/$size)\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $log 0 0 0
71 if(defined $log);
72 }
73  
74 0 ▶ if ($type eq "bam"){$ext = ".bam";}
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'bam') { } 0 0 0
75 0 else{$ext = ".bed";}
76 0 $bn = basename($infile,$ext);
77 0 $fn = fileparse($infile);
78  
79 0 $fn_bg_tmp = file($dest,$fn.".tmp.bg");
80 0 $fn_bg = file($dest,$bn.".bg");
81 0 ▶ if($strand eq "+"){
Branch: 1 branch
BranchTrueFalseTotal
if ($strand eq '+') { } 0 0 0
82 0 $fn_bw = file($dest,$bn.".pos.bw");
83 }
84 else {
85 0 $fn_bw = file($dest,$bn.".neg.bw");
86 }
87  
88 0 $cmd = "$genomeCoverageBed -bg -scale $factor -split ";
89 0 ▶ if ($type eq "bed"){ $cmd .= "-i $infile -g $chromsizes"; } # chrom.sizes only required for processing BED
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'bed') { } 0 0 0
90 0 else { $cmd .= "-ibam $infile "; }
91 0 $cmd .= " > $fn_bg_tmp";
92  
93 0 ▶ if($strand eq "-"){
Branch: 1 branch
BranchTrueFalseTotal
if ($strand eq '-') { } 0 0 0
94 0 $cmd .= " && cat $fn_bg_tmp | $awk \'{ \$4 = - \$4 ; print \$0 }\' > $fn_bg";
95 }
96 else{
97 0 $fn_bg = $fn_bg_tmp;
98 }
99 0 $cmd .= " && $bedGraphToBigWig $fn_bg $chromsizes $fn_bw";
100  
101 0 ▶ if (defined $log){ print LOG "LOG [$this_function] $cmd\n";}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $log) 0 0 0
102  
103 0 my( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) =
104 run( command => $cmd, verbose => 0 );
105  
106 0 ▶ if( !$success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 0 0
107 0 print STDERR "ERROR [$this_function] External command call unsuccessful\n";
108 0 print STDERR "ERROR: this is what the command printed:\n";
109 0 print join "", @$full_buf;
110 0 croak $!;
111 }
112 0 ▶ if (defined $log){ close(LOG); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $log) 0 0 0
113  
114 0 unlink ($fn_bg_tmp);
115 0 unlink ($fn_bg);
116 0 return $fn_bw;
117 }
118  
119 0 ▶sub bed2bigBed {
Subroutine bed2bigBed: not called CC=10 SCAR=47.0
Pod documented
120 0 my ($infile,$chromsizes,$dest,$log) = @_;
121 0 my ($bn,$path,$ext,$cmd,$outfile);
122 0 my $this_function = (caller(0))[3];
123 0 ▶ my $bed2bigBed = can_run('bedToBigBed') or
Branch: 1 branch
BranchTrueFalseTotal
my $bed2bigBed = can_run("bedToBigBed") or croak("ERROR [$this_function] bedToBigBed utility not found") 0 0 0
124 croak "ERROR [$this_function] bedToBigBed utility not found";
125  
126 0 ▶ if (defined $log){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $log) 0 0 0
127 0 ▶ open(LOG, ">>", $log) or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
open LOG, ">>", $log or croak($!) 0 0 0
128 0 print LOG "LOG [$this_function] \$infile: $infile -- \$chromsizes: $chromsizes --\$dest: $dest\n";
129 }
130  
131 0 ▶ croak "ERROR [$this_function] Cannot find $infile"
Branch: 1 branch
BranchTrueFalseTotal
unless -e $infile 0 0 0
132 unless (-e $infile);
133 0 ▶ croak "ERROR [$this_function] Cannot find $chromsizes"
Branch: 1 branch
BranchTrueFalseTotal
unless -e $chromsizes 0 0 0
134 unless (-e $chromsizes);
135 0 ▶ croak "ERROR [$this_function] $dest does not exist"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dest 0 0 0
136 unless (-d $dest);
137  
138 # .bed6 .bed12 extensions are replaced by .bb
139 0 ($bn,$path,$ext) = fileparse($infile, qr /\.bed[126]?/);
140 0 $outfile = file($dest, "$bn.bb");
141  
142 0 $cmd = "$bed2bigBed $infile -extraIndex=name -tab $chromsizes $outfile";
143 0 ▶ if (defined $log){ print LOG "LOG [$this_function] $cmd\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $log) 0 0 0
144 0 my( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) =
145 run( command => $cmd, verbose => 0 );
146  
147 0 ▶ if( !$success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 0 0
148 0 print STDERR "ERROR [$this_function] Call to $bed2bigBed unsuccessful\n";
149 0 print STDERR "ERROR: this is what the command printed:\n";
150 0 print join "", @$full_buf;
151 0 croak $!;
152 }
153  
154 0 ▶ if (defined $log){ close(LOG); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $log) 0 0 0
155  
156 0 return $outfile;
157 }
158  
159 0 ▶sub sortbed {
Subroutine sortbed: not called CC=12 SCAR=50.5
Pod documented
160 0 my ($infile,$dest,$outfile,$rm_orig,$log) = @_;
161 0 my ($cmd,$out);
162 0 my $this_function = (caller(0))[3];
163 0 ▶ my $bedtools = can_run('bedtools') or
Branch: 1 branch
BranchTrueFalseTotal
my $bedtools = can_run("bedtools") or croak("ERROR [$this_function] bedtools utility not found") 0 0 0
164 croak "ERROR [$this_function] bedtools utility not found";
165  
166 0 ▶ croak "ERROR [$this_function] Cannot find $infile"
Branch: 1 branch
BranchTrueFalseTotal
unless -e $infile 0 0 0
167 unless (-e $infile);
168 0 ▶ croak "ERROR [$this_function] $dest does not exist"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dest 0 0 0
169 unless (-d $dest);
170 0 ▶ if (defined $log){open(LOG, ">>", $log) or croak $!;}
Branch: 2 branches
BranchTrueFalseTotal
if (defined $log) 0 0 0
open LOG, ">>", $log or croak($!) 0 0 0
171  
172 0 $out = file($dest,$outfile);
173 0 $cmd = "$bedtools sort -i $infile > $out";
174 0 ▶ if (defined $log){ print LOG "LOG [$this_function] $cmd\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $log) 0 0 0
175 0 my ( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) =
176 run( command => $cmd, verbose => 0 );
177 0 ▶ if( !$success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 0 0
178 0 print STDERR "ERROR [$this_function] Call to $bedtools unsuccessful\n";
179 0 print STDERR "ERROR: this is what the command printed:\n";
180 0 print join "", @$full_buf;
181 0 croak $!;
182 }
183  
184 0 ▶ if($rm_orig){
Branch: 1 branch
BranchTrueFalseTotal
if ($rm_orig) 0 0 0
185 0 ▶ unlink($infile) or
Branch: 1 branch
BranchTrueFalseTotal
unlink $infile or carp("WARN [$this_function] Could not unlink $infile") 0 0 0
186 carp "WARN [$this_function] Could not unlink $infile";
187 0 ▶ if (defined $log){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $log) 0 0 0
188 0 print LOG "[$this_function] removed $infile $!\n";
189 }
190 }
191  
192 0 ▶ if (defined $log){ close(LOG); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $log) 0 0 0
193 }
194  
195 0 ▶sub unique_array{
Subroutine unique_array: not called CC=2 SCAR=17.9
Pod documented
196  
197 0 my $arrayref = shift;
198 0 my @array = @{$arrayref};
199  
200 0 foreach my $item (@array)
201 {
202 0 $unique{$item} ++;
203 }
204 0 my @arrayuid = sort {$a cmp $b} keys %unique;
205  
206 0 return(\@arrayuid);
207 }
208  
209 0 ▶sub kmer_enrichment{
Subroutine kmer_enrichment: not called CC=4 SCAR=30.0
Pod documented
210  
211 0 my @seqs = @{$_[0]};
212 0 my $klen = $_[1];
213 # my @seq = split( //, $read_tmp );
214 0 my $kstring ='';
215 #return variables
216 0 my %km;
217 0 foreach my $sequences (@seqs){
218 # print STDERR $sequences,"\n";
219 0 my @seq = split( //, $sequences );
220 0 ▶ for ( my $seq_pos = 0; $seq_pos <= $#seq-$klen ; $seq_pos++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seq_pos <= $#seq - $klen) 0 0 0
221 0 ▶ for (my $i=$seq_pos;$i<=$seq_pos+($klen-1);$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $seq_pos + ($klen - 1)) 0 0 0
222 0 $kstring .= $seq[$i];
223 }
224 0 $km{$kstring}++;
225 0 $kstring = "";
226 }
227 }
228 0 return( \%km );
229 }
230  
231 0 ▶sub fetch_chrom_sizes{
Subroutine fetch_chrom_sizes: not called CC=7 SCAR=40.3
Pod documented
232 0 my $species = shift;
233 0 my %sizes;
234 my @chromsize;
235 0 my $this_function = (caller(0))[3];
236  
237 0 ▶ my $test_fetchChromSizes = can_run('fetchChromSizes') or
Branch: 1 branch
BranchTrueFalseTotal
my $test_fetchChromSizes = can_run("fetchChromSizes") or croak("ERROR [$this_function] fetchChromSizes utility not found") 0 0 0
238 croak "ERROR [$this_function] fetchChromSizes utility not found";
239  
240 0 my $cmd = "fetchChromSizes $species";
241 0 my( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) =
242 run(command => $cmd, verbose => 0);
243 0 ▶ if ($success){
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 0 0 0
244 0 @chromsize = @{$stdout_buf};
245 }
246 else{
247 0 carp "WARN [$this_function] Using UCSCs fetchChromSizes failed, trying alternative mysql fetch!\n";
248 0 ▶ my $test_fetchChromSizes = can_run('mysql') or
Branch: 1 branch
BranchTrueFalseTotal
my $test_fetchChromSizes = can_run("mysql") or croak("ERROR [$this_function] mysql utility not found") 0 0 0
249 croak "ERROR [$this_function] mysql utility not found";
250 0 $cmd = "mysql --user=genome --host=genome-mysql.cse.ucsc.edu -A -e \"select chrom, size from $species.chromInfo\""; ### Alternative to UCSC fetchChromSizes, has mysql dependency
251 0 my( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) =
252 run(command => $cmd, verbose => 0);
253 0 ▶ if ($success){
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 0 0 0
254 0 @chromsize = @{$stdout_buf};
255 }
256 else{
257 0 carp "ERROR [$this_function] External command call unsuccessful\n";
258 0 carp "ERROR [$this_function] this is what the command printed:\n";
259 0 print join "", @$full_buf;
260 0 confess "Fetching of chromosome sizes failed, please either download fetchChromSizes from the UCSC script collection, or install mysql!\n";
261 }
262 }
263  
264 0 foreach (@chromsize){
265 0 chomp($_);
266 0 foreach (split(/\n/,$_)){
267 0 my ($chr,$size)=split (/\t/,$_);
268 0 $sizes{$chr}=$size;
269 }
270 }
271  
272 0 return(\%sizes);
273 }
274  
275 0 ▶sub mkdircheck {
Subroutine mkdircheck: not called CC=8 SCAR=42.8
Pod documented
276 0 my @dirstocreate=();
277 0 my $this_function = (caller(0))[3];
278 0 ▶ while(@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
279 0 push @dirstocreate, shift(@_);
280 }
281 0 foreach (@dirstocreate){
282 0 my @total = split(/[\/\\]/,$_);
283 0 ▶ if ($total[0] eq '~'){
Branch: 1 branch
BranchTrueFalseTotal
if ($total[0] eq "~") 0 0 0
284 0 $total[0]=$HOME;
285 0 carp "WARNING [$this_function] replacing \'~\' in path with \'$HOME\'";
286 }
287 0 my $dir;
288 0 ▶ while(@total){
Branch: 1 branch
BranchTrueFalseTotal
if (@total) 0 0 0
289 0 ▶ $dir = dir(shift(@total)) unless (defined $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir 0 0 0
290 0 $dir = dir($dir,shift(@total));
291 }
292 0 my $check = $dir->stringify();
293 0 ▶ return if (-d $check);
Branch: 1 branch
BranchTrueFalseTotal
if -d $check 0 0 0
294 0 ▶ make_path($dir,{ verbose => 0 }) or
Branch: 1 branch
BranchTrueFalseTotal
make_path($dir, {"verbose", 0}) or croak("ERROR [$this_function] Cannot create directory $check") 0 0 0
295 croak "ERROR [$this_function] Cannot create directory $check";
296 }
297 }
298  
299 0 ▶sub rmdircheck {
Subroutine rmdircheck: not called CC=7 SCAR=40.3
Pod documented
300 0 my @dirstorm=();
301 0 my $this_function = (caller(0))[3];
302 0 ▶ while(@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
303 0 push @dirstorm, shift(@_);
304 }
305 0 foreach (@dirstorm){
306 0 my @total = split(/[\/\\]/,$_);
307 0 my $dir;
308 0 ▶ while(@total){
Branch: 1 branch
BranchTrueFalseTotal
if (@total) 0 0 0
309 0 ▶ $dir = dir(shift(@total)) unless (defined $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir 0 0 0
310 0 $dir = dir($dir,shift(@total));
311 }
312 0 my $check = $dir->stringify();
313 0 ▶ return if (!-d $check);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $check 0 0 0
314 0 ▶ remove_tree($dir,{ verbose => 0 }) or
Branch: 1 branch
BranchTrueFalseTotal
remove_tree($dir, {"verbose", 0}) or croak("ERROR [$this_function] Cannot remove directory $check") 0 0 0
315 croak "ERROR [$this_function] Cannot remove directory $check";
316 }
317 }
318  
319 1 1;
320 __END__
« lib/Bio/ViennaNGS/UCSC.pm ↑ Summary lib/Bio/ViennaNGS/FeatureIO.pm »