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.
23 import java.io.IOException;
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();
64 return parser.errormessage;
69 public String Identify(FileParse source)
71 return Identify(source, true); // preserves original behaviour prior to
76 * Identify contents of source, closing it or resetting source to start
81 * @return filetype string
83 public String Identify(FileParse source, boolean closeSource)
85 String reply = "PFAM";
88 boolean lineswereskipped = false;
89 boolean isBinary = false; // true if length is non-zero and non-printable
90 // characters are encountered
97 while ((data = source.nextLine()) != null)
99 length += data.length();
100 if (!lineswereskipped)
102 for (int i = 0; !isBinary && i < data.length(); i++)
104 char c = data.charAt(i);
105 isBinary = (c < 32 && c != '\t' && c != '\n' && c != '\r'
106 && c != 5 && c != 27); // nominal binary character filter
107 // excluding CR, LF, tab,DEL and ^E
108 // for certain blast ids
113 // jar files are special - since they contain all sorts of random
115 if (source.inFile != null)
117 String fileStr = source.inFile.getName();
118 // possibly a Jalview archive.
119 if (fileStr.lastIndexOf(".jar") > -1
120 || fileStr.lastIndexOf(".zip") > -1)
125 if (!lineswereskipped && data.startsWith("PK"))
127 reply = "Jalview"; // archive.
131 data = data.toUpperCase();
133 if ((data.indexOf("# STOCKHOLM") > -1))
139 if (data.indexOf("{\"") > -1)
141 reply = JSONFile.FILE_DESC;
144 // if (data.matches("<(\"[^\"]*\"|'[^']*'|[^'\">])*>"))
145 if (data.matches("<(?i)html(\"[^\"]*\"|'[^']*'|[^'\">])*>"))
151 if (data.matches("<(?i)rnaml (\"[^\"]*\"|'[^']*'|[^'\">])*>"))
158 if ((data.length() < 1) || (data.indexOf("#") == 0))
160 lineswereskipped = true;
164 if (data.indexOf("PILEUP") > -1)
171 if ((data.indexOf("//") == 0)
172 || ((data.indexOf("!!") > -1) && (data.indexOf("!!") < data
173 .indexOf("_MULTIPLE_ALIGNMENT "))))
179 else if (data.indexOf("CLUSTAL") > -1)
186 else if (data.indexOf(">") > -1)
188 // FASTA, PIR file or BLC file
189 boolean checkPIR = false, starterm = false;
190 if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
192 // watch for PIR file attributes
196 // could also be BLC file, read next line to confirm
197 data = source.nextLine();
199 if (data.indexOf(">") > -1)
205 // Is this a single line BLC file?
206 String data1 = source.nextLine();
207 String data2 = source.nextLine();
211 starterm = (data1 != null && data1.indexOf("*") > -1)
212 || (data2 != null && data2.indexOf("*") > -1);
214 if (data2 != null && (c1 = data.indexOf("*")) > -1)
216 if (c1 == 0 && c1 == data2.indexOf("*"))
222 reply = "FASTA"; // possibly a bad choice - may be recognised as
225 // otherwise can still possibly be a PIR file
230 // TODO : AMSA File is indicated if there is annotation in the
231 // FASTA file - but FASTA will automatically generate this at the
239 // final check for PIR content. require
240 // >P1;title\n<blah>\nterminated sequence to occur at least once.
242 // TODO the PIR/fasta ambiguity may be the use case that is needed to
244 // a 'Parse as type XXX' parameter for the applet/application.
254 dta = source.nextLine();
255 } catch (IOException ex)
259 if (dta != null && dta.indexOf("*") > -1)
263 } while (dta != null && !starterm);
272 reply = "FASTA"; // probably a bad choice!
275 // read as a FASTA (probably)
278 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
283 else if (data.matches("\\s*\\d+\\s+\\d+\\s*"))
285 reply = PhylipFile.FILE_DESC;
291 * // TODO comment out SimpleBLAST identification for Jalview 2.4.1 else
292 * if (!lineswereskipped && data.indexOf("BLAST")<4) { reply =
293 * "SimpleBLAST"; break;
295 * } // end comments for Jalview 2.4.1
297 else if (!lineswereskipped && data.charAt(0) != '*'
298 && data.charAt(0) != ' '
299 && data.indexOf(":") < data.indexOf(",")) // &&
300 // data.indexOf(",")<data.indexOf(",",
301 // data.indexOf(",")))
303 // file looks like a concise JNet file
308 lineswereskipped = true; // this means there was some junk before any
309 // key file signature
317 source.reset(); // so the file can be parsed from the beginning again.
319 } catch (Exception ex)
321 System.err.println("File Identification failed!\n" + ex);
322 return source.errormessage;
327 .println("File Identification failed! - Empty file was read.");
328 return "EMPTY DATA FILE";
333 public static void main(String[] args)
336 for (int i = 0; args != null && i < args.length; i++)
338 IdentifyFile ider = new IdentifyFile();
339 String type = ider.Identify(args[i], AppletFormatAdapter.FILE);
340 System.out.println("Type of " + args[i] + " is " + type);
342 if (args == null || args.length == 0)
344 System.err.println("Usage: <Filename> [<Filename> ...]");