计算和显示频繁出现的字符串(Java)

时间:2014-01-22 22:54:17

标签: java

如何跟踪每个核碱基的最小和最大数量的DNA链并将这些链打印到输出中?

让我说我把它写进控制台(当我输入“end”时输出会显示):

A   
CC   
AATA   
GGG  
TTT  
end   

控制台应该产生如下输出:

A count: 4  
C count: 2  
G count: 3  
T count: 4  
Low A count: CC  
High A count: AATA  
Low C count: A  
High C count: CC  
Low G count: A  
High G count: GGG  
Low T count: A  
High T count: TTT  

这是我的代码:

package com.trouen;

import java.util.Scanner;

public class DNA {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);

        process(s);
    }

    public static void process(Scanner s) {

        int aCount = 0;
        int cCount = 0;
        int gCount = 0;
        int tCount = 0;

        String lowACount = null;
        String lowCCount = null;
        String lowGCount = null;
        String lowTCount = null;

        String highACount = null;
        String highCCount = null;
        String highGCount = null;
        String highTCount = null;

        String printout = null;

        while (s.hasNext()) {

            String input = s.next();

            for (int i = 0; i < input.length(); i++)
                if (input.charAt(i) == 'A') {
                    aCount++;
                } else if (input.charAt(i) == 'C') {
                    cCount++;
                    // lowCCount = input;
                } else if (input.charAt(i) == 'G') {
                    gCount++;
                    // lowGCount = input;
                } else if (input.charAt(i) == 'T') {
                    tCount++;
                    // lowTCount = input;
                }

            if (input.equalsIgnoreCase("end")) {
                break;
            }
        }

        s.close();

        System.out.println("A count: " + aCount);
        System.out.println("C count: " + cCount);
        System.out.println("G count: " + gCount);
        System.out.println("T count: " + tCount);
        System.out.println("Low A count: " + lowACount);
        System.out.println("High A count: " + highACount);
        System.out.println("Low C count: " + lowCCount);
        System.out.println("High C count: ");
        System.out.println("Low G count: " + lowGCount);
        System.out.println("High G count: ");
        System.out.println("Low T count: " + lowTCount);
        System.out.println("High T count: ");

    }
}

我似乎已经计算了字母数量,但我似乎无法弄清楚如何使程序整理出每个字母的低位和高位。我不允许使用任何类型的集合,OOP或数组。一切都应该在流程方法中完成。谁能帮我?

2 个答案:

答案 0 :(得分:0)

保持跟踪的一种可能方法是使用2个阵列。第一个数组是2维的。它应该有4行2列,反之亦然。对于这个例子,我将使用4列2行。每列代表A,C,T或G中的一个。然后该列的两行表示具有该核苷酸出现次数最多和最低的字符串。同样,制作另一个2x4数组并将计数值存储在该数组中。这样,您可以比较第二个数组中的值,并将它们存储在第一个数组中。

编辑:Pseudoish / Java示例代码

String[][] sequences = [4][2];
int[][] sequenceCounts = [4][2];

//put your looping code to read in and process each of the sequences here

int numA = findPattern(currentSequence, "A");
int numC = findPattern(currentSequence, "C");
int numG = findPattern(currentSequence, "G");
int numT = findPattern(currentSequence, "T");

if(/* check for whether the current sequence has less A/C/G/Ts than the previous minimum */)
{

}

if(/*check for whether the current sequence has more A/C/G/T than the previous maximum*/)
{

}

这应该让你开始(希望如此)。 findPattern方法是您必须编写的方法,即它不包含在Java中。我现在正准备去吃晚饭,所以我会在一两个小时后离开,但我会回来的!不要害怕!

答案 1 :(得分:0)

package com.trouen;

import java.util.Scanner;

public class DNA {

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);

    process(s);
}

public static void process(Scanner s) {
while (s.hasNext()) {
    String input = s.next();
if(input.equalsIgnoreCase("end")    
{
      break;
}
else
{
    ACount = ACount + findPattern(input,'A');
    CCount = CCount + findPattern(input,'C');
    GCount = GCount + findPattern(input,'G');
    TCount = TCount + findPattern(input,'T');

    if(c == 0)
    {
        lowA = ACount;
        highA = ACount;
        lowAVal = input;
        highAVal = input;

        lowC = CCount;
        highC = CCount;
        lowCVal = input;
        highCVal = input;

        lowG = GCount;
        highG = GCount;
        lowGVal = input;
        highGVal = input;

        lowT = TCount;
        highT = TCount;
        lowTVal = input;
        highTVal = input;
    }
    else
    {
        if(ACount < lowA)
        {
            lowAVal = input;
        }
        if(ACount > highA)
        {
            highAVal = input;
        }
        lowA = ACount;
        highA = ACount;

        if(CCount < lowC)
        {
            lowCVal = input;
        }
        if(CCount > highC)
        {
            highCVal = input;
        }
        lowC = CCount;
        highC = CCount;

        if(GCount < lowG)
        {
            lowGVal = input;
        }
        if(GCount > highG)
        {
            highGVal = input;
        }
        lowG = GCount;
        highG = GCount;

        if(TCount < lowT)
        {
            lowTVal = input;
        }
        if(TCount > highT)
        {
            highTVal = input;
        }
        lowT = TCount;
        highT = TCount;
    }
}
}
s.close();

    System.out.println("A count: " + ACount);
    System.out.println("C count: " + CCount);
    System.out.println("G count: " + GCount);
    System.out.println("T count: " + TCount);
    System.out.println("Low A count: " + lowAVal);
    System.out.println("High A count: " + highAVal);
    System.out.println("Low C count: " + lowCVal);
    System.out.println("High C count: "+highCVal);
    System.out.println("Low G count: " + lowGVal);
    System.out.println("High G count: "+highGVal);
    System.out.println("Low T count: " + lowTVal);
    System.out.println("High T count: "+highTVal);

}

public int findPattern(String strValue,char checkChar)throws Exception
   {
       int count = 0;
       for(int i=0;i<strValue.length();i++)
       {
           if(strValue.charAt(i) == checkChar)
           {
               count++;
           }
       }
       return count;
   }
}
you can use this to get low and high count.