Question: Perl: Implementing A Circular List /Circular Iteration
1
gravatar for Neal
5.4 years ago by
Neal40
Norway
Neal40 wrote:

Hello all,

I need a bit of help implementing a circular list or perhaps a circular iteration( whichever can get the job done)

What is required
For a given peptide string , find all the possible sub-peptides including single amino acids and print their masses. For example's sake let's say A = 1, B = 2, C = 3, D = 4

eg

Input: A cyclic peptide :

ABCD

Output

A  B  C  D  AB  BC  CD  DA  ABC  BCD  CDA  DABC  
1  2  3  4  3   5   7   5   6    9    8    10

I initially tried implementing a simple substr based approach. The pseudocode is as under. So it is untested:

my $i =0;
my $j = 1;
my %peptide;
my $len = length $_; # $_ being the input peptide string
while( $i <= $len-1 ) {

       my $mass = 0;
       while( $j <= $len-1 ){
            my $substr = substr $_, $i, $j   if ( $i != $len -1 && $j  < $len-1 )
            my $amino_acid_mass = subMass ($substr) ;
            my $mass += $amino_acid_mass; 

            push @{$peptide{$mass}}, $substr ; # because multiple sub-peptides can have same mass
            $j++;

           #To perform a very poor version of circular iteration
           #if at last element 'N' , take out that element + add it to first  'N-2' elements
           if( $j == $len-1 && $i == $len ){

                     my $substr_L = substr $_, $i, 1;# last 1 element
                     my $substr_F = substr $_, 0, $j-2  #first 2 elements
                     my $string = $substr_L.$substr_F # append
                     #Extract mass and add to the %peptide hash
                    }
          }
       $i++;
      }

Clearly this isn't the most elegant nor a fully working idea. It would break easily at peptide length > 3 ( 0,1,2,3). Furthermore, even the subroutine will break quickly as I realized later that a variable length $substr would be its input.

So I thought perhaps I should implement an array. I searched on how to implement a circular array. This is what I came up with after finding a bit of hint in the Perl Cookbook online... :

my @arr = qw/A B C D/;
my $i = 0;
while($i <= $#arr ){
    unshift(@arr, pop(@arr) );
    say "@arr";

    my $y = 0;
    my $z = 1;


    foreach (@arr){

        say "Element : $_";
        say "2 elements: $arr[$y]$arr[$z] " if exists $arr[$z];
        $y++;
        $z++;
    }                
    $i++;
}

But really I am stuck now and just cannot figure out what to do next or how to proceed further. I would be really grateful for any help or guidance.

perl list • 2.2k views
ADD COMMENTlink modified 5.4 years ago by Michael Dondrup45k • written 5.4 years ago by Neal40

This looks like one of the assignments from Bioinformatics Algorithms on coursera

ADD REPLYlink written 5.4 years ago by Sudeep1.6k

It does...I want to get better at programming. I am stuck in this...

ADD REPLYlink written 5.4 years ago by Neal40

Right now I am just thinking of working on the assignments ;)

ADD REPLYlink written 5.4 years ago by Sudeep1.6k

Yes !! And we may get many questions here from Coursera Assignments !! ;)

ADD REPLYlink written 5.4 years ago by always_learning960

Ah so long as one wants to improve!

ADD REPLYlink written 5.4 years ago by Neal40
1
gravatar for Michael Dondrup
5.4 years ago by
Bergen, Norway
Michael Dondrup45k wrote:

Hint: Try to concatenate the string with itself.

ADD COMMENTlink written 5.4 years ago by Michael Dondrup45k

Many many thanks Michael :)

It took me some time to understand what you meant and how it would help. But I did it! Finally! yay!

ADD REPLYlink written 5.4 years ago by Neal40
1

Nice it helped without spoiling the question :)

ADD REPLYlink written 5.4 years ago by Michael Dondrup45k

Yea I am relieved that I could figure it out with a little bit of nudge from you! :)

ADD REPLYlink written 5.4 years ago by Neal40
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: 1399 users visited in the last hour