2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
28 public class IdentifyFile
31 * Identify a datasource's file content.
33 * @note Do not use this method for stream sources - create a FileParse object
42 public String Identify(String file, String protocol)
44 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
45 FileParse parser = null;
48 parser = new FileParse(file, protocol);
51 return Identify(parser);
55 System.err.println("Error whilst identifying");
56 e.printStackTrace(System.err);
57 emessage = e.getMessage();
60 return parser.errormessage;
64 public String Identify(FileParse source)
66 return Identify(source, true); // preserves original behaviour prior to
71 * Identify contents of source, closing it or resetting source to start
76 * @return filetype string
78 public String Identify(FileParse source, boolean closeSource)
80 String reply = "PFAM";
83 boolean lineswereskipped = false;
84 boolean isBinary = false; // true if length is non-zero and non-printable
85 // characters are encountered
92 while ((data = source.nextLine()) != null)
94 length += data.length();
95 if (!lineswereskipped)
97 for (int i = 0; !isBinary && i < data.length(); i++)
99 char c = data.charAt(i);
100 isBinary = (c < 32 && c != '\t' && c != '\n' && c != '\r'
101 && c != 5 && c != 27); // nominal binary character filter
102 // excluding CR, LF, tab,DEL and ^E
103 // for certain blast ids
108 // jar files are special - since they contain all sorts of random
110 if (source.inFile != null)
112 String fileStr = source.inFile.getName();
113 // possibly a Jalview archive.
114 if (fileStr.lastIndexOf(".jar") > -1
115 || fileStr.lastIndexOf(".zip") > -1)
120 if (!lineswereskipped && data.startsWith("PK"))
122 reply = "Jalview"; // archive.
126 data = data.toUpperCase();
128 if ((data.indexOf("# STOCKHOLM") > -1))
135 if ((data.indexOf("<") > -1))
142 if ((data.length() < 1) || (data.indexOf("#") == 0))
144 lineswereskipped = true;
148 if (data.indexOf("PILEUP") > -1)
155 if ((data.indexOf("//") == 0)
156 || ((data.indexOf("!!") > -1) && (data.indexOf("!!") < data
157 .indexOf("_MULTIPLE_ALIGNMENT "))))
163 else if (data.indexOf("CLUSTAL") > -1)
171 else if (data.indexOf(">") > -1)
173 // FASTA, PIR file or BLC file
174 boolean checkPIR = false, starterm = false;
175 if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
177 // watch for PIR file attributes
181 // could also be BLC file, read next line to confirm
182 data = source.nextLine();
184 if (data.indexOf(">") > -1)
190 // Is this a single line BLC file?
191 String data1 = source.nextLine();
192 String data2 = source.nextLine();
195 starterm = (data1 != null && data1.indexOf("*") > -1)
196 || (data2 != null && data2.indexOf("*") > -1);
198 if (data2 != null && data.indexOf("*") > -1)
200 if (data.indexOf("*") == data2.indexOf("*"))
204 // otherwise can still possibly be a PIR file
209 // TODO : AMSA File is indicated if there is annotation in the
210 // FASTA file - but FASTA will automatically generate this at the
218 // final check for PIR content. require
219 // >P1;title\n<blah>\nterminated sequence to occur at least once.
221 // TODO the PIR/fasta ambiguity may be the use case that is needed to
223 // a 'Parse as type XXX' parameter for the applet/application.
233 dta = source.nextLine();
234 } catch (IOException ex)
238 if (dta != null && dta.indexOf("*") > -1)
242 } while (dta != null && !starterm);
251 reply = "FASTA"; // probably a bad choice!
254 // read as a FASTA (probably)
257 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
263 * // TODO comment out SimpleBLAST identification for Jalview 2.4.1 else
264 * if (!lineswereskipped && data.indexOf("BLAST")<4) { reply =
265 * "SimpleBLAST"; break;
267 * } // end comments for Jalview 2.4.1
269 else if (!lineswereskipped && data.charAt(0) != '*'
270 && data.charAt(0) != ' '
271 && data.indexOf(":") < data.indexOf(",")) // &&
272 // data.indexOf(",")<data.indexOf(",",
273 // data.indexOf(",")))
275 // file looks like a concise JNet file
280 lineswereskipped = true; // this means there was some junk before any
281 // key file signature
289 source.reset(); // so the file can be parsed from the beginning again.
291 } catch (Exception ex)
293 System.err.println("File Identification failed!\n" + ex);
294 return source.errormessage;
299 .println("File Identification failed! - Empty file was read.");
300 return "EMPTY DATA FILE";
305 public static void main(String[] args)
307 for (int i = 0; args != null && i < args.length; i++)
309 IdentifyFile ider = new IdentifyFile();
310 String type = ider.Identify(args[i], AppletFormatAdapter.FILE);
311 System.out.println("Type of " + args[i] + " is " + type);
313 if (args == null || args.length == 0)
315 System.err.println("Usage: <Filename> [<Filename> ...]");