comparison spades.pl @ 0:addd8265834b draft default tip

planemo upload commit 18fef9393a17a3442ab7927d76b301bb43ec3de4
author nml
date Tue, 09 Aug 2016 10:52:40 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:addd8265834b
1 #!/usr/bin/env perl
2 ## A wrapper script to call spades.py and collect its output
3 use strict;
4 use warnings;
5 use File::Temp qw/ tempfile tempdir /;
6 use File::Copy;
7 use Getopt::Long;
8
9 # Parse arguments
10 my ($out_contigs_file,
11 $out_paths_file,
12 $out_log_file,
13 $new_name,
14 @sysargs) = @ARGV;
15
16
17 my $output_dir = 'output_dir';
18
19 # Create log handle
20 open my $log, '>', $out_log_file or die "Cannot write to $out_log_file: $?\n";
21
22 # Run program
23 runSpades(@sysargs);
24 collectOutput($new_name);
25 print $log "Done\n";
26 close $log;
27 exit 0;
28
29 # Run spades
30 sub runSpades {
31 my $cmd = join(" ", @_) . " -o $output_dir";
32 my $return_code = system($cmd);
33 if ($return_code) {
34 print $log "Failed with code $return_code\nCommand $cmd\nMessage: $?\n";
35 die "Failed with code $return_code\nCommand $cmd\nMessage: $?\n";
36 }
37 return 0;
38 }
39
40 # Collect output
41 sub collectOutput{
42 my ($new_name) = @_;
43
44 # To do: check that the files are there
45 # Collects output
46 if ( not -e "$output_dir/transcripts.fasta") {
47 die "Could not find transcripts.fasta file\n";
48 }
49 if ( not -e "$output_dir/transcripts.paths") {
50 die "Could not find transcripts.paths file\n";
51 }
52
53 #if a new name is given for the contigs, change them before moving them
54 if ( $new_name ne 'NODE') {
55 renameContigs($new_name);
56 }
57 else {
58 move "$output_dir/transcripts.fasta", $out_contigs_file;
59 move "$output_dir/transcripts.paths", $out_paths_file;
60 }
61
62
63
64 open LOG, '<', "$output_dir/spades.log"
65 or die "Cannot open log file $output_dir/spades.log: $?";
66 print $log $_ while (<LOG>);
67 return 0;
68 }
69
70 #Change name in contig and fastg file
71 sub renameContigs{
72 my ($name) = @_;
73
74 open my $in, '<',"$output_dir/transcripts.fasta" or die $!;
75 open my $out,'>', $out_contigs_file;
76
77 while ( my $line = <$in>) {
78 #remove the NODE_ so we can rebuilt the display_id with our contig name with the contig number.
79 #also move the remainder of the length
80 if ( $line =~ />NODE_(\d+)_(.+)/) {
81 $line = ">$name" . "_$1 $2\n";
82 }
83 print $out $line;
84 }
85 close $in;
86 close $out;
87
88
89 open $in, '<',"$output_dir/transcripts.paths" or die $!;
90 open $out,'>', $out_paths_file;
91
92 while ( my $line = <$in>) {
93 #remove the NODE_ so we can rebuilt the display_id with our contig name with the contig number.
94 #also move the remainder of the length
95 if ( $line =~ />NODE_(\d+)_(.+)/) {
96 $line = ">$name" . "_$1 $2\n";
97 }
98 print $out $line;
99 }
100 close $in;
101 close $out;
102
103 }