HiveBrain v1.2.0
Get Started
← Back to all entries
patternrubyMinor

Calculating protein mass

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
masscalculatingprotein

Problem

This question is part of a series solving the Rosalind challenges. For the previous question in this series, see A sequence of mistakes. The repository with all my up-to-date solutions so far can be found here.

Problem: PRTM


In a weighted alphabet, every symbol is assigned a positive real number called a weight. A string formed from a weighted alphabet is called a weighted string, and its weight is equal to the sum of the weights of its symbols.


The standard weight assigned to each member of the 20-symbol amino acid alphabet is the monoisotopic mass of the corresponding amino acid.

Given:


A protein string \$P\$ of length at most 1000 aa.

Return:


The total weight of \$P\$. Consult the monoisotopic mass table.

Sample Dataset:

SKADYEK


Sample Output:

821.392


My solution solves the sample dataset and the actual dataset given.

Dataset:

FQLCECNPKWNEEKRQYGACNQRNEYQDMKMVLRLNTRALAFLVEKFCTQATANSHGWQYRIENPLQLNHLPLTICRHGFDEDRIQRYQDDHNGVHIHVAMQITHSEWVLETWASSHQHANISPLSGNSQRYYPANHEGRRIQCCQFDCESHLMEDLWREATRSHESKVTRFKHDHNTPLHFTVFWPSDMEHDNGKTEYKSGWQLMFKIEGSVGKECELHTSYFSMKVKYTTVEIQTNPIERMCYAELVYWLRAEMEGVAKDRDHKWAMEHEQYIYSPMEGLINSVEYHMVYPQMARSIAKDRYWTPATLRRIHVWYCREIVQIMWIDHPKGLVCVMPDSNVGALIKWYTHYTDVKLETHQTCRKNNAGMVRDGRKKVAAEVFIIYEMGMPEYKPHEDANRDYRIPIDHPWRPHNNMCFGISGEWRRSCMKHQIQLSRWWANITKIMPMRWSIESGTTLRFVQGMVYRVNRFDDRYPRMNDCYWDAIWVCGAWTMHLTVPDLLMQMHHCMGAIHDVVFSEMGVKIAYQAVWFASVRIPASFQDWFCHEDHFQRGLRKCKLEHNDVLYETWIAMEPASYHTVHHCWHWREVFCHPCVFIHQMYSYPEFHIPDVELSLHYSQIWMNRETGWSPIRMELMCTSAPPNPDGGTPLLSFWYFGFIIQRHCGCETIMIGQWDTCFNSHHDVWAVWVHPENSQFTKERICCASFTDNSDTRECKQTVQHSSPGPTELWADSKTQVQWWYISQWLGKGLMYSEFQVSISGLSTPMLVYWLPNSMIEMNDVMEILNSPAGAQITTCIAGVNFRMIFETSWRTWYINRH


Output:

97283.202


PRTM.rb:

```
module Rosalind
RESIDUE_BY_MONOISOTOPIC_MASS = {
71.03711 => ["A"],
103.00919 => ["C"],
115.02694 => ["D"],
129.04259 => ["E"],
147.06841 => ["F"],
57.02146 => ["G"],
137.05891 => ["H"],
113.08406 => ["I", "

Solution

About your code:

-
I am glad you liked the idea of building the inverse structure programmatically, but here I don't think it's necessary :) Only two aminoacids have the same weight, I'd write the reversed hash.

-
Why residue instead of aminoacid?

-
I don't see why you need take_while here.

-
xs.reduce(0) { |acc, x| acc.method(x) } -> xs.reduce(0, :method).

-
Minor detail: personally I prefer explicit uses of $stdin.readline instead of gets.

I'd write:

module Rosalind
  MONOISOTOPIC_MASS_BY_AMINOACID = {
    "A" => 71.03711,
    "C" => 103.00919,
    "D" => 115.02694,
    "E" => 129.04259,
    "F" => 147.06841,
    "G" => 57.02146,
    "H" => 137.05891,
    "I" => 113.08406,
    "L" => 113.08406,
    "K" => 128.09496,
    "M" => 131.04049,
    "N" => 114.04293,
    "P" => 97.05276,
    "Q" => 128.05858,
    "R" => 156.10111,
    "S" => 87.03203,
    "T" => 101.04768,
    "V" => 99.06841,
    "W" => 186.07931,
    "Y" => 163.06333,
  }

  def self.problem_prtm(aminoacids_string)
    aminoacids_string.
      chars.
      map { |aminoacid| MONOISOTOPIC_MASS_BY_AMINOACID[aminoacid] }.
      reduce(0, :+)
  end  
end 

protein = $stdin.readline.rstrip
weight = '%.3f' % Rosalind.problem_prtm(protein)
puts(weight)


Same idea, slightly different approach:

module Rosalind
  ...
  def self.problem_prtm(aminoacids_string)
    aminoacids = aminoacids_string.chars
    masses = MONOISOTOPIC_MASS_BY_AMINOACID.values_at(*aminoacids)
    masses.reduce(0, :+)
  end
end

Code Snippets

module Rosalind
  MONOISOTOPIC_MASS_BY_AMINOACID = {
    "A" => 71.03711,
    "C" => 103.00919,
    "D" => 115.02694,
    "E" => 129.04259,
    "F" => 147.06841,
    "G" => 57.02146,
    "H" => 137.05891,
    "I" => 113.08406,
    "L" => 113.08406,
    "K" => 128.09496,
    "M" => 131.04049,
    "N" => 114.04293,
    "P" => 97.05276,
    "Q" => 128.05858,
    "R" => 156.10111,
    "S" => 87.03203,
    "T" => 101.04768,
    "V" => 99.06841,
    "W" => 186.07931,
    "Y" => 163.06333,
  }

  def self.problem_prtm(aminoacids_string)
    aminoacids_string.
      chars.
      map { |aminoacid| MONOISOTOPIC_MASS_BY_AMINOACID[aminoacid] }.
      reduce(0, :+)
  end  
end 

protein = $stdin.readline.rstrip
weight = '%.3f' % Rosalind.problem_prtm(protein)
puts(weight)
module Rosalind
  ...
  def self.problem_prtm(aminoacids_string)
    aminoacids = aminoacids_string.chars
    masses = MONOISOTOPIC_MASS_BY_AMINOACID.values_at(*aminoacids)
    masses.reduce(0, :+)
  end
end

Context

StackExchange Code Review Q#132905, answer score: 5

Revisions (0)

No revisions yet.