annotate tools/spades_3_0/spades.pl @ 8:ff058438080a draft

Version 0.8, supports SPAdes 3.0.0
author lionelguy
date Wed, 05 Feb 2014 05:19:03 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
1 #!/usr/bin/env perl
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
2 ## A wrapper script to call spades.py and collect its output
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
3 use strict;
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
4 use warnings;
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
5 use File::Temp qw/ tempfile tempdir /;
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
6 use File::Copy;
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
7 use Getopt::Long;
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
8
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
9 # Parse arguments
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
10 my ($out_contigs_file,
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
11 $out_contigs_stats,
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
12 $out_scaffolds_file,
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
13 $out_scaffolds_stats,
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
14 $out_log_file,
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
15 @sysargs) = @ARGV;
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
16
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
17 ## GetOptions not compatible with parsing the rest of the arguments in an array.
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
18 ## Keeping the not-so-nice parse-in-one-go method, without named arguments.
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
19 # GetOptions(
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
20 # 'contigs-file=s' => \$out_contigs_file,
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
21 # 'contigs-stats=s' => \$out_contigs_stats,
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
22 # 'scaffolds-file=s' => \$out_scaffolds_file,
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
23 # 'scaffolds-stats=s' => \$out_scaffolds_stats,
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
24 # 'out_log_file=s' => \$out_log_file,
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
25 # );
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
26
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
27 # my @sysargs = @ARGV;
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
28
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
29 # Create temporary folder to store files, delete after use
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
30 #my $output_dir = tempdir( CLEANUP => 0 );
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
31 my $output_dir = 'output_dir';
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
32 # Link "dat" files as fastq, otherwise spades complains about file format
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
33
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
34 # Create log handle
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
35 open my $log, '>', $out_log_file or die "Cannot write to $out_log_file: $?\n";
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
36
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
37 # Run program
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
38 # To do: record time
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
39 &runSpades(@sysargs);
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
40 &collectOutput();
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
41 &extractCoverageLength($out_contigs_file, $out_contigs_stats);
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
42 &extractCoverageLength($out_scaffolds_file, $out_scaffolds_stats);
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
43 print $log "Done\n";
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
44 close $log;
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
45 exit 0;
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
46
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
47 # Run spades
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
48 sub runSpades {
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
49 my $cmd = join(" ", @_) . " -o $output_dir";
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
50 my $return_code = system($cmd);
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
51 if ($return_code) {
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
52 print $log "Failed with code $return_code\nCommand $cmd\nMessage: $?\n";
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
53 die "Failed with code $return_code\nCommand $cmd\nMessage: $?\n";
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
54 }
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
55 return 0;
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
56 }
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
57
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
58 # Collect output
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
59 sub collectOutput{
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
60 # To do: check that the files are there
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
61 # Collects output
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
62 move "$output_dir/contigs.fasta", $out_contigs_file;
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
63 move "$output_dir/scaffolds.fasta", $out_scaffolds_file;
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
64 open LOG, '<', "$output_dir/spades.log"
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
65 or die "Cannot open log file $output_dir/spades.log: $?";
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
66 print $log $_ while (<LOG>);
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
67 return 0;
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
68 }
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
69
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
70 # Extract
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
71 sub extractCoverageLength{
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
72 my ($in, $out) = @_;
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
73 open FASTA, '<', $in or die $!;
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
74 open TAB, '>', $out or die $!;
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
75 print TAB "#name\tlength\tcoverage\n";
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
76 while (<FASTA>){
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
77 next unless /^>/;
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
78 chomp;
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
79 die "Not all elements found in $_\n" if (! m/^>NODE_(\d+)_length_(\d+)_cov_(\d+\.*\d*)/);
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
80 my ($n, $l, $cov) = ($1, $2, $3);
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
81 print TAB "NODE_$n\t$l\t$cov\n";
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
82 }
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
83 close TAB;
ff058438080a Version 0.8, supports SPAdes 3.0.0
lionelguy
parents:
diff changeset
84 }