| 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__ | ||
| 107 | |||
| 108 | |||
| 109 | =head1 NAME | ||
| 110 | |||
| 111 | bam_to_bigWig.pl - Make bigWig coverage profiles from BAM files | ||
| 112 | |||
| 113 | =head1 SYNOPSIS | ||
| 114 | |||
| 115 | bam_to_bigWig.pl [--bam I<FILE>] [--cs I<FILE>] [--strand I<+/->] [options] | ||
| 116 | |||
| 117 | =head1 DESCRIPTION | ||
| 118 | |||
| 119 | Produce bigWig coverage profiles from (aligned) BAM files, explicitly | ||
| 120 | considering strandedness. The most natural use case of this tool is to | ||
| 121 | create strand-aware coverage profiles in bigWig format for genome | ||
| 122 | browser visualization. | ||
| 123 | |||
| 124 | =head1 OPTIONS | ||
| 125 | |||
| 126 | =over | ||
| 127 | |||
| 128 | =item B<--bam -b> | ||
| 129 | |||
| 130 | Input file in BAM format | ||
| 131 | |||
| 132 | =item B<--cs -c> | ||
| 133 | |||
| 134 | Chromosome sizes file | ||
| 135 | |||
| 136 | =item B<--strand -s> | ||
| 137 | |||
| 138 | Use this option if the input BAM file is strictly strand-specific, | ||
| 139 | ie. contains B<only> reads mapped to either the positive or negative | ||
| 140 | strand. Possible values are either '+' or '-'. If the value given here | ||
| 141 | is '+', the interim bedGraph file will be created with positive | ||
| 142 | values. A '-' given here will create the inerim bedGraph file with | ||
| 143 | negative values, which is required for proper visualization of bigWig | ||
| 144 | files holding coverage profiles of reads mapped to the negative strand | ||
| 145 | in the UCSC genome browser. If the input BAM file is not | ||
| 146 | strand-specific, ie contains reads mapped to both positive and | ||
| 147 | negative strand, then the default value '+' will be used, resulting in | ||
| 148 | bigWig coverage profiles rendered in positive (y-axis direction) in | ||
| 149 | the UCSC genome browser. | ||
| 150 | |||
| 151 | =item B<-o> | ||
| 152 | |||
| 153 | Output directory | ||
| 154 | |||
| 155 | =item B<--log -l> | ||
| 156 | |||
| 157 | Name of the log file. Unless specified, the default log file will be | ||
| 158 | "bam_to_bigwig.log" in the given output directory. | ||
| 159 | |||
| 160 | |||
| 161 | =item B<--help -h> | ||
| 162 | |||
| 163 | Print short help | ||
| 164 | |||
| 165 | =item B<--man> | ||
| 166 | |||
| 167 | Prints the manual page and exits | ||
| 168 | |||
| 169 | =back | ||
| 170 | |||
| 171 | =head1 AUTHOR | ||
| 172 | |||
| 173 | Michael T. Wolfinger E<lt>michael@wolfinger.euE<gt> | ||
| 174 | |||
| 175 | =cut | ||
| 176 |