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.
24 import java.io.IOException;
25 import java.util.Locale;
33 public class IdentifyFile
36 public FileFormatI identify(Object file, DataSourceType protocol) throws FileFormatException
39 return (file instanceof File ? identify((File) file, protocol) : identify((String) file, protocol));
43 public FileFormatI identify(File file, DataSourceType sourceType)
44 throws FileFormatException
47 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
48 FileParse parser = null;
51 parser = new FileParse(file, sourceType);
54 return identify(parser);
58 System.err.println("Error whilst identifying " + file);
59 e.printStackTrace(System.err);
60 emessage = e.getMessage();
64 throw new FileFormatException(parser.errormessage);
66 throw new FileFormatException(emessage);
70 * Identify a datasource's file content.
72 * @note Do not use this method for stream sources - create a FileParse object
78 * @throws FileFormatException
80 public FileFormatI identify(String file, DataSourceType sourceType)
81 throws FileFormatException
83 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
84 FileParse parser = null;
87 parser = new FileParse(file, sourceType);
90 return identify(parser);
94 System.err.println("Error whilst identifying " + file);
95 e.printStackTrace(System.err);
96 emessage = e.getMessage();
100 throw new FileFormatException(parser.errormessage);
102 throw new FileFormatException(emessage);
105 public FileFormatI identify(FileParse source) throws FileFormatException
107 return identify(source, true);
108 // preserves original behaviour prior to version 2.3
111 public FileFormatI identify(AlignmentFileReaderI file,
112 boolean closeSource) throws IOException
114 FileParse fp = new FileParse(file.getInFile(),
115 file.getDataSourceType());
116 return identify(fp, closeSource);
120 * Identify contents of source, closing it or resetting source to start
125 * @return (best guess at) file format
126 * @throws FileFormatException
128 public FileFormatI identify(FileParse source, boolean closeSource)
129 throws FileFormatException
131 FileFormatI reply = FileFormat.Pfam;
134 int trimmedLength = 0;
135 boolean lineswereskipped = false;
136 boolean isBinary = false; // true if length is non-zero and non-printable
137 // characters are encountered
145 boolean aaIndexHeaderRead = false;
147 while ((data = source.nextLine()) != null)
149 bytesRead += data.length();
150 trimmedLength += data.trim().length();
151 if (!lineswereskipped)
153 for (int i = 0; !isBinary && i < data.length(); i++)
155 char c = data.charAt(i);
156 isBinary = (c < 32 && c != '\t' && c != '\n' && c != '\r'
157 && c != 5 && c != 27); // nominal binary character filter
158 // excluding CR, LF, tab,DEL and ^E
159 // for certain blast ids
164 // jar files are special - since they contain all sorts of random
166 if (source.inFile != null)
168 String fileStr = source.inFile.getName();
169 if (fileStr.contains(".jar")
170 || fileStr.contains(".zip") || fileStr.contains(".jvp"))
172 // possibly a Jalview archive (but check further)
173 reply = FileFormat.Jalview;
176 if (!lineswereskipped && data.startsWith("PK"))
178 reply = FileFormat.Jalview; // archive
182 data = data.toUpperCase(Locale.ROOT);
184 if (data.startsWith(ScoreMatrixFile.SCOREMATRIX))
186 reply = FileFormat.ScoreMatrix;
189 if (data.startsWith("H ") && !aaIndexHeaderRead)
191 aaIndexHeaderRead = true;
193 if (data.startsWith("D ") && aaIndexHeaderRead)
195 reply = FileFormat.ScoreMatrix;
198 if (data.startsWith("##GFF-VERSION"))
200 // GFF - possibly embedded in a Jalview features file!
201 reply = FileFormat.Features;
204 if (looksLikeFeatureData(data))
206 reply = FileFormat.Features;
209 if (data.indexOf("# STOCKHOLM") > -1)
211 reply = FileFormat.Stockholm;
214 if (data.indexOf("_ENTRY.ID") > -1
215 || data.indexOf("_AUDIT_AUTHOR.NAME") > -1
216 || data.indexOf("_ATOM_SITE.") > -1)
218 reply = FileFormat.MMCif;
221 // if (data.indexOf(">") > -1)
222 if (data.startsWith(">"))
224 // FASTA, PIR file or BLC file
225 boolean checkPIR = false, starterm = false;
226 if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
228 // watch for PIR file attributes
230 reply = FileFormat.PIR;
232 // could also be BLC file, read next line to confirm
233 data = source.nextLine();
235 if (data.indexOf(">") > -1)
237 reply = FileFormat.BLC;
241 // Is this a single line BLC file?
242 String data1 = source.nextLine();
243 String data2 = source.nextLine();
247 starterm = (data1 != null && data1.indexOf("*") > -1)
248 || (data2 != null && data2.indexOf("*") > -1);
250 if (data2 != null && (c1 = data.indexOf("*")) > -1)
252 if (c1 == 0 && c1 == data2.indexOf("*"))
254 reply = FileFormat.BLC;
258 reply = FileFormat.Fasta; // possibly a bad choice - may be
262 // otherwise can still possibly be a PIR file
266 reply = FileFormat.Fasta;
267 // TODO : AMSA File is indicated if there is annotation in the
268 // FASTA file - but FASTA will automatically generate this at the
276 // final check for PIR content. require
277 // >P1;title\n<blah>\nterminated sequence to occur at least once.
279 // TODO the PIR/fasta ambiguity may be the use case that is needed to
281 // a 'Parse as type XXX' parameter for the applet/application.
291 dta = source.nextLine();
292 } catch (IOException ex)
295 if (dta != null && dta.indexOf("*") > -1)
299 } while (dta != null && !starterm);
303 reply = FileFormat.PIR;
308 reply = FileFormat.Fasta; // probably a bad choice!
311 // read as a FASTA (probably)
314 if (data.indexOf("{\"") > -1)
316 reply = FileFormat.Json;
319 int lessThan = data.indexOf("<");
320 if ((lessThan > -1)) // possible Markup Language data i.e HTML,
323 String upper = data.toUpperCase(Locale.ROOT);
324 if (upper.substring(lessThan).startsWith("<HTML"))
326 reply = FileFormat.Html;
329 if (upper.substring(lessThan).startsWith("<RNAML"))
331 reply = FileFormat.Rnaml;
336 if ((data.length() < 1) || (data.indexOf("#") == 0))
338 lineswereskipped = true;
342 if (data.indexOf("PILEUP") > -1)
344 reply = FileFormat.Pileup;
349 if ((data.indexOf("//") == 0) || ((data.indexOf("!!") > -1) && (data
350 .indexOf("!!") < data.indexOf("_MULTIPLE_ALIGNMENT "))))
352 reply = FileFormat.MSF;
356 else if (data.indexOf("CLUSTAL") > -1)
358 reply = FileFormat.Clustal;
363 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
365 reply = FileFormat.PDB;
368 else if (data.matches("\\s*\\d+\\s+\\d+\\s*"))
370 reply = FileFormat.Phylip;
375 if (!lineswereskipped && looksLikeJnetData(data))
377 reply = FileFormat.Jnet;
382 lineswereskipped = true; // this means there was some junk before any
383 // key file signature
391 source.reset(bytesRead); // so the file can be parsed from the mark
393 } catch (Exception ex)
395 System.err.println("File Identification failed!\n" + ex);
396 throw new FileFormatException(source.errormessage);
398 if (trimmedLength == 0)
401 "File Identification failed! - Empty file was read.");
402 throw new FileFormatException("EMPTY DATA FILE");
404 System.out.println("File format identified as " + reply.toString());
409 * Returns true if the data appears to be Jnet concise annotation format
414 protected boolean looksLikeJnetData(String data)
416 char firstChar = data.charAt(0);
417 int colonPos = data.indexOf(":");
418 int commaPos = data.indexOf(",");
419 boolean isJnet = firstChar != '*' && firstChar != ' ' && colonPos > -1
420 && commaPos > -1 && colonPos < commaPos;
421 // && data.indexOf(",")<data.indexOf(",", data.indexOf(","))) / ??
426 * Returns true if the data has at least 6 tab-delimited fields _and_ fields 4
427 * and 5 are integer (start/end)
432 protected boolean looksLikeFeatureData(String data)
438 String[] columns = data.split("\t");
439 if (columns.length < 6)
443 for (int col = 3; col < 5; col++)
447 Integer.parseInt(columns[col]);
448 } catch (NumberFormatException e)
461 public static void main(String[] args)
463 for (int i = 0; args != null && i < args.length; i++)
465 IdentifyFile ider = new IdentifyFile();
466 FileFormatI type = null;
469 type = ider.identify(args[i], DataSourceType.FILE);
470 } catch (FileFormatException e)
473 String.format("Error '%s' identifying file type for %s",
474 args[i], e.getMessage()));
476 System.out.println("Type of " + args[i] + " is " + type);
478 if (args == null || args.length == 0)
480 System.err.println("Usage: <Filename> [<Filename> ...]");