2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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
33 public static final String GFF3File = "GFF v2 or v3";
36 * Identify a datasource's file content.
38 * @note Do not use this method for stream sources - create a FileParse object
47 public String Identify(String file, String protocol)
49 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
50 FileParse parser = null;
53 parser = new FileParse(file, protocol);
56 return Identify(parser);
60 System.err.println("Error whilst identifying");
61 e.printStackTrace(System.err);
62 emessage = e.getMessage();
66 return parser.errormessage;
71 public String Identify(FileParse source)
73 return Identify(source, true); // preserves original behaviour prior to
78 * Identify contents of source, closing it or resetting source to start
83 * @return filetype string
85 public String Identify(FileParse source, boolean closeSource)
87 String reply = "PFAM";
90 boolean lineswereskipped = false;
91 boolean isBinary = false; // true if length is non-zero and non-printable
92 // characters are encountered
99 while ((data = source.nextLine()) != null)
101 length += data.trim().length();
102 if (!lineswereskipped)
104 for (int i = 0; !isBinary && i < data.length(); i++)
106 char c = data.charAt(i);
107 isBinary = (c < 32 && c != '\t' && c != '\n' && c != '\r'
108 && c != 5 && c != 27); // nominal binary character filter
109 // excluding CR, LF, tab,DEL and ^E
110 // for certain blast ids
115 // jar files are special - since they contain all sorts of random
117 if (source.inFile != null)
119 String fileStr = source.inFile.getName();
120 // possibly a Jalview archive.
121 if (fileStr.lastIndexOf(".jar") > -1
122 || fileStr.lastIndexOf(".zip") > -1)
127 if (!lineswereskipped && data.startsWith("PK"))
129 reply = "Jalview"; // archive.
133 data = data.toUpperCase();
135 if (data.startsWith("##GFF-VERSION"))
140 if (data.indexOf("# STOCKHOLM") > -1)
145 // if (data.indexOf(">") > -1)
146 if (data.startsWith(">"))
148 // FASTA, PIR file or BLC file
149 boolean checkPIR = false, starterm = false;
150 if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
152 // watch for PIR file attributes
156 // could also be BLC file, read next line to confirm
157 data = source.nextLine();
159 if (data.indexOf(">") > -1)
165 // Is this a single line BLC file?
166 String data1 = source.nextLine();
167 String data2 = source.nextLine();
171 starterm = (data1 != null && data1.indexOf("*") > -1)
172 || (data2 != null && data2.indexOf("*") > -1);
174 if (data2 != null && (c1 = data.indexOf("*")) > -1)
176 if (c1 == 0 && c1 == data2.indexOf("*"))
182 reply = "FASTA"; // possibly a bad choice - may be recognised as
185 // otherwise can still possibly be a PIR file
190 // TODO : AMSA File is indicated if there is annotation in the
191 // FASTA file - but FASTA will automatically generate this at the
199 // final check for PIR content. require
200 // >P1;title\n<blah>\nterminated sequence to occur at least once.
202 // TODO the PIR/fasta ambiguity may be the use case that is needed to
204 // a 'Parse as type XXX' parameter for the applet/application.
214 dta = source.nextLine();
215 } catch (IOException ex)
219 if (dta != null && dta.indexOf("*") > -1)
223 } while (dta != null && !starterm);
232 reply = "FASTA"; // probably a bad choice!
235 // read as a FASTA (probably)
238 if ((data.indexOf("<") > -1)) // possible Markup Language data i.e HTML,
241 boolean identified = false;
244 if (data.matches("<(?i)html(\"[^\"]*\"|'[^']*'|[^'\">])*>"))
246 reply = HtmlFile.FILE_DESC;
251 if (data.matches("<(?i)rnaml (\"[^\"]*\"|'[^']*'|[^'\">])*>"))
257 } while ((data = source.nextLine()) != null);
265 if (data.indexOf("{\"") > -1)
267 reply = JSONFile.FILE_DESC;
270 if ((data.length() < 1) || (data.indexOf("#") == 0))
272 lineswereskipped = true;
276 if (data.indexOf("PILEUP") > -1)
283 if ((data.indexOf("//") == 0)
284 || ((data.indexOf("!!") > -1) && (data.indexOf("!!") < data
285 .indexOf("_MULTIPLE_ALIGNMENT "))))
291 else if (data.indexOf("CLUSTAL") > -1)
298 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
303 else if (data.matches("\\s*\\d+\\s+\\d+\\s*"))
305 reply = PhylipFile.FILE_DESC;
311 * // TODO comment out SimpleBLAST identification for Jalview 2.4.1 else
312 * if (!lineswereskipped && data.indexOf("BLAST")<4) { reply =
313 * "SimpleBLAST"; break;
315 * } // end comments for Jalview 2.4.1
317 else if (!lineswereskipped && data.charAt(0) != '*'
318 && data.charAt(0) != ' '
319 && data.indexOf(":") < data.indexOf(",")) // &&
320 // data.indexOf(",")<data.indexOf(",",
321 // data.indexOf(",")))
323 // file looks like a concise JNet file
328 lineswereskipped = true; // this means there was some junk before any
329 // key file signature
337 source.reset(); // so the file can be parsed from the beginning again.
339 } catch (Exception ex)
341 System.err.println("File Identification failed!\n" + ex);
342 return source.errormessage;
347 .println("File Identification failed! - Empty file was read.");
348 return "EMPTY DATA FILE";
353 public static void main(String[] args)
356 for (int i = 0; args != null && i < args.length; i++)
358 IdentifyFile ider = new IdentifyFile();
359 String type = ider.Identify(args[i], AppletFormatAdapter.FILE);
360 System.out.println("Type of " + args[i] + " is " + type);
362 if (args == null || args.length == 0)
364 System.err.println("Usage: <Filename> [<Filename> ...]");