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(">") > -1)
166 // FASTA, PIR file or BLC file
167 boolean checkPIR = false;
168 if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
170 // watch for PIR file attributes
174 // could also be BLC file, read next line to confirm
175 data = source.nextLine();
177 if (data.indexOf(">") > -1)
183 // Is this a single line BLC file?
185 String data2 = source.nextLine();
186 if (data2 != null && data.indexOf("*") > -1)
188 if (data.indexOf("*") == data2.indexOf("*"))
192 // otherwise can still possibly be a PIR file
197 // TODO : AMSA File is indicated if there is annotation in the
198 // FASTA file - but FASTA will automatically generate this at the
203 // TODO final check for PIR content. require >P1;title\n<blah>\nterminated sequence to occur at least once.
204 // the PIR/fasta ambiguity may be the use case that is needed to have a 'Parse as type XXX' parameter for the applet/application.
207 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
212 else if (!lineswereskipped && data.charAt(0) != '*'
213 && data.charAt(0) != ' '
214 && data.indexOf(":") < data.indexOf(",")) // &&
215 // data.indexOf(",")<data.indexOf(",",
216 // data.indexOf(",")))
218 // file looks like a concise JNet file
223 lineswereskipped = true; // this means there was some junk before any
224 // key file signature
232 source.reset(); // so the file can be parsed from the beginning again.
234 } catch (Exception ex)
236 System.err.println("File Identification failed!\n" + ex);
237 return source.errormessage;
242 .println("File Identification failed! - Empty file was read.");
243 return "EMPTY DATA FILE";
248 public static void main(String[] args)
250 for (int i = 0; args != null && i < args.length; i++)
252 IdentifyFile ider = new IdentifyFile();
253 String type = ider.Identify(args[i], AppletFormatAdapter.FILE);
254 System.out.println("Type of " + args[i] + " is " + type);
256 if (args == null || args.length == 0)
258 System.err.println("Usage: <Filename> [<Filename> ...]");