Next version of JABA
[jabaws.git] / runner / compbio / pipeline / _jpred / BlastParser.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  *  \r
3  *  Jalview Web Services @version: 2.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 package compbio.pipeline._jpred;\r
19 import java.io.*;\r
20 import java.util.*;\r
21 \r
22 import javax.xml.stream.XMLInputFactory;\r
23 import javax.xml.stream.XMLStreamException;\r
24 import javax.xml.stream.XMLStreamReader;\r
25 \r
26 \r
27 public class BlastParser {\r
28         \r
29         static class Psiseq {\r
30                 String id;\r
31                 String seq;\r
32         }\r
33         /**\r
34          * args[0] is assumed to be the name of a Blast output file\r
35          * @throws XMLStreamException \r
36          * @throws FileNotFoundException \r
37          */\r
38         public static void main(String[] args) throws FileNotFoundException, XMLStreamException {\r
39                 XMLInputFactory f = XMLInputFactory.newInstance();\r
40                 XMLStreamReader r = f.createXMLStreamReader( new BufferedInputStream(new FileInputStream(new File(args[0]))));\r
41                 List<Psiseq> pl = new ArrayList<Psiseq>();\r
42                 Psiseq psi = null;\r
43                 while(r.hasNext()) {\r
44                         int idx = r.next(); \r
45                         //System.out.println(idx);\r
46 \r
47                         if(r.isStartElement()) {\r
48                                 String name = r.getLocalName();\r
49                                 if(name.equals("Hit") ) {\r
50                                         psi = new Psiseq();\r
51                                 }\r
52                                 if(name.equals("Hit_id") ) {\r
53                                         //System.out.println(r.getElementText());\r
54                                         psi.id = r.getElementText();\r
55                                         System.out.println(psi.id);\r
56                                 } \r
57                                 if(name.equals("Hsp_hseq")) {\r
58                                         psi.seq = r.getElementText();\r
59                                         System.out.println(psi.seq);\r
60                                 } \r
61                         }\r
62                         \r
63                         if(r.isEndElement()) {\r
64                                 String name = r.getLocalName();\r
65                                 if(name.equals("Hit") ) {\r
66                                         pl.add(psi);\r
67                                         psi = null;\r
68                                 }\r
69                         }\r
70 \r
71                 } \r
72 \r
73         }\r
74 }\r