Hi everyone, how are you? In this time I want to discuss about one of the problems at rosalind.info's web. The title is "Complementing a Strand of DNA". For the reference, you can first check out the problem that will be discussed (here).
Overview
In this problem we will given a string stand for DNA strain. Our job is searching the complement (or the pair) of that DNA strain. As we know, each of the DNA molecule has a pair: adenine ('A') paired with thymine ('T'), cytosine ('C') paired with guanine ('G').
It can be said that searching DNA complement is like putting the two ends of necklace together; the most right ball in the necklace meet the most left one, the 2nd right meet the 2nd left, and so on (like in the image).
Consider that that DNA strain is the necklace and those "ball" have number 1, 2, 3, 4, 5, and 6 in order. If those peaks put together, so the number 1 will meet the number 6, number 2 meet number 5, and number 3 meet number 4. If the DNAs of number 1, 2, and 3 are 'A', 'C', 'G'; so those reverse complements are number 6, 5, and 4 that have DNA 'T', 'G', 'C'.
Thus, for searching the answer of this problem we can first reversing the DNA strain (ex. "TCAC" to "CACT"). Then, after DNA strain has reversed we go on replacing each of DNA characters with those complement: 'A' > 'T', 'T' > 'A', 'G' > 'C', and 'C' > 'G'.
The Code & Explaination
This is the code for solving this problem with java language:
public void solve() {
String s = in.read();
for (int i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) == 'A') {
out.print('T');
}
else if (s.charAt(i) == 'T') {
out.print('A');
}
else if (s.charAt(i) == 'G') {
out.print('C');
}
else if (s.charAt(i) == 'C') {
out.print('G');
}
}
}
As what you see, the code is quite simple. We just have to read that DNA reversely (from back) while replacing those characters with those complements ('A'='T', 'T'='A', 'C'='G', 'G'='C').
Above, I used in.read() function for entering the string form data. That function is a extend from one of the input systems are available in java, called BufferedReader.
And for the output I used function out.println(). That function is modification from function System.out.println() that is very familiar in java. You can see the additional code for that modification 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/Nucleic_acid_secondary_structure
No comments:
Post a Comment