Saturday 6 November 2021

Calculating Protein Mass (Rosalind | English)

rosalind


Hi everyone, how are you? This time, I want to discuss about one problem (which is bioinformatics problem) that exist in rosalind.info's web. The title is "Calculating Protein Mass". For the reference, you can first check out the problem that will be discussed (here). 

Overwiew

In this problem, we will given a string representing a protein-strain dataset. Then, our task is to determine the total mass of that protein strain. We can calculate the total mass of that protein by enumerating those masses letter by letter based on the protein-mass table

The Code 

This is the code for solving this problem (with java language): 
  1.  static void solve() {
  2. Scanner sc = new Scanner(System.in);
  3. String s = "";
  4. while (sc.hasNext()) {
  5. s += sc.next();
  6. }
  7. HashMap<Character, Double> pair = new HashMap<>();
  8. pair.put('A', 71.03711 );
  9. pair.put('C', 103.00919);
  10. pair.put('D', 115.02694);
  11. pair.put('E', 129.04259);
  12. pair.put('F', 147.06841);
  13. pair.put('G', 57.02146 );
  14. pair.put('H', 137.05891);
  15. pair.put('I', 113.08406);
  16. pair.put('K', 128.09496);
  17. pair.put('L', 113.08406);
  18. pair.put('M', 131.04049);
  19. pair.put('N', 114.04293);
  20. pair.put('P', 97.05276 );
  21. pair.put('Q', 128.05858);
  22. pair.put('R', 156.10111);
  23. pair.put('S', 87.03203 );
  24. pair.put('T', 101.04768);
  25. pair.put('V', 99.06841 );
  26. pair.put('W', 186.07931);
  27. pair.put('Y', 163.06333);
  28. Double res = 0.0;
  29. for (char chs : s.toCharArray()) {
  30. res += pair.get(chs);
  31. }
  32. out.printf("%.3f\n", res);
  33.     }    

First of all, we copy the protein-mass table into the HashMap function (line 7-27). 

After that, we can just total the numbers of protein value (based on protein mass table) letter by letter (line 28-31). 

The result is formed as a double-format output (line 32). In my output, I used "%.3f\n"-format to ensure that my output is exactly formed with 3-digit behind the coma. 

Input and Output

In the code above, I used next() function for entering the string-form dataset (line 5). 

I also used another input function called hasNext() (line 4). That function is very useful especially if we need to process an unknown-amount dataset, just like a FASTA format

And for the output I used out.printf() function (line 32). That function is a modification from System.out.printf() function which is very familiar in java. You can see the additional code for that modification (input and output) in my complete code at github.

That's it. If you want to ask something, you can write it in the comment section below. I hope this article is useful and see you in the next article!


Reference :
Source of image 1 :https://www.facebook.com/ProjectRosalind/

No comments:

Post a Comment