-
Notifications
You must be signed in to change notification settings - Fork 15
/
normalise_seqlengths.pl
executable file
·225 lines (188 loc) · 5.58 KB
/
normalise_seqlengths.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
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
#!/usr/bin/perl
use warnings;
use strict;
use Pod::Usage; ## uses pod documentation in usage code
use Getopt::Long qw(:config auto_version auto_help);
use POSIX; ## for ceil
our $VERSION = "0.1";
our $DEBUG = 0;
=head1 NAME
normalise_seqlengths.pl -- converts all sequences in a FASTA/FASTQ
file to the same length
=head1 SYNOPSIS
./normalise_seqlengths.pl <reads.fa> [options]
=head2 Options
=over 2
=item B<-help>
Only display this help message
=item B<-fraglength>
Target fragment length (in base-pairs, default 2000)
=item B<-overlap>
Minimum overlap length (in base-pairs, default 200)
=item B<-short>
Keep short sequences (shorter than I<fraglength>)
=back
=head1 DESCRIPTION
Converts all sequences in the input FASTA file to the same
length. Sequences shorter than the target length are dropped, and
sequences longer than the target length are split into overlapping
subsequences covering the entire range. This prepares the sequences
for use in an overlap-consensus assembler requiring constant-length
sequences (such as edena).
=head1 METHODS
=cut
=head2 processFastaSeq(id, seq, length, minoverlap)
Convert the FASTA sequence I<seq> into a set of subsequences (possibly none)
that are exactly I<length> bp, and overlap at least by I<minoverlap>
bp. The converted sequences are written to stdout in FASTA format.
=cut
sub processFastaSeq{
my ($id, $seq, $lf, $lo) = @_;
if(!$id || (length($seq) < $lf)){
return;
}
if(length($seq) == $lf){
$seq =~ s/(.{70})/$1\n/g;
$seq =~ s/\s+$//;
print(">${id}\n${seq}\n");
}
my $ls = length($seq);
# number of full or partial fragments in sequence (minimum number of splits)
my $nf = ceil($ls / $lf);
# total overlap if the minimum number of fragments were used
my $dl = ($nf * $lf) - $ls;
# additional fragments required, derived from...
my $k = ceil(($lo * ($nf - 1) - $dl) / ($lf - $lo));
# ... mean_overlap = ($dl + $k * $lf) / ($nf + $k - 1)
my $mo = ($dl + $k * $lf) / ($nf + $k - 1);
$nf += $k;
for(my $i = 0; $i < $nf; $i++){
my $ss = int($i * ($lf-$mo));
my $se = $ss + $lf;
my $subSeq = substr($seq, $ss, $lf);
if($i == ($nf - 1)){
$ss = $ls - $lf;
$se = $ls;
$subSeq = substr($seq, $ls - $lf);
}
my $subID = $id;
my $mor = sprintf("%0.2f", $mo);
$mor =~ s/\.00$//;
$subID =~ s/^(.*?)(\s|$)/$1#$i$2/;
if($i == 0){
$subID =~ s/^(.*?)(\s|$)/$1 \[$ls bp, $nf fragments, $mor overlap\]$2/;
} else {
$subID =~ s/^(.*?)(\s|$)/$1 \[$ss..$se\]$2/;
}
$subSeq =~ s/(.{70})/$1\n/g;
$subSeq =~ s/\s+$//;
print(">${subID}\n${subSeq}\n");
}
}
=head2 processFastqSeq(id, seq, qual length, minoverlap)
Convert the FASTQ sequence I<seq>,I<qual> into a set of subsequences
(possibly none) that are exactly I<length> bp, and overlap at least by
I<minoverlap> bp. The converted sequences are written to stdout in
FASTA format.
=cut
sub processFastqSeq{
my ($id, $seq, $qual, $lf, $lo) = @_;
if(!$id || (length($seq) < $lf)){
return;
}
if(length($seq) == $lf){
print("@"."${id}\n${seq}\n+\n${qual}\n");
}
my $ls = length($seq);
# number of full or partial fragments in sequence (minimum number of splits)
my $nf = ceil($ls / $lf);
# total overlap if the minimum number of fragments were used
my $dl = ($nf * $lf) - $ls;
# additional fragments required, derived from...
my $k = ceil(($lo * ($nf - 1) - $dl) / ($lf - $lo));
# ... mean_overlap = ($dl + $k * $lf) / ($nf + $k - 1)
my $mo = ($nf == 1) ? 0 : ($dl + $k * $lf) / ($nf + $k - 1);
my $mor = sprintf("%0.0f", $mo);
$nf += $k;
for(my $i = 0; $i < $nf; $i++){
my $ss = int($i * ($lf-$mo));
my $se = $ss + $lf;
my $subSeq = substr($seq, $ss, $lf);
my $subQual = substr($qual, $ss, $lf);
if($i == ($nf - 1)){
$ss = $ls - $lf;
$se = $ls;
$subSeq = substr($seq, $ls - $lf);
$subQual = substr($qual, $ls - $lf);
}
my $subID = $id;
$subID =~ s/^(.*?)(\s|$)/$1#$i$2/;
if($i == 0){
$subID =~ s/^(.*?)(\s|$)/$1 \[$ls bp, $nf fragments, $mor overlap\]$2/;
} else {
$subID =~ s/^(.*?)(\s|$)/$1 \[$ss..$se\]$2/;
}
print("@"."${subID}\n${subSeq}\n+\n${subQual}\n");
}
}
####################################################
# Command line parsing and verification starts here
####################################################
my $argLine = join(" ",@ARGV);
my $options =
{
"fraglength" => 2000,
"overlap" => 200,
};
GetOptions($options,
'fraglength|f=i',
'overlap|o=i',
'short!',
'debug!' => \$DEBUG,
) or pod2usage(1);
my $inQual = 0; # false
my $seqID = "";
my $qualID = "";
my $seq = "";
my $qual = "";
while(<>){
chomp;
chomp;
if(!$inQual){
if(/^(>|@)((.+?)( .*?\s*)?)$/){
my $newSeqID = $2;
my $newShortID = $3;
if($seqID){
if($qual){
processFastqSeq($seqID, $seq, $qual, $options->{"fraglength"},
$options->{"overlap"});
} else {
processFastaSeq($seqID, $seq, $options->{"fraglength"},
$options->{"overlap"});
}
}
$seq = "";
$qual = "";
$seqID = $newSeqID;
} elsif(/^\+(.*)$/) {
$inQual = 1; # true
$qualID = $1;
} else {
$seq .= $_;
}
} else {
$qual .= $_;
if(length($qual) >= length($seq)){
$inQual = 0; # false
}
}
}
if($seqID){
if($qual){
processFastqSeq($seqID, $seq, $qual, $options->{"fraglength"},
$options->{"overlap"});
} else {
processFastaSeq($seqID, $seq, $options->{"fraglength"},
$options->{"overlap"});
}
}