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 jalview.jsdev.Constants;
24 import jalview.jsdev.GenericFileAdapter;
26 import java.io.IOException;
34 public class IdentifyFile
36 public static final String GFF3File = "GFF v2 or v3";
39 * Identify a datasource's file content.
41 * @note Do not use this method for stream sources - create a FileParse object
50 public String Identify(String file, String protocol)
52 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
53 FileParse parser = null;
56 parser = new FileParse(file, protocol);
59 return Identify(parser);
63 System.err.println("Error whilst identifying");
64 e.printStackTrace(System.err);
65 emessage = e.getMessage();
69 return parser.errormessage;
74 public String Identify(FileParse source)
76 return Identify(source, true); // preserves original behaviour prior to
81 * Identify contents of source, closing it or resetting source to start
86 * @return filetype string
88 public String Identify(FileParse source, boolean closeSource)
90 String reply = "PFAM";
93 boolean lineswereskipped = false;
94 boolean isBinary = false; // true if length is non-zero and non-printable
95 // characters are encountered
102 while ((data = source.nextLine()) != null)
104 length += data.trim().length();
105 if (!lineswereskipped)
107 for (int i = 0; !isBinary && i < data.length(); i++)
109 char c = data.charAt(i);
110 isBinary = (c < 32 && c != '\t' && c != '\n' && c != '\r'
111 && c != 5 && c != 27); // nominal binary character filter
112 // excluding CR, LF, tab,DEL and ^E
113 // for certain blast ids
118 // jar files are special - since they contain all sorts of random
120 if (source.inFile != null)
122 String fileStr = source.inFile.getName();
123 // possibly a Jalview archive.
124 if (fileStr.lastIndexOf(".jar") > -1
125 || fileStr.lastIndexOf(".zip") > -1)
130 if (!lineswereskipped && data.startsWith("PK"))
132 reply = "Jalview"; // archive.
136 data = data.toUpperCase();
138 if (data.startsWith("##GFF-VERSION"))
143 if (data.indexOf("# STOCKHOLM") > -1)
148 // if (data.matches("<(\"[^\"]*\"|'[^']*'|[^'\">])*>"))
149 if (data.matches("<HTML(\"[^\"]*\"|'[^']*'|[^'\">])*>")) // BH was (?i)rnaml, but JavaScript regex does not support (?i); data is already upper case
151 reply = Constants.Html_FILE_DESC;
155 if (data.matches("<RNAML (\"[^\"]*\"|'[^']*'|[^'\">])*>")) // BH was (?i)rnaml, but JavaScript regex does not support (?i); data is already upper case
161 if (data.indexOf("{\"") > -1)
163 reply = Constants.JSON_FILE_DESC;
166 if ((data.length() < 1) || (data.indexOf("#") == 0))
168 lineswereskipped = true;
172 if (data.indexOf("PILEUP") > -1)
179 if ((data.indexOf("//") == 0)
180 || ((data.indexOf("!!") > -1) && (data.indexOf("!!") < data
181 .indexOf("_MULTIPLE_ALIGNMENT "))))
187 else if (data.indexOf("CLUSTAL") > -1)
194 else if (data.indexOf(">") > -1)
196 // FASTA, PIR file or BLC file
197 boolean checkPIR = false, starterm = false;
198 if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
200 // watch for PIR file attributes
204 // could also be BLC file, read next line to confirm
205 data = source.nextLine();
207 if (data.indexOf(">") > -1)
213 // Is this a single line BLC file?
214 String data1 = source.nextLine();
215 String data2 = source.nextLine();
219 starterm = (data1 != null && data1.indexOf("*") > -1)
220 || (data2 != null && data2.indexOf("*") > -1);
222 if (data2 != null && (c1 = data.indexOf("*")) > -1)
224 if (c1 == 0 && c1 == data2.indexOf("*"))
230 reply = "FASTA"; // possibly a bad choice - may be recognised as
233 // otherwise can still possibly be a PIR file
238 // TODO : AMSA File is indicated if there is annotation in the
239 // FASTA file - but FASTA will automatically generate this at the
247 // final check for PIR content. require
248 // >P1;title\n<blah>\nterminated sequence to occur at least once.
250 // TODO the PIR/fasta ambiguity may be the use case that is needed to
252 // a 'Parse as type XXX' parameter for the applet/application.
262 dta = source.nextLine();
263 } catch (IOException ex)
267 if (dta != null && dta.indexOf("*") > -1)
271 } while (dta != null && !starterm);
280 reply = "FASTA"; // probably a bad choice!
283 // read as a FASTA (probably)
286 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
291 else if (data.matches("\\s*\\d+\\s+\\d+\\s*"))
293 reply = Constants.Phylip_FILE_DESC;
299 * // TODO comment out SimpleBLAST identification for Jalview 2.4.1 else
300 * if (!lineswereskipped && data.indexOf("BLAST")<4) { reply =
301 * "SimpleBLAST"; break;
303 * } // end comments for Jalview 2.4.1
305 else if (!lineswereskipped && data.charAt(0) != '*'
306 && data.charAt(0) != ' '
307 && data.indexOf(":") < data.indexOf(",")) // &&
308 // data.indexOf(",")<data.indexOf(",",
309 // data.indexOf(",")))
311 // file looks like a concise JNet file
316 lineswereskipped = true; // this means there was some junk before any
317 // key file signature
325 source.reset(); // so the file can be parsed from the beginning again.
327 } catch (Exception ex)
329 System.err.println("File Identification failed!\n" + ex);
330 return source.errormessage;
335 .println("File Identification failed! - Empty file was read.");
336 return "EMPTY DATA FILE";
346 public static void main(String[] args)
349 for (int i = 0; args != null && i < args.length; i++)
351 IdentifyFile ider = new IdentifyFile();
352 String type = ider.Identify(args[i], AppletFormatAdapter.FILE);
353 System.out.println("Type of " + args[i] + " is " + type);
355 if (args == null || args.length == 0)
357 System.err.println("Usage: <Filename> [<Filename> ...]");