729a86040aab30234553b705008803d08c1d5da9
[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 static String Identify(String file, String protocol)\r
43     {\r
44         String reply = "PFAM";\r
45         String error =  "FILE NOT FOUND";\r
46 \r
47         try\r
48         {\r
49             BufferedReader reader = null;\r
50 \r
51             if (protocol.equalsIgnoreCase("File"))\r
52             {\r
53                 reader = new BufferedReader(new FileReader(file));\r
54             }\r
55             else if (protocol.equalsIgnoreCase("URL"))\r
56             {\r
57                 error = "URL NOT FOUND";\r
58                 URL url = new URL(file);\r
59                 reader = new BufferedReader(new InputStreamReader(\r
60                             url.openStream()));\r
61 \r
62             }\r
63             else if (protocol.equalsIgnoreCase("Paste"))\r
64             {\r
65                 reader = new BufferedReader(new StringReader(file));\r
66             }\r
67 \r
68             String data;\r
69 \r
70             while ((data = reader.readLine()) != null)\r
71             {\r
72                 data = data.toUpperCase();\r
73 \r
74                 if ((data.indexOf("#") == 0) || (data.length() < 1))\r
75                 {\r
76                     continue;\r
77                 }\r
78 \r
79                 if (data.indexOf("PILEUP") > -1)\r
80                 {\r
81                     reply = "PileUp";\r
82 \r
83                     break;\r
84                 }\r
85 \r
86                 if ((data.indexOf("//") == 0) ||\r
87                         ((data.indexOf("!!") > -1) &&\r
88                         (data.indexOf("!!") < data.indexOf(\r
89                             "_MULTIPLE_ALIGNMENT "))))\r
90                 {\r
91                     reply = "MSF";\r
92 \r
93                     break;\r
94                 }\r
95                 else if (data.indexOf("CLUSTAL") > -1)\r
96                 {\r
97                     reply = "CLUSTAL";\r
98 \r
99                     break;\r
100                 }\r
101                 else if ((data.indexOf(">P1;") > -1) ||\r
102                         (data.indexOf(">DL;") > -1))\r
103                 {\r
104                     reply = "PIR";\r
105 \r
106                     break;\r
107                 }\r
108                 else if (data.indexOf(">") > -1)\r
109                 {\r
110                     // could be BLC file, read next line to confirm\r
111                     data = reader.readLine();\r
112 \r
113                     if (data.indexOf(">") > -1)\r
114                     {\r
115                         reply = "BLC";\r
116                     }\r
117                     else\r
118                     {\r
119                         reply = "FASTA";\r
120                     }\r
121 \r
122                     break;\r
123                 }\r
124             }\r
125 \r
126             reader.close();\r
127         }\r
128         catch (Exception ex)\r
129         {\r
130             System.err.println("File Identification failed!\n" + ex);\r
131             return error;\r
132         }\r
133         return reply;\r
134     }\r
135 }\r