blib/script/bam_split.pl untested

filter: Statementstmt 0.0% 0 / 72 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod n/a totaltotal 0.0% 0 / 100 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/bin/env perl
2 # -*-CPerl-*-
3 # Last changed Time-stamp: <2016-01-19 12:02:19 mtw>
4 #
5 # Split BAM files according to their strands, optionally filter unique
6 # mappers
7 #
8 # ***********************************************************************
9 # * Copyright notice
10 # *
11 # * Copyright 2015 Michael T. Wolfinger <michael@wolfinger.eu>
12 # * All rights reserved
13 # *
14 # * This program is free software: you can redistribute it and/or modify
15 # * it under the terms of the GNU General Public License as published by
16 # * the Free Software Foundation, either version 3 of the License, or
17 # * (at your option) any later version.
18 # *
19 # * This program is distributed in the hope that it will be useful,
20 # * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # * GNU General Public License for more details.
23 # *
24 # * You should have received a copy of the GNU General Public License
25 # * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 # *
27 # * This copyright notice MUST APPEAR in all copies of the script!
28 # ***********************************************************************
29  
30 use strict;
31 use warnings;
32 use Getopt::Long qw( :config posix_default bundling no_ignore_case );
33 use Pod::Usage;
34 use Data::Dumper;
35 use File::Basename;
36 use Path::Class;
37 use Bio::ViennaNGS::Bam qw(split_bam);
38 use Bio::ViennaNGS::Util qw(bed_or_bam2bw mkdircheck);
39  
40 #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^#
41 #^^^^^^^^^^ Variables ^^^^^^^^^^^#
42 #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^#
43 my ($bam_p,$bam_n,$bed_p,$bed_n,$size_p,$size_n,$basename,$bamdir,$bamext,$lf);
44 my ($rev,$wantuniq,$wantbed,$wantnorm,$bw) = (0)x5;
45 my $logext = ".bam_split.log";
46 my $cs_in = "-";
47 my $bam_in = "-";
48 my $outdir = ".";
49 my $visdir = "vis";
50 my $scale = 1000000;
51 my @result = ();
52  
53 #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^#
54 #^^^^^^^^^^^^^^ Main ^^^^^^^^^^^^^#
55 #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^#
56  
57 Getopt::Long::config('no_ignore_case');
58 pod2usage(-verbose => 1) unless GetOptions("bam=s" => \$bam_in,
59 "bed" => sub{$wantbed = 1},
60 "bw" => sub{$bw = 1},
61 "bwdir=s" => \$visdir,
62 "cs=s" => \$cs_in,
63 "norm" => sub{$wantnorm = 1},
64 "o|out=s" => \$outdir,
65 "r|reverse" => sub{$rev = 1},
66 "scale=s" => \$scale,
67 "uniq" => sub{$wantuniq = 1},
68 "l|log=s" => \$logext,
69 "man" => sub{pod2usage(-verbose => 2)},
70 "help|h" => sub{pod2usage(1)}
71 );
72  
73 unless ($bam_in =~ /^\// || $bam_in =~ /\.\//){$bam_in = "./".$bam_in;}
74 unless (-f $bam_in){
75 warn "Could not find input file $bam_in given via --bam option";
76 pod2usage(-verbose => 0);
77 }
78  
79 if ($bw == 1) {
80 unless (-f $cs_in){
81 warn "Could not find input file $cs_in given via --cs option";
82 pod2usage(-verbose => 0);
83 }
84 unless ($wantbed == 1){$wantbed = 1;}
85 }
86  
87 #TODO check if we are allowed to write to $outdir
88 unless (-d $outdir){mkdircheck($outdir)}
89  
90 ($basename,$bamdir,$bamext) = fileparse($bam_in,qr/\..*/);
91  
92 $lf = file($outdir,$basename.$logext);
93  
94 @result = split_bam($bam_in,$rev,$wantuniq,$wantbed,$outdir,$lf);
95 $bam_p = $result[0]; # BAM file containing fragments of [+] strand
96 $bam_n = $result[1]; # BAM file containing fragments of [-] strand
97 $size_p = $result[2]; # of alignments on [+] strand
98 $size_n = $result[3]; # of alignments on [-] srand
99 $bed_p = $result[4]; # BED file containing fragments of [+] strand
100 $bed_n = $result[5]; # BED file containing fragments of [-] strand
101  
102 if ($bw == 1) {
103 my $od = dir($outdir,$visdir);
104 mkdircheck($od->stringify());
105 bed_or_bam2bw("bed",$bed_p,$cs_in,"+",$od,$wantnorm,$size_p,$scale,$lf);
106 bed_or_bam2bw("bed",$bed_n,$cs_in,"-",$od,$wantnorm,$size_n,$scale,$lf);
107 }
108  
109  
110 __END__
111  
112  
113 =head1 NAME
114  
115 bam_split.pl - Split a BAM file by strands
116  
117 =head1 SYNOPSIS
118  
119 bam_split.pl [--bam I<FILE>] [options]
120  
121 =head1 DESCRIPTION
122  
123 Split a BAM file by strands and create two new BAM file: One
124 containing all reads that map to the positive strand and another one
125 with all reads mapped to the negative strand. Optionally filter unique
126 alignments by inspecting NH:i SAM attribute.
127  
128 Optionally create bedGraph and (stranded |normalized) bigWig coverage
129 for UCSC visualization
130  
131 =head1 OPTIONS
132  
133 =over
134  
135 =item B<--bam>
136  
137 Input file in BAM format
138  
139 =item B<--bed>
140  
141 Create a BED6 file for each split BAM file
142  
143 =item B<--bw>
144  
145 Create BedGraph and bigWig coverage files for e.g. genome browser
146 visualization.
147  
148 =item B<--bwdir>
149  
150 Directory name for resulting bigWig files. This directory is created
151 as subdirectory of the output directory. Default is 'vis'.
152  
153 =item B<--cs>
154  
155 Chromosome sizes file (required if B<--bw> is given).
156  
157 =item B<--norm>
158  
159 Normalize resulting bigWig files
160  
161 =item B<--out -o>
162  
163 Output directory
164  
165 =item B<--reverse -r>
166  
167 Reverse the +/- strand mapping. This is required to achieve proper
168 strand assignments for certain RNA-seq library preparation protocol.
169  
170 =item B<--scale>
171  
172 If B<--bw> is given, scale bigWig files to this number. Default is 1000000.
173  
174 =item B<--uniq>
175  
176 Filter uniquely mapped reads by inspecting the NH:i: SAM
177 attribute. See also the I<bam_uniq.pl> utility, which extracts both
178 uniquely and multiply mapped reads from BAM files without
179 strand-splitting.
180  
181 =item B<--log -l>
182  
183 Log file extension. Default is ".bam_split.log". The log file is
184 created in the directory given via B<-o> and its name is constructed
185 from the base name of the input BAM file and the log filename extension.
186  
187 =item B<--help -h>
188  
189 Print short help
190  
191 =item B<--man>
192  
193 Prints the manual page and exits
194  
195 =back
196  
197 =head1 AUTHOR
198  
199 Michael T. Wolfinger E<lt>michael@wolfinger.euE<gt>
200  
201 =cut
202