| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
|
| 5 |
# Examines an atac mapping, counts the number of times a scaffold is |
| 6 |
# mapped to wildly different places -- wildly being more than a few |
| 7 |
# bp away on the same chromosome (mind the gap, please!) or (gasp!) a |
| 8 |
# different chromosome. |
| 9 |
# |
| 10 |
# Assumes that the Aannotation primary axis is chromosomes. |
| 11 |
# |
| 12 |
# Change the first . in the m// below to restrict to specific types |
| 13 |
# of regions, e.g., N's. Useful choices here are: |
| 14 |
# . - all regions |
| 15 |
# U - unmapped (will do nothing) |
| 16 |
# 1 - has only one mapping |
| 17 |
# Y - just those they agree on |
| 18 |
# N - disagree, but on the same destination |
| 19 |
# ! - disagree, and on different destinations |
| 20 |
# ? - inconsistent mapping (OK, this one isn't useful) |
| 21 |
# |
| 22 |
# 1Y - the regions that have no disagreement |
| 23 |
# 1YN! - all consistent regions |
| 24 |
|
| 25 |
if (scalar(@ARGV != 2)) { |
| 26 |
print STDERR "usage: $0 some.Aannotation outprefix\n"; |
| 27 |
exit(1); |
| 28 |
} |
| 29 |
|
| 30 |
my $filename = shift @ARGV; |
| 31 |
my $outprefix = shift @ARGV; |
| 32 |
|
| 33 |
# scafA is really scaffolds-from-map1, and scafB is scaffolds-from-map2. |
| 34 |
|
| 35 |
my (%scafA_to_chr, %scafA_to_chr_mismatch, $scafAlen); |
| 36 |
my (%scafB_to_chr, %scafB_to_chr_mismatch, $scafBlen); |
| 37 |
|
| 38 |
open(F, "< $filename"); |
| 39 |
while (<F>) { |
| 40 |
chomp; |
| 41 |
|
| 42 |
if (m/^[1YN!]\s+(\d+):(\d+)-(\d+)\[\s*\d+\].*\s(\d+):\s*(\d+)-\s*(\d+)\).*\s(\d+):\s*(\d+)-\s*(\d+)\)/) { |
| 43 |
my ($id1, $b1, $e1) = ($1, $2, $3); |
| 44 |
my ($id2a, $b2a, $e2a) = ($4, $5, $6); |
| 45 |
my ($id2b, $b2b, $e2b) = ($7, $8, $9); |
| 46 |
|
| 47 |
# If we have a mapping from method A or method B, save the |
| 48 |
# chromosome that the scaffold mapped to. If we've already |
| 49 |
# mapped this scaffold to some other chromosome, call it a |
| 50 |
# mismatch. |
| 51 |
|
| 52 |
if (($id2a > 0) && ($e2a > 0)) { |
| 53 |
if (defined($scafA_to_chr{$id2a})) { |
| 54 |
if ($scafA_to_chr{$id2a} != $id1) { |
| 55 |
$scafA_to_chr_mismatch{$id2a} = $scafA_to_chr{$id2a} if (! defined($scafA_to_chr_mismatch{$id2a})); |
| 56 |
$scafA_to_chr_mismatch{$id2a} .= "\1$id1\0$_"; |
| 57 |
} |
| 58 |
} else { |
| 59 |
$scafA_to_chr{$id2a} = "$id1\0$_"; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
if (($id2b > 0) && ($e2b > 0)) { |
| 64 |
if (defined($scafB_to_chr{$id2b})) { |
| 65 |
if ($scafB_to_chr{$id2b} != $id1) { |
| 66 |
$scafB_to_chr_mismatch{$id2b} = $scafB_to_chr{$id2b} if (! defined($scafB_to_chr_mismatch{$id2b})); |
| 67 |
$scafB_to_chr_mismatch{$id2b} .= "\1$id1\0$_"; |
| 68 |
} |
| 69 |
} else { |
| 70 |
$scafB_to_chr{$id2b} = "$id1\0$_"; |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
close(F); |
| 76 |
|
| 77 |
# Count the number of things in *_mismatch that are the same. |
| 78 |
# |
| 79 |
my %merge; |
| 80 |
my $both; |
| 81 |
foreach my $f (keys %scafA_to_chr_mismatch) { |
| 82 |
$merge{$f}++; |
| 83 |
} |
| 84 |
foreach my $f (keys %scafB_to_chr_mismatch) { |
| 85 |
$merge{$f}++; |
| 86 |
} |
| 87 |
foreach my $f (keys %merge) { |
| 88 |
if (defined($scafA_to_chr_mismatch{$f}) && defined($scafB_to_chr_mismatch{$f})) { |
| 89 |
$both++; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
print "num scafA: ", scalar(keys %scafA_to_chr_mismatch), "\n"; |
| 94 |
print "num scafB: ", scalar(keys %scafB_to_chr_mismatch), "\n"; |
| 95 |
print "num both: ", $both, "\n"; |
| 96 |
|
| 97 |
# Run through the input again, pulling out matches that map a |
| 98 |
# single scaffold to two different chromosomes, then what? we |
| 99 |
# saved the iid of the scaffold that maps to different |
| 100 |
# chromosomes as the key, so just parse the matches again, |
| 101 |
# pulling out all those scaffolds. |
| 102 |
|
| 103 |
open(A, "| sort -k6n -k7n > $outprefix.scaffold-consistency.map1dups"); |
| 104 |
open(B, "| sort -k11n -k12n > $outprefix.scaffold-consistency.map2dups"); |
| 105 |
my $matchesA = 0; |
| 106 |
my $matchesB = 0; |
| 107 |
open(F, "< $filename"); |
| 108 |
while (<F>) { |
| 109 |
if (m/^.\s+(\d+):(\d+)-(\d+)\[\s*\d+\].*\s(\d+):\s*(\d+)-\s*(\d+)\).*\s(\d+):\s*(\d+)-\s*(\d+)\)/) { |
| 110 |
my ($id1, $b1, $e1) = ($1, $2, $3); |
| 111 |
my ($id2a, $b2a, $e2a) = ($4, $5, $6); |
| 112 |
my ($id2b, $b2b, $e2b) = ($7, $8, $9); |
| 113 |
|
| 114 |
if (defined($scafA_to_chr_mismatch{$id2a}) || defined($scafA_to_chr_mismatch{$id2b})) { |
| 115 |
print A $_; |
| 116 |
$matchesA++; |
| 117 |
} |
| 118 |
|
| 119 |
if (defined($scafB_to_chr_mismatch{$id2a}) || defined($scafB_to_chr_mismatch{$id2b})) { |
| 120 |
print B $_; |
| 121 |
$matchesB++; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
close(A); |
| 126 |
close(B); |
| 127 |
|
| 128 |
print STDERR "matches for map1: $matchesA\n"; |
| 129 |
print STDERR "matches for map2: $matchesB\n"; |