Convert fastq to fasta

From HPCwiki
Jump to navigation Jump to search

Converting fasq to fasta can sometimes be useful. Here a simple perl script is described that can do this. The script works on a stream, and will output to STDOUT. Optional is filtering for reads that have a certain minimum length

Example for invoking the script, assuming the script is named fastq_to_fasta.pl:

gunzip -c fastqfile.fq.gz | perl fastq_to_fasta.pl -l 50 | gzip -c >fastafile.fa.gz

Here is the perl script itself:

 #!/usr/bin/perl -w
 use strict;
 use warnings;
 use Getopt::Std;
 my %opts = ();
 $opts{l}=60;
 getopt('l', \%opts);
 my $minlength = $opts{l};
 my $count=0;
 my $name='nn';
 while (<>){
        my $line = $_;
        chomp $line;
        ++$count;
        if ($count==1){
                $name = $line;
                $name =~ s/^@/>/;
        }
        if ($count==2){
                if (length $line > 60){
                        print "$name\n$line\n";
                }
        }
        if ($count==4){
                $count=0;
        }
 }
 exit;