2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
29 public class IdentifyFile
32 * Identify a datasource's file content.
34 * @note Do not use this method for stream sources - create a FileParse object
43 public String Identify(String file, String protocol)
45 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
46 FileParse parser = null;
49 parser = new FileParse(file, protocol);
52 return Identify(parser);
56 System.err.println("Error whilst identifying");
57 e.printStackTrace(System.err);
58 emessage = e.getMessage();
61 return parser.errormessage;
65 public String Identify(FileParse source)
67 return Identify(source, true); // preserves original behaviour prior to
72 * Identify contents of source, closing it or resetting source to start
77 * @return filetype string
79 public String Identify(FileParse source, boolean closeSource)
81 String reply = "PFAM";
84 boolean lineswereskipped = false;
85 boolean isBinary = false; // true if length is non-zero and non-printable
86 // characters are encountered
93 while ((data = source.nextLine()) != null)
95 length += data.length();
96 if (!lineswereskipped)
98 for (int i = 0; !isBinary && i < data.length(); i++)
100 char c = data.charAt(i);
101 isBinary = (c < 32 && c != '\t' && c != '\n' && c != '\r'
102 && c != 5 && c != 27); // nominal binary character filter
103 // excluding CR, LF, tab,DEL and ^E
104 // for certain blast ids
109 // jar files are special - since they contain all sorts of random
111 if (source.inFile != null)
113 String fileStr = source.inFile.getName();
114 // possibly a Jalview archive.
115 if (fileStr.lastIndexOf(".jar") > -1
116 || fileStr.lastIndexOf(".zip") > -1)
121 if (!lineswereskipped && data.startsWith("PK"))
123 reply = "Jalview"; // archive.
127 data = data.toUpperCase();
129 if ((data.indexOf("# STOCKHOLM") > -1))
136 if ((data.indexOf("<") > -1))
143 if ((data.length() < 1) || (data.indexOf("#") == 0))
145 lineswereskipped = true;
149 if (data.indexOf("PILEUP") > -1)
156 if ((data.indexOf("//") == 0)
157 || ((data.indexOf("!!") > -1) && (data.indexOf("!!") < data
158 .indexOf("_MULTIPLE_ALIGNMENT "))))
164 else if (data.indexOf("CLUSTAL") > -1)
172 else if (data.indexOf(">") > -1)
174 // FASTA, PIR file or BLC file
175 boolean checkPIR = false, starterm = false;
176 if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
178 // watch for PIR file attributes
182 // could also be BLC file, read next line to confirm
183 data = source.nextLine();
185 if (data.indexOf(">") > -1)
191 // Is this a single line BLC file?
192 String data1 = source.nextLine();
193 String data2 = source.nextLine();
196 starterm = (data1 != null && data1.indexOf("*") > -1)
197 || (data2 != null && data2.indexOf("*") > -1);
199 if (data2 != null && data.indexOf("*") > -1)
201 if (data.indexOf("*") == data2.indexOf("*"))
205 // otherwise can still possibly be a PIR file
210 // TODO : AMSA File is indicated if there is annotation in the
211 // FASTA file - but FASTA will automatically generate this at the
219 // final check for PIR content. require
220 // >P1;title\n<blah>\nterminated sequence to occur at least once.
222 // TODO the PIR/fasta ambiguity may be the use case that is needed to
224 // a 'Parse as type XXX' parameter for the applet/application.
234 dta = source.nextLine();
235 } catch (IOException ex)
239 if (dta != null && dta.indexOf("*") > -1)
243 } while (dta != null && !starterm);
252 reply = "FASTA"; // probably a bad choice!
255 // read as a FASTA (probably)
258 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
264 * // TODO comment out SimpleBLAST identification for Jalview 2.4.1 else
265 * if (!lineswereskipped && data.indexOf("BLAST")<4) { reply =
266 * "SimpleBLAST"; break;
268 * } // end comments for Jalview 2.4.1
270 else if (!lineswereskipped && data.charAt(0) != '*'
271 && data.charAt(0) != ' '
272 && data.indexOf(":") < data.indexOf(",")) // &&
273 // data.indexOf(",")<data.indexOf(",",
274 // data.indexOf(",")))
276 // file looks like a concise JNet file
281 lineswereskipped = true; // this means there was some junk before any
282 // key file signature
290 source.reset(); // so the file can be parsed from the beginning again.
292 } catch (Exception ex)
294 System.err.println("File Identification failed!\n" + ex);
295 return source.errormessage;
300 .println("File Identification failed! - Empty file was read.");
301 return "EMPTY DATA FILE";
306 public static void main(String[] args)
308 for (int i = 0; args != null && i < args.length; i++)
310 IdentifyFile ider = new IdentifyFile();
311 String type = ider.Identify(args[i], AppletFormatAdapter.FILE);
312 System.out.println("Type of " + args[i] + " is " + type);
314 if (args == null || args.length == 0)
316 System.err.println("Usage: <Filename> [<Filename> ...]");