| 1 | #!/usr/bin/env perl | ||
| 2 | # -*-CPerl-*- | ||
| 3 | # Last changed Time-stamp: <2015-02-27 22:18:58 mtw> | ||
| 4 | # | ||
| 5 | # *********************************************************************** | ||
| 6 | # * Copyright notice | ||
| 7 | # * | ||
| 8 | # * Copyright 2015 Michael T. Wolfinger <michael@wolfinger.eu> | ||
| 9 | # * All rights reserved | ||
| 10 | # * | ||
| 11 | # * This program is free software: you can redistribute it and/or modify | ||
| 12 | # * it under the terms of the GNU General Public License as published by | ||
| 13 | # * the Free Software Foundation, either version 3 of the License, or | ||
| 14 | # * (at your option) any later version. | ||
| 15 | # * | ||
| 16 | # * This program is distributed in the hope that it will be useful, | ||
| 17 | # * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 18 | # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 19 | # * GNU General Public License for more details. | ||
| 20 | # * | ||
| 21 | # * You should have received a copy of the GNU General Public License | ||
| 22 | # * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 23 | # * | ||
| 24 | # * This copyright notice MUST APPEAR in all copies of the script! | ||
| 25 | # *********************************************************************** | ||
| 26 | |||
| 27 | use strict; | ||
| 28 | use warnings; | ||
| 29 | use Getopt::Long qw( :config posix_default bundling no_ignore_case ); | ||
| 30 | use Pod::Usage; | ||
| 31 | use Data::Dumper; | ||
| 32 | use Path::Class; | ||
| 33 | use File::Basename; | ||
| 34 | use Bio::ViennaNGS::Util qw(bed_or_bam2bw); | ||
| 35 | use Bio::DB::Sam; | ||
| 36 | |||
| 37 | |||
| 38 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 39 | #^^^^^^^^^^ Variables ^^^^^^^^^^^# | ||
| 40 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 41 | |||
| 42 | my ($bam_in,$lf,$bwfile,$bam,$read,$header,$basename); | ||
| 43 | my $logext = ".bam_to_bigwig.log"; | ||
| 44 | my $outdir = "./"; | ||
| 45 | my $want_bigbed = 0; | ||
| 46 | my $cs_in = "-"; | ||
| 47 | my $strand = "+"; | ||
| 48 | my $counter = 0; | ||
| 49 | my $scale = 1000000; | ||
| 50 | my $wantnorm = 0; | ||
| 51 | |||
| 52 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 53 | #^^^^^^^^^^^^^^ Main ^^^^^^^^^^^^^# | ||
| 54 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 55 | |||
| 56 | Getopt::Long::config('no_ignore_case'); | ||
| 57 | pod2usage(-verbose => 1) unless GetOptions("b|bam=s" => \$bam_in, | ||
| 58 | "c|cs=s" => \$cs_in, | ||
| 59 | "o=s" => \$outdir, | ||
| 60 | "l|log=s" => \$logext, | ||
| 61 | "s|strand=s" => \$strand, | ||
| 62 | "scale=s" => \$scale, | ||
| 63 | "n|norm" => sub{$wantnorm = 1}, | ||
| 64 | "man" => sub{pod2usage(-verbose => 2)}, | ||
| 65 | "help|h" => sub{pod2usage(1)} | ||
| 66 | ); | ||
| 67 | |||
| 68 | |||
| 69 | unless ($bam_in =~ /^\//) {$bam_in = "./".$bam_in;} | ||
| 70 | unless (-f $bam_in){ | ||
| 71 | warn "Could not find input file $bam_in given via --bam option"; | ||
| 72 | pod2usage(-verbose => 0); | ||
| 73 | } | ||
| 74 | unless (-f $cs_in){ | ||
| 75 | warn "Could not find input file $cs_in given via --cs option"; | ||
| 76 | pod2usage(-verbose => 0); | ||
| 77 | } | ||
| 78 | unless ($strand =~ /[\+\-]/) { | ||
| 79 | warn "Invalid value '$strand' given via -s option. Please specify | ||
| 80 | either '+' or '-' for positive or negative strand, respectively"; | ||
| 81 | pod2usage(-verbose => 0); | ||
| 82 | } | ||
| 83 | |||
| 84 | #TODO check if we are allowed to write to $outdir | ||
| 85 | unless ($outdir =~ /\/$/){$outdir .= "/";} | ||
| 86 | unless (-d $outdir){mkdir $outdir or die $!;} | ||
| 87 | |||
| 88 | $basename = basename($bam_in,".bam"); | ||
| 89 | $lf = file($outdir,$basename.$logext); | ||
| 90 | |||
| 91 | if ($wantnorm == 1){ | ||
| 92 | # count number of alignments in BAM file | ||
| 93 | $bam = Bio::DB::Bam->open($bam_in, "r"); | ||
| 94 | $header = $bam->header; | ||
| 95 | while ($read= $bam->read1() ) { | ||
| 96 | $counter++; | ||
| 97 | } | ||
| 98 | print STDERR "INFO bam_to_bigwig.pl >$bam_in< | # of alignments is: $counter\n"; | ||
| 99 | $bwfile = bed_or_bam2bw("bam",$bam_in,$cs_in,$strand,$outdir,$wantnorm,$counter,$scale,$lf); | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | $bwfile = bed_or_bam2bw("bam",$bam_in,$cs_in,$strand,$outdir,0,0,1.,$lf); | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | __END__ |