2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
31 public class IdentifyFile
34 * Identify a datasource's file content.
36 * @note Do not use this method for stream sources - create a FileParse object
45 public String Identify(String file, String protocol)
47 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
48 FileParse parser = null;
51 parser = new FileParse(file, protocol);
54 return Identify(parser);
58 System.err.println("Error whilst identifying");
59 e.printStackTrace(System.err);
60 emessage = e.getMessage();
63 return parser.errormessage;
67 public String Identify(FileParse source)
69 return Identify(source, true); // preserves original behaviour prior to
74 * Identify contents of source, closing it or resetting source to start
79 * @return filetype string
81 public String Identify(FileParse source, boolean closeSource)
83 String reply = "PFAM";
86 boolean lineswereskipped = false;
87 boolean isBinary = false; // true if length is non-zero and non-printable
88 // characters are encountered
95 while ((data = source.nextLine()) != null)
97 length += data.length();
98 if (!lineswereskipped)
100 for (int i = 0; !isBinary && i < data.length(); i++)
102 char c = data.charAt(i);
103 isBinary = (c < 32 && c != '\t' && c != '\n' && c != '\r'
104 && c != 5 && c != 27); // nominal binary character filter
105 // excluding CR, LF, tab,DEL and ^E
106 // for certain blast ids
111 // jar files are special - since they contain all sorts of random
113 if (source.inFile != null)
115 String fileStr = source.inFile.getName();
116 // possibly a Jalview archive.
117 if (fileStr.lastIndexOf(".jar") > -1
118 || fileStr.lastIndexOf(".zip") > -1)
123 if (!lineswereskipped && data.startsWith("PK"))
125 reply = "Jalview"; // archive.
129 data = data.toUpperCase();
131 if ((data.indexOf("# STOCKHOLM") > -1))
138 if ((data.indexOf("<") > -1))
145 if ((data.length() < 1) || (data.indexOf("#") == 0))
147 lineswereskipped = true;
151 if (data.indexOf("PILEUP") > -1)
158 if ((data.indexOf("//") == 0)
159 || ((data.indexOf("!!") > -1) && (data.indexOf("!!") < data
160 .indexOf("_MULTIPLE_ALIGNMENT "))))
166 else if (data.indexOf("CLUSTAL") > -1)
173 else if (data.indexOf(">") > -1)
175 // FASTA, PIR file or BLC file
176 boolean checkPIR = false, starterm = false;
177 if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
179 // watch for PIR file attributes
183 // could also be BLC file, read next line to confirm
184 data = source.nextLine();
186 if (data.indexOf(">") > -1)
192 // Is this a single line BLC file?
193 String data1 = source.nextLine();
194 String data2 = source.nextLine();
198 starterm = (data1 != null && data1.indexOf("*") > -1)
199 || (data2 != null && data2.indexOf("*") > -1);
201 if (data2 != null && (c1 = data.indexOf("*")) > -1)
203 if (c1 == 0 && c1 == data2.indexOf("*"))
209 reply = "FASTA"; // possibly a bad choice - may be recognised as
212 // otherwise can still possibly be a PIR file
217 // TODO : AMSA File is indicated if there is annotation in the
218 // FASTA file - but FASTA will automatically generate this at the
226 // final check for PIR content. require
227 // >P1;title\n<blah>\nterminated sequence to occur at least once.
229 // TODO the PIR/fasta ambiguity may be the use case that is needed to
231 // a 'Parse as type XXX' parameter for the applet/application.
241 dta = source.nextLine();
242 } catch (IOException ex)
246 if (dta != null && dta.indexOf("*") > -1)
250 } while (dta != null && !starterm);
259 reply = "FASTA"; // probably a bad choice!
262 // read as a FASTA (probably)
265 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
271 * // TODO comment out SimpleBLAST identification for Jalview 2.4.1 else
272 * if (!lineswereskipped && data.indexOf("BLAST")<4) { reply =
273 * "SimpleBLAST"; break;
275 * } // end comments for Jalview 2.4.1
277 else if (!lineswereskipped && data.charAt(0) != '*'
278 && data.charAt(0) != ' '
279 && data.indexOf(":") < data.indexOf(",")) // &&
280 // data.indexOf(",")<data.indexOf(",",
281 // data.indexOf(",")))
283 // file looks like a concise JNet file
288 lineswereskipped = true; // this means there was some junk before any
289 // key file signature
297 source.reset(); // so the file can be parsed from the beginning again.
299 } catch (Exception ex)
301 System.err.println("File Identification failed!\n" + ex);
302 return source.errormessage;
307 .println("File Identification failed! - Empty file was read.");
308 return "EMPTY DATA FILE";
313 public static void main(String[] args)
315 for (int i = 0; args != null && i < args.length; i++)
317 IdentifyFile ider = new IdentifyFile();
318 String type = ider.Identify(args[i], AppletFormatAdapter.FILE);
319 System.out.println("Type of " + args[i] + " is " + type);
321 if (args == null || args.length == 0)
323 System.err.println("Usage: <Filename> [<Filename> ...]");