| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# Takes a path to a properly formatted atac file, uses that file to |
| 4 |
# detect potential chimeric scaffolds. |
| 5 |
|
| 6 |
use strict; |
| 7 |
|
| 8 |
my $atacFile = undef; |
| 9 |
my $reference = "A"; |
| 10 |
my $noiseLevel = 1; |
| 11 |
|
| 12 |
while (scalar(@ARGV)) { |
| 13 |
my $arg = shift @ARGV; |
| 14 |
|
| 15 |
if ($arg eq "-A") { |
| 16 |
$reference = "A"; |
| 17 |
} elsif ($arg eq "-B") { |
| 18 |
$reference = "B"; |
| 19 |
} elsif ($arg eq "-n") { |
| 20 |
$noiseLevel = shift @ARGV; |
| 21 |
} elsif (-e $arg) { |
| 22 |
$atacFile = $arg; |
| 23 |
} else { |
| 24 |
print STDERR "Unknown option (or input file) '$arg'\n"; |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
if (! -e "$atacFile") { |
| 29 |
print STDERR "usage: $0 [-A | -B] file.atac\n"; |
| 30 |
print STDERR " -A use the first assembly as the reference (default)\n"; |
| 31 |
print STDERR " -B use the second assembly as the reference\n"; |
| 32 |
exit(1); |
| 33 |
} |
| 34 |
|
| 35 |
open(ATAC, "< $atacFile") or die; |
| 36 |
my @ATAC = <ATAC>; |
| 37 |
chomp @ATAC; |
| 38 |
close(ATAC); |
| 39 |
|
| 40 |
my %ATACtoUID; |
| 41 |
|
| 42 |
foreach my $line (@ATAC) { |
| 43 |
if ($line =~ m/assemblyFile(\d)=(.*)$/) { |
| 44 |
chomp $line; |
| 45 |
|
| 46 |
my $sequenceFile; |
| 47 |
|
| 48 |
if (($1 == 1) && ($reference eq "B")) { |
| 49 |
$sequenceFile = $2; |
| 50 |
} |
| 51 |
if (($1 == 2) && ($reference eq "A")) { |
| 52 |
$sequenceFile = $2; |
| 53 |
} |
| 54 |
|
| 55 |
# If not defined, we don't need to read in these ID's. |
| 56 |
|
| 57 |
if (defined($sequenceFile)) { |
| 58 |
$sequenceFile =~ s/.fasta/.info/; |
| 59 |
|
| 60 |
die "Failed to find info on '$sequenceFile'\n" if (! -e $sequenceFile); |
| 61 |
|
| 62 |
print STDERR "Reading ATAC to UID map for '$sequenceFile'\n"; |
| 63 |
|
| 64 |
open(F, "< $sequenceFile"); |
| 65 |
while (<F>) { |
| 66 |
if (m/^G/) { |
| 67 |
my @vals = split '\s+', $_; |
| 68 |
$ATACtoUID{$vals[2]} = $vals[13]; |
| 69 |
} |
| 70 |
} |
| 71 |
close(F); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
| 78 |
# M u H4467431a11 r1 B35LC:0 56097 66 1 HUREF4:36734 812 66 -1 # 10867 |
| 79 |
# M u H4467431a10 r1 B35LC:0 56163 29 1 HUREF4:36734 782 29 -1 # 10867 |
| 80 |
|
| 81 |
# Note that our match below does not match the non-clump marker "-1" |
| 82 |
|
| 83 |
|
| 84 |
# Find the scaffolds with errors |
| 85 |
# |
| 86 |
# Save the clump id for the first instance of every scaffold. If |
| 87 |
# we've seen the scaffold before, and the clump id is now different, |
| 88 |
# remember this scaffold. |
| 89 |
|
| 90 |
my %scaffold; |
| 91 |
my %errors; |
| 92 |
|
| 93 |
foreach (@ATAC) { |
| 94 |
if (m/^M\su\s.*\s#\s(\d+)$/) { |
| 95 |
my @v = split '\s+', $_; |
| 96 |
|
| 97 |
if (!defined($scaffold{$v[8]})) { |
| 98 |
$scaffold{$v[8]} = $v[13]; |
| 99 |
} elsif ($scaffold{$v[8]} ne $v[13]) { |
| 100 |
$errors{$v[8]}++; |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
# Print them |
| 106 |
# |
| 107 |
# Go through the map again, remembering the number of times we see a |
| 108 |
# scaffold/clump pair. It's also useful to remember the sum of the |
| 109 |
# lengths for this pair, and the chromosome it maps to. |
| 110 |
|
| 111 |
my %counts; |
| 112 |
my %length; |
| 113 |
my %chrid; |
| 114 |
|
| 115 |
foreach (@ATAC) { |
| 116 |
if (m/^M\su\s.*\s#\s(\d+)$/) { |
| 117 |
my @v = split '\s+', $_; |
| 118 |
|
| 119 |
if (defined($errors{$v[8]})) { |
| 120 |
my $string = "$v[8]\t$ATACtoUID{$v[8]}\t$v[13]"; |
| 121 |
$counts{$string}++; |
| 122 |
$length{$string} += $v[10]; |
| 123 |
$chrid{$string} = $v[4]; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
# We could provide a raw dump of this data, but we'd like |
| 129 |
# to first denoise it. A very simple denoser works - just |
| 130 |
# don't report anything with one match. |
| 131 |
|
| 132 |
open(F, "| sort -k3,3"); |
| 133 |
foreach my $s (keys %counts) { |
| 134 |
if ($counts{$s} > $noiseLevel) { |
| 135 |
print F "$counts{$s}\t$length{$s}\t$s\t$chrid{$s}\n"; |
| 136 |
} |
| 137 |
} |
| 138 |
close(F); |