added unofficial support for concise output parsing from the JNet server to extract...
[jalview.git] / src / jalview / io / IdentifyFile.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4 *\r
5 * This program is free software; you can redistribute it and/or\r
6 * modify it under the terms of the GNU General Public License\r
7 * as published by the Free Software Foundation; either version 2\r
8 * of the License, or (at your option) any later version.\r
9 *\r
10 * This program is distributed in the hope that it will be useful,\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 * GNU General Public License for more details.\r
14 *\r
15 * You should have received a copy of the GNU General Public License\r
16 * along with this program; if not, write to the Free Software\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18 */\r
19 package jalview.io;\r
20 \r
21 import java.io.*;\r
22 \r
23 import java.net.*;\r
24 \r
25 \r
26 /**\r
27  * DOCUMENT ME!\r
28  *\r
29  * @author $author$\r
30  * @version $Revision$\r
31  */\r
32 public class IdentifyFile\r
33 {\r
34     /**\r
35      * DOCUMENT ME!\r
36      *\r
37      * @param file DOCUMENT ME!\r
38      * @param protocol DOCUMENT ME!\r
39      *\r
40      * @return DOCUMENT ME!\r
41      */\r
42     public String Identify(String file, String protocol)\r
43     {\r
44         String reply = "PFAM";\r
45         String error =  "FILE NOT FOUND";\r
46         try\r
47         {\r
48             BufferedReader reader = null;\r
49 \r
50             if (protocol.equals(AppletFormatAdapter.FILE))\r
51             {\r
52               reader = new BufferedReader(new FileReader(file));\r
53             }\r
54             else if (protocol.equals(AppletFormatAdapter.URL))\r
55             {\r
56               error = "URL NOT FOUND";\r
57               URL url = new URL(file);\r
58               reader = new BufferedReader(new InputStreamReader(\r
59                   url.openStream()));\r
60 \r
61             }\r
62             else if (protocol.equals(AppletFormatAdapter.PASTE))\r
63             {\r
64               reader = new BufferedReader(new StringReader(file));\r
65             }\r
66             else if (protocol.equals(AppletFormatAdapter.CLASSLOADER))\r
67             {\r
68               java.io.InputStream is = getClass().getResourceAsStream("/" +\r
69                   file);\r
70               reader = new BufferedReader(new java.io.InputStreamReader(is));\r
71             }\r
72 \r
73             String data;\r
74 \r
75             while ((data = reader.readLine()) != null)\r
76             {\r
77                 data = data.toUpperCase();\r
78 \r
79                 if ((data.indexOf("#") == 0) || (data.length() < 1))\r
80                 {\r
81                     continue;\r
82                 }\r
83 \r
84                 if (data.indexOf("PILEUP") > -1)\r
85                 {\r
86                     reply = "PileUp";\r
87 \r
88                     break;\r
89                 }\r
90 \r
91                 if ((data.indexOf("//") == 0) ||\r
92                         ((data.indexOf("!!") > -1) &&\r
93                         (data.indexOf("!!") < data.indexOf(\r
94                             "_MULTIPLE_ALIGNMENT "))))\r
95                 {\r
96                     reply = "MSF";\r
97 \r
98                     break;\r
99                 }\r
100                 else if (data.indexOf("CLUSTAL") > -1)\r
101                 {\r
102                     reply = "CLUSTAL";\r
103 \r
104                     break;\r
105                 }\r
106                 else if ((data.indexOf(">P1;") > -1) ||\r
107                         (data.indexOf(">DL;") > -1))\r
108                 {\r
109                     reply = "PIR";\r
110 \r
111                     break;\r
112                 }\r
113                 else if (data.indexOf(">") > -1)\r
114                 {\r
115                     // could be BLC file, read next line to confirm\r
116                     data = reader.readLine();\r
117 \r
118                     if (data.indexOf(">") > -1 || data.indexOf("*") >-1 )\r
119                     {\r
120                         reply = "BLC";\r
121                     }\r
122                     else\r
123                     {\r
124                         reply = "FASTA";\r
125                     }\r
126 \r
127                     break;\r
128                 }\r
129                 else if (data.indexOf(":")<data.indexOf(",")) //  && data.indexOf(",")<data.indexOf(",", data.indexOf(",")))\r
130                 {\r
131                   // file looks like a concise JNet file\r
132                   reply="JnetFile";\r
133                   break;\r
134                 }\r
135             }\r
136 \r
137             reader.close();\r
138         }\r
139         catch (Exception ex)\r
140         {\r
141             System.err.println("File Identification failed!\n" + ex);\r
142             return error;\r
143         }\r
144         return reply;\r
145     }\r
146 }\r