File: exec_velvet.pl

package info (click to toggle)
stacks 1.44-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,272 kB
  • ctags: 6,756
  • sloc: cpp: 33,308; ansic: 31,921; perl: 4,169; php: 4,059; sh: 1,709; makefile: 468; sql: 386; python: 289
file content (310 lines) | stat: -rwxr-xr-x 8,924 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/env perl
#
# Copyright 2011, Julian Catchen <jcatchen@uoregon.edu>
#
# This file is part of Stacks.
#
# Stacks is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Stacks is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Stacks.  If not, see <http://www.gnu.org/licenses/>.
#

use strict;
use constant stacks_version => "_VERSION_";

my $debug        = 0;
my $paired       = 0;
my $amos         = 0;
my $ins_len_dist = 0;
my $single_path  = "";
my $paired_path  = "";
my $sanger_path  = "";
my $out_path     = ".";
my $white_list   = "";
my $hash_len     = 27;
my $insert_len   = 0;
my $exp_cov      = 0;
my $cov_cut      = 0.0;
my $min_len      = 0;
my $read_trk     = 0;
my $clean        = 1;
my $collate      = 0;
my $exe_path     = "";
my $velveth      = "velveth";
my $velvetg      = "velvetg";

parse_command_line();

if (length($exe_path) > 0) {
    $velveth = $exe_path . "/" . $velveth;
    $velvetg = $exe_path . "/" . $velvetg;
}

#
# Test that we can execute the velvet programs
#
die ("Unable to find '" . $velveth . "'.\n") if (!-e $velveth || !-x $velveth);
die ("Unable to find '" . $velvetg . "'.\n") if (!-e $velvetg || !-x $velvetg);

my (@locus_files, $num_files, $file, $input_file, $output_file, $hres_file, $gres_file, $collate_fh);

build_file_list(\@locus_files);
$num_files = scalar(@locus_files);

if ($collate) {
    open($collate_fh, ">$out_path/collated.fa") or die("Unable to open collate file, $!\n");
}

my ($sing_data, $pair_data, $sang_data, $ins, $cov, $afg, $cut, $min, $read, $cln);

$ins   = $paired   > 0 ? "-ins_length $insert_len"   : "-ins_length auto";
$cov   = $paired   > 0 ? "-exp_cov $exp_cov"         : "-exp_cov auto";
$cut   = $cov_cut  > 0 ? "-cov_cutoff $cov_cut"      : "-cov_cutoff auto";
$read  = $read_trk > 0 ? "-read_trkg yes"            : "";
$cln   = $clean    > 0 ? "-very_clean yes"           : "";
#$min   = $min_len  > 0 ? "-min_contig_lgth $min_len" : ""

#
# Write out the parameters for this assembly
#
open(PARAM, "> $out_path/velvet_parameters.txt") or die("Unable to open parameter file: $!\n");
print PARAM
    "Single-end Path: ", $single_path, "\n",
    "Paired-end Path: ", $paired_path, "\n",
    "Sanger Path:     ", $sanger_path, "\n",
    "Hash Length:     ", $hash_len, "\n",
    "Insert Length:   ", $ins, "\n",
    "Coverage:        ", $cov, "\n",
    "Coverage Cutoff: ", $cut, "\n",
    "Miniumum contig length: ", $min, "\n",
    "Read tracking:   ", $read, "\n",
    "Very Clean:      ", $cln, "\n"; 
close(PARAM);

my $i = 1;

foreach $file (@locus_files) {

    ($file) = ($file =~ /(.+)\.fa/); 
    $output_file = $out_path . "/" . $file;
    $hres_file   = $out_path . "/" . $file . "-h.output";
    $gres_file   = $out_path . "/" . $file . "-g.output";

    $sing_data = length($single_path) > 0 ? '-short '       . $single_path . "/" . $file . ".fa" : "";
    $pair_data = length($paired_path) > 0 ? '-shortPaired ' . $paired_path . "/" . $file . ".fa" : "";
    $sang_data = length($sanger_path) > 0 ? '-long '        . $sanger_path . "/" . $file . ".fa" : "";

    print STDERR "Assembling locus '$file'; run $i of $num_files        \r";

    # Execute velveth to build hash table, then velvetg to assemble
    print STDERR "$velveth $output_file $hash_len -fasta $sing_data $pair_data $sang_data &> $hres_file\n" if ($debug);
    `$velveth $output_file $hash_len -fasta $sing_data $pair_data $sang_data &> $hres_file`;

    print STDERR "$velvetg $output_file $ins $cov $cut $min $read $cln &> $gres_file\n" if ($debug);
    `$velvetg $output_file $ins $cov $cut $min $read $cln &> $gres_file`;

    collate_and_clean($out_path, $file, $collate_fh) if ($collate);

    $i++;
}

close($collate_fh) if ($collate);

sub collate_and_clean {
    my ($out_path, $file, $collate_fh) = @_;

    my (@seqs, $seq);

    parse_fasta("$out_path/$file/contigs.fa", \@seqs);

    foreach $seq (@seqs) {
	next if (length($seq->{'seq'}) < $min_len);

	$seq->{'id'} = $file . "|" . $seq->{'id'};

	print_fasta($collate_fh, $seq);
    }

    `rm $out_path/$file-g.output`;
    `rm $out_path/$file-h.output`;
    `rm -r $out_path/$file`;
}

sub parse_fasta {
    my ($file, $seqs) = @_;

    my ($fh, $line, $buf, $id, $seq);

    open($fh, "<$file") 
	or die("Unable to open Velvet output file: $file, $!\n");

    while ($line = <$fh>) {
	chomp $line;

	if (substr($line, 0, 1) eq ">") {
	    if (length($buf) > 0) {
		$seq = {};
		$seq->{'id'}  = $id;
		$seq->{'seq'} = $buf;

		push(@{$seqs}, $seq);
		$buf = "";
	    }
	    $id = substr($line, 1);

	} else {
	    $buf .= $line;
	}
    }

    if (length($buf) > 0 && length($id) > 0) {
	$seq = {};
	$seq->{'id'}  = $id;
	$seq->{'seq'} = $buf;
	push(@{$seqs}, $seq);
    }

    close($fh);
}

sub print_fasta {
    my ($fh, $seq) = @_;

    my ($s);

    print $fh ">", $seq->{'id'}, "\n";

    $s = $seq->{'seq'};

    while (length($s) > 60) {
	print $fh substr($s, 0, 60), "\n";
	$s = substr($s, 60);
    }

    print $fh $s, "\n" if (length($s) > 0);
}

sub build_file_list {
    my ($files) = @_;

    my (@ls, $line, $file, $path);

    # Load a white list of files to process if it is supplied.
    my @wl;
    if (length($white_list) > 0) {
	load_white_list(\@wl);
    }

    $path = length($paired_path) > 0 ? $paired_path : $single_path;

    @ls = `ls -1 $path/`;

    foreach $line (@ls) {
	chomp $line;

	next if (length($line) == 0);
        next if ($line !~ /.+\.fa$/ && $line !~ /.+\.fasta$/);	

	($file) = ($line =~ /^(.+\.fas?t?a?)/); 

        if (scalar(@wl) > 0) {
	    next if (!grep(/^$file$/, @wl));
	}

	push(@{$files}, $file);
    }
}

sub load_white_list {
    my ($wl) = @_;

    open(WHITE, "<" . $white_list) 
	or die("Unable to open white list file '$white_list': $!\n");

    my $line   = "";

    while ($line = <WHITE>) {
	chomp $line;

	next if (length($line) == 0);

	push(@{$wl}, $line);
    }

    close(WHITE);
}

sub parse_command_line {
    while (@ARGV) {
	$_ = shift @ARGV;
	if    ($_ =~ /^-s$/) { $single_path = shift @ARGV; }
	elsif ($_ =~ /^-p$/) { $paired_path = shift @ARGV; }
	elsif ($_ =~ /^-l$/) { $sanger_path = shift @ARGV; }
	elsif ($_ =~ /^-o$/) { $out_path    = shift @ARGV; }
	elsif ($_ =~ /^-c$/) { $collate++; }
	elsif ($_ =~ /^-W$/) { $white_list  = shift @ARGV; }
	elsif ($_ =~ /^-I$/) { $insert_len  = shift @ARGV; }
	elsif ($_ =~ /^-C$/) { $exp_cov     = shift @ARGV; }
	elsif ($_ =~ /^-T$/) { $cov_cut     = shift @ARGV; }
	elsif ($_ =~ /^-R$/) { $read_trk    = shift @ARGV; }
	elsif ($_ =~ /^-M$/) { $min_len     = shift @ARGV; }
	elsif ($_ =~ /^-H$/) { $hash_len    = shift @ARGV; }
	elsif ($_ =~ /^-P$/) { $paired++; }
	elsif ($_ =~ /^-L$/) { $clean    = 0; }
	elsif ($_ =~ /^-e$/) { $exe_path = shift @ARGV; }
	elsif ($_ =~ /^-v$/) { version(); exit(); }
	elsif ($_ =~ /^-d$/) { $debug++; }
	elsif ($_ =~ /^-h$/) { usage(); }
	else {
	    print STDERR "Unknown command line option '$_'\n";
	    usage();
	}
    }

    $single_path = substr($single_path,  0, -1) if (substr($single_path, -1)  eq "/");
    $paired_path = substr($paired_path,  0, -1) if (substr($paired_path, -1)  eq "/");
    $out_path    = substr($out_path, 0, -1)     if (substr($out_path, -1)     eq "/");
    $exe_path    = substr($exe_path, 0, -1)     if (substr($exe_path, -1)     eq "/");
}

sub version {
    print STDERR "exec_velvet.pl ", stacks_version, "\n";
}

sub usage {
    version();

    print STDERR <<EOQ; 
exec_velvet.pl -p path -s path [-l path] -o path [-c] [-H len] [-P] [-I len] [-C exp_cov] [-T cov_cut]
              [-W file_white_list] [-w marker_white_list] [-L] [-e path] [-d] [-h]
  p: path to the paired-end FASTA files to assemble.
  s: path to the single-end FASTA files to assemble.
  l: path to long, sanger-style reads to assemble.
  o: path to output the assembled files.
  c: collate the resulting velvet runs into a single FASTA file, clean velvet directories.
  W: a white list of files to process in the input path.
  H: length of overlap required for reads (hash length, default 27).
  P: process paired-end reads.
  I: insert length (for paired-end reads; see velvet documentation).
  C: expected coverage (for paired-end reads).
  T: coverage cutoff.
  M: minimum contig length, discard contigs shorter than this value.
  R: turn on velvet's read tracking (uses additional memory).
  L: leave velvet's intermediate files behind.
  e: executable path, location of velvet programs.
  h: display this help message.
  d: turn on debug output.

EOQ

  exit(0);
}