| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# Reads an atac file, relabels the sequence names (e.g., WGSA:4) with |
| 4 |
# the defline ID's. |
| 5 |
|
| 6 |
use strict; |
| 7 |
|
| 8 |
sub readDeflines ($) { |
| 9 |
my $file = $_[0]; |
| 10 |
if ($file =~ m/^(.*).fasta/) { |
| 11 |
$file = $1; |
| 12 |
} |
| 13 |
|
| 14 |
if (-e "$file.deflines") { |
| 15 |
$file = "$file.deflines"; |
| 16 |
} elsif (-e "$file.fasta.deflines") { |
| 17 |
$file = "$file.fasta.deflines"; |
| 18 |
} else { |
| 19 |
print STDERR "Dang, gotta grep the deflines!\n"; |
| 20 |
system("grep '>' $file.fasta > $file.deflines"); |
| 21 |
$file = "$file.deflines"; |
| 22 |
} |
| 23 |
|
| 24 |
my @nameA; |
| 25 |
|
| 26 |
#print STDERR "$file\n"; |
| 27 |
|
| 28 |
open(Z, "< $file") or die "Failed to open '$file'\n"; |
| 29 |
while (!eof(Z)) { |
| 30 |
my $n = <Z>; |
| 31 |
if ($n =~ m/^\>\s*(\S+)\s*/) { |
| 32 |
push @nameA, $1; |
| 33 |
} else { |
| 34 |
chomp $n; |
| 35 |
print STDERR "Failed to match defline '$n'\n"; |
| 36 |
} |
| 37 |
} |
| 38 |
close(Z); |
| 39 |
|
| 40 |
return(@nameA); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
my $file = shift @ARGV; |
| 45 |
my @nameA; |
| 46 |
my @nameB; |
| 47 |
|
| 48 |
if ($file eq "-A") { |
| 49 |
@nameA = readDeflines(shift @ARGV); |
| 50 |
$file = shift @ARGV; |
| 51 |
} |
| 52 |
if ($file eq "-B") { |
| 53 |
@nameB = readDeflines(shift @ARGV); |
| 54 |
$file = shift @ARGV; |
| 55 |
} |
| 56 |
|
| 57 |
open(F, "< $file") or die "Failed to open '$file' for input\n"; |
| 58 |
open(G, "> $file.uids") or die "Failed to open '$file.uids' for output\n"; |
| 59 |
|
| 60 |
while (<F>) { |
| 61 |
if (m/assemblyFile1=(.*)$/) { |
| 62 |
@nameA = readDeflines($1); |
| 63 |
print STDERR "num nameA = ", scalar(@nameA), "\n"; |
| 64 |
} |
| 65 |
if (m/assemblyFile2=(.*)$/) { |
| 66 |
@nameB = readDeflines($1); |
| 67 |
print STDERR "num nameB = ", scalar(@nameB), "\n"; |
| 68 |
} |
| 69 |
|
| 70 |
if (m/^M\s/) { |
| 71 |
my @v = split '\s+', $_; |
| 72 |
|
| 73 |
if ($v[4] =~ m/^\w+:(\d+)$/) { |
| 74 |
if (defined($nameA[$1])) { |
| 75 |
$v[4] = $nameA[$1]; |
| 76 |
} else { |
| 77 |
die "Didn't find nameA for $1\n"; |
| 78 |
} |
| 79 |
} else { |
| 80 |
die "Didn't match v[4] = $v[4]\n"; |
| 81 |
} |
| 82 |
|
| 83 |
if ($v[8] =~ m/^\w+:(\d+)$/) { |
| 84 |
if (defined($nameB[$1])) { |
| 85 |
$v[8] = $nameB[$1]; |
| 86 |
} else { |
| 87 |
die "Didn't find nameA for $1\n"; |
| 88 |
} |
| 89 |
} else { |
| 90 |
die "Didn't match v[8] = $v[8]\n"; |
| 91 |
} |
| 92 |
|
| 93 |
# Special case stuff.... |
| 94 |
# |
| 95 |
if ($v[4] =~ m/^Chr(\d+)$/) { |
| 96 |
$v[4] = "mchr$1"; |
| 97 |
} elsif ($v[4] =~ m/^Chr(\d+)_random$/) { |
| 98 |
$v[4] = "mchr${1}r"; |
| 99 |
} elsif ($v[4] =~ m/^SCAFFOLD(\d+)$/) { |
| 100 |
$v[4] = "bscf$1"; |
| 101 |
} elsif ($v[4] =~ m/^Contig(\d+)$/) { |
| 102 |
$v[4] = "wscf$1"; |
| 103 |
} elsif ($v[4] =~ m/^chr(\d+)$/) { |
| 104 |
$v[4] = "hchr$1"; |
| 105 |
} |
| 106 |
|
| 107 |
if ($v[8] =~ m/^SCAFFOLD(\d+)$/) { |
| 108 |
$v[8] = "bscf$1"; |
| 109 |
} elsif ($v[8] =~ m/^Contig(\d+)$/) { |
| 110 |
$v[8] = "wscf$1"; |
| 111 |
} elsif ($v[8] =~ m/^chr(\d+)$/) { |
| 112 |
$v[8] = "hchr$1"; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
my $line = join " ", @v; |
| 118 |
print G "$line\n"; |
| 119 |
} else { |
| 120 |
print G $_; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
close(G); |
| 125 |
close(F); |