How to alter the program to a universal format converter
1
0
Entering edit mode
5.2 years ago

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);
Bioperl Bioseq Perl Format converter • 941 views
ADD COMMENT
3
Entering edit mode
5.2 years ago
JC 13k

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 COMMENT

Login before adding your answer.

Traffic: 1974 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6