Debian Codegraph @

atac-driver/run-comparison.pl

raw
1 #!/usr/bin/perl
2
3 # Runs the full assembly-to-assembly mapping comparison.
4 #
5 # Takes two ATAC-format mapping files and:
6 # shifts 1bp gaps to the 3' end
7 # compute the assembly annotation
8 #
9 # Generate statistics:
10 # sum and histogram of the different annotations
11 # sum and histogram of the disagreement
12 #
13 # number of matches in each
14 # number of scaffolds mapped to multiple chromosomes
15 #
16 # Nx (0 <= x <= 100)
17 #
18 # histogram of match length
19 # histogram of run length
20 # histogram of clump length
21 #
22
23 my $bin = "/bioinfo/assembly/walenz/hummap2/src/genomics/atac-driver";
24 my $overlap = "$bin/alignOverlap/overlap";
25 my $gapShifter = "$bin/gapShifter/gapShifter";
26 my $mismatchCounter = "$bin/mismatchCounter/mismatchCounter";
27
28 if (scalar(@ARGV) != 2) {
29 print STDERR "usage: $0 <amap-mapping> <atac-mapping>\n";
30 exit(1);
31 }
32
33 my $amap = shift @ARGV;
34 my $atac = shift @ARGV;
35 my $dir = "COMPARE";
36 my $tmp = "COMPARE"; # /tmp on assembly-a is pathetic
37
38 # If we're given relative paths, make them absolute
39 #
40 my $pwd = `pwd`; chomp $pwd;
41
42 $amap = "$pwd/$amap" if ($amap !~ m!^/!);
43 $atac = "$pwd/$atac" if ($atac !~ m!^/!);
44
45
46 # We run stuff in the $dir directory, set that up
47 #
48 system("mkdir $dir") if (! -d "$dir");
49 system("ln -s $amap $dir/amap.atac") if (! -e "$dir/amap.atac");
50 system("ln -s $atac $dir/atac.atac") if (! -e "$dir/atac.atac");
51
52
53 if (! -e "$dir/amap.shift.atac") {
54 print STDERR "GAP SHIFTER NEEDS assembly-a, so that the whole genome can be loaded!\n";
55 system("$gapShifter -g 1 < $dir/amap.atac > $dir/amap.shift.atac") and die "Failed to shift amap.\n";
56 }
57
58 if (! -e "$dir/atac.shift.atac") {
59 print STDERR "GAP SHIFTER NEEDS assembly-a, so that the whole genome can be loaded!\n";
60 system("$gapShifter -g 1 < $dir/atac.atac > $dir/atac.shift.atac") and die "Failed to shift atac.\n";
61 }
62
63
64 # Run overlap
65 #
66 if (! -e "$dir/overlap.map1annotation") {
67 system("$overlap $dir/amap.shift.atac $dir/atac.shift.atac $dir/overlap") and die "Failed to overlap.\n";
68 }
69
70
71 # Reads the annotation output of overlap.C and adds percent identity
72 # for each match.
73 #
74 # This is done by first converting the overlap.C output into atac
75 # format (one file for each mapping), running mismatchCounter on each
76 # of those files, then merging the results together.
77 #
78 if (! -e "overlap.map1annotation.identity") {
79 if ((-e "$tmp/c.otoi") && (-e "$tmp/d.otoi")) {
80 print STDERR "Using $tmp/c.otoi and $tmp/d.otoi\n";
81 } else {
82 overlapToAtac("$dir/overlap.map1annotation", "$tmp/a.otoi", "$tmp/b.otoi");
83
84 print STDERR "Counting mismatches.\n";
85
86 system("$mismatchCounter < $tmp/a.otoi > $tmp/c.otoi") and die "Failed mismatchCounter on A.\n";
87 system("$mismatchCounter < $tmp/b.otoi > $tmp/d.otoi") and die "Failed mismatchCounter on B.\n";
88 }
89
90 open(A, "< $tmp/c.otoi") or die "Failed to open $tmp/c.otoi\n";
91 open(B, "< $tmp/d.otoi") or die "Failed to open $tmp/d.otoi\n";
92 open(M, "< $dir/overlap.map1annotation");
93 open(O, "> $dir/overlap.map1annotation.identity");
94
95 my $a;
96 my $b;
97 my $m;
98
99 print STDERR "Merging results.\n";
100
101 while (!eof(A) && !eof(B) && !eof(M)) {
102 $m = <M>; chomp $m;
103
104 # Skip any ATAC headers in A and B
105 do { $a = <A>; chomp $a; } while ($a =~ m/^\//);
106 do { $b = <B>; chomp $b; } while ($b =~ m/^\//);
107
108 my @av = split '\s+', $a;
109 my @bv = split '\s+', $a;
110
111 my ($aid, $abeg, $alen, $aori, $amis, $aident) = (undef, undef, undef, undef, undef, "0.0");
112 my ($bid, $bbeg, $blen, $bori, $bmis, $bident) = (undef, undef, undef, undef, undef, "0.0");
113
114 if ($a =~ m/HUREF:(\d+)\s+(\d+)\s+(\d+)\s+(-*\d+)\s+>\s+\/mismatches=(\d+)\s+\/identity=(\d+\.\d+)/) {
115 $aid = $1;
116 $abeg = $2;
117 $alen = $3;
118 $aori = $4;
119 $amis = $5;
120 $aident = $6;
121 } elsif ($a !~ m/^M\sm\s/) {
122 print "Anope $a\n";
123 }
124
125 if ($b =~ m/HUREF:(\d+)\s+(\d+)\s+(\d+)\s+(-*\d+)\s+>\s+\/mismatches=(\d+)\s+\/identity=(\d+\.\d+)/) {
126 $bid = $1;
127 $bbeg = $2;
128 $blen = $3;
129 $bori = $4;
130 $bmis = $5;
131 $bident = $6;
132 } elsif ($b !~ m/^M\sm\s/) {
133 print "Bnope $b\n";
134 }
135
136 $aident = substr(" $aident", -7);
137 $bident = substr(" $bident", -7);
138
139 if ($m =~ m/^(.*\]\s+\d+\s+\(.*\))\s+(\d+\s+\(.*\))/) {
140 print O "$1 $aident $2 $bident\n";
141 } else {
142 print "Mnope $m\n";
143 exit(1);
144 }
145 }
146 }
147
148
149
150
151 # Reads the annotation output of overlap.C and writes two atac format
152 # files, one for map1, one for map2. There is a 1-1 map between
153 # lines, unmapped B35 (either because B35 was unmapped by both
154 # mappings, or unmapped by the other mapping) are noted in 'm'
155 # matches ("M m UID . B35LC:xxxxx beg len 1")
156 #
157 sub overlapToAtac {
158 my $infile = shift @_;
159 my $outfile1 = shift @_;
160 my $outfile2 = shift @_;
161
162 open(I, "< $infile") or die "Can't open '$infile' for reading.\n";
163 open(O1, "> $outfile1") or die "Can't open '$outfile1' for writing.\n";
164 open(O2, "> $outfile2") or die "Can't open '$outfile2' for writing.\n";
165
166 print STDERR "Converting $infile -> $outfile1 and $outfile2\n";
167
168 print O1 "/assemblyFile1=MERYL/B35LC.fasta\n";
169 print O1 "/assemblyFile2=MERYL/HUREF2.fasta\n";
170 print O1 "/assemblyId1=B35LC\n";
171 print O1 "/assemblyId2=HUREF2\n";
172
173 print O2 "/assemblyFile1=MERYL/B35LC.fasta\n";
174 print O2 "/assemblyFile2=MERYL/HUREF2.fasta\n";
175 print O2 "/assemblyId1=B35LC\n";
176 print O2 "/assemblyId2=HUREF2\n";
177
178 my $id = 0;
179
180 while (<I>) {
181 if (m/^.\s+(\d+):(\d+)-(\d+)\[\s*\d+\]\s(\d+)\s\(\s*(\d+):\s*(\d+)-\s*(\d+)\)\s(\d+)\s\(\s*(\d+):\s*(\d+)-\s*(\d+)\)\s*$/) {
182 my $id1 = $1;
183 my $b1 = $2;
184 my $e1 = $3;
185 my $l1 = $e1 - $b1;
186
187 my $mid2a = $4;
188 my $id2a = $5;
189 my $b2a = $6;
190 my $e2a = $7;
191 my $l2a = $e2a - $b2a;
192 my $oria = 1;
193
194 my $mid2b = $8;
195 my $id2b = $9;
196 my $b2b = $10;
197 my $e2b = $11;
198 my $l2b = $e2b - $b2b;
199 my $orib = 1;
200
201 $b1 =~ s/^0+//;
202 $e1 =~ s/^0+//;
203
204 $b1 = 0 if ($b1 == 0); # fix for blowing away all the zeros
205
206 if ($e2a < $b2a) {
207 ($b2a, $e2a) = ($e2a, $b2a);
208 $l2a = $e2a - $b2a;
209 $oria = -1
210 }
211 if ($e2b < $b2b) {
212 ($b2b, $e2b) = ($e2b, $b2b);
213 $l2b = $e2b - $b2b;
214 $orib = -1
215 }
216
217 $mid2a =~ s/^0+//;
218 $mid2b =~ s/^0+//;
219
220 if ($e2a > 0) {
221 print O1 "M u $id . B35LC:$id1 $b1 $l1 1 HUREF:$id2a $b2a $l2a $oria\n";
222 } else {
223 print O1 "M m $id . B35LC:$id1 $b1 $l1 1\n";
224 }
225
226 if ($e2b > 0) {
227 print O2 "M u $id . B35LC:$id1 $b1 $l1 1 HUREF:$id2b $b2b $l2b $orib\n";
228 } else {
229 print O2 "M m $id . B35LC:$id1 $b1 $l1 1\n";
230 }
231
232 $id++;
233 } else {
234 #print "Nope.\n";
235 #exit(1);
236 }
237 }
238
239 close(O1);
240 close(O2);
241 close(I);
242 }