Question: Find A Substring And Count The Number Of Occurrences
0
gravatar for Elena
7.6 years ago by
Elena240
Elena240 wrote:

The program should find the substrings in a given string and count their number of occurrences. Moreover the substring must check the occurrences only for every three alphabets.

for eg: String: AGAUUUAGA

output:

AGA-2 UUU-1

print"Enter the mRNA Sequence\n";    
$count = 0;
$count1 = 0;
$seq = <>;    
chomp($seq);    
$p = '';    
$ln = length($seq);    
$j = $ln/3;    
for($i=0,$k=0;$i<$ln,$k<$j;$k++)    
    {   
    $fra[$k]=substr($seq,$i,3);    
    $i=$i+3;


    if({$fra[$k]} eq AGA)    
        {    
        $count++;

        print"The number of AGA is $count";
        }    
    elseif({$fra[$k]} eq UUU)    
    {
        $count1++;
        print" The number of UUU is $count1";
    }

}
perl homework • 3.7k views
ADD COMMENTlink modified 4.8 years ago by Woa2.7k • written 7.6 years ago by Elena240
1

downvote - it's tagged as homework and not a question.

ADD REPLYlink written 7.6 years ago by Casbon3.2k
1

@Casbon Please note that the tags can be edited by someone else than the OP, as is the case here. The homework tag has been added by another user with admin rights. I agree that Payaliya should make a greater effort when posting questions.

ADD REPLYlink written 7.6 years ago by Eric Normandeau9.9k

which error are you getting? What is the expected output?

ADD REPLYlink written 7.6 years ago by Giovanni M Dall'Olio26k

I have reformatted the code to make it easier to read on this website. Please use the "Code block" button when posting code. Moreover, I've improved the spelling of your question and the title, check whether it is ok.

ADD REPLYlink written 7.6 years ago by Giovanni M Dall'Olio26k

What is the question?

ADD REPLYlink written 7.6 years ago by Joachim2.8k

Payaliya please stop editing the title of the question when you think it should be [closed] - questions are not 'closed' just because there is a single acceptable answer, they are closed for being off-topic or duplicated, or not a real question!

ADD REPLYlink written 7.6 years ago by Daniel Swan13k
5
gravatar for Stefano Berri
7.6 years ago by
Stefano Berri4.0k
Cambridge, UK
Stefano Berri4.0k wrote:

Hi. here your version of the code corrected so that it works

print"Enter the mRNA Sequence\n";    
$count = 0;
$count1 = 0;
$seq = <>;    
chomp($seq);    
$p = '';    
$ln = length($seq);    
$j = $ln/3;    
for($i=0,$k=0;$i<$ln,$k<$j;$k++)    
    {   
    $fra[$k]=substr($seq,$i,3);    
    $i=$i+3;
    print "$k -> @fra\n";
    print "$k -> {$fra[$k]}\n";
    if($fra[$k] eq 'AGA')          
        {        
        $count++;

        print"The number of AGA is $count\n";
        }        
    elsif($fra[$k] eq 'UUU')          
    {   
        $count1++;
        print" The number of UUU is $count1\n";
    }

}

with some printing here and there to show you what is going on. The main problem was that you compare {$fra[$k]} with UUU, but {$fra[$k]} is {UUU} (including braces)

Here a shorter version, possibly easier to read

use strict;
print"Enter the mRNA Sequence\n";

my $seq = <>;
chomp($seq);
my %freq;

my @codons = split(/(\w{3})/, $seq);
foreach my $thisCodon (@codons) {
    $freq{$thisCodon}++;
}

print "AGA found $freq{'AGA'} times\n";
print "UUU found $freq{'UUU'} times\n";
ADD COMMENTlink written 7.6 years ago by Stefano Berri4.0k
1
gravatar for Jarretinha
7.6 years ago by
Jarretinha3.3k
São Paulo, Brazil
Jarretinha3.3k wrote:

Hi there,

Bioperl has the Bio::Tools::SeqWords module which already implements a solution to this problem for an arbitrary word size. It can handle overlapping words too. Just check the code to see how it works.

ADD COMMENTlink modified 7.6 years ago • written 7.6 years ago by Jarretinha3.3k
0
gravatar for Phis
7.6 years ago by
Phis1.0k
CH
Phis1.0k wrote:

The program should find the substrings in a given string and count their number of occurrences.

I don't know whether that's relevant to you at all, but if this is for speed/efficiency and/or must handle lots of data and/or you need repeated lookups, I'd recommend using a suffix array approach to substring searching. There is obviously a cost to building the suffix array in the first place, but once you have it, locating substrings is very fast.

ADD COMMENTlink written 7.6 years ago by Phis1.0k
0
gravatar for Woa
4.8 years ago by
Woa2.7k
United States
Woa2.7k wrote:

This includes overlapping codons:

use strict;
use warnings;
my $string="AGAUUUAGA";
my @trinucs=($string=~/(?=(.{3}))/g);
my %tri_count=();
$tri_count{$_}++ for @trinucs;
print $_,":",$tri_count{$_},"\n" for sort keys(%tri_count);

Output is:

AGA:2
AUU:1
GAU:1
UAG:1
UUA:1
UUU:1
ADD COMMENTlink written 4.8 years ago by Woa2.7k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 693 users visited in the last hour