Debian Codegraph @

atac-driver/atac.pl

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 #!/usr/bin/env perl
2 #
3 # This file is part of A2Amapper.
4 # Copyright (c) 2005-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 use strict;
22 use FindBin;
23
24 my $id1 = undef;
25 my $seq1 = undef;
26 my $id2 = undef;
27 my $seq2 = undef;
28
29 my $ATACdir = undef;
30 my $GENOMEdir = "default"; # Location of genome assemblies
31 my $MERYLdir = "default"; # Location of genome mercount databases
32
33 my $BINdir = "$FindBin::Bin";
34 my $LIBdir = "$FindBin::Bin/../lib";
35
36 my $mersize = 20; # the mer size
37 my $minfill = 20; # the mimimum fill for a reported match.
38 my $merlimit = 1; # unique mers only
39 my $maxgap = 0; # the maximum substitution gap
40
41 # annotates the resulting atac file with parameters
42 # for cross species, also sets match extender options
43 my $crossSpecies = 0;
44
45 my $matchExtenderOpts = "";
46
47 my $filtername = "$LIBdir/filter-heavychains.so";
48 my $filteropts = "-S 100 -J 100000";
49
50 my $numSegments = 1; # More than one not supported in seatac; search for -use too
51 my $numThreads = 4;
52
53 my $merylThreads = 2;
54
55 my $merylOnly = 0;
56
57 # Check that we have everything we need to run
58 #
59 my $leaff = "$BINdir/leaff";
60 my $meryl = "$BINdir/meryl";
61 my $existDB = "$BINdir/existDB";
62 my $seatac = "$BINdir/seatac";
63 my $chainer = "$BINdir/AtacDriver.py";
64 my $correctgaps = "$BINdir/correctGaps";
65 my $statsgenerator = "$BINdir/statsGenerator";
66 my $makeplot = "perl $BINdir/makeplot.pl";
67
68 die "Can't run $leaff\n" if (! -x $leaff);
69 die "Can't run $meryl\n" if (! -x $meryl);
70 die "Can't run $existDB\n" if (! -x $existDB);
71 die "Can't run $seatac\n" if (! -x $seatac);
72 die "Can't find $chainer\n" if (! -e $chainer);
73 die "Can't find $filtername\n" if (! -e $filtername);
74 die "Can't run $correctgaps\n" if (! -x $correctgaps);
75 die "Can't run $statsgenerator\n" if (! -x $statsgenerator);
76
77
78 # Main begins here!
79 #
80 # We used to use a long descriptive name for the matches, which
81 # encoded some parameters, but since we never really change those
82 # parameters, we stop encoding.
83 #
84 # It used to be "${id1}vs${id2}.k$mersize.u$merlimit.f$minfill.g$maxgap"
85
86 parseArgs();
87 findSources();
88
89 my $matches = "${id1}vs${id2}";
90
91 if (! -e "$ATACdir/$matches.atac") {
92 my $mercount1 = countMers($id1, $mersize, $merlimit);
93 my $mercount2 = countMers($id2, $mersize, $merlimit);
94
95 buildMask($mercount1, $mercount2);
96
97 my @segmentIDs = findHits();
98
99 extendMatches(@segmentIDs);
100
101 makeChains();
102 closeGaps();
103 makeClumps();
104 generateStatistics();
105 rewriteUIDs("$ATACdir/$matches.atac");
106
107 print STDERR "\n";
108 print STDERR "Finished! Output is:\n";
109 print STDERR " matches and runs -- $ATACdir/$matches.atac\n";
110 print STDERR " clumps -- $ATACdir/$matches.*clump*.atac\n";
111 }
112
113 # Subroutines below!
114
115
116 sub usage {
117 print STDERR "usage: $0 -dir AvsB -id1 A -seq1 A.fasta -id2 B -seq2 B.fasta -meryldir M [opts\n";
118 print STDERR "\n";
119 print STDERR "ATAC will compute and place results in the run-directory.\n";
120 print STDERR "The meryl directory is used to store assembly-specific\n";
121 print STDERR "intermediate files. Internally, atac uses an ID to refer to\n";
122 print STDERR "a assembly; if the same ID/seq pair is used across multiple\n";
123 print STDERR "runs, the assembly-specific intermediate files can be reused.\n";
124 print STDERR "\n";
125 print STDERR "A * indicates a required argument.\n";
126 print STDERR "\n";
127 print STDERR "* -dir run-directory -- path to the RESULTS directory\n";
128 print STDERR "* -meryldir path -- path to the MERYL directory\n";
129 print STDERR " -genomedir path -- path to the GENOMES directory\n";
130 print STDERR "\n";
131 print STDERR "* -id1 id1 -- ID of the A assembly\n";
132 print STDERR "* -seq1 seq1.fasta -- sequence of the A assembly\n";
133 print STDERR "* -id2 id2 -- ID of the B assembly\n";
134 print STDERR "* -seq2 seq2.fasta -- sequence of the B assembly\n";
135 print STDERR "\n";
136 print STDERR "NOTE: A hash table will be built for id1. For space and\n";
137 print STDERR " performance, this should usually be the smaller assembly.\n";
138 print STDERR "\n";
139 print STDERR "NOTE: It is generally assumed that id1 is the REFERENCE assembly.\n";
140 print STDERR "\n";
141 print STDERR " -numsegments s -- number of segments to do the search in\n";
142 print STDERR " (doubling segments halves memory usage)\n";
143 print STDERR " -numsegments NOT SUPPORTED; DO NOT USE\n";
144 print STDERR "\n";
145 print STDERR " -numthreads t -- number of threads to use per search\n";
146 print STDERR " (slight increase in memory usage)\n";
147 print STDERR "\n";
148 print STDERR " -merylonly -- only run the meryl components\n";
149 print STDERR " -merylthreads t -- number of threads to use for meryl\n";
150 print STDERR "\n";
151 print STDERR " -samespecies -- use magic values for same species\n";
152 print STDERR " -crossspecies -- use guesses for different species\n";
153 print STDERR "\n";
154 print STDERR " -segmentid x -- only run segment with id x\n";
155 print STDERR " (don't use)\n";
156 exit(1);
157 }
158
159 sub parseArgs {
160
161 while (scalar(@ARGV) > 0) {
162 my $arg = shift @ARGV;
163
164 if ($arg eq "-dir") {
165 $ATACdir = shift @ARGV;
166 } elsif ($arg eq "-id1") {
167 $id1 = shift @ARGV;
168 } elsif ($arg eq "-seq1") {
169 $seq1 = shift @ARGV;
170 } elsif ($arg eq "-id2") {
171 $id2 = shift @ARGV;
172 } elsif ($arg eq "-seq2") {
173 $seq2 = shift @ARGV;
174 } elsif ($arg eq "-genomedir") {
175 $GENOMEdir = shift @ARGV;
176 } elsif ($arg eq "-meryldir") {
177 $MERYLdir = shift @ARGV;
178 } elsif ($arg eq "-numsegments") {
179 $numSegments = shift @ARGV;
180 } elsif ($arg eq "-numthreads") {
181 $numThreads = shift @ARGV;
182 } elsif ($arg eq "-merylonly") {
183 $merylOnly = 1;
184 } elsif ($arg eq "-merylthreads") {
185 $merylThreads = shift @ARGV;
186 } elsif ($arg eq "-samespecies") {
187 $mersize = 20; # the mer size
188 $merlimit = 1; # unique mers only
189 $minfill = 20; # the mimimum fill for a reported match.
190 $maxgap = 0; # the maximum substitution gap
191 } elsif ($arg eq "-samespecies9") {
192 $mersize = 20; # the mer size
193 $merlimit = 9; # mostly unique mers only
194 $minfill = 20; # the mimimum fill for a reported match.
195 $maxgap = 0; # the maximum substitution gap
196 } elsif ($arg eq "-crossspecies20") {
197 $mersize = 20; # the mer size
198 $merlimit = 9; # mostly unique mers only
199 $minfill = 20; # the mimimum fill for a reported match.
200 $maxgap = 0; # the maximum substitution gap
201 $crossSpecies = 1; # extra parameters in the atac file
202 } elsif ($arg eq "-crossspecies") {
203 $mersize = 18; # the mer size
204 $merlimit = 9; # mostly unique mers only
205 $minfill = 18; # the mimimum fill for a reported match.
206 $maxgap = 0; # the maximum substitution gap
207 $crossSpecies = 1; # extra parameters in the atac file
208 } elsif($arg eq "-filtername") {
209 $filtername = shift @ARGV;
210 } elsif($arg eq "-filteropts") {
211 $filteropts = shift @ARGV;
212 } elsif ($arg eq "-mersize") {
213 $mersize = shift @ARGV;
214 $minfill = $mersize;
215 } elsif ($arg eq "-merlimit") {
216 $merlimit = shift @ARGV;
217 } elsif ($arg eq "-justtestingifitworks") {
218 exit(0);
219 } else {
220 die "unknown option $arg\n";
221 }
222 }
223
224 # Search for -use too.
225 die "-numsegments NOT SUPPORTED.\n" if ($numSegments != 1);
226
227 if (!defined($id1) ||
228 !defined($id2)) {
229 usage();
230 }
231
232 my $pwd = `pwd`;
233 $pwd =~ s/^\s+//;
234 $pwd =~ s/\s+$//;
235
236 $GENOMEdir = "$pwd/$GENOMEdir" if ($GENOMEdir !~ m!^/!);
237 $MERYLdir = "$pwd/$MERYLdir" if ($MERYLdir !~ m!^/!);
238 $ATACdir = "$pwd/$ATACdir" if ($ATACdir !~ m!^/!);
239
240 die "Unset GENOMEdir?'\n" if (! defined($GENOMEdir));
241 die "Unset MERYLdir?'\n" if (! defined($MERYLdir));
242 die "Unset ATACdir?'\n" if (! defined($ATACdir));
243
244 if (!defined($seq1) || (!defined($seq2))) {
245 die "Can't find the GENOMEdir '$GENOMEdir'\n" if (! -d $GENOMEdir);
246 }
247 if (defined($seq1)) {
248 $seq1 = "$pwd/$seq1" if ($seq1 !~ m!^/!);
249 }
250 if (defined($seq2)) {
251 $seq2 = "$pwd/$seq2" if ($seq2 !~ m!^/!);
252 }
253
254 system("mkdir $ATACdir") if (! -d "$ATACdir");
255 system("mkdir $ATACdir/work") if (! -d "$ATACdir/work");
256 system("mkdir $ATACdir/stats") if (! -d "$ATACdir/stats");
257 system("mkdir $MERYLdir") if (! -d "$MERYLdir");
258 }
259
260
261 # Read the nickname file, set up symlinks to the data sources
262 #
263 sub findSources {
264 my %GENOMEaliases;
265
266 # Read all the *.atai files in the genome directory, save only
267 # those nicknames that have actual files associated with them.
268 # This lets us have multiple collections of assemblies, and also
269 # lets us move the directory around (e.g., for running on a
270 # laptop).
271 #
272 if (-d $GENOMEdir) {
273 # What? No GENOMEdir? The main already checked that we know both
274 # sequence files. Plus, we'd just fail below.
275
276 open(A, "ls $GENOMEdir |");
277 while (<A>) {
278 chomp;
279 if (m/\.atai$/) {
280 my $ataifile = "$GENOMEdir/$_";
281 open(F, "< $ataifile") or die "Can't open '$ataifile'\n";
282 while (<F>) {
283 chomp;
284
285 if (m/^!\s*format\s+atac\s+(.*)$/) {
286 print STDERR "Found format $1\n";
287 } elsif (m/^S\s+(\S+)\s+(\S+)$/) {
288 if (-e $2) {
289 $GENOMEaliases{$1} = $2;
290 } else {
291 print STDERR "WARNING: File '$2' not found for alias '$1'.\n";
292 }
293 } else {
294 #die "Error parsing genome description.\n '$_'\n";
295 }
296 }
297 close(F);
298 }
299 }
300 }
301 close(A);
302
303 # If the user gave both an id and a sequence, make sure that
304 # the id is distinct.
305 #
306 die "No id1 supplied!\n" if (!defined($id1));
307 die "No id2 supplied!\n" if (!defined($id2));
308
309 die "id1 = '$id1' is already used by sequence '$GENOMEaliases{$id1}'\n" if (defined($GENOMEaliases{$id1}) && defined($seq1));
310 die "id2 = '$id2' is already used by sequence '$GENOMEaliases{$id2}'\n" if (defined($GENOMEaliases{$id2}) && defined($seq2));
311
312 $seq1 = $GENOMEaliases{$id1} if (!defined($seq1));
313 $seq2 = $GENOMEaliases{$id2} if (!defined($seq2));
314
315 die "Unknown alias $id1.\n" if (!defined($seq1));
316 die "Unknown alias $id2.\n" if (!defined($seq2));
317
318 die "File '$seq1' doesn't exist for alias $id1.\n" if (! -e $seq1);
319 die "File '$seq2' doesn't exist for alias $id2.\n" if (! -e $seq2);
320
321 #system("ln -s $seq1 $MERYLdir/$id1.fasta") if (! -e "$MERYLdir/$id1.fasta");
322 #system("ln -s $seq2 $MERYLdir/$id2.fasta") if (! -e "$MERYLdir/$id2.fasta");
323
324 #system("ln -s ${seq1}idx $MERYLdir/$id1.fastaidx") if (! -e "$MERYLdir/$id1.fastaidx") && (-e "${seq1}idx");
325 #system("ln -s ${seq2}idx $MERYLdir/$id2.fastaidx") if (! -e "$MERYLdir/$id2.fastaidx") && (-e "${seq2}idx");
326
327 removeNewLines($seq1, "$MERYLdir/$id1.fasta");
328 removeNewLines($seq2, "$MERYLdir/$id2.fasta");
329 }
330
331
332
333 sub removeNewLines ($$) {
334 my $in = shift @_;
335 my $ot = shift @_;
336
337 return if (-e "$ot");
338
339 print STDERR "Rewriting '$in' to '$ot', removing newlines.\n";
340
341 open(F, "< $in") or die "Failed to open '$in' for reading: $!\n";
342 open(O, "> $ot") or die "Failed to open '$ot' for writing: $!\n";
343
344 $_ = <F>; print O $_;
345
346 while (<F>) {
347 if (m/^>/) {
348 print O "\n";
349 } else {
350 chomp;
351 }
352
353 print O $_;
354 }
355 close(O);
356 close(F);
357 }
358
359
360
361 # Check that meryl is finished for each of the inputs
362 #
363 sub countMers {
364 my ($id, $mersize, $merlimit) = @_;
365
366 # Using "-H 32" is needed if the two sequences aren't about the
367 # same order of magnitude in size. This value is appropriate for
368 # sequences that are genome size.
369
370 if (! -e "$MERYLdir/$id.ms$mersize.mcdat") {
371 my $cmd;
372 $cmd = "$meryl -B -C ";
373 $cmd .= "-threads $merylThreads ";
374 $cmd .= "-m $mersize ";
375 $cmd .= "-s $MERYLdir/$id.fasta ";
376 $cmd .= "-o $MERYLdir/$id.ms$mersize ";
377 #die "why rebuild $MERYLdir/$id.ms$mersize.mcdat\n";
378 if (runCommand($cmd)) {
379 unlink "$MERYLdir/$id.ms$mersize.mcidx";
380 unlink "$MERYLdir/$id.ms$mersize.mcdat";
381 die "Failed to count mers in $id\n";
382 }
383 }
384
385 if (! -e "$MERYLdir/$id.ms$mersize.le$merlimit.mcdat") {
386 my $cmd;
387 $cmd = "$meryl -v ";
388 $cmd .= "-M lessthanorequal $merlimit ";
389 $cmd .= "-s $MERYLdir/$id.ms$mersize ";
390 $cmd .= "-o $MERYLdir/$id.ms$mersize.le$merlimit ";
391 #die "why rebuild $MERYLdir/$id.ms$mersize.le$merlimit.mcdat\n";
392 if (runCommand($cmd)) {
393 unlink "$MERYLdir/$id.ms$mersize.le$merlimit.mcidx";
394 unlink "$MERYLdir/$id.ms$mersize.le$merlimit.mcdat";
395 die "Failed to count mers lessthanorequal $merlimit in $id\n";
396 }
397 }
398
399 return "$id.ms$mersize.le$merlimit";
400 }
401
402
403 # Return the number of mers in a meryl file.
404 #
405 sub numberOfMers ($) {
406 my $mers = 0;
407 open(F, "$meryl -Dc -s $_[0] |");
408 while (<F>) {
409 $mers = $1 if (m/Found\s(\d+)\smers/);
410 }
411 close(F);
412 print STDERR "$_[0] has $mers mers.\n";
413 return($mers);
414 }
415
416
417
418
419 sub buildMask ($$) {
420 my $mercount1 = shift @_;
421 my $mercount2 = shift @_;
422
423 return if (-e "$ATACdir/work/$matches.mask.done");
424
425 my $minFile="min.$mercount1.$mercount2";
426
427 # $mercount1 and $mercount2 are the mers we want to use for
428 # searching. Obviously, only in-common mers can be found, we
429 # make a file of those mers here.
430
431 if (! -e "$ATACdir/work/$minFile.mcdat") {
432 print STDERR "Finding the min count between $mercount1 and $mercount2.\n";
433
434 my $cmd;
435 $cmd = "$meryl ";
436 $cmd .= "-M min ";
437 $cmd .= "-s $MERYLdir/$mercount1 ";
438 $cmd .= "-s $MERYLdir/$mercount2 ";
439 $cmd .= "-o $ATACdir/work/$minFile ";
440
441 if (runCommand($cmd)) {
442 unlink "$ATACdir/work/$minFile.mcidx";
443 unlink "$ATACdir/work/$minFile.mcdat";
444 die "Failed to find the min count between $mercount1 and $mercount2\n";
445 }
446 }
447
448 die "Failed to make the mask?\n" if (! -e "$ATACdir/work/$minFile.mcdat");
449
450 # From that list of in-common mers (in-common and below some
451 # count) we want to make a list of the mers that can be used in
452 # the search table. We can either make a positive (use these
453 # mers) or negative (don't use these mers) list, we just want to
454 # pick the smaller of the two.
455 #
456 #
457 # The positive 'include' list is just the 'min' mers found above.
458 #
459 # The negative 'exclude' list is the min mers, removed from the mers in id1.
460 #
461 my $includeSize = (-s "$ATACdir/work/$minFile.mcdat");
462 my $excludeSize = (-s "$MERYLdir/$id1.ms$mersize.mcdat") - (-s "$ATACdir/work/$minFile.mcdat");
463
464 print STDERR "includeSize is proportional to $includeSize.\n";
465 print STDERR "excludeSize is proportional to $excludeSize.\n";
466
467 # But this sometimes breaks (if the mcidx files are different sizes), so we now
468 # pay the cost of actually counting the number of mers.
469 #
470 $includeSize = numberOfMers("$ATACdir/work/$minFile");
471 $excludeSize = numberOfMers("$MERYLdir/$id1.ms$mersize") - $includeSize;
472
473 print STDERR "includeSize is exactly $includeSize mers.\n";
474 print STDERR "excludeSize is exactly $excludeSize mers.\n";
475
476 if ($includeSize < $excludeSize) {
477 rename "$ATACdir/work/$minFile.mcidx", "$ATACdir/work/$matches.include.mcidx";
478 rename "$ATACdir/work/$minFile.mcdat", "$ATACdir/work/$matches.include.mcdat";
479 } else {
480 if (! -e "$ATACdir/work/$matches.exclude.mcdat") {
481 print STDERR "Finding 'exclude' mers!\n";
482
483 # Our use of xor here is really just a subtraction. We
484 # want to report those mers that are only in the first
485 # file, not in the second. All mers in the second file
486 # should be in the first file, by construction.
487
488 my $cmd;
489 $cmd = "$meryl ";
490 $cmd .= "-M xor ";
491 $cmd .= "-s $MERYLdir/$id1.ms$mersize ";
492 $cmd .= "-s $ATACdir/work/$minFile ";
493 $cmd .= "-o $ATACdir/work/$matches.exclude ";
494
495 if (runCommand($cmd)) {
496 unlink "$ATACdir/work/$matches.exclude.mcidx";
497 unlink "$ATACdir/work/$matches.exclude.mcdat";
498 die "Failed to make exclude mers!\n";
499 }
500 }
501
502 if (-e "$ATACdir/work/$matches.exclude.mcdat") {
503 unlink "$ATACdir/work/$minFile.mcdat";
504 unlink "$ATACdir/work/$minFile.mcidx";
505 } else {
506 die "Failed to find exclude mers?\n";
507 }
508 }
509
510 # Success!
511 #
512 open(F, "> $ATACdir/work/$matches.mask.done");
513 close(F);
514
515 exit(0) if ($merylOnly == 1);
516 }
517
518
519
520 sub findHits {
521 my $segmentID = "000";
522 my @segmentIDs;
523
524 open(F, "$leaff --partitionmap $numSegments $MERYLdir/$id1.fasta |");
525 $numSegments = <F>;
526 while(<F>) {
527 my $segments = "";
528 my @pieces = split '\s+', $_;
529 my $memory = shift @pieces;
530
531 foreach my $piece (@pieces) {
532 if ($piece =~ m/(\d+)\(\d+\)/) {
533 $segments .= "$1\n";
534 } else {
535 die "Error parsing segment: $piece\n";
536 }
537 }
538
539 open(S, "> $ATACdir/work/$matches-segment-$segmentID");
540 print S $segments;
541 close(S);
542
543 push @segmentIDs, $segmentID;
544
545 $segmentID++;
546 }
547 close(F);
548
549 die "No segments found?\n" if (scalar(@segmentIDs) == 0);
550
551 #
552 # Now, for each segment that hasn't run, run it.
553 #
554
555 foreach my $segmentID (@segmentIDs) {
556
557 # For large runs, while developing, we found it very useful
558 # to build the tables first, save them to disk, then do the
559 # compute. This is also mandatory if one wants to segment
560 # the other assembly to reduce the time each piece runs.
561 #
562 # However, doing so adds a lot of complexity to this script,
563 # and isn't terribly useful anymore.
564 #
565
566 my $cmd;
567 $cmd = "$seatac ";
568 $cmd .= "-verbose ";
569 $cmd .= "-mersize $mersize ";
570 $cmd .= "-minlength $minfill ";
571 $cmd .= "-maxgap $maxgap ";
572 $cmd .= "-numthreads $numThreads ";
573 $cmd .= "-table $MERYLdir/$id1.fasta ";
574 $cmd .= "-stream $MERYLdir/$id2.fasta ";
575 $cmd .= "-only $ATACdir/work/$matches.include " if (-e "$ATACdir/work/$matches.include.mcdat");
576 $cmd .= "-mask $ATACdir/work/$matches.exclude " if (-e "$ATACdir/work/$matches.exclude.mcdat");
577 # Until we fix the -use support in seatac.
578 #$cmd .= "-use $ATACdir/work/$matches-segment-$segmentID ";
579 $cmd .= "-output $ATACdir/work/$matches-segment-$segmentID.matches ";
580 $cmd .= "-filtername $filtername " if (defined($filtername));
581 $cmd .= "-filteropts \"-1 $id1 -2 $id2 $filteropts\" ";
582 $cmd .= "> $ATACdir/work/$matches-$segmentID.out 2>&1";
583
584 if (! -e "$ATACdir/work/$matches-segment-$segmentID.matches") {
585 if (runCommand($cmd)) {
586 unlink "$ATACdir/work/$matches-segment-$segmentID.matches.crash";
587 rename "$ATACdir/work/$matches-segment-$segmentID.matches", "$ATACdir/work/$matches-segment-$segmentID.matches.crash";
588 die "Failed to run $matches-$segmentID\n";
589 }
590 }
591 }
592
593 return(@segmentIDs);
594 }
595
596
597
598 sub extendMatches (@) {
599 my @segmentIDs = @_;
600
601 return if (-e "$ATACdir/work/$matches.matches.extended");
602
603 # Check that each search finished.
604 #
605 foreach my $segmentID (@segmentIDs) {
606 if (! -e "$ATACdir/work/$matches-segment-$segmentID.matches") {
607 die "$ATACdir/work/$matches-segment-$segmentID.matches failed to complete.\n";
608 }
609 }
610
611 if ($crossSpecies) {
612 $matchExtenderOpts = "-e 4 -b 5 -s 5 -i 0.70 -p 100 -d 25";
613 }
614
615 # Build the header file.
616 #
617 open(ATACFILE, "> $ATACdir/work/$matches.header") or die;
618 print ATACFILE "!format atac 1.0\n";
619 print ATACFILE "#\n";
620 print ATACFILE "# Legend:\n";
621 print ATACFILE "#\n";
622 print ATACFILE "# Field 0: the row class\n";
623 print ATACFILE "# Field 1: the match type u=ungapped, x=exact, ....\n";
624 print ATACFILE "# Field 2: the match instance index\n";
625 print ATACFILE "# Field 3: the parent index\n";
626 print ATACFILE "# Field 4: the FASTA sequence id in the first assembly\n";
627 print ATACFILE "# Field 5: the offset from the start of the sequence for the match\n";
628 print ATACFILE "# Field 6: the length of the match in the first assembly\n";
629 print ATACFILE "# Field 7: the orientation of the match sequence in the first assembly.\n";
630 print ATACFILE "# Field 8: the FASTA sequence id for the second assembly\n";
631 print ATACFILE "# Field 9: the offset from the start of the sequence for the match\n";
632 print ATACFILE "# Field 10: the length of the match in the second assembly\n";
633 print ATACFILE "# Field 11: the orientation of the match sequence in the second assembly.\n";
634 print ATACFILE "#\n";
635 print ATACFILE "/assemblyId1=$id1\n";
636 print ATACFILE "/assemblyId2=$id2\n";
637 print ATACFILE "/assemblyFile1=$MERYLdir/$id1.fasta\n";
638 print ATACFILE "/assemblyFile2=$MERYLdir/$id2.fasta\n";
639
640 # We used to trim off the fasta from the filename...why?
641 my $seq1trimmed = $seq1;
642 my $seq2trimmed = $seq2;
643 $seq1trimmed = $1 if ($seq1trimmed =~ m/(.*).fasta$/);
644 $seq2trimmed = $1 if ($seq2trimmed =~ m/(.*).fasta$/);
645
646 print ATACFILE "/rawMatchMerSize=$mersize\n";
647 print ATACFILE "/rawMatchMerMaxDegeneracy=$merlimit\n";
648 print ATACFILE "/rawMatchAllowedSubstutionBlockSize=$maxgap\n";
649 print ATACFILE "/rawMatchMinFillSize=$minfill\n";
650
651 print ATACFILE "/heavyChainsOn=1\n";
652 print ATACFILE "/heavyMaxJump=100000\n";
653 print ATACFILE "/heavyMinFill=100\n";
654
655 print ATACFILE "/matchExtenderOn=1\n";
656
657 print ATACFILE "/uniqueFilterOn=1\n";
658 print ATACFILE "/fillIntraRunGapsOn=1\n";
659
660 if ($crossSpecies){
661 # The non-default parameters for Mouse versus Rat.
662 print ATACFILE "/matchExtenderMinEndRunLen=4\n";
663 print ATACFILE "/matchExtenderMaxMMBlock=5\n";
664 print ATACFILE "/matchExtenderMinBlockSep=5\n";
665 print ATACFILE "/matchExtenderMinIdentity=0.7\n";
666 print ATACFILE "/matchExtenderMaxNbrSep=100\n";
667 print ATACFILE "/matchExtenderMaxNbrPathMM=25\n";
668 print ATACFILE "/globalMatchMinSize=20\n";
669 print ATACFILE "/fillIntraRunGapsErate=0.30\n";
670 }
671 close(ATACFILE);
672
673
674 # run matchExtender
675 #
676 my $cmd;
677 $cmd = "$BINdir/matchExtender $matchExtenderOpts ";
678 $cmd .= "$ATACdir/work/$matches.header ";
679 foreach my $segmentID (@segmentIDs) {
680 $cmd .= " $ATACdir/work/$matches-segment-$segmentID.matches";
681 }
682 $cmd .= " > $ATACdir/work/$matches.matches.extended";
683
684 if (runCommand($cmd)) {
685 rename "$ATACdir/work/$matches.matches.extended", "$ATACdir/work/$matches.matches.extended.FAILED";
686 die "Failed.\n";
687 }
688
689
690 # Copy all the matches to the matchExtender. We take the liberty
691 # of making new match uids for these, since seatac can't make
692 # unique ids if it is run in multiple passes.
693 #
694 if (0) {
695 my $uid = "000000000";
696 my $comma = $,; $, = " ";
697 my $slash = $\; $\ = "\n";
698 foreach my $segmentID (@segmentIDs) {
699 open(MATCHES, "< $ATACdir/work/$matches-segment-$segmentID.matches") or die "Failed to open '$ATACdir/work/$matches-segment-$segmentID.matches'\n";
700 while (<MATCHES>) {
701 if (m/^M/) {
702 my @v = split '\s+', $_;
703 $v[2] = "m$uid";
704 undef $v[12];
705 undef $v[13];
706 undef $v[14];
707 undef $v[15];
708 print ATACFILE @v;
709 $uid++;
710 }
711 }
712 close(MATCHES);
713 }
714
715 $, = $comma;
716 $\ = $slash;
717 }
718 }
719
720
721
722 sub makeChains {
723 return if (-e "$ATACdir/work/$matches.matches.extended.chained.atac");
724
725 if (!defined($ENV{"TMPDIR"})) {
726 print STDERR "WARNING: TMPDIR not set, defaulting to '$ATACdir'.\n";
727 $ENV{"TMPDIR"} = $ATACdir;
728 }
729
730 # Path to the python shared-objects (in lib) and the python scripts.
731 #
732 $ENV{'PYTHONPATH'} = "$LIBdir";
733
734 if (runCommand("python $chainer $ATACdir/work/$matches.matches.extended")) {
735 print STDERR "PYTHONPATH=$ENV{'PYTHONPATH'}\n";
736 die "Chainer failed.\n";
737 }
738
739 }
740
741
742
743 sub closeGaps {
744 return if (-e "$ATACdir/$matches.atac");
745
746 my $cmd;
747 $cmd = "$correctgaps ";
748 $cmd .= " -m $ATACdir/work/$matches.matches.extended.chained.atac ";
749 $cmd .= " -l $ATACdir/work/$matches.matches.extended.chained.gapsclosed.log";
750 $cmd .= " > $ATACdir/work/$matches.matches.extended.chained.gapsclosed.atac";
751
752 if (runCommand($cmd)) {
753 rename "$ATACdir/work/$matches.matches.extended.chained.gapsclosed.atac", "$ATACdir/work/$matches.matches.extended.chained.gapsclosed.FAILED";
754 die "Failed to close gaps!\n";
755 }
756
757 if (! -e "$ATACdir/work/$matches.atac") {
758 system("ln -s $ATACdir/work/$matches.matches.extended.chained.gapsclosed.atac $ATACdir/work/$matches.atac");
759 }
760 if (! -e "$ATACdir/$matches.atac") {
761 system("ln $ATACdir/work/$matches.matches.extended.chained.gapsclosed.atac $ATACdir/$matches.atac");
762 }
763 }
764
765
766
767 sub makeClumps {
768 my $cmd;
769 my $ref;
770 my $rid;
771 my $clumpCost = 5000;
772
773 $ref = 1;
774 $rid = $id1;
775 if (! -e "$ATACdir/$matches.ref=$rid.clumpCost=$clumpCost.atac") {
776 $cmd = "cd $ATACdir ";
777 $cmd .= "&& ";
778 $cmd .= "$BINdir/clumpMaker ";
779 $cmd .= " -c $clumpCost ";
780 $cmd .= " -$ref ";
781 $cmd .= " -f $ATACdir/$matches.atac ";
782 $cmd .= "> $ATACdir/$matches.ref=$rid.clumpCost=$clumpCost.atac";
783 if (runCommand($cmd)) {
784 rename "$ATACdir/$matches.ref=$rid.clumpCost=$clumpCost.atac", "$ATACdir/$matches.ref=$rid.clumpCost=$clumpCost.atac.FAILED";
785 die "Failed to make clumps!\n";
786 }
787 }
788 rewriteUIDs("$ATACdir/$matches.ref=$rid.clumpCost=$clumpCost.atac");
789
790 $ref = 2;
791 $rid = $id2;
792 if (! -e "$ATACdir/$matches.ref=$rid.clumpCost=$clumpCost.atac") {
793 $cmd = "cd $ATACdir ";
794 $cmd .= "&& ";
795 $cmd .= "$BINdir/clumpMaker ";
796 $cmd .= " -c $clumpCost ";
797 $cmd .= " -$ref ";
798 $cmd .= " -f $ATACdir/$matches.atac ";
799 $cmd .= "> $ATACdir/$matches.ref=$rid.clumpCost=$clumpCost.atac";
800 if (runCommand($cmd)) {
801 rename "$ATACdir/$matches.ref=$rid.clumpCost=$clumpCost.atac", "$ATACdir/$matches.ref=$rid.clumpCost=$clumpCost.atac";
802 die "Failed to make clumps!\n";
803 }
804 }
805 rewriteUIDs("$ATACdir/$matches.ref=$rid.clumpCost=$clumpCost.atac");
806 }
807
808
809
810 sub generateStatistics {
811 my $cmd;
812
813 if (! -e "$ATACdir/stats/$matches.stats") {
814 $cmd = "$statsgenerator ";
815 $cmd .= "-a $ATACdir/$matches.atac ";
816 $cmd .= "-p $ATACdir/stats/$matches ";
817 $cmd .= "-g A ";
818 $cmd .= "> $ATACdir/stats/$matches.stats";
819 if (runCommand($cmd)) {
820 rename "$ATACdir/stats/$matches.stats", "$ATACdir/stats/$matches.stats.FAILED";
821 die "Failed to ganerate statistics.\n";
822 }
823 }
824
825 if (! -e "$ATACdir/stats/$matches.matches.png") {
826 $cmd = "$makeplot u $ATACdir/$matches.atac $ATACdir/stats/$matches.matches.png";
827 if (runCommand($cmd)) {
828 unlink "$ATACdir/stats/$matches.matches.png";
829 unlink "$ATACdir/stats/$matches.matches.ps";
830 die "Failed to ganerate dot plots.\n";
831 }
832 }
833
834 if (! -e "$ATACdir/stats/$matches.runs.png") {
835 $cmd = "$makeplot r $ATACdir/$matches.atac $ATACdir/stats/$matches.runs.png";
836 if (runCommand($cmd)) {
837 unlink "$ATACdir/stats/$matches.runs.png";
838 unlink "$ATACdir/stats/$matches.runs.ps";
839 die "Failed to ganerate dot plots.\n";
840 }
841 }
842 }
843
844
845
846 # Reads an atac file with atac-format IDs, writes an atac file with
847 # UIDs (the first word in the defline).
848 sub rewriteUIDs ($) {
849 my $infile = shift @_;
850 my $otfile = "$infile.uids";
851
852 return if (-e "$infile.uids");
853
854 my $seqA;
855 my $tagA;
856 my %uidA;
857
858 my $seqB;
859 my $tagB;
860 my %uidB;
861
862 my $iid;
863
864 open(F, "< $infile") or die "Failed to open '$infile'\n";
865 while (!defined($seqA) || !defined($tagA) || !defined($seqB) || !defined($tagB)) {
866 $_ = <F>;
867 $seqA = $1 if (m/^\/assemblyFile1=(.*)$/);
868 $tagA = $1 if (m/^\/assemblyId1=(.*)$/);
869 $seqB = $1 if (m/^\/assemblyFile2=(.*)$/);
870 $tagB = $1 if (m/^\/assemblyId2=(.*)$/);
871 }
872 close(F);
873
874 if (!defined($seqA) || !defined($tagA) || !defined($seqB) || !defined($tagB)) {
875 die "Something fishy. Didn't find seqs or tags in '$infile'.\n";
876 }
877
878 $iid = 0;
879 open(F, "< $seqA") or die "Failed to open '$seqA'\n";
880 while (<F>) {
881 if (m/^>(\S+)\s*.*$/) {
882 #chomp;
883 #print STDERR "$tagA:$iid -> $_\n";
884 $uidA{"$tagA:$iid"} = $1;
885 $iid++;
886 }
887 }
888 close(F);
889
890 $iid = 0;
891 open(F, "< $seqB") or die "Failed to open '$seqA'\n";
892 while (<F>) {
893 if (m/^>(\S+)\s*.*$/) {
894 #chomp;
895 #print STDERR "$tagB:$iid -> $_\n";
896 $uidB{"$tagB:$iid"} = $1;
897 $iid++;
898 }
899 }
900 close(F);
901
902 open(F, "< $infile") or die;
903 open(G, "> $otfile") or die;
904 while (<F>) {
905 chomp $_;
906
907 my @v = split '\s+', $_;
908
909 if (m/^M/) {
910 die "Didn't find uidA for $v[4]\n" if (!defined($uidA{$v[4]}));
911 die "Didn't find uidB for $v[8]\n" if (!defined($uidB{$v[8]}));
912
913 $v[4] = $uidA{$v[4]};
914 $v[8] = $uidB{$v[8]};
915
916 $_ = join ' ', @v;
917 }
918
919 print G "$_\n";
920 }
921 close(G);
922 close(F);
923 }
924
925
926
927
928
929
930
931
932 # Utility to run a command and check the exit status. We used to try
933 # to decode the exit status...sigh.
934 #
935 sub runCommand {
936 my $cmd = shift @_;
937
938 print STDERR "\n$cmd\n\n";
939
940 if (system($cmd)) {
941 return(1);
942 }
943 return(0);
944 }