Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
(rosalind) SGRA [correct version after failure]
  • Loading branch information
grondilu committed May 10, 2013
1 parent f059431 commit ac897af
Showing 1 changed file with 10 additions and 23 deletions.
33 changes: 10 additions & 23 deletions rosalind/sgra-grondilu.pl
Expand Up @@ -3,31 +3,19 @@

my %mass-table = slurp('monoisotopic-mass-table.txt').words;
my @L = sort lines;
# <1 132.04049>;
# <3524.8542 3623.5245 3710.9335 3841.974 3929.00603 3970.0326 4026.05879 4057.0646 4083.08025>;
my %invert-mass-table = %mass-table.invert.hash;
# for %mass-table.invert { %invert-mass-table{.key}.push: .value }
my @masses = sort +*, %mass-table.values;
my %precision = @L Z=> map { $^x / $x.subst(/\./, '') }, @L;

sub spectrum-graph(@L) {
my %edges;
for ^@L -> $i {
my $u = @L[$i];
note
my $u = @L[$i];
for @L[$i+1..*-1] -> $v {
note my $precision = max %precision{$u, $v};
my $diff = $v - $u;
my $bound = upper-bound(@masses, $diff);
my $mass = $bound == 0 ?? @masses[0] !!
$bound == @masses.elems ?? @masses[*-1] !!
min :by({abs($diff-$_)}), @masses[$bound-1, $bound];
if abs($diff - $mass) < $precision {
#note "$i $precision $diff $mass";
%edges{$u}.push: {
next-mass => $v,
amino-acid => %invert-mass-table{$mass}
};
}
my $mass = %invert-mass-table{$v - $u};
%edges{$u}.push: {
next-mass => $v,
amino-acid => $mass;
} if defined $mass;
}
}
return %edges;
Expand All @@ -36,12 +24,11 @@
my %graph = spectrum-graph(@L);
sub find-protein($initial-mass) {
return '' unless %graph{$initial-mass} :exists;
gather for %graph{$initial-mass}[] -> $node {
take $node<amino-acid> «~« find-protein($node<next-mass>);
gather for %graph{$initial-mass}[] {
take .<amino-acid> «~« find-protein(.<next-mass>);
}
}

my @protein = map &find-protein, @L;
say max :by(*.chars), @protein;
say max :by(*.chars), map &find-protein, @L;

# vim: ft=perl6

0 comments on commit ac897af

Please sign in to comment.