ensure lastSeq is refreshed if new feature added
[jalview.git] / src / jalview / io / IdentifyFile.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2006 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("# STOCKHOLM") > -1))\r
80                 {\r
81                   reply = "STH";\r
82 \r
83                   break;\r
84                 }\r
85 \r
86                 if ((data.indexOf("#") == 0) || (data.length() < 1))\r
87                 {\r
88                     continue;\r
89                 }\r
90 \r
91                 if (data.indexOf("PILEUP") > -1)\r
92                 {\r
93                     reply = "PileUp";\r
94 \r
95                     break;\r
96                 }\r
97 \r
98                 if ((data.indexOf("//") == 0) ||\r
99                         ((data.indexOf("!!") > -1) &&\r
100                         (data.indexOf("!!") < data.indexOf(\r
101                             "_MULTIPLE_ALIGNMENT "))))\r
102                 {\r
103                     reply = "MSF";\r
104 \r
105                     break;\r
106                 }\r
107                 else if (data.indexOf("CLUSTAL") > -1)\r
108                 {\r
109                     reply = "CLUSTAL";\r
110 \r
111                     break;\r
112                 }\r
113                 else if ((data.indexOf(">P1;") > -1) ||\r
114                         (data.indexOf(">DL;") > -1))\r
115                 {\r
116                     reply = "PIR";\r
117 \r
118                     break;\r
119                 }\r
120                 else if (data.indexOf(">") > -1)\r
121                 {\r
122                     // could be BLC file, read next line to confirm\r
123                     data = reader.readLine();\r
124 \r
125                     if (data.indexOf(">") > -1)\r
126                     {\r
127                         reply = "BLC";\r
128                     }\r
129                     else\r
130                     {\r
131                       //Is this a single line BLC file?\r
132                       reader.readLine();\r
133                       String data2 = reader.readLine();\r
134                       if ( data2 != null\r
135                            && data.indexOf("*")>-1\r
136                            && data.indexOf("*")==data2.indexOf("*"))\r
137                       {\r
138                         reply = "BLC";\r
139                       }\r
140                       else\r
141                       {\r
142                         reply = "FASTA";\r
143                       }\r
144                     }\r
145 \r
146                     break;\r
147                   }\r
148                   else if (data.indexOf("HEADER") > -1 ||\r
149                            data.indexOf("ATOM") > -1)\r
150                   {\r
151                     reply = "PDB";\r
152                     break;\r
153                   }\r
154                   else if (data.indexOf(":") < data.indexOf(",")) //  && data.indexOf(",")<data.indexOf(",", data.indexOf(",")))\r
155                   {\r
156                     // file looks like a concise JNet file\r
157                     reply = "JnetFile";\r
158                     break;\r
159                   }\r
160             }\r
161 \r
162             reader.close();\r
163         }\r
164         catch (Exception ex)\r
165         {\r
166             System.err.println("File Identification failed!\n" + ex);\r
167             return error;\r
168         }\r
169 \r
170         return reply;\r
171     }\r
172 }\r