http://www.perlmonks.org?node_id=1136388

Sosi has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone! I'm trying to get intergenic sequences for many genebank files. To do so I'm trying something like this:

use strict; use warnings; use autodie; use Data::Dump; use Data::Dumper; use Bio::SeqIO; STDOUT->autoflush(1); my $gbffpath='C:\My gbff path'; opendir(DIR, "$gbffpath"); my @files= grep {/\.gbff$/ } readdir(DIR); closedir(DIR); my $featc=0; foreach my $gbfffile (@files){ my $in = Bio::SeqIO->new(-file=>"<$gbffpath\\$gbfffile", -format=> +"genbank"); my @allpos; my $obj = $in->next_seq(); my @features = $obj->get_SeqFeatures(); my @two = shift @features; foreach my $fefe (@features){ my $pt = $fefe->primary_tag(); if ($pt eq "CDS" or $pt eq "rRNA" or $pt eq "tRNA") { $featc++; last if $featc==4; push @two, $fefe; my $starti= $two[1]->start(); my $endi; my $end; my $start; if($two[0]->primary_tag() eq "source"){ $endi = $starti; $end = $endi +1; $start = $starti -1; shift @two; } else{ $endi = $two[0]->end(); $end = $endi +1; $start = $starti -1; #do something else # change $start to be -21 nucleotides # my $subseq = $obj->subseq($end,$start) unless ($end> +=$start); my $subseq = $obj->subseq($end,$start) unless ($end>=$ +start); shift @two; dd ($gbfffile,$pt,"\$two\[0\]:","endi:$endi","\$two\[1 +\]:","starti:$starti","after:","end:$end","start:$start",$subseq); } } }

However, $subseq always returns undefined and I cannot understand why. If I change that to $obj->seq() the same happens. Can someone please help me?

Edit: this question was cross-posted at https://www.biostars.org/p/152157/.

Replies are listed 'Best First'.
Re: Trying to get intergenic sequences using BioPerl
by toolic (Bishop) on Jul 27, 2015 at 00:19 UTC

      I know, that's why I have that dd ($gbfffile,$pt,"\$two\[0\]:","endi:$endi","\$two\[1\]:","starti:$starti","after:","end:$end","start:$start",$subseq);there, but I see nothing wrong with $start or $end

Re: Trying to get intergenic sequences using BioPerl
by kevbot (Vicar) on Jul 27, 2015 at 00:34 UTC
      I dont know how that got there, I don't recall having posted it twice. I just edited my original question (or so I thought). Sorry if I did something wrong
Re: Trying to get intergenic sequences using BioPerl
by biohisham (Priest) on Jul 27, 2015 at 14:12 UTC

    What is the relevance of my @two = shift @features;?. Since shift shortens @features by one through removing the first element held therein you might end mis-assigning feature tags to variables since you can't be sure that all features in a genbank file are consistent for every annotation.

    Have you printed out the value of $pt to verify that it is either "CDS" , "rRNA" or "tRNA" ?

    Within the same if block you're pushing into and shifting out of @two inside the foreach loop. A practice that can make it harder for you to track current elements within @two

    If we define intergenic regions as any region upstream of the next gene and downstream of the first gene and since genes can be in the forward or the backward strands; right next to each other spaced apart by a random number of nucleotides then extracting the intergenic region might be very tricky since you have to consider the orientation of the genes flanking the region and their start and end (start>end for forward strand genes and start<end for backward strand)

    From my experience, dealing with Genbank or any other format besides gff is suboptimal due to inconsistent and erroneous annotations. However, try FeatureExtract.py with the  -i flag. Be very vigilant for inconsistencies in extracted regions and consider that the output of FeatureExtract might need further cleaning up.

    My approach is to parse a GFF file myself and get the coordinates for the features I want to extract then use sfetch to extract the sequences from the corresponding genbank file by passing these coordinates for the features (genes, CDs, RNA, tRNA ..etc). The sfetch utility appropriately takes care of the orientation for you in that if you pass it a "from" position that is larger than a "to" position then it'd extract and reverse translate the negative strand.

    In the case of intergenic regions this can simply be reading between the lines of the gff file. I am passing you my one-liners for this activity which you could modify to your content. I put appropriate comments there to get you started.

    #Given a gff file and embl file for a genome extract the upstream sequ +ences that correspond to intergenic regions #STEP1: Parse a GFF file to get upstream regions #The idea is to create a hash using the line numbers as keys (line num +bers correspond to genes), upstream regions for positive #strand genes are enclosed between $F[3] of the current line and $F[4] + of the PREVIOUS line. Upstream regions for negative #strand genes are enclosed between $F[4] of the current line and $F[3] + of the SUBSEQUENT line. #if the first gene record was on the positive strand then the upstream + region is counted from 1 upto $F[3]. cat file.gff | perl -F'\t' -lane 'push @{$hash{$.}}, @F }{ @record_lin +e_numbers= sort {$a<=>$b} keys %hash; foreach $element (@record_line_ +numbers){if($element ==1 && @{$hash{$element}}[6] eq "+" ){print "$el +ement\t1\t@{$hash{$element}}[3]\t@{$hash{$element}}[6] "}elsif(@{$has +h{$element}}[6] eq "+"){$prev_line=$element-1;print "$element\t@{$has +h{$prev_line}}[4]\t@{$hash{$element}}[3]\t@{$hash{$element}}[6]" }els +if(@{$hash{$element}}[6] eq "-" ){$next_line=$element+1; print "$elem +ent\t@{$hash{$element}}[4]\t@{$hash{$next_line}}[3]\t@{$hash{$element +}}[6]" } } ' | perl -F'\t' -lane 'next if(($F[2] - $F[1]) < 10); +print' > file.upstream.coord.txt #extract sequences 200 nts upstream upto 5 nts after the start codon t +hrough sfetch cat file.upstream.coord.txt | perl -F'\t' -lane '$length=$F[2]-$F[1]; +if($length>200 && $F[3] eq "+"){$from=$F[2]-200;$to=$F[2]+7; $name = +join("_",$F[0],$from,$to,$F[3]); print "sfetch -d bacteria.embl -F \" +fasta\" -f $from -t $to -r \"$name\" ." }elsif($length < 200 && $F[3] + eq "+"){$from=$F[1];$to=$F[2]+7; $name=join("_", $F[0],$from,$to, $F +[3]); print "sfetch -d bacteria.embl -F \"fasta\" -f $from -t $to -r +\"$name\" ." }elsif($length > 200 && $F[3] eq "-"){$from=$F[1]+200; $ +to=$F[1]-7; $name =join("_", $F[0],$from,$to, $F[3]); print "sfetch - +d bacteria.embl -F \"fasta\" -f $from -t $to -r \"$name\" ." }elsif($ +length <200 && $F[3] eq "-"){$from=$F[2];$to=$F[1]-7;$name=join("_", +$F[0],$from,$to, $F[3]); print "bacteria.embl -F \"fasta\" -f $from - +t $to -r \"$name\" ."} ' | sh > file.upstream.fa

    Something or the other, a monk since 2009

      Thank you very much! Yes, I read somewhere that many people criticize gbff for this sort of thing, so I started playing around with the .gff. However, the next question that raises is: in the case of organisms with multiple chromosomes/plasmids, how do you then get the subseqs out of the gbff (once you have the coordinates from the gff) by pointing for a specific chromosome? I will now look into your code and see how it goes, I hope I can convert it to regular script! Thank you very much!

      <quote>Have you printed out the value of $pt to verify that it is either "CDS" , "rRNA" or "tRNA" ?</quote>

      Yes, it was retrieving "CDS", "rRNA" and "tRNA". Which is strange because then I can find some "Source" within "@two"

        I am not sure I can answer your question without context except through an educated guess. What species are your working with ? The subseq coordinates in the gff file should ideally have been assigned as an offset from the start of the sequence file to where they are regardless of their chromosomal position. Another possibility is that the gff files for each chromosome have been generated individually. In the first case using the coordinates can get you the region corresponding to the chromosome without needing to worry about chromosomal location. In the second scenario extracting the subsequence will be as simple as accessing the right chromosomal gff file.


        Something or the other, a monk since 2009
Re: Trying to get intergenic sequences using BioPerl
by poj (Abbot) on Jul 27, 2015 at 11:14 UTC

    I haven't yet worked out what you are doing with the @two array but here is what I have working so far.

    #!perl use strict; use warnings; use autodie; use Bio::SeqIO; STDOUT->autoflush(1); my $gbffpath = 'your path'; opendir DIR, $gbffpath; my @files = grep {/\.gbff$/ } readdir DIR; closedir DIR; foreach my $gbfffile (@files){ my $in = Bio::SeqIO->new(-file=>$gbffpath.'/'.$gbfffile, -format=>"genbank"); my $obj = $in->next_seq(); my @feat = $obj->get_SeqFeatures(); my $start = 0; my $length = 9; foreach my $n (0..$#feat){ my $pt = $feat[$n]->primary_tag(); $start += $feat[$n]->start(); my $end = $start + $length; my $subseq = $obj->subseq($start,$end); printf "%2d %-10s %2d %2d %s\n",$n,$pt,$start,$end,$subseq; $start = $end; } }
    poj
Re: Trying to get intergenic sequences using BioPerl
by pvaldes (Chaplain) on Jul 27, 2015 at 11:20 UTC

    (hum, I'm feeling a little anonymous today)... ok, what is this dd function?

    I see nothing wrong with $start or $end

    $endi = $starti; ## uh? start substring position = end substring position?
     ## end + 1 = start?, start -1 = end?; substring between 'end' and 'end' position = undef?
Re: Trying to get intergenic sequences using BioPerl
by Anonymous Monk on Jul 27, 2015 at 11:15 UTC
    what does dd here?
Re: Trying to get intergenic sequences using BioPerl
by pvaldes (Chaplain) on Aug 01, 2015 at 23:59 UTC

    ups, I did it again... stoopid and sexy anonymous monkey. Fix and repeat.

    if ($pt eq "CDS" or $pt eq "rRNA" or $pt eq "tRNA")

    You can also wrote this line as:

    if ($pt =~/^(CDS|[rt]?RNA)$/)