B. L. B. L. - 6 days ago 5
Java Question

Java Code not printing Keys and Values of HashMap

My code inputs a user-defined string and categorizes it as DNA, RNA, or ODD. The output is supposed to print the keys and values of the HashMap nucleotide_hmap but the code currently does not. What should I change in my code so that it works?

Here is what the code should output:
(if user inputs sequence ATGC)


  • ATGC

  • A 1 : 0.25

  • T 1 : 0.25

  • C 1 : 0.25

  • G 1 : 0.25

  • Sequence Length: 4

  • Reverse sequence: CGTA

  • Reverse complement: GCAT



This is my controller class:

import sequenceclasses.*;
import java.util.Scanner;

public class SeqAssign {
private static Scanner user_input;
public static void main(String[] args){
user_input = new Scanner(System.in);
String seq1;
System.out.print("Enter your sequence: ");
seq1 = user_input.next().toUpperCase();
System.out.println(seq1);

if (seq1.matches("[ATCG]+")) {
DNASequence dna_sequence = new DNASequence(seq1);
dna_sequence.lengthseq(seq1);
dna_sequence.nucleotidecontent(seq1);
dna_sequence.reverse(seq1, true);
dna_sequence.reversecomplement(seq1);
}
else if (seq1.matches("[AUGC]+")) {
RNASequence rna_sequence = new RNASequence(seq1);
rna_sequence.lengthseq(seq1);
rna_sequence.nucleotidecontent(seq1);
rna_sequence.reverse(seq1, true);
rna_sequence.reversecomplement(seq1);
}
else if (seq1.matches("[ATGCPQ]+")) {
ODDSequence odd_sequence = new ODDSequence(seq1);
odd_sequence.lengthseq(seq1);
odd_sequence.nucleotidecontent(seq1);
odd_sequence.reverse(seq1, true);
odd_sequence.reversecomplement(seq1);
}
else {
System.out.println("Error, unexpected base found! Please enter a DNA, RNA, or ODD sequence.");
}
}
}


This is my superclass with nucleotide_hmap:

package sequenceclasses;

import java.util.*;

public class MainSequence {
protected HashMap<Character, Character> complement_hmap = new HashMap<Character, Character>();
protected HashMap<Character, String> nucleotide_hmap = new HashMap<Character, String>();
protected String the_sequence;
private String rev_seq;

public Integer lengthseq(String seq1){
Integer seq_length = seq1.length();
System.out.println("Sequence Length : " + seq_length);
return seq_length;
}

public void nucleotidecontent(String seq1){
for (Map.Entry<Character, String> entry : nucleotide_hmap.entrySet()){
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}


public String reverse(String seq1, Boolean boo) {
String rev = "";
for (int i = 0; i < seq1.length(); i++){
rev = seq1.charAt(i) + rev;
}
if (boo == true){
System.out.println("Reverse Sequence : " + rev);
}
return rev;
}


public StringBuffer reversecomplement(String seq1){
StringBuffer rev_comp = new StringBuffer();
rev_seq = reverse(seq1, false);
for(int i = 0; i < seq1.length(); i++){
char base = rev_seq.charAt(i);
Character comp = complement_hmap.get(base);
rev_comp.append(comp);
}
rev_comp.toString();
System.out.println("Reverse Complement Sequence : " + rev_comp);
return rev_comp;

}

}


And this is one of my subclasses:

package sequenceclasses;

public class DNASequence extends MainSequence {
public DNASequence(String dna_sequence){
the_sequence = dna_sequence;
complement_hmap.put('A', 'T');
complement_hmap.put('T', 'A');
complement_hmap.put('C', 'G');
complement_hmap.put('G', 'C');
}
public void nuc_content(String dna_sequence){
the_sequence = dna_sequence;
double[] counts_ratios = new double[8];
for (int i = 0; i < dna_sequence.length(); i++){
if (dna_sequence.charAt(i) == 'A'){
counts_ratios[0] += 1;}
if (dna_sequence.charAt(i) == 'T'){
counts_ratios[1] += 1;}
if (dna_sequence.charAt(i) == 'C'){
counts_ratios[2] += 1;}
if (dna_sequence.charAt(i) == 'G'){
counts_ratios[3] += 1;}
}
if (dna_sequence.length() > 0){
counts_ratios[4] = counts_ratios[0] / dna_sequence.length();
counts_ratios[5] = counts_ratios[1] / dna_sequence.length();
counts_ratios[6] = counts_ratios[2] / dna_sequence.length();
counts_ratios[7] = counts_ratios[3] / dna_sequence.length();
}
String A_content = Double.toString(counts_ratios[0]) + " , " + Double.toString(counts_ratios[4]);
String T_content = Double.toString(counts_ratios[1]) + " , " + Double.toString(counts_ratios[5]);
String C_content = Double.toString(counts_ratios[2]) + " , " + Double.toString(counts_ratios[6]);
String G_content = Double.toString(counts_ratios[3]) + " , " + Double.toString(counts_ratios[7]);

nucleotide_hmap.put('A', A_content);
nucleotide_hmap.put('T', T_content);
nucleotide_hmap.put('C', C_content);
nucleotide_hmap.put('G', G_content);
}
}

Answer

You never call the nuc_content method in the DNASequence class. To solve this, add the line nuc_content(dna_sequence); to the DNASequence class. The constructor should now look like this:

public DNASequence(String dna_sequence){
        the_sequence = dna_sequence; 
        complement_hmap.put('A', 'T');
        complement_hmap.put('T', 'A');
        complement_hmap.put('C', 'G');
        complement_hmap.put('G', 'C');
        nuc_content(dna_sequence);
    }