Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl: getting all increasing and decreasing Strips in an array (use in Bioinformatics)

I'm new at Perl and im having trouble at designing a certain function in Perl.

The Function should find and return all Increasing and Decreasing Strips. What does that mean? Two Positions are neighbors if they're neighboring numbers. i.e. (2,3) or (8,7). A Increasing Strip is an increasing Strip of neighbors. i.e. (3,4,5,6). Decreasing Strip is defined similar. At the beginning of every Array a 0 gets added and at the end the length of the array+1. Single Numbers without neighbors are decreasing. 0 and n+1 are increasing.

So if i have the array (0,3,4,5,9,8,6,2,1,7,10) i should get the following results: Increasing Strips are: (3,4,5) (10) (0) Decreasing Strips are: (9,8), (6), (2,1) (7)

I tried to reduce the problem to only getting all Decreasing Strips, but this is as far as i get: http://pastebin.com/yStbgNme

Code here:

sub getIncs{
    my @$bar = shift;
    my %incs;
    my $inccount = 0;
    my $i=0;
    while($i<@bar-1){
        for($j=$i; 1; $j++;){
            if($bar[$j] == $bar[$j+1]+1){
                $incs{$inccount} = ($i,$j);
            } else {
                $inccount++;
                last;
            }
        }
    }

//edit1: I found a Python-Program that contains said function getStrips(), but my python is sporadic at best. http://www.csbio.unc.edu/mcmillan/Media/breakpointReversalSort.txt

//edit2: Every number is exactly one Time in the array So there can be no overlap.

like image 394
WeGi Avatar asked Dec 06 '25 10:12

WeGi


1 Answers

use strict;
my @s = (0,3,4,5,9,8,6,2,1,7,10);
my $i = 0;
my $j = 0; #size of @s
my $inc = "Increasing: ";
my $dec = "Decreasing: ";

# Prepend the beginning with 0, if necessary
if($s[0] != 0 || @s == 0 ) { unshift @s, 0; }
$j = @s;

foreach(@s) {
  # Increasing
  if( ($s[$i] == 0) || ($i == $j-1) || ($s[$i+1] - $s[$i]) == 1 || ($s[$i] - $s[$i-1] == 1)) {
    if($s[$i] - $s[$i-1] != 1) { $inc .= "("; }
    $inc .= $s[$i];    
    if($s[$i+1] - $s[$i] != 1) { $inc .= ")"; }
    if($s[$i+1] - $s[$i] == 1) { $inc .= ","; }
  }

  #Decreasing
  if( ($s[$i]-$s[$i-1] != 1) && ($s[$i+1] - $s[$i] != 1) && ($s[$i] != 0) && ($i != $j-1) ) {
    if($s[$i-1] - $s[$i] != 1) { $dec .= "("; }
    $dec .= $s[$i];
    if($s[$i] - $s[$i+1] != 1) { $dec .= ")"; }
    if($s[$i] - $s[$i+1] == 1) { $dec .= ","; }
  }
  $i++;
}

$inc =~ s/\)\(/\),\(/g;
$dec =~ s/\)\(/\),\(/g;
print "$inc\n";
print "$dec\n";

Result:

Increasing: (0),(3,4,5),(10)
Decreasing: (9,8),(6),(2,1),(7)
like image 120
John Dewey Avatar answered Dec 09 '25 00:12

John Dewey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!