Question: How to alter the program to a universal format converter
0
gravatar for Mimmi Ahlmén
2.0 years ago by
Mimmi Ahlmén20 wrote:

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 modified 2.0 years ago by JC12k • written 2.0 years ago by Mimmi Ahlmén20
3
gravatar for JC
2.0 years ago by
JC12k
Mexico
JC12k wrote:

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 modified 2.0 years ago • written 2.0 years ago by JC12k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 2244 users visited in the last hour
_