In: Computer Science
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
/**
* Class BasicBioinformatics contains static methods for performing common
* DNA-based operations in bioinformatics.
*
*
*/
public class BasicBioinformatics {
/**
* Calculates and returns the number of times each type of nucleotide occurs
* in a DNA sequence.
*
* @param dna
* a char array representing a DNA sequence of arbitrary length,
* containing only the characters A, C, G and T
*
* @return an int array of length 4, where subscripts 0, 1, 2 and 3 contain
* the number of 'A', 'C', 'G' and 'T' characters (respectively) in
* the given sequence
*/
public static int[] nucleotideCounts(char[] dna) {
// creating an int array of size 4, values will be 0 by default.
int result[] = new int[4];
// looping through dna array
for (int i = 0; i < dna.length; i++) {
// if current char is A, incrementing array element at index 0
if (dna[i] == 'A') {
result[0]++;
}
// if current char is C, incrementing array element at index 1
else if (dna[i] == 'C') {
result[1]++;
}
// if current char is G, incrementing array element at index 2
else if (dna[i] == 'G') {
result[2]++;
}
// now only T is remaining, if the input is valid, then we don't
// need to check for T, simply increment element at index 3
else {
result[3]++;
}
}
return result;
}
/**
* Calculates and returns the number of times each type of nucleotide occurs
* in a DNA sequence.
*
* @param dna
* a String representing a DNA sequence of arbitrary length,
* containing only the characters A, C, G and T
*
* @return an int array of length 4, where subscripts 0, 1, 2 and 3 contain
* the number of 'A', 'C', 'G' and 'T' characters (respectively) in
* the given sequence
*/
public static int[] nucleotideCounts(String dna) {
return nucleotideCounts(dna.toCharArray());
}
}