[英]How to include a print statement from another method inside the class
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? 我的问题是如何更改代码,以使其打印出第一条打印行以及从testBuildCodonMap
方法打印的行? The print line that currently doesn't print out is System.out.println(s+"\\t"+codonMap.get(s));
当前未打印出的打印行是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
测试样本文件: CGTTCAAGTTCAA
EDIT: I want the output to look something like this: 编辑:我希望输出看起来像这样:
Reading frame starting with 0 results in 3 unique codons 以0开头的阅读框会产生3个唯一密码子
and most common codon is TCA with count 2 最常见的密码子是TCA,计数为2
Counts of codons between 1 and 5 inclusive are: 1至5(含)之间的密码子计数为:
CGT 1 CGT 1
TCA 2 三氯乙酸
AGT 1 AGT 1
Reading frame starting with 1 results in 2 unique codons 以1开头的阅读框会产生2个唯一的密码子
and most common codon is CAA with count 2 最常见的密码子是带有2的CAA
Counts of codons between 1 and 5 inclusive are: 1至5(含)之间的密码子计数为:
CAA 2 CAA 2
GTT 2 GTT 2
Reading frame starting with 2 results in 2 unique codons 以2开头的阅读框会产生2个唯一密码子
and most common codon is TTC with count 2 最常见的密码子是TTC,计数为2
Counts of codons between 1 and 5 inclusive are: 1至5(含)之间的密码子计数为:
TTC 2 TTC 2
AAG 1 AAG 1
I got it! 我知道了! The line printCodonCounts(4,8);
行printCodonCounts(4,8);
inside of the public void testBuildCodonMap()
method needs to be changed to be printCodonCounts(1,3);
内部public void testBuildCodonMap()
方法的内部需要更改为printCodonCounts(1,3);
. 。 That allows the print statement inside of method private void printCodonCounts(int start, int end)
to execute. 这样就可以执行方法private void printCodonCounts(int start, int end)
内部的print语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.