-
Notifications
You must be signed in to change notification settings - Fork 7
/
fasta2fastq.pl
executable file
·216 lines (164 loc) · 6.04 KB
/
fasta2fastq.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
#!/usr/bin/perl
#
# INGLÊS/ENGLISH
# 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.
# http://www.gnu.org/copyleft/gpl.html
#
#
# PORTUGUÊS/PORTUGUESE
# Este programa é distribuído na expectativa de ser útil aos seus
# usuários, porém NÃO TEM NENHUMA GARANTIA, EXPLÍCITAS OU IMPLÍCITAS,
# COMERCIAIS OU DE ATENDIMENTO A UMA DETERMINADA FINALIDADE. Consulte
# a Licença Pública Geral GNU para maiores detalhes.
# http://www.gnu.org/copyleft/gpl.html
#
# Copyright (C) 2010 Fundação Hemocentro de Ribeirão Preto
#
# Laboratório de Genética Molecular e Bioinformática
# Núcleo de Bioinformática
# BiT - Bioinformatics Team
# Fundação Hemocentro de Ribeirão Preto
# Rua Tenente Catão Roxo, 2501
# Ribeirão Preto - São Paulo
# Brasil
# CEP 14051-140
# Fone: 55 16 39639300 Ramal 9603
#
# Daniel Guariz Pinheiro
# http://lgmb.fmrp.usp.br
#
# $Id$
=head1 NAME
fasta2fastq.pl - Convert sequences in fasta/qual files to fastq file format.
=head1 SYNOPSIS
# create a fastq file from fasta and qual files
$ fasta2fastq.pl -f infile.fasta -q infile.fasta.qual -fq outfile.fastq
# create a fastq file from fasta file and quality default 25
$ fasta2fastq.pl -f infile.fasta -qd 25 -fq outfile.fastq
# The default ASCII offset is 33, the example below changes the offset to 64
$ fasta2fastq.pl -f infile.fasta -qd 25 -o 64 -fq outfile.fastq
=head1 ABSTRACT
=head1 DESCRIPTION
Convert sequences in fasta/qual files to fastq file format using an
specified quality file or a quality default value.
=head1 AUTHOR
Daniel Guariz Pinheiro E<lt>[email protected]<gt>
Copyright (c) 2010 Regional Blood Center of Ribeirão Preto
=head1 LICENSE
GNU General Public License
http://www.gnu.org/copyleft/gpl.html
=cut
use strict;
use warnings;
use Getopt::Long;
my ($fasta, $qual, $offset, $fastq, $qualdefault);
Usage("Too few arguments") if $#ARGV < 0;
GetOptions( "h|?|help" => sub { &Usage(); },
"f|fasta=s"=>\$fasta,
"q|qual=s"=>\$qual,
"qd|qualdefault=i"=>\$qualdefault,
"o|offset=i"=>\$offset,
"fq|fastq=s"=>\$fastq
) or &Usage();
$offset||=33;
die "Missing input fasta file" unless ($fasta);
die "Missing output fastq file" unless ($fastq);
die "Wrong input fasta file ($fasta)" unless (-e $fasta);
open(FASTA, "<", $fasta) or die $!;
open(FASTQ, ">", $fastq) or die $!;
$/ = ">";
{
my $debris = <FASTA>;
}
my $count = 0;
if ($qual) {
die "Wrong input qual file ($qual)" unless (-e $qual);
open(QUAL, "<", $qual) or die $!;
{
my $debris = <QUAL>;
}
while (my $fastarec = <FASTA>) {
$fastarec=~s/>$//;
$fastarec=~s/\n+$//;
my $qualrec = <QUAL>;
die "Missing qual record" unless ($qualrec);
$qualrec=~s/>$//;
$qualrec=~s/\n+$//;
my ($fastahead, $fastabody) = $fastarec=~/([^\n]+)\n(.*)/s;
$fastabody=~s/\s+|\n+//sg;
my ($fastaid) = $fastahead=~/^(\S+)/;
# print ">$fastaid\n$fastabody\n";
my ($qualhead, $qualbody) = $qualrec=~/([^\n]+)\n(.*)/s;
$qualbody=~s/\n+/ /sg;
my ($qualid) = $qualhead=~/^(\S+)/;
# print ">$qualid\n$qualbody\n";
$qualbody=~s/^\s+|\s+$//;
my @qual = split(/\s+/, $qualbody);
# print join('-', @qual),"\n";
die "FASTA and QUAL identifiers don't match each other" unless ($fastaid eq $qualid);
print FASTQ '@',$fastaid,"\n",
$fastabody,"\n",
'+',$qualid,"\n",
join('', map { chr($_+$offset) } @qual),"\n";
$count++;
if ($count % 10000 == 0) {
print STDERR "Converted records: $count \r";
}
}
print STDERR "Converted records: $count \n";
close(QUAL);
}
else {
unless (defined $qualdefault) {
die "This script needs input qual file or quality default";
}
else {
while (my $fastarec = <FASTA>) {
$fastarec=~s/>$//;
$fastarec=~s/\n+$//;
my ($fastahead, $fastabody) = $fastarec=~/([^\n]+)\n(.*)/s;
$fastabody=~s/\s+|\n+//sg;
my ($fastaid) = $fastahead=~/^(\S+)/;
# print ">$fastaid\n$fastabody\n";
my $qualid = $fastaid;
my @qual = ( $qualdefault ) x length($fastabody);
print FASTQ '@',$fastaid,"\n",
$fastabody,"\n",
'+',$qualid,"\n",
join('', map { chr($_+$offset) } @qual),"\n";
$count++;
if ($count % 10000 == 0) {
print STDERR "Converted records with quality default ($qualdefault): $count \r";
}
}
print STDERR "Converted records with quality default ($qualdefault): $count \n";
}
}
close(FASTA);
close(FASTQ);
# Subroutines
sub Usage {
my ($msg) = @_;
my $USAGE = <<"END_USAGE";
Daniel Guariz Pinheiro (dgpinheiro\@gmail.com)
(c)2010 Regional Blood Center of Ribeirão Preto
Usage
$0 [-h/--help] [-f <input-fasta-file> -fq <output-fastq-file>] \
[-q <input-qual-file> | -qd <quality-default>] [-o <ascii-offset>]
Argument(s)
-h --help Help
-f --fasta Input fasta file
-q --qual Input qual file
-qd --qualdefault Quality default
-o --offset ASCII offset (default:33)
-fq --fastq Output fastq file
END_USAGE
print STDERR "\nERR: $msg\n\n" if $msg;
print STDERR qq[$0 ] . q[$Revision$] . qq[\n];
print STDERR $USAGE;
exit(1);
}