Perl: Implementing A Circular List /Circular Iteration
1
1
Entering edit mode
11.0 years ago
Neal ▴ 60

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 • 3.6k views
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

Ah so long as one wants to improve!

ADD REPLY
1
Entering edit mode
11.0 years ago
Michael 55k

Hint: Try to concatenate the string with itself.

ADD COMMENT
0
Entering edit mode

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 REPLY
1
Entering edit mode

Nice it helped without spoiling the question :)

ADD REPLY
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 886 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6