-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvelvetk.pl
executable file
·179 lines (158 loc) · 5.16 KB
/
velvetk.pl
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
#!/usr/bin/perl
use strict;
use warnings;
use Fatal;
use IO::File;
use IO::Handle;
use List::Util qw(sum);
use Data::Dumper;
# velvetk.pl
#
# Examine k-mer coverage of your reads for a given target genome size.
# Assumes uniform coverage of your genome.
#
# Copyright (C) 2012- Torsten Seemann
#
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
my(@Options, $verbose, $size, $cov, $range, $genome, $best);
setOptions();
die "Please only use one of --size or --genome" if $size and $genome;
if ($genome) {
print STDERR "Estimating target genome size from '$genome'\n";
$size=0;
# doing this manually to avoid BioPerl dependency
open my $in, '<', $genome;
while (<$in>) {
next if m/>/;
chomp;
$size += length $_;
}
}
my %multiplier = (K=>1E3, M=>1E6, G=>1E9);
if ($size =~ m/(.*)([KMG])$/i) {
$size = $1 * $multiplier{uc $2};
}
die "Target genome size '$size' isn't an integer" if $size !~ m/^[0-9E\-+]+$/i;
die "Target genome size is zero... can't proceed" if $size <=0;
print STDERR "Target genome size is $size bp\n";
print STDERR "Desire k-mer coverage of $cov\n";
my %num_with_len;
my $dna;
for my $file (@ARGV) {
my $nseq=0;
my $fh = open_maybe_compressed($file);
while (my $line = $fh->getline) {
print STDERR "\rReading: $nseq" if $nseq % 9871 == 0;
if ($line =~ m/^>/) {
# ok,fasta
$dna = $fh->getline;
}
elsif ($line =~ m/^\@/) {
# ok, fastq
$dna = $fh->getline;
$fh->getline; # skip id2
$fh->getline; # skip qual
}
else {
# dunno, skip and try next line!
next;
}
# process the seq line
chomp $dna;
$num_with_len{ length($dna) }++;
$nseq++;
}
print STDERR "\rRead $nseq sequences from $file\n";
}
my @l = sort { $a <=> $b } keys %num_with_len;
die "no reads found" if not @l;
my $nreads = sum( values %num_with_len );
print STDERR "Considered $nreads reads with lengths $l[0]..$l[-1] bp\n";
print Dumper(\%num_with_len) if $verbose;
my $bestkc = 0;
my $bestk = 0;
print "K\t#Kmers\tKmer-Cov\n" if not $best;
for (my $k=1; $k <= $l[-1]; $k+=2) {
my $n = num_kmers($k, \%num_with_len);
my $kc = $n/$size;
if (abs($kc-$cov) <= abs($bestkc-$cov)) {
$bestk = $k;
$bestkc = $kc;
}
# next if $kc < $cov-$range-0.5 or $kc > $cov+$range+0.5;
printf "$k\t$n\t%.1f\n", $kc if not $best;
}
print "$bestk\n" if $best;
#----------------------------------------------------------------------
sub num_kmers {
my($k, $num_with_len) = @_;
my $n=0;
for my $L (keys %{$num_with_len}) {
next if $L < $k;
$n += ($L-$k+1) * $num_with_len->{$L};
}
return $n;
}
#----------------------------------------------------------------------
sub open_maybe_compressed {
my($fname) = @_;
my @try = ("pbzip2 -dc", "pigz -dc", "bzip2 -dc", "gzip -dc", "cat");
my $fh;
for my $exe (@try) {
print STDERR "Trying: $exe $fname\n" if $verbose;
my $io = IO::File->new("$exe \Q$fname\E 2> /dev/null |");
my $c = $io->getc;
if (defined $c) {
$io->ungetc(ord $c);
print STDERR "Using $exe for $fname\n";
return $io;
}
}
die "could not open $fname";
}
#----------------------------------------------------------------------
# Option setting routines
sub setOptions {
use Getopt::Long;
@Options = (
{OPT=>"help", VAR=>\&usage, DESC=>"This help"},
{OPT=>"verbose!", VAR=>\$verbose, DEFAULT=>0, DESC=>"Verbose output"},
{OPT=>"size=s", VAR=>\$size, DEFAULT=>0, DESC=>"Target genome size in bp (can use k/M/G suffix)"},
{OPT=>"genome=s", VAR=>\$genome, DEFAULT=>'', DESC=>"Target genome (FASTA)"},
{OPT=>"cov=i", VAR=>\$cov, DEFAULT=>25, DESC=>"Target k-mer coverage"},
# {OPT=>"range=i", VAR=>\$range, DEFAULT=>10, DESC=>"Print target +/- this range"},
{OPT=>"best!", VAR=>\$best, DEFAULT=>0, DESC=>"Just print best k-mer to stdout"},
);
(!@ARGV) && (usage());
&GetOptions(map {$_->{OPT}, $_->{VAR}} @Options) || usage();
# Now setup default values.
foreach (@Options) {
if (defined($_->{DEFAULT}) && !defined(${$_->{VAR}})) {
${$_->{VAR}} = $_->{DEFAULT};
}
}
}
sub usage {
my $exe = $0;
$exe =~ s{^.*/}{};
print STDERR "Synopsis:\n List suitable k-mer sizes given target genome and reads\n";
print STDERR "Author:\n Torsten Seemann <torsten\@seemann.id.au>\n";
print STDERR "Usage:\n $exe [--size X | --genome F] [options] <readfile[.gz][.bz2] ...>\n";
print STDERR "Options:\n";
foreach (@Options) {
printf STDERR " --%-13s %s%s.\n",$_->{OPT},$_->{DESC},
defined($_->{DEFAULT}) ? " (default '$_->{DEFAULT}')" : "";
}
exit(1);
}