Reputation: 743
I want to convert all the fastq files contained in a folder to fasta format, each file will keep the original name but in fasta extension, I have made the next code in perl, however it only extract the last sequence of each file !!!
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
my ($dir, $files, $file_name, $file_format);
GetOptions (
'dr=s' =>\$dir
);
foreach $files (glob("$dir/*.fastq")) {
open LINE, "<", $files or die "can't read open $files";
while(
defined(my $head = <LINE>) &&
defined(my $seq = <LINE>) &&
defined(my $qhead = <LINE>) &&
defined(my $quality = <LINE>)
){
substr($head, 0, 1, '>');
($file_name, $file_format) = split (/\./,$files);
open OUTFILE, '>', $file_name.".fasta";
print OUTFILE $head, $seq;
}
}
close LINE;
close OUTFILE;
exit;
and extra question, I want to use the pice next code, as a subroutine, I want to develop the same scrip to have the option of convert all fastq files in a folder or single file to fasta file !!!
while(
defined(my $head = <LINE>) &&
defined(my $seq = <LINE>) &&
defined(my $qhead = <LINE>) &&
defined(my $quality = <LINE>)
){
Upvotes: 0
Views: 697
Reputation: 434
Kinda of new to perl myself and don't know a thing about bioinformatics, but it looks like you are overwriting the output file with every turn of the while loop. I think you want to append to the file so you want a >> not just >. example
open OUTFILE, '>>', $file_name.".fasta";
Hope that helps.
Upvotes: 1