Java 提取DNA并打印出蛋白质区域

Java 提取DNA并打印出蛋白质区域,java,Java,我应该在读一个fasta文件中的DNA字符串,到目前为止我一直在工作。然而,我试图只打印出以ATG开头,以TAA、TGA或TAG结尾的段,但它只是打印出文件中的整行。有没有办法打印出行中较小的段?这是我到目前为止对代码的了解: import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class DNAApp { public static void main(Str

我应该在读一个fasta文件中的DNA字符串,到目前为止我一直在工作。然而,我试图只打印出以ATG开头,以TAA、TGA或TAG结尾的段,但它只是打印出文件中的整行。有没有办法打印出行中较小的段?这是我到目前为止对代码的了解:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class DNAApp
{
 public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);
    System.out.println("Please enter the full path to the fasta file.");
    String input = in.nextLine();
    File file = new File(input);

    try
    {
        Scanner fileScan = new Scanner( file );

        while ( fileScan.hasNextLine() )
        {
            String line=fileScan.nextLine();
            line.replaceAll("X", "A");
            if(line.contains("ATG"))
            {
                line.substring(0 + 3);
                System.out.println(line);
            }
        }
    }
    catch ( FileNotFoundException fnfe )
    {
        fnfe.printStackTrace();
    }

 }
}
这是我得到的输出:

Please enter the full path to the fasta file.
C:\Users\Downloads\test.fasta.txt
AACGACGGCGTGCATGCTTGACGXXXXXXXXXXXXXXXXXXXXXXXXXXA
CTACAAATCAATGAGCCACCCACTTCAATCGTCAGGAGCATCCTCAGGAA
CGCTACCTTTTGAAAATGATGCATTGAAAAAGGAAATTGCTTTGTACAAG
TCAACTCCTTTAATTTCAAAAAATGATTTAGACTCCTTTGATTTGCTTAA
TAACGTGGATATGACTATCTTCATTGCCTTTAATACTCAAGGTCAAGGAA
GTGTATGGCTTTGAATACTTGGATAAAATCCAATTTGAATTTACTTTCCT
TAAATACAAGAAAGAATGCCAAGAAA

Process finished with exit code 0

任何帮助都将不胜感激。

这应该行得通。应该有更好的方法找出三个大于-1的索引中的最低索引,而不是将它们放入数组中进行比较。请尝试此代码。你的台词没有一句是以TGA开头的。因此,您可能希望在开始测试之前更改它。我在测试时修改了dna文件

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class DNAApp {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Please enter the full path to the fasta file.");
    String input = in.nextLine();
    File file = new File(input);

    try {
        Scanner fileScan = new Scanner(file);
        while (fileScan.hasNextLine()) {
            String line = fileScan.nextLine();
            line = line.replaceAll("X", "A");
            if (line.startsWith("ATG")) {
                int[] dnaArray = new int[3];
                //Get the lowest index out of TAA, TGA, or TAG
                dnaArray[0] = line.indexOf("TAA",3);
                dnaArray[1] = line.indexOf("TGA",3);
                dnaArray[2] = line.indexOf("TAG",3);
                //We cannot use Math.min, because it will consider -1 as the lowest if it does not find a string with the index passed to it.
                int lowestIndex = Integer.MAX_VALUE;
                //We try to find the lowest of all indexes.
                for(int index : dnaArray) {
                    //Check only if the current index is greater than -1 and less than the current lowest index.
                    if(index > -1 && index < lowestIndex) {
                        lowestIndex = index;
                    }
                }
                //Do a substring from the line only if lowestIndex is not equal to the Integer.MAX_VALUE (intial value set
                if(lowestIndex != Integer.MAX_VALUE) {
                    line = line.substring(0,lowestIndex + 3);
                }
                System.out.println(line);
            }
        }
    } catch (FileNotFoundException fnfe) {
        fnfe.printStackTrace();
    }

}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.Scanner;
公共类DNAApp{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
System.out.println(“请输入fasta文件的完整路径”);
字符串输入=in.nextLine();
文件=新文件(输入);
试一试{
Scanner fileScan=新扫描仪(文件);
while(fileScan.hasNextLine()){
String line=fileScan.nextLine();
行=行。替换所有(“X”、“A”);
if(行起始带(“ATG”)){
int[]dnaArray=新int[3];
//从TAA、TGA或TAG中获取最低索引
dnaArray[0]=行索引(“TAA”,3);
dnaArray[1]=行索引(“TGA”,3);
dnaArray[2]=第行indexOf(“TAG”,3);
//我们不能使用Mth.min,因为如果它没有找到索引传递给它的字符串,那么它将被认为是最低的。
int lowestinex=整数的最大值;
//我们试图找到所有索引中最低的。
for(int索引:dnaArray){
//仅当当前索引大于-1且小于当前最低索引时才选中。
如果(索引>-1&&index

}

这应该行得通。应该有更好的方法找出大于-1的三个索引中的最低索引,而不是将它们放入数组并进行比较。请尝试此代码。您的所有行实际上都不是以TGA开头的。因此,您可能希望在开始测试之前更改它。我在测试时修改了dna文件

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class DNAApp {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Please enter the full path to the fasta file.");
    String input = in.nextLine();
    File file = new File(input);

    try {
        Scanner fileScan = new Scanner(file);
        while (fileScan.hasNextLine()) {
            String line = fileScan.nextLine();
            line = line.replaceAll("X", "A");
            if (line.startsWith("ATG")) {
                int[] dnaArray = new int[3];
                //Get the lowest index out of TAA, TGA, or TAG
                dnaArray[0] = line.indexOf("TAA",3);
                dnaArray[1] = line.indexOf("TGA",3);
                dnaArray[2] = line.indexOf("TAG",3);
                //We cannot use Math.min, because it will consider -1 as the lowest if it does not find a string with the index passed to it.
                int lowestIndex = Integer.MAX_VALUE;
                //We try to find the lowest of all indexes.
                for(int index : dnaArray) {
                    //Check only if the current index is greater than -1 and less than the current lowest index.
                    if(index > -1 && index < lowestIndex) {
                        lowestIndex = index;
                    }
                }
                //Do a substring from the line only if lowestIndex is not equal to the Integer.MAX_VALUE (intial value set
                if(lowestIndex != Integer.MAX_VALUE) {
                    line = line.substring(0,lowestIndex + 3);
                }
                System.out.println(line);
            }
        }
    } catch (FileNotFoundException fnfe) {
        fnfe.printStackTrace();
    }

}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.Scanner;
公共类DNAApp{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
System.out.println(“请输入fasta文件的完整路径”);
字符串输入=in.nextLine();
文件=新文件(输入);
试一试{
Scanner fileScan=新扫描仪(文件);
while(fileScan.hasNextLine()){
String line=fileScan.nextLine();
行=行。替换所有(“X”、“A”);
if(行起始带(“ATG”)){
int[]dnaArray=新int[3];
//从TAA、TGA或TAG中获取最低索引
dnaArray[0]=行索引(“TAA”,3);
dnaArray[1]=行索引(“TGA”,3);
dnaArray[2]=第行indexOf(“TAG”,3);
//我们不能使用Mth.min,因为如果它没有找到索引传递给它的字符串,那么它将被认为是最低的。
int lowestinex=整数的最大值;
//我们试图找到所有索引中最低的。
for(int索引:dnaArray){
//仅当当前索引大于-1且小于当前最低索引时才选中。
如果(索引>-1&&index

}

字符串是不可变的。
line.substring(0+3);
不执行任何操作,因为您忽略了返回值。查找ATG的索引。从该点查找TAA、TGA和标记的索引。然后从三个索引中选取索引号最低的索引,不包括-1。从那里使用两个索引创建
子字符串(firstIndex,LowerIndex of Three+3);
字符串是不可变的。
line.substring(0+3)
什么也不做,因为您忽略了返回值。找到ATG的索引。从这一点上找到TAA、TGA和标记的索引。然后从三个索引中取索引号最低的索引,但不包括-1。从那里使用这两个索引创建一个子字符串(firstIndex,LowestIndexOfThree+3);