Debian Codegraph @

atac-driver/makeplot.pl

raw
1 #!/usr/bin/env perl
2 #
3 # This file is part of A2Amapper.
4 # Copyright (c) 2008-2009 J. Craig Venter Institute
5 # Author: Brian Walenz
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received (LICENSE.txt) a copy of the GNU General Public
18 # License along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
21 #
22 # The reference MUST by the first sequence, and it MUST be a single sequence.
23 #
24
25 use strict;
26 use FindBin;
27
28 my $mm = shift @ARGV;
29 my $in = shift @ARGV;
30 my $ot = shift @ARGV;
31
32 if (($mm ne "u") && ($mm ne "r")) {
33 die "First arg must be 'u' (ungapped matches) or 'r' (runs).\n";
34 }
35
36 if (!defined($ot) && ($in =~ m/(.*).atac/)) {
37 $ot = $1;
38 }
39 if ($ot =~ m/^(.*).png/) {
40 $ot = $1;
41 }
42 if ($ot =~ m/^(.*).ps/) {
43 $ot = $1;
44 }
45
46 die if (!defined($in));
47 die if (!defined($ot));
48
49 my $version = `gnuplot -V`;
50
51 if ($version =~ m/gnuplot\s+(\d+\.\d+)\s+/) {
52 $version = $1;
53 } else {
54 chomp $version;
55 print STDERR "WARNING: Unknown gnuplot version '$version'\n";
56 $version = 0;
57 }
58
59 if ($version < 4.2) {
60 print STDERR "gnuplot version 4.2 is needed for plots.\n";
61 exit(0);
62 }
63
64 open(FD, "> $ot.fdat");
65 open(RD, "> $ot.rdat");
66 open(GP, "> $ot.gp");
67
68 print GP "set size 1,1\n";
69 print GP "set grid\n";
70 print GP "unset key\n";
71 print GP "set border 10\n";
72 print GP "set tics scale 0\n";
73 print GP "set xlabel \"REF\"\n";
74 print GP "set ylabel \"ASM\"\n";
75 print GP "set title \"$ot\"\n";
76 print GP "set format \"%.0f\"\n";
77 #print GP "set mouse format \"%.0f\"\n";
78 #print GP "set mouse mouseformat \"[%.0f, %.0f]\"\n";
79 #print GP "set mouse clipboardformat \"[%.0f, %.0f]\"\n";
80 print GP "set style line 1 lt 1 lw 1 pt 6 ps 1\n";
81 print GP "set style line 2 lt 3 lw 1 pt 6 ps 1\n";
82 print GP "set style line 3 lt 2 lw 1 pt 6 ps 1\n";
83
84 # We need to know the length of the reference so we can cycle
85 # the coordinates
86 #
87 my $refLength = 0;
88
89 # And we need to know the lengths of the scaffolds in the assembly.
90 #
91 my $asmFile1;
92 my $asmId1;
93 my $asmFile2;
94 my $asmId2;
95
96 open(IN, "< $in") or die;
97 while (<IN>) {
98 if (m/assemblyId1=(.*)/) {
99 $asmId1 = $1;
100 }
101 if (m/assemblyFile1=(.*)/) {
102 $asmFile1 = $1;
103 }
104 if (m/assemblyFile2=(.*)/) {
105 $asmFile2 = $1;
106 }
107 if (m/assemblyId2=(.*)/) {
108 $asmId2 = $1;
109 }
110 }
111 close(IN);
112
113
114 # Figure out which scaffolds are reversed.
115
116 my %reversed;
117 my %goofed;
118
119 {
120 open(IN, "< $in") or die;
121 while (<IN>) {
122 my @v = split '\s+', $_;
123 if (($v[0] eq "M") && ($v[1] eq $mm)) {
124 $reversed{$v[8]} += $v[10] * $v[11]; # length * orientation
125
126 # Remember if there was a goofy inversion in this
127 # scaffold. First time through we remember the
128 # orientation of the first match, then later matches
129 # check it is the same, reseting to a token value (2) if
130 # they differ.
131
132 if (!defined($goofed{$v[8]})) {
133 $goofed{$v[8]} = $v[11];
134 } else {
135 if ($goofed{$v[8]} != $v[11]) {
136 $goofed{$v[8]} = 2;
137 }
138 }
139 }
140 }
141 close(IN);
142 foreach my $k (keys %reversed) {
143 if ($reversed{$k} < 0) {
144 $reversed{$k} = 1;
145 } else {
146 $reversed{$k} = 0;
147 }
148 if ($goofed{$k} == 2) {
149 $goofed{$k} = 1;
150 } else {
151 $goofed{$k} = 0;
152 }
153 }
154 }
155
156
157
158 # Find the reference length
159
160 my $refLength;
161
162 open(F, "< $asmFile1") or die "Failed to open genome reference '$asmFile1'\n";
163 $_ = <F>; # defline
164 while (<F>) {
165 s/^\s+//;
166 s/\s+$//;
167 $refLength += length ($_);
168 }
169 close(F);
170
171
172 # Find the assembly lengths -- ignore anything without a match.
173
174 my %asmLength;
175 my $asmLength;
176
177 open(F, "$FindBin::Bin/leaff -F $asmFile2 -i $asmId2 |");
178 while (<F>) {
179 my @v = split '\s+', $_;
180 if ($v[0] eq "G") {
181 $asmLength{$v[2]} = $v[3] if (exists($reversed{$v[2]}));
182 $asmLength += $v[3];
183 }
184 }
185 close(F);
186
187 print GP "set xrange [0:", $refLength+1, "]\n";
188 print GP "set yrange [0:", $asmLength+1, "]\n";
189
190
191 # Figure out where to split the reference - we shift both X and Y, so
192 # we arbitrarily pick any assembly sequence and anchor it at the
193 # origin. Well, not arbitrary. We pick the longest only so that
194 # we're pretty sure we didn't pick some crappy tiny contig.
195
196 my $refSplit = 0;
197
198 {
199 my $asmAnchorSequence;
200 my $minAsm = 999999999;
201
202 # Pick the longest sequence as the anchor -- skipping this block
203 # will instead pick the first thing in the file.
204 #
205 # Don't pick anything with reversed crap on the ends; this
206 # greatly screws up.
207 #
208 foreach my $k (keys %asmLength) {
209 if ($goofed{$k}) {
210 next;
211 }
212 if (!defined($asmAnchorSequence) || ($asmLength{$asmAnchorSequence} < $asmLength{$k})) {
213 $asmAnchorSequence = $k;
214 }
215 }
216
217 # Except when all scaffolds are goofed. *sigh*
218
219 if (!defined($asmAnchorSequence)) {
220 foreach my $k (keys %asmLength) {
221 if (!defined($asmAnchorSequence) || ($asmLength{$asmAnchorSequence} < $asmLength{$k})) {
222 $asmAnchorSequence = $k;
223 }
224 }
225 }
226
227 open(IN, "< $in") or die;
228 while (<IN>) {
229 my @v = split '\s+', $_;
230
231 if (($v[0] eq "M") && ($v[1] eq $mm)) {
232 if ($reversed{$v[8]}) {
233 $v[9] = $asmLength{$v[8]} - ($v[9] + $v[10]);
234 $v[11] *= -1;
235 }
236
237 if (!defined($asmAnchorSequence)) {
238 $asmAnchorSequence = $v[8];
239 }
240
241 if ($v[8] eq $asmAnchorSequence) {
242 if ($v[9] < $minAsm) {
243 $minAsm = $v[9];
244 $refSplit = $v[5];
245 }
246 }
247 }
248 }
249 close(IN);
250 }
251
252 # Figure out how to place the assembly sequences
253 #
254 # $refSplit controls where we shift the reference origin.
255 # %offsetRef controls where we place the assembly in the Y axis.
256 #
257 # We want to rotate the reference so the largest scaffold is placed
258 # at the origin.
259
260 my %offsetRef;
261
262 {
263 my %minY;
264 my %minYbackup;
265
266 open(IN, "< $in") or die;
267 while (<IN>) {
268 my @v = split '\s+', $_;
269 if (($v[0] eq "M") && ($v[1] eq $mm)) {
270 if ($reversed{$v[8]}) {
271 $v[9] = $asmLength{$v[8]} - ($v[9] + $v[10]);
272 $v[11] *= -1;
273 }
274
275 # Confusing. Rotate the reference coordinate, then
276 # remember the smallest for each scaffold.
277
278 $v[5] -= $refSplit;
279 $v[5] += $refLength if ($v[5] < 0);
280
281 my $d = $v[5];
282
283 # Ignore if this is a tiny crappy little thing. This
284 # allows us to place most of the real matches on the
285 # diagonal, showing obvious small chimers.
286
287 if ($v[10] >= 2000) {
288 if (!exists($minY{$v[8]}) || ($d < $minY{$v[8]})) {
289 $minY{$v[8]} = $d;
290 }
291 } else {
292 if (!exists($minY{$v[8]}) || ($d < $minYbackup{$v[8]})) {
293 $minYbackup{$v[8]} = $d;
294 }
295 }
296 }
297 }
298 close(IN);
299
300 # If we never found a large block, use the biggest we did find.
301
302 foreach my $k (keys %minYbackup) {
303 if (!exists($minY{$k})) {
304 $minY{$k} = $minYbackup{$k};
305 }
306 }
307
308 my @sortme;
309 my $lengthsum = 0;
310 foreach my $k (keys %minY) {
311 push @sortme, "$minY{$k}\0$k";
312 }
313 @sortme = sort { $a <=> $b } @sortme;
314 foreach my $v (@sortme) {
315 my ($p, $k) = split '\0', $v;
316 $offsetRef{$k} = $lengthsum;
317 $lengthsum += $asmLength{$k};
318 }
319 }
320
321 print GP "set ytics ( \\\n";
322
323 {
324 # Ugh, gross.
325 my @keys = keys %offsetRef;
326 while (scalar(@keys)) {
327 my $k = shift @keys;
328
329 if (scalar(@keys) > 0) {
330 print GP " \"$k\" $offsetRef{$k},\\\n";
331 } else {
332 print GP " \"$k\" $offsetRef{$k}\\\n";
333 }
334 }
335 }
336 print GP ")\n";
337
338 print GP "set xtics ( \\\n";
339 #for (my $p=500000; $p<$refLength; $p += 500000) {
340 # my $i = $p - $refSplit;
341 # if ($i < 0) {
342 # $i += $refLength;
343 # }
344 # print GP " \"$p\" $i,\\\n";
345 #}
346 print GP " \"origin\" ", $refLength - $refSplit, "\\\n";
347 print GP ")\n";
348
349
350
351
352 my $hasFdat = 0;
353 my $hasRdat = 0;
354
355 open(IN, "< $in") or die;
356 while (<IN>) {
357 my @v = split '\s+', $_;
358
359 if (($v[0] eq "M") && ($v[1] eq $mm)) {
360 if ($reversed{$v[8]}) {
361 $v[9] = $asmLength{$v[8]} - ($v[9] + $v[10]);
362 $v[11] *= -1;
363 }
364
365 my $abeg = $v[5];
366 my $aend = $v[5] + $v[6];
367 my $bbeg = $v[9];
368 my $bend = $v[9] + $v[10];
369
370 $abeg -= $refSplit;
371 $aend -= $refSplit;
372
373 if (($abeg < 0) || ($aend < 0)) {
374 $abeg += $refLength;
375 $aend += $refLength;
376 }
377
378 $bbeg += $offsetRef{$v[8]};
379 $bend += $offsetRef{$v[8]};
380
381 if ($v[11] == 1) {
382 $hasFdat++;
383 print FD "$abeg $bbeg\n";
384 print FD "$aend $bend\n";
385 print FD "\n\n";
386 } else {
387 $hasRdat++;
388 print RD "$abeg $bend\n";
389 print RD "$aend $bbeg\n";
390 print RD "\n\n";
391 }
392 }
393 }
394 close(IN);
395
396 close(FD);
397 close(RD);
398
399 print GP "set terminal png tiny size 800,800\n";
400 print GP "set output \"$ot.png\"\n";
401
402 if ($hasFdat && $hasRdat) {
403 print GP "plot \\\n";
404 print GP " \"$ot.fdat\" w lp ls 1, \\\n";
405 print GP " \"$ot.rdat\" w lp ls 2\n";
406 #print GP "pause -1\n";
407 } elsif ($hasFdat) {
408 print GP "plot \\\n";
409 print GP " \"$ot.fdat\" w lp ls 1\n";
410 #print GP "pause -1\n";
411 } elsif ($hasRdat) {
412 print GP "plot \\\n";
413 print GP " \"$ot.rdat\" w lp ls 2\n";
414 #print GP "pause -1\n";
415 } else {
416 # No matches??
417 #die;
418 }
419
420 print GP "set terminal postscript color\n";
421 print GP "set output \"$ot.ps\"\n";
422 print GP "replot\n";
423
424 close(GP);
425
426 system("gnuplot $ot.gp");
427
428 #unlink "$ot.fdat";
429 #unlink "$ot.rdat";
430 #unlink "$ot.gp";