2011-03-25 181 views
-3

您好我已經編寫了一個Java程序來從文件中獲取分子功能和生物過程,如果ID匹配但我即將開始StringIndexOutofBoundsException。 可以任何一個請更正嗎?提前致謝。 這是我輸入:從文件中提取字符串

chr11 RAP3_rep mRNA 17114958 17117968 . + . ID=Os11t0448200-01;Name=Os11t0448200-01;Gene_symbols=AM14;GO=Molecular Function: protein kinase activity (GO:0004672),Molecular Function: ATP binding (GO:0005524),Biological Process: protein amino acid phosphorylation (GO:0006468),Molecular Function: protein tyrosine kinase activity (GO:0004713),Molecular Function: protein serine/threonine kinase activity (GO:0004674);ID_converter=Os11g0448200;InterPro=Protein kinase, core (IPR000719),Tyrosine protein kinase (IPR001245),Serine/threonine protein kinase (IPR002290),Serine/threonine protein kinase, active site (IPR008271),Protein kinase-like (IPR011009),Serine/threonine protein kinase-related (IPR017442);Link_to=8185 (Oryzabase),Protein kinase%2C core (Plant Gene Family Database);Locus_id=Os11g0448200;Note=Arbuscular mycorrhizal specific marker 14.;ORF_evidence=Q53JE9 (UniProt);Transcript_evidence=Inferred from reference;Sequence_download=Os11t0448200-01;References=19033527%2C 15905328;Status=manual curation (Oct 29%2C 2010) 
chr11 RAP3_rep CDS 17114958 17115039 . + . Parent=Os11t0448200-01 
chr11 RAP3_rep CDS 17115846 17115869 . + . Parent=Os11t0448200-01 
chr11 RAP3_rep CDS 17115970 17116095 . + . Parent=Os11t0448200-01 
chr11 RAP3_rep CDS 17116205 17116546 . + . Parent=Os11t0448200-01 
chr11 RAP3_rep CDS 17116669 17116784 . + . Parent=Os11t0448200-01 
chr11 RAP3_rep CDS 17116880 17117140 . + . Parent=Os11t0448200-01 
chr11 RAP3_rep CDS 17117589 17117786 . + . Parent=Os11t0448200-01 
chr11 RAP3_rep CDS 17117891 17117968 . + . Parent=Os11t0448200-01 
chr11 RAP3_rep mRNA 17565866 17568694 . - . ID=Os11t0455500-01;Name=Os11t0455500-01;Alias=AK059712,AK060299,AK119539,AK122115;ID_converter=Os11g0455500;Link_to=S-adenosyl-L-homocysteine hydrolase (Plant Gene Family Database);Locus_id=Os11g0455500;NIAS_FLcDNA=001-032-F05;Note=Similar to Adenosylhomocysteinase-like protein.;ORF_evidence=Q84VE1 (UniProt);Transcript_evidence=AK059712 (DDBJ%2C Best hit);Sequence_download=Os11t0455500-01;InterPro=NAD(P)-binding (IPR016040),S-adenosyl-L-homocysteine hydrolase (IPR000043),S-adenosyl-L-homocysteine hydrolase%2C NAD binding (IPR015878);GO=Molecular Function: catalytic activity (GO:0003824),Molecular Function: binding (GO:0005488),Biological Process: metabolic process (GO:0008152),Molecular Function: adenosylhomocysteinase activity (GO:0004013),Biological Process: one-carbon compound metabolic process (GO:0006730);Expression=AK059712 
chr11 RAP3_rep CDS 17567891 17568694 . - . Parent=Os11t0455500-01; 
chr11 RAP3_rep CDS 17566493 17567029 . - . Parent=Os11t0455500-01; 
chr11 RAP3_rep CDS 17566191 17566400 . - . Parent=Os11t0455500-01; 

和程序

import java.io.BufferedReader; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.InputStreamReader; 
import java.io.ObjectInputStream.GetField; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class Sample 
{ 
public static void main(String args[]) throws FileNotFoundException 
{ 
    Sample s=new Sample(); 
    String inputID="Os11t0120200-01"; 

    //System.out.println("Enter the value"); 
    //Scanner sc=new Scanner(System.in); 
    //n=sc.nextLong(); 

    ArrayList<String> IDlist=new ArrayList<String>(); 
    ArrayList<String> InputIDlist=new ArrayList<String>(); 
    int n; 
    try 
    { 
    File nf=new File("textfile1.txt"); 
    FileOutputStream fop1=new FileOutputStream(nf,true); 
    String os =""; 

    FileInputStream fis1=new FileInputStream("chr11.gb"); 
    FileInputStream fis2=new FileInputStream("1.txt"); 
    InputStreamReader in1 = new InputStreamReader(fis1, "UTF-8"); 
    InputStreamReader in2 = new InputStreamReader(fis2, "UTF-8"); 
    BufferedReader input1 = new BufferedReader(in1); 
    BufferedReader input2 = new BufferedReader(in2); 

    String line1; 
    String line2; 

    FileInputStream fis=new FileInputStream("chr11.GB"); 
    InputStreamReader in = new InputStreamReader(fis, "UTF-8"); 
    BufferedReader input = new BufferedReader(in); 
    String line; 

    File f=new File("1.GB"); 
    FileOutputStream fop=new FileOutputStream(f); 

    if(f.exists()) 
    { 
     os="This data is written through the program\t\n"; 
     fop1.write(os.getBytes()); 

     String str1=""; 
     String str2=""; 
     os="The data has been written\t\n"; 
     fop1.write(os.getBytes()); 

     while((line=input.readLine())!=null) 
     { 
      String splits[]=line.split("\t"); 
      if(splits[2].equalsIgnoreCase("mrna")) 
      { 
      IDlist.add((splits[8])); 
      } 
     } 

     while((line=input2.readLine())!=null) 
     { 
      String splits[]=line.split("\t"); 
      if(splits[0]!="") 
      { 
      InputIDlist.add((splits[0])); 
      } 
     } 
     for(int j=0; j<InputIDlist.size(); j++) 
     { 
      for(int i=0; i<IDlist.size(); i++) 
      { 
      if((IDlist.get(i).substring(3, 18).toString()).equals(InputIDlist.get(j))) 
      { 
       if(IDlist.get(i).contains("Alias")) 
       { 
       os=IDlist.get(i).substring(IDlist.get(i).lastIndexOf("Alias"),IDlist.get(i).lastIndexOf("ID_converter"))+"\t\n"; 
       fop1.write(os.getBytes()); 
       } 
       if(IDlist.get(i).contains("Biological Process")) 
       { 
       //n=IDlist.get(i).lastIndexOf("Biological Process"); 
       os=IDlist.get(i).substring(IDlist.get(i).lastIndexOf("Biological Process"),IDlist.get(i).lastIndexOf(";"))+"\t\n"; 
       fop1.write(os.getBytes()); 
       } 
       if(IDlist.get(i).contains("Molecular Function")) 
       { 
       //n=IDlist.get(i).lastIndexOf("Molecular Function"); 
       os=IDlist.get(i).substring(IDlist.get(i).lastIndexOf("Molecular Function"), IDlist.get(i).lastIndexOf(","))+"\t\n"; 
       fop1.write(os.getBytes()); 
       } 
       break; 
      } 
      String p="\n"; 
      fop1.write(p.getBytes()); 
      } 
     } 
    } 
    else 
    { 
     System.out.println("This file is not exist"); 
    } 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 
} 
+4

你已經發布了大量的數據和大量的代碼,但並沒有說明你在哪裏得到異常,無論是閱讀哪行或者代碼中發生了什麼。代碼有各種各樣的錯誤(首先,我從不會看到關閉任何東西,並且使用默認編碼寫入文件,這很少是個好主意 - 使用OutputStreamWriter用適當的編碼代替),但是很難確定真正有用的信息,哪些事情失敗。 – 2011-03-25 07:06:34

+0

* javascript *在哪裏涉及? – asgs 2011-03-25 07:06:42

+0

程序是否不告訴你引發異常的代碼的LINE NUMBER? – Paul 2011-03-25 07:10:31

回答

2

我對這個問題的意見同意,但我還是會嘗試猜測:

最有可能的,這是以下文件(由於字符串 IndexOutOfBoundsException):IDlist.get(i).substring(3, 18)。如果這個更短,你會得到這個例外。

這樣做的一個原因可能是這一部分:

if(splits[0]!="") 
{ 
    InputIDlist.add((splits[0])); 
} 

如果splits[0]是空的,==可能仍然不是真的(因此!=可能是真的)。在這裏使用!splits[0].equals("")(或更好的!"".equals(splits[0])來說明splits[0]可能永遠爲空的可能性)。請注意,==檢查引用相等性,即兩個引用是否指向同一對象(就C++而言,它是否是相同的指針),而equals檢查邏輯相等性(可能會針對每個對象執行不同的操作)。

編輯:

該異常另一種可能性是那些行之一:

os=IDlist.get(i).substring(IDlist.get(i).lastIndexOf("Alias"),IDlist.get(i).lastIndexOf("ID_converter")) 

您檢查 「別名」,所以lastIndexOf("Alias")不應該返回-1,但IDlist.get(i).lastIndexOf("ID_converter")威力。如果是這樣,你是超越界限的。

編輯2:

還有另一件事:即使兩個字符串(「別名」和「ID_converter」)是源字符串中,但在錯誤的順序(「ID_converter ....別名」),你也會得到這個異常,因爲begin index > end index這是不允許的(請閱讀String.substring()上的JavaDoc)。

+0

根據情況給出合理的答案 – phooji 2011-03-25 07:39:01

+0

而不是'!string.equals(「」)'或'!「」。equals(string)'我建議使用更好的可讀版本'string.length()> 0 '。 – Mot 2011-03-25 08:09:32

+0

好吧,如果'string'爲null,'string.length()'可能導致NPE。如果apache commons lang是一個選項,'StringUtils.isNotEmpty(string)'會更好(因爲它返回null和「」),甚至是'StringUtils.isNotBlank(string)'(返回false爲null,「」和任何只包含空格的字符串)。 – Thomas 2011-03-25 08:59:03

1

變化:

if (IDlist.get(i).contains("Alias")) 

要:

if ((IDlist.get(i).contains("Alias")) && (IDlist.get(i).contains("ID_converter"))) 

任何設置一個斷點來檢查爲什麼是第二個條件是假的,如果它不進去if語句即可。

+0

'和'不是Java關鍵字,而是使用'&&'。 – Thomas 2011-03-25 08:59:49

+0

對!感謝您的糾正 – CloudyMarble 2011-03-25 09:03:20