1

My question is how do I change my code so that it prints out my first print line along with the lines printed from the testBuildCodonMap method? The print line that currently doesn't print out is System.out.println(s+"\t"+codonMap.get(s));. I want this to be included with the other print statements.

import java.util.*;
import edu.duke.*;

public class CodonCount {
    private HashMap<String,Integer> codonMap;
    public CodonCount() {
        codonMap = new HashMap<String,Integer>();
    }
    private void buildCodonMap(int start, String dna) {
        //clear out map before building
        codonMap.clear();
        //This method will build a new map of codons mapped to 
        //their counts from the string dna with the reading frame
        //with the position start (a value of 0, 1, or 2).
        for (int index=start; dna.length() - index > 3;index+=3) {
            String currentCodon = dna.substring(index,index+3);
            if (!codonMap.containsKey(currentCodon)) {
                codonMap.put(currentCodon,1);
            }
            else {
                codonMap.put(currentCodon,codonMap.get(currentCodon)+1);
            }
        }
    }
    private String getMostCommonCodon() {
        //get the codon in a reading frame that has the largest count
        //this method assumes the HashMap of codons to counts has already been built
        int currentHigh = 0;
        String mostCommonCodon = "";
        for (String s : codonMap.keySet()) {
            int currentCount = codonMap.get(s);
            if (currentCount > currentHigh) {
                mostCommonCodon = s;
                currentHigh = currentCount;
            }
        }
        return mostCommonCodon;
    }
    private void printCodonCounts(int start, int end) {
        //This method prints all the codons in the HashMap along with their
        //counts if their count is between start and end, inclusive.
        for (String s : codonMap.keySet()) {
            if (codonMap.get(s) >= start && codonMap.get(s) <= end) {
                System.out.println(s+"\t"+codonMap.get(s));
            }
        }
    }
    public void testBuildCodonMap() {
        FileResource fileResource = new FileResource();
        String dna = fileResource.asString();
        dna = dna.toUpperCase();

        for (int index=0;index <= 2;index++) {
            System.out.println("\nTesting with start position "+index+":\n");
            buildCodonMap(index,dna);
            String mostCommonCodon = getMostCommonCodon();
            System.out.println("Total unique codons found: "+codonMap.size());
            System.out.println("\nMost common codon: "+mostCommonCodon
                                +"\t"+codonMap.get(mostCommonCodon));
            printCodonCounts(4,8);
        }
    }
}

Sample file for testing: CGTTCAAGTTCAA

EDIT: I want the output to look something like this:

Reading frame starting with 0 results in 3 unique codons

and most common codon is TCA with count 2

Counts of codons between 1 and 5 inclusive are:

CGT 1

TCA 2

AGT 1

Reading frame starting with 1 results in 2 unique codons

and most common codon is CAA with count 2

Counts of codons between 1 and 5 inclusive are:

CAA 2

GTT 2

Reading frame starting with 2 results in 2 unique codons

and most common codon is TTC with count 2

Counts of codons between 1 and 5 inclusive are:

TTC 2

AAG 1

Bri Jackson
  • 35
  • 1
  • 9
  • 1
    Did you run over the code with a debugger to see why it doesn't print? – user1803551 Jan 13 '16 at 23:27
  • After your edit I suggest you post an [MCVE](http://stackoverflow.com/help/mcve). – user1803551 Jan 13 '16 at 23:30
  • @user1803551 I did, and CTG came up as one of the codons being calculated. But I found nothing else. – Bri Jackson Jan 13 '16 at 23:33
  • Edited question to include MCVE. – Bri Jackson Jan 13 '16 at 23:34
  • 1
    Are you calling `printCodonCounts` anywhere else in your code? The error does state that you're calling it somewhere, and that it requires two ints, and somewhere you called it as `printCodonCounts()`. You have shown you have called it once in the provided code, and it was done correctly. – Drew Kennedy Jan 13 '16 at 23:35
  • That's not an MCVE, we can't copy-paste your code and run it in our IDEs to see the problem. – user1803551 Jan 13 '16 at 23:36
  • Are you sure? I've included all of the code. Is there something missing I'm not seeing? – Bri Jackson Jan 13 '16 at 23:38
  • Show your `main()` method in a separate code block please. – Drew Kennedy Jan 13 '16 at 23:39
  • It's missing a `main` and `FileResource` in undefined. I suggest you supply a sample input. – user1803551 Jan 13 '16 at 23:41
  • I'm using BlueJ so we don't have to use a `main()` method. I think the `main()` would be `testBuildCodonMap()` in another IDE. – Bri Jackson Jan 13 '16 at 23:42
  • Well the code provided wouldn't cause this exception. Take a look at [this question](http://stackoverflow.com/questions/22813484/java-error-actual-and-formal-argument-lists-differ-in-length). This is what causes the problem you're having. – Drew Kennedy Jan 13 '16 at 23:44
  • I tested your code, it throws no error. I get this output: `Testing with start position 0: Total unique codons found: 3 Most common codon: TCA 2 Testing with start position 1: Total unique codons found: 2 Most common codon: GTT 2 Testing with start position 2: Total unique codons found: 2 Most common codon: TTC 2` – user1803551 Jan 13 '16 at 23:46
  • Yes, that's true. But I wanted the line inside `private void printCodonCounts(int start, int end)` to be included. It doesn't seem to be printing out. – Bri Jackson Jan 13 '16 at 23:48
  • if you debug it you will see that the `if` condition is never `true`, so of course it won't print anything. Besides, is your question about some error or about printing? – user1803551 Jan 13 '16 at 23:50
  • It is more about printing the right output. Sorry for the confusion. – Bri Jackson Jan 13 '16 at 23:52
  • The output you posted is nothing like the prints you have in your code. I suggest you formulate a clear question with information relevant to the code you posted and add an explanation *after using a debugger* of what you need help with. – user1803551 Jan 13 '16 at 23:57

1 Answers1

1

I got it! The line printCodonCounts(4,8); inside of the public void testBuildCodonMap() method needs to be changed to be printCodonCounts(1,3);. That allows the print statement inside of method private void printCodonCounts(int start, int end) to execute.

Bri Jackson
  • 35
  • 1
  • 9