| 1 | #!/usr/local/bin/perl -w | ||
| 2 | use strict; | ||
| 3 | use List::Util 'sum'; | ||
| 4 | use Getopt::Long; | ||
| 5 | |||
| 6 | use vars qw/$start $end $current_chr @scores %seen %highest $win $normal $bam/; | ||
| 7 | |||
| 8 | use constant WIN => 25; | ||
| 9 | |||
| 10 | # A script to make bam coverage windows in WIG/BED 4 format | ||
| 11 | # requires that samtools be installed | ||
| 12 | # This script operates on one bam file at a time. If you are comparing | ||
| 13 | # across bam files of diffenrent sizes (read numbers), take note | ||
| 14 | # of the normalization option. | ||
| 15 | # Sheldon McKay (sheldon.mckay@gmail.com) | ||
| 16 | |||
| 17 | |||
| 18 | BEGIN { | ||
| 19 | die "samtools must be installed!" unless `which samtools`; | ||
| 20 | } | ||
| 21 | |||
| 22 | |||
| 23 | GetOptions ("normalize=i" => \$normal, | ||
| 24 | "bam=s" => \$bam, | ||
| 25 | "window=i" => \$win); | ||
| 26 | |||
| 27 | $bam or usage(); | ||
| 28 | |||
| 29 | open BAM, "samtools depth $bam |"; | ||
| 30 | |||
| 31 | $win ||= WIN; | ||
| 32 | $start = 1; | ||
| 33 | $end = $win; | ||
| 34 | |||
| 35 | my $factor = normalization_factor($normal); | ||
| 36 | |||
| 37 | chomp(my $name = `basename $bam .bam`); | ||
| 38 | print qq(track type=wiggle_0 name="$name" description="read coverage for $bam (window size $win)"\n); | ||
| 39 | |||
| 40 | while (<BAM>) { | ||
| 41 | chomp; | ||
| 42 | my ($chr,$coord,$score) = split; | ||
| 43 | $current_chr ||= $chr; | ||
| 44 | |||
| 45 | check_sorted($chr,$coord); | ||
| 46 | |||
| 47 | if ( $chr ne $current_chr || | ||
| 48 | $coord > $end ) { | ||
| 49 | open_window($chr,$coord); | ||
| 50 | } | ||
| 51 | |||
| 52 | push @scores, $score; | ||
| 53 | $current_chr = $chr; | ||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | sub open_window { | ||
| 58 | my ($chr,$coord) = @_; | ||
| 59 | |||
| 60 | if ($chr ne $current_chr) { | ||
| 61 | $seen{$current_chr}++; | ||
| 62 | } | ||
| 63 | |||
| 64 | # close the last window, if needed | ||
| 65 | if (@scores > 0) { | ||
| 66 | close_window(); | ||
| 67 | } | ||
| 68 | |||
| 69 | $start = nearest_start($coord); | ||
| 70 | $end = $start + $win; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub close_window { | ||
| 74 | my $sum = sum(@scores) or return 0; | ||
| 75 | my $score = $sum/$win; | ||
| 76 | $score *= $factor; | ||
| 77 | print join("\t",$current_chr,$start,$end,$score), "\n"; | ||
| 78 | @scores = (); | ||
| 79 | exit 0 unless $score; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub nearest_start { | ||
| 83 | my $start = shift; | ||
| 84 | |||
| 85 | return 1 if $start < $win; | ||
| 86 | |||
| 87 | while ($start % $win) { | ||
| 88 | $start--; | ||
| 89 | } | ||
| 90 | |||
| 91 | return $start; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub normalization_factor { | ||
| 95 | return 1 unless my $nahm = shift; | ||
| 96 | print STDERR "Calculating total number of reads in $bam\n"; | ||
| 97 | chomp(my $total = `samtools view -c $bam`); | ||
| 98 | print STDERR "$bam has $total reads\n"; | ||
| 99 | return $total/$nahm; | ||
| 100 | } | ||
| 101 | |||
| 102 | # sanity check for unsorted bam | ||
| 103 | sub check_sorted { | ||
| 104 | my ($chr,$coord) = @_; | ||
| 105 | |||
| 106 | return 1 if $coord > ($highest{$chr} || 0); | ||
| 107 | $highest{$chr} = $coord; | ||
| 108 | |||
| 109 | return 1 unless $seen{$chr}; | ||
| 110 | |||
| 111 | die_unsorted($chr,$coord); | ||
| 112 | } | ||
| 113 | |||
| 114 | sub die_unsorted { | ||
| 115 | my ($chr,$coord) = @_; | ||
| 116 | die "$chr $coord: $bam does not appear to be sorted\n", | ||
| 117 | "Please try 'samtools sort' first"; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub usage { | ||
| 121 | die ' | ||
| 122 | Usage: perl bam_coverage_windows.pl -b bamfile -n 10_000_000 -w 25 | ||
| 123 | -b name of bam file to read REQUIRED | ||
| 124 | -w window size (default 25) | ||
| 125 | -n normalized read number -- if you will be comparing multiple bam files | ||
| 126 | select the read number to normalize against. | ||
| 127 | all counts will be adjusted by a factor of: | ||
| 128 | actual readnum/normalized read num | ||
| 129 | |||
| 130 | ' | ||
| 131 | } |