#!/usr/local/bin/perl -w use strict; use Bio::Index::Fasta; use Bio::SeqIO; my $out = new Bio::SeqIO(-format => 'fasta'); my $indexfile = 'ecoli.idx'; my $dbfile = '/usr/local/bioperl_course/db/ecoli.aa'; my $dbh = new Bio::Index::Fasta(-filename => $indexfile, -write_flag=>1); $dbh->id_parser(\&parse_id); $dbh->make_index($dbfile); my $acc1 = 'gi|1786201|gb|AAC73130.1|'; my $acc2 = 'AAC73130.1'; foreach my $acc ($acc1,$acc2) { my $seq = $dbh->get_Seq_by_acc($acc); if( $seq ) { $seq->display_id($acc); $out->write_seq($seq); } else { print STDERR "cannot find $acc\n"; } } sub parse_id { my $header = shift; if( $header =~ /^>\s*(\S+)/ ) { my @fields = split(/\|/,$1); my ($noacc) = ($fields[3] =~ /(\S+)\./); return ($fields[1],$fields[3],$noacc); } }