I'm trying to put the complement results from a char into a char array complement1. I thought I can use the operator but that's only for strings but I'm having a hard time trying to figure out how to put each letter into the array so it can display the results. Any help would be great thank you.
public static char[] complement(char[] dna) {
char[] complement1 = new char[17];
char comp = '\0';
for (char i = 0; i < dna.length; i++)
if (dna[i] == 'A') {
comp = 'T';
} else if (dna[i] == 'T') {
comp = 'A';
} else if (dna[i] == 'G') {
comp = 'C';
} else if (dna[i] == 'C') {
comp = 'G';
} else {
return null;
}
for (char c : dna)
return complement1;
return complement1;
}
public static void main(String[] args) {
String testData1 = "GCCTGTCGTAGCTTATC",
testData2 = "GGCTGACGTAGCGTAAC";
System.out.printf("%s <-- complement --> %s%n", testData1,
complement(testData1));
// int[] baseCounts = nucleotideCounts(testData1);
// System.out.printf("Nucleotide counts for %s: A: %d C: %d G: %d T: %d%n",
// testData1, baseCounts[0],
// baseCounts[1], baseCounts[2], baseCounts[3]);
System.out.printf("%s <-- reverse complement --> %s%n",
testData1, reverseComplement(testData1));
System.out.printf("%s GC-content: %f%n", testData1,
gcContent(testData1));
System.out.printf("Hamming distance between %s and %s: %d%n",
testData1, testData2,
hammingDistance(testData1, testData2));
// System.out.printf("Mutation points between %s and %s:%n%s%n", testData1,
// testData2,
// Arrays.toString(mutationPoints(testData1, testData2))); */
}
}
The code could be simplified by using switch
statement for mapping nucleotides.
Also the length of the complement
was fixed to be the same as that of input dna
, and a runtime exception is thrown if the input is invalid.
public static String complement(String dna) {
final int n = dna.length();
char[] complement = new char[n];
for (int i = 0; i < n; i++) {
char comp = '\0';
switch (dna.charAt(i)) {
case 'A': comp = 'T'; break;
case 'T': comp = 'A'; break;
case 'G': comp = 'C'; break;
case 'C': comp = 'G'; break;
default:
throw new IllegalArgumentException("Invalid nucleotide detected at " + i + ": " + dna.charAt(i) + " in DNA: " + dna);
}
complement[i] = comp;
}
return new String(complement);
}
Test
String testData1 = "GCCTGTCGTAGCTTATC",
testData2 = "GGCTGACGTAGCGTAAC";
System.out.printf("%s <-- complement --> %s%n", testData1, complement(testData1));
System.out.printf("%s <-- complement --> %s%n", testData2, complement(testData2));
Output:
GCCTGTCGTAGCTTATC <-- complement --> CGGACAGCATCGAATAG
GGCTGACGTAGCGTAAC <-- complement --> CCGACTGCATCGCATTG
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.