How do I only iterate over a few records in a BioPerl structure instead of all of them?

Viewed 45

I'm starting in perl and I made this script to read a file. But the file contains more than 10k lines, how to limit reading to 5 lines, or between line 5 and 10?

my $pdb = Structure->new(
-file => 'arq.txt',
-format => 'PB'
);

my $strutur = $pb->next_structure;

foreach my $chain ($strutur->get_chains) {
   my $chain_id = $chain->id;

   foreach my $residue ($strutur->get_residues($chain)) {
      my $residue_id = $residue->id;

        print "Chain: ".$chain_id."\tResidue: ".$residue_id."\n";
   }
}

First i tried display five lines and break the loop.

use strict;
while(my $strutur = $pb->next_structure)  {   
    print "Chain: ".$chain_id."\tResidue: ".$residue_id."\n";    
    last if $. == 5;
}
close $pb;

And get the output Global symbol "$strutur" requires explicit package name (did you forget to declare "my $estrutura"?)
Then i tried

use strict;
while( my $residue_id = $residue->id)  {   
    print "Chain: ".$chain_id."\tResidue: ".$residue_id."\n";    
    last if $. == 5;
}
close $residue;

That made an infinite loop with just the first line

1 Answers

It looks like this is all part of BioPerl. I'm not familiar with this at all, but it's easy enough to find the documentation.

You've not been entirely clear which part you only want 5 of. So let's go through your code and see what you can do. I am sure you don't mean "how do I read 5 lines" because this code abstracts away lines in a file format. We're talking entities of something here. I don't know anything about biology, or what this stuff is used for.

my $pdb = Structure->new(
-file => 'arq.txt',
-format => 'PB'
);

# (1) This gives you one structure. Call this again, and you'll get another one. 
my $strutur = $pb->next_structure;

# (2) This returns all the chains and iterates them, we can limit these
foreach my $chain ($strutur->get_chains) {
   my $chain_id = $chain->id;

   # (3) This returns all residues in a chain and iterates them.
   #     We can limit these too.
   foreach my $residue ($strutur->get_residues($chain)) {
      my $residue_id = $residue->id;

        print "Chain: ".$chain_id."\tResidue: ".$residue_id."\n";
      }
   }
}

There are three different things we can do. Some of these can be solved in various ways.

  1. Every time you call $pb->next_structure, you will get a new structure. If you want five of these, call it five times, or do it in a loop. I don't think that's what you are talking about.

  2. The get_chains() method returns all chains associated with a structure. I don't know what either of these are, but we can easily make this only iterate over 5 of them.

    my @chains = $strutur->get_chains;
    for my $i ( 0 .. 4 ) {
      my $chain = $chains[$i];
      my $chain_id = $chain->id;
      # ...
    

    If you want to get records 5 to 9, you would change it to 4 .. 8. The index always starts at zero. To get 5, and then the next 5, you'd do 0 ... 4 and then 5 .. 9, and so on.

  3. To process only 5 redidues, you do basically the same thing as in 2.

Related