| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
|
| 5 |
# Computes the number and cumulative length of regions where two atac |
| 6 |
# mappings disagree. |
| 7 |
# |
| 8 |
# Reports when the region maps to: |
| 9 |
# small -- same sequence, close together |
| 10 |
# large -- same scaffold, not close together |
| 11 |
# major -- different scaffold |
| 12 |
# |
| 13 |
# Automagically generates a plot |
| 14 |
# |
| 15 |
|
| 16 |
if (scalar(@ARGV != 2)) { |
| 17 |
print STDERR "usage: $0 some.atac outprefix\n"; |
| 18 |
exit(1); |
| 19 |
} |
| 20 |
|
| 21 |
my $filename = shift @ARGV; |
| 22 |
my $outprefix = shift @ARGV; |
| 23 |
my $smallLimit = 400; |
| 24 |
|
| 25 |
my @smallH; |
| 26 |
my @smallHLen; |
| 27 |
my $large = 0; |
| 28 |
my $largeLen = 0; |
| 29 |
my $major = 0; |
| 30 |
my $majorLen = 0; |
| 31 |
|
| 32 |
open(F, "< $filename") or die "Failed to open $filename.\n"; |
| 33 |
while (<F>) { |
| 34 |
if (m/^[N!]\s+(\d+):(\d+)-(\d+)\[\s*\d+\].*\s(\d+):\s*(\d+)-\s*(\d+)\).*\s(\d+):\s*(\d+)-\s*(\d+)\)/) { |
| 35 |
my ($id1, $b1, $e1) = ($1, $2, $3); |
| 36 |
my ($id2a, $b2a, $e2a) = ($4, $5, $6); |
| 37 |
my ($id2b, $b2b, $e2b) = ($7, $8, $9); |
| 38 |
|
| 39 |
if ($id2a == $id2b) { |
| 40 |
my $diff; |
| 41 |
$diff = $b2b - $b2a; |
| 42 |
$diff = $b2a - $b2b if ($b2a > $b2b); |
| 43 |
|
| 44 |
if ($diff < $smallLimit) { |
| 45 |
$smallH[$diff]++; |
| 46 |
$smallHLen[$diff] += $e1 - $b1; |
| 47 |
} else { |
| 48 |
$large++; |
| 49 |
$largeLen += $e1 - $b1; |
| 50 |
} |
| 51 |
} else { |
| 52 |
$major++; |
| 53 |
$majorLen += $e1 - $b1; |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
close(F); |
| 58 |
|
| 59 |
# output is |
| 60 |
# |
| 61 |
# distance away |
| 62 |
# number of regions |
| 63 |
# number of bp in those regions |
| 64 |
# cumulative number of regions |
| 65 |
# cumulative number of bp in those regions |
| 66 |
# |
| 67 |
|
| 68 |
|
| 69 |
my $sumH = 0; |
| 70 |
my $sumHLen = 0; |
| 71 |
open(F, "> $outprefix.dat"); |
| 72 |
for (my $i=1; $i<$smallLimit; $i++) { |
| 73 |
$sumH += $smallH[$i]; |
| 74 |
$sumHLen += $smallHLen[$i] / 10; |
| 75 |
print F "$i $smallH[$i] $smallHLen[$i] $sumH $sumHLen\n" if (defined($smallH[$i])); |
| 76 |
} |
| 77 |
close(F); |
| 78 |
|
| 79 |
print STDERR "at most $smallLimit bp away: $sumH regions $sumHLen bp\n"; |
| 80 |
print STDERR "at least $smallLimit bp away: $large regions $largeLen bp\n"; |
| 81 |
print STDERR "different sequence: $major regions $majorLen bp\n"; |
| 82 |
|
| 83 |
open(F, "> $outprefix.gnuplot"); |
| 84 |
print F "set terminal postscript color\n"; |
| 85 |
print F "set output \"$outprefix.ps\"\n"; |
| 86 |
print F "set xlabel \"bp Difference in Match Location\"\n"; |
| 87 |
print F "set ylabel \"\"\n"; |
| 88 |
print F "plot [][0:300000] \"$outprefix.dat\" using 2 with lines title \"Number of Regions\", \\\n"; |
| 89 |
print F " \"$outprefix.dat\" using 3 with lines title \"bp in Regions\", \\\n"; |
| 90 |
print F " \"$outprefix.dat\" using 4 with lines title \"Cumulative Number of Regions\", \\\n"; |
| 91 |
print F " \"$outprefix.dat\" using 5 with lines title \"Cumulative bp in Regions / 10\"\n"; |
| 92 |
print F "plot [0:100][0:300000] \"$outprefix.dat\" using 2 with lines title \"Number of Regions\", \\\n"; |
| 93 |
print F " \"$outprefix.dat\" using 3 with lines title \"bp in Regions\", \\\n"; |
| 94 |
print F " \"$outprefix.dat\" using 4 with lines title \"Cumulative Number of Regions\", \\\n"; |
| 95 |
print F " \"$outprefix.dat\" using 5 with lines title \"Cumulative bp in Regions / 10\"\n"; |
| 96 |
close(F); |
| 97 |
|
| 98 |
system("gnuplot < $outprefix.gnuplot"); |
| 99 |
|
| 100 |
|