Debian Codegraph @

atac-driver/atac.pl

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