Biostar Beta. Not for public use.
Question: How to alter the program to a universal format converter
0
Entering edit mode

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);
ADD COMMENTlink 11 months ago Mimmi Ahlmén • 10 • updated 11 months ago JC 7.9k
3
Entering edit mode

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

ADD COMMENTlink 11 months ago JC 7.9k

Login before adding your answer.

Similar Posts
Loading Similar Posts
Powered by the version 2.0