How to alter the program to a universal format converter
Hello, a perl dummy here. The following script takes a input fasta file and converts it to the format swiss. How can I alter the script so that I can convert a file with any format to a new file in any other format, e.g. a universal format converter?
use strict;
use warnings;
use Bio::Seq;
use Bio::SeqIO;
my $file = 'infile.fas';
# Opens a sequence file
my $seqio = Bio::SeqIO->new( '-format' => 'fasta' , -file => $file);
# Fetch the next sequence from the stream, creates a Bio::Seq object
my $seqobj = $seqio->next_seq();
# Write the sequence to a new file, in a different format
my $out = Bio::SeqIO->new( -file => '>infile.swiss', -format => 'swiss' );
$out->write_seq($seqobj);
• 1,471 views
•
link
1 answer
the trick is to pass the -format parameter for Bio::SeqIO as a variable, for example, we can pass the file names and formats as command line arguments:
#!/usr/bin/perl
use strict;
use warnings;
use Bio::Seq;
use Bio::SeqIO;
$ARGV[3] or die "use bioperl_converter.pl FILE_IN FORMAT_IN FILE_OUT FORMAT_OUT\n";
my $file_in = shift @ARGV;
my $format_in = shift @ARGV;
my $file_out = shift @ARGV;
my $format_in = shift @ARGV;
# Opens a sequence file
my $seqio = Bio::SeqIO->new( '-format' => $format_in , -file => $file_in);
# Fetch the next sequence from the stream, creates a Bio::Seq object
my $seqobj = $seqio->next_seq();
# Write the sequence to a new file, in a different format
my $out = Bio::SeqIO->new( -file => $file_out, -format => $format_out );
$out->write_seq($seqobj);
So, now you can execute:
perl bioperl_converter.pl infile.fas fasta infile.swiss swiss
• 0 views
•
link
Log in to answer this question.