| 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__ |