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 FeaturesFile = "GFF or Jalview features";
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 int trimmedLength = 0;
91 boolean lineswereskipped = false;
92 boolean isBinary = false; // true if length is non-zero and non-printable
93 // characters are encountered
100 while ((data = source.nextLine()) != null)
102 bytesRead += data.length();
103 trimmedLength += data.trim().length();
104 if (!lineswereskipped)
106 for (int i = 0; !isBinary && i < data.length(); i++)
108 char c = data.charAt(i);
109 isBinary = (c < 32 && c != '\t' && c != '\n' && c != '\r'
110 && c != 5 && c != 27); // nominal binary character filter
111 // excluding CR, LF, tab,DEL and ^E
112 // for certain blast ids
117 // jar files are special - since they contain all sorts of random
119 if (source.inFile != null)
121 String fileStr = source.inFile.getName();
122 // possibly a Jalview archive.
123 if (fileStr.lastIndexOf(".jar") > -1
124 || fileStr.lastIndexOf(".zip") > -1)
129 if (!lineswereskipped && data.startsWith("PK"))
131 reply = "Jalview"; // archive.
135 data = data.toUpperCase();
137 if (data.startsWith("##GFF-VERSION"))
139 // GFF - possibly embedded in a Jalview features file!
140 reply = FeaturesFile;
143 if (looksLikeFeatureData(data))
145 reply = FeaturesFile;
148 if (data.indexOf("# STOCKHOLM") > -1)
153 if (data.indexOf("_ENTRY.ID") > -1
154 || data.indexOf("_AUDIT_AUTHOR.NAME") > -1
155 || data.indexOf("_ATOM_SITE.") > -1)
160 // if (data.indexOf(">") > -1)
161 if (data.startsWith(">"))
163 // FASTA, PIR file or BLC file
164 boolean checkPIR = false, starterm = false;
165 if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
167 // watch for PIR file attributes
171 // could also be BLC file, read next line to confirm
172 data = source.nextLine();
174 if (data.indexOf(">") > -1)
180 // Is this a single line BLC file?
181 String data1 = source.nextLine();
182 String data2 = source.nextLine();
186 starterm = (data1 != null && data1.indexOf("*") > -1)
187 || (data2 != null && data2.indexOf("*") > -1);
189 if (data2 != null && (c1 = data.indexOf("*")) > -1)
191 if (c1 == 0 && c1 == data2.indexOf("*"))
197 reply = "FASTA"; // possibly a bad choice - may be recognised as
200 // otherwise can still possibly be a PIR file
205 // TODO : AMSA File is indicated if there is annotation in the
206 // FASTA file - but FASTA will automatically generate this at the
214 // final check for PIR content. require
215 // >P1;title\n<blah>\nterminated sequence to occur at least once.
217 // TODO the PIR/fasta ambiguity may be the use case that is needed to
219 // a 'Parse as type XXX' parameter for the applet/application.
229 dta = source.nextLine();
230 } catch (IOException ex)
233 if (dta != null && dta.indexOf("*") > -1)
237 } while (dta != null && !starterm);
246 reply = "FASTA"; // probably a bad choice!
249 // read as a FASTA (probably)
252 int lessThan = data.indexOf("<");
253 if ((lessThan > -1)) // possible Markup Language data i.e HTML,
256 String upper = data.toUpperCase();
257 if (upper.substring(lessThan).startsWith("<HTML"))
259 reply = HtmlFile.FILE_DESC;
262 if (upper.substring(lessThan).startsWith("<RNAML"))
269 if (data.indexOf("{\"") > -1)
271 reply = JSONFile.FILE_DESC;
274 if ((data.length() < 1) || (data.indexOf("#") == 0))
276 lineswereskipped = true;
280 if (data.indexOf("PILEUP") > -1)
287 if ((data.indexOf("//") == 0)
288 || ((data.indexOf("!!") > -1) && (data.indexOf("!!") < data
289 .indexOf("_MULTIPLE_ALIGNMENT "))))
295 else if (data.indexOf("CLUSTAL") > -1)
302 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
307 else if (data.matches("\\s*\\d+\\s+\\d+\\s*"))
309 reply = PhylipFile.FILE_DESC;
314 if (!lineswereskipped && looksLikeJnetData(data))
321 lineswereskipped = true; // this means there was some junk before any
322 // key file signature
330 source.reset(bytesRead); // so the file can be parsed from the mark
332 } catch (Exception ex)
334 System.err.println("File Identification failed!\n" + ex);
335 return source.errormessage;
337 if (trimmedLength == 0)
340 .println("File Identification failed! - Empty file was read.");
341 return "EMPTY DATA FILE";
347 * Returns true if the data appears to be Jnet concise annotation format
352 protected boolean looksLikeJnetData(String data)
354 char firstChar = data.charAt(0);
355 int colonPos = data.indexOf(":");
356 int commaPos = data.indexOf(",");
357 boolean isJnet = firstChar != '*' && firstChar != ' ' && colonPos > -1
358 && commaPos > -1 && colonPos < commaPos;
359 // && data.indexOf(",")<data.indexOf(",", data.indexOf(","))) / ??
364 * Returns true if the data has at least 6 tab-delimited fields _and_
365 * fields 4 and 5 are integer (start/end)
369 protected boolean looksLikeFeatureData(String data)
375 String[] columns = data.split("\t");
376 if (columns.length < 6) {
379 for (int col = 3; col < 5; col++)
382 Integer.parseInt(columns[col]);
383 } catch (NumberFormatException e) {
390 public static void main(String[] args)
393 for (int i = 0; args != null && i < args.length; i++)
395 IdentifyFile ider = new IdentifyFile();
396 String type = ider.identify(args[i], AppletFormatAdapter.FILE);
397 System.out.println("Type of " + args[i] + " is " + type);
399 if (args == null || args.length == 0)
401 System.err.println("Usage: <Filename> [<Filename> ...]");