2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
30 public class IdentifyFile
33 * Identify a datasource's file content.
34 * @note Do not use this method
35 * for stream sources - create a FileParse object instead.
37 * @param file DOCUMENT ME!
38 * @param protocol DOCUMENT ME!
41 public String Identify(String file, String protocol)
43 FileParse parser = null;
45 parser = new FileParse(file, protocol);
46 if (parser.isValid()) {
47 return Identify(parser);
49 } catch (Exception e) {
50 System.err.println("Error whilst identifying");
51 e.printStackTrace(System.err);
54 return parser.errormessage;
55 return "UNIDENTIFIED FILE PARSING ERROR";
57 public String Identify(FileParse source) {
58 return Identify(source, true); // preserves original behaviour prior to version 2.3
61 * Identify contents of source, closing it or resetting source to start afterwards.
64 * @return filetype string
66 public String Identify(FileParse source, boolean closeSource) {
67 String reply = "PFAM";
69 boolean lineswereskipped=false;
71 while ( (data = source.nextLine()) != null)
73 data = data.toUpperCase();
75 if ( (data.indexOf("# STOCKHOLM") > -1))
82 if ((data.length() < 1) || (data.indexOf("#") == 0))
84 lineswereskipped=true;
88 if (data.indexOf("PILEUP") > -1)
95 if ( (data.indexOf("//") == 0) ||
96 ( (data.indexOf("!!") > -1) &&
97 (data.indexOf("!!") < data.indexOf(
98 "_MULTIPLE_ALIGNMENT "))))
104 else if (data.indexOf("CLUSTAL") > -1)
110 else if ( (data.indexOf(">P1;") > -1) ||
111 (data.indexOf(">DL;") > -1))
117 else if (data.indexOf(">") > -1)
119 // could be BLC file, read next line to confirm
120 data = source.nextLine();
122 if (data.indexOf(">") > -1)
128 //Is this a single line BLC file?
130 String data2 = source.nextLine();
132 && data.indexOf("*") > -1
133 && data.indexOf("*") == data2.indexOf("*"))
145 else if (data.indexOf("HEADER") == 0 ||
146 data.indexOf("ATOM") == 0)
151 else if (!lineswereskipped
152 && data.charAt(0)!='*'
153 && data.charAt(0)!=' '
154 && data.indexOf(":") < data.indexOf(",")) // && data.indexOf(",")<data.indexOf(",", data.indexOf(",")))
156 // file looks like a concise JNet file
160 else if (source.inFile!=null)
162 String fileStr=source.inFile.getName();
163 // possibly a Jalview archive.
164 if (fileStr.lastIndexOf(".jar")>-1 || fileStr.lastIndexOf(".zip")>-1)
168 } else if (data.startsWith("PK")) {
169 reply="Jalview"; // archive.
173 lineswereskipped=true; // this means there was some junk before any key file signature
178 source.reset(); // so the file can be parsed from the beginning again.
183 System.err.println("File Identification failed!\n" + ex);
184 return source.errormessage;