Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Get Fasta file with Protein Sequences given a file with Genbank Ids using Perl

by kevbot (Vicar)
on Mar 20, 2016 at 22:18 UTC ( [id://1158386]=note: print w/replies, xml ) Need Help??


in reply to Get Fasta file with Protein Sequences given a file with Genbank Ids using Perl

Hello dimitris852,

The documentation for Bio::DB::GenBank states that the set_Seq_by_id method takes only one argument which is the id (as a string) of a sequence. It does not appear that you can supply a list of sequences as the argument.

So, if you want to write multiple sequences you will need to do something like this:

foreach my $id (@lines) { my $seq = $gb->get_Seq_by_id($id); write_sequence( ">>roar.fa", 'fasta', $seq ); }
Note that I changed the write mode from > to >> in order to append to the file. So, you should delete any old roar.fa file that may exist before running the script again.

Replies are listed 'Best First'.
Re^2: Get Fasta file with Protein Sequences given a file with Genbank Ids using Perl
by dimitris852 (Acolyte) on Mar 21, 2016 at 05:09 UTC

    hello 1nickt and kevbot,

    I'm trying this code:
    use strict; use warnings; use Bio::DB::GenBank; use File::Slurp; my @lines = read_file('sec.txt'); chomp @lines; print "@lines\n"; foreach my $id (@lines) { my $gb=Bio::DB::GenBank; my $seq = $gb->get_Seq_by_id($id); write_sequence( ">>roar.fa", 'fasta', $seq ); }
    Everything seem logic but I get this Error: Bareword "Bio::DB::GenBank" not allowed while "strict subs" in use at .... line 15.

    I have absolutely no idea how to fix it ....

      Line 15 of the code
          my $gb=Bio::DB::GenBank;
      should be
          my $gb = new Bio::DB::GenBank;
      (indirect object notation), or better yet
          my $gb = Bio::DB::GenBank->new;
      or
          my $gb = Bio::DB::GenBank->new();
      which avoid the syntactic ambiguities of indirect object notation.


      Give a man a fish:  <%-{-{-{-<

        thanks! but after fixing that I get this: Undefined subroutine &main::write_sequence called at .... line 20.

      Sure you do.

      If something stops working, go back and look at what worked. Your OP had:

      my $gb=new Bio::DB::GenBank;
      Now this is actually deprecated syntax and should be written as:
      my $gb = Bio::DB::GenBank->new();
      ... but either style shows the point: you need to call the new() method to get an instance of the class Bio::DB::GenBank, aka, an object, and store it in $gb. Your existing code just assigns the bareword 'Bio::DB::GenBank' to $gb -- or tries to, but barewords are not allowed in your script because you're sensibly using strict. This is a great example of why to use strict: it tells you exactly what the problem is and where to find it.

      Also, don't use File::Slurp, it's broken. Use File::Slurper or Path::Tiny.

      Hope this helps!


      The way forward always starts with a minimal test.
      Change this:
      my $gb=Bio::DB::GenBank;
      to this:
      my $gb = new Bio::DB::GenBank;
      and see if that gets things working for you.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1158386]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-19 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found