* Calculates and returns where two DNA sequences of equal lengths differ. For ex
ID: 3730593 • Letter: #
Question
* Calculates and returns where two DNA sequences of equal lengths differ. For example, given
sequences "ATGT" and "GTGA", the result should be array { true, false, false, true }.
* @param dna1 a char array representing a DNA sequence, containing only the characters A, C, G
and T, with the same length as parameter dna2
* @param dna2 a char array representing a DNA sequence, containing only the characters A, C, G
and T, with the same length as parameter dna1
* @return an array of boolean values, of length equivalent to both parameters' lengths,
containing true in each subscript where the parameter strings differ, and false where
they do not differ
public static boolean[] mutationPoints(char[] dna1, char[] dna2) {
//implementent method here
}
* 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) {
// implement method
}
* Calculates and returns the reverse complement of a DNA sequence. In DNA sequences, 'A' and 'T'
are complements of each other, as are 'C' and 'G'. The reverse complement is formed by
reversing the symbols of a sequence, then taking the complement of each symbol (e.g., the
reverse complement of "GTCA" is "TGAC").
* @param dna a char array representing a DNA sequence of arbitrary length, containing only the
characters A, C, G and T
* @return a char array representation of the reverse complement of the given sequence
public static char[] reverseComplement(char[] dna) {
// implement here
}
Explanation / Answer
DNA.java
public class DNA {
public static boolean[] mutationPoints(char[] dna1, char[] dna2) {
// Declaring new boolean array
boolean[] boolArr = new boolean[dna1.length];
for(int i=0; i<dna1.length; i++) {
// Comparing element of both DNA's
if(dna1[i] == dna2[i]) {
boolArr[i] = false;
} else {
boolArr[i] = true;
}
}
// Returning boolean array
return boolArr;
}
public static int[] nucleotideCounts(char[] dna) {
// Declaring new int array
int[] nucleotides = new int[4];
// Iterating through the DNA
for(char dn:dna) {
// Comparing the each element of DNA array and incrementing the corresponding value in int array
if(dn == 'A') {
nucleotides[0] += 1;
} else if(dn == 'C') {
nucleotides[1] += 1;
} else if(dn == 'G') {
nucleotides[2] += 1;
} else if(dn == 'T') {
nucleotides[3] += 1;
}
}
// Returning int array
return nucleotides;
}
public static char[] reverseComplement(char[] dna) {
// Declaring new char array
char[] reversedDna = new char[dna.length];
// Iterating through the DNA array
for(int i=1; i<=dna.length; i++) {
// Reversing the element
reversedDna[i-1] = dna[dna.length-i];
// Complementing the element
if(reversedDna[i-1] == 'A') {
reversedDna[i-1] = 'T';
} else if(reversedDna[i-1] == 'C') {
reversedDna[i-1] = 'G';
} else if(reversedDna[i-1] == 'G') {
reversedDna[i-1] = 'C';
} else if(reversedDna[i-1] == 'T') {
reversedDna[i-1] = 'A';
}
}
// Returning the reversed complemented array
return reversedDna;
}
public static void main(String[] args) {
char[] dna1 = {'A', 'A', 'A', 'C', 'C', 'G', 'G', 'T', 'T'};
char[] dna2 = {'A', 'A', 'A', 'C', 'C', 'C', 'G', 'T', 'A'};
boolean[] boolArr = mutationPoints(dna1, dna2);
System.out.println("Mutation Points");
for(boolean bool:boolArr)
System.out.println(bool);
int[] intArr = nucleotideCounts(dna2);
System.out.println("Nucleotide Count");
for(int intA:intArr)
System.out.println(intA);
char[] charArr = reverseComplement(dna1);
System.out.println("Reverse Complement");
for(char charA:charArr)
System.out.println(charA);
}
}
Output:
Mutation Points
false
false
false
false
false
true
false
false
true
Nucleotide Count
4
3
1
1
Reverse Complement
A
A
C
C
G
G
T
T
T
NOTE: Do let me know in the comments section if you have any issues.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.