This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Bioperl: Extracting Complement Statement Along With Location

Hi,

I have a code bioperl (below) that does the job of extracting CDS locations (e.g., (234..1234) from a GenBank file, but does not extract the complement statement when they are indicated such as complement(234530..235249). How can I extract the complement statement together with the location whenever they indicated as mentioned.

if ($feat_object->primary_tag eq "CDS") {
  my $start = $feat_object->location->start;
  my $end = $feat_object->location->end;
  print $start . ".." . $end . "\n";
}

Thanks

bioperl perl

1 answer

will it help to use foreach?

if ( $feature->primary_tag eq 'CDS' ) {
 foreach my $location ( $feature->location->each_Location ) {
  print $location->start, "..", $location->end, "\n";
 }
}

-- update with full code --

#!/bin/perl
use strict;
use Bio::SeqIO;

my $seqio_object = Bio::SeqIO->new(
 -file   => 'tmp/NC_017187.gbk',
 -format => 'genbank'
);

my $seq_object = $seqio_object->next_seq;

foreach my $feature ( $seq_object->top_SeqFeatures ) {
 if ( $feature->primary_tag eq 'CDS' ) {
  foreach my $location ( $feature->location->each_Location ) {
   print $location->start, "..", $location->end, "\n";
  }
 }
}

this code produces output:

...
2254886..2255341
2255356..2255934
2255982..2256446

from the "complement" statement

$ grep "2255356..2255934" NC_017187.gbk
     gene            complement(2255356..2255934)
     CDS             complement(2255356..2255934)

Thanks seninp. Unfortunately, I got the following error: Can't call "location" on an undefined value at line: foreach my $location ( $feature->location->each_Location ).

just updated the reply

Many thanks Pavel, updated solution using grep did the trick. Very much appreciated!

Log in to answer this question.