Tuesday, 26 October 2021

Computing GC Content (Rosalind | English)

      

rosalind


Hi everyone, how are you? On this occasion, I want to discuss about a problem from rosalind.info's web. The title is "Computing GC Content". For the reference, you can first check out the problem what will be discussed (here). 

 

An analogy of pairing DNA molecule: thymine - adenine, cytosine - guanine.


Overview

In this problem we will given several string-form input based on FASTA format. Then, from those string we are assigned to find which string has the highest score for GC-content. GC-content itself is a representation for the ratio of guanine (G) and cytosine (C) molecule in the DNA. For example, the DNA strain "GCATA" has 2/5 (0.4) in GC-content score. 

The Code 

This is the code for solving this problem (with java language): 

static class Data {
double Max = 0.0;
String Name = "";
}
static void solve() {
Scanner sc = new Scanner(System.in);
Data result = new Data();
String name = "";
String nameCadangan = "";
boolean nameCadanganDipake = false;
while (sc.hasNext()) {
         if (!nameCadanganDipake) name = sc.next();
else {
name = nameCadangan;
nameCadanganDipake = false;
}
name = name.substring(1, name.length());
String s = "";
while (sc.hasNext()) {
nameCadangan = sc.next();
if (nameCadangan.charAt(0) == '>') {
nameCadanganDipake = true;
break;
}
s += nameCadangan;
}
int GC = 0;
for (char x : s.toCharArray()) {
if (x == 'G' || x == 'C') GC++;
}
double val = 100.0 * GC / s.length();
if (val > result.Max) {
result.Max = val;
result.Name = name;
}
}
out.printf("%s\n%.6f", result.Name, result.Max);
}

Input and Output

In the code above, I used Scanner function for entering the string-form data. That is because in Scanner we can use the hasNext() function which is useful for entering the unkown-amount of data. 

And for the output I used function out.println(). That function is modification from function System.out.println() 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 from me. 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/
Source of image 2 :https://en.wikipedia.org/wiki/GC-content

No comments:

Post a Comment