2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 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.
35 * @note Do not use this method for stream sources - create a FileParse object
44 public String Identify(String file, String protocol)
46 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
47 FileParse parser = null;
50 parser = new FileParse(file, protocol);
53 return Identify(parser);
57 System.err.println("Error whilst identifying");
58 e.printStackTrace(System.err);
59 emessage = e.getMessage();
62 return parser.errormessage;
66 public String Identify(FileParse source)
68 return Identify(source, true); // preserves original behaviour prior to
73 * Identify contents of source, closing it or resetting source to start
78 * @return filetype string
80 public String Identify(FileParse source, boolean closeSource)
82 String reply = "PFAM";
85 boolean lineswereskipped = false;
86 boolean isBinary = false; // true if length is non-zero and non-printable
87 // characters are encountered
94 while ((data = source.nextLine()) != null)
96 length += data.length();
97 if (!lineswereskipped)
99 for (int i = 0; !isBinary && i < data.length(); i++)
101 char c = data.charAt(i);
102 isBinary = (c < 32 && c != '\t' && c != '\n' && c != '\r'
103 && c != 5 && c != 27); // nominal binary character filter
104 // excluding CR, LF, tab,DEL and ^E
105 // for certain blast ids
110 // jar files are special - since they contain all sorts of random
112 if (source.inFile != null)
114 String fileStr = source.inFile.getName();
115 // possibly a Jalview archive.
116 if (fileStr.lastIndexOf(".jar") > -1
117 || fileStr.lastIndexOf(".zip") > -1)
122 if (!lineswereskipped && data.startsWith("PK"))
124 reply = "Jalview"; // archive.
128 data = data.toUpperCase();
130 if ((data.indexOf("# STOCKHOLM") > -1))
137 if ((data.length() < 1) || (data.indexOf("#") == 0))
139 lineswereskipped = true;
143 if (data.indexOf("PILEUP") > -1)
150 if ((data.indexOf("//") == 0)
151 || ((data.indexOf("!!") > -1) && (data.indexOf("!!") < data
152 .indexOf("_MULTIPLE_ALIGNMENT "))))
158 else if (data.indexOf("CLUSTAL") > -1)
164 else if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
170 else if (data.indexOf(">") > -1)
172 // could be BLC file, read next line to confirm
173 data = source.nextLine();
175 if (data.indexOf(">") > -1)
181 // Is this a single line BLC file?
183 String data2 = source.nextLine();
184 if (data2 != null && data.indexOf("*") > -1
185 && data.indexOf("*") == data2.indexOf("*"))
192 // TODO : AMSA File is indicated if there is annotation in the
193 // FASTA file - but FASTA will automatically generate this at the
199 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
204 else if (!lineswereskipped && data.charAt(0) != '*'
205 && data.charAt(0) != ' '
206 && data.indexOf(":") < data.indexOf(",")) // &&
207 // data.indexOf(",")<data.indexOf(",",
208 // data.indexOf(",")))
210 // file looks like a concise JNet file
215 lineswereskipped = true; // this means there was some junk before any
216 // key file signature
224 source.reset(); // so the file can be parsed from the beginning again.
226 } catch (Exception ex)
228 System.err.println("File Identification failed!\n" + ex);
229 return source.errormessage;
234 .println("File Identification failed! - Empty file was read.");
235 return "EMPTY DATA FILE";
240 public static void main(String[] args)
242 for (int i = 0; args != null && i < args.length; i++)
244 IdentifyFile ider = new IdentifyFile();
245 String type = ider.Identify(args[i], AppletFormatAdapter.FILE);
246 System.out.println("Type of " + args[i] + " is " + type);
248 if (args == null || args.length == 0)
250 System.err.println("Usage: <Filename> [<Filename> ...]");