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.util.Locale;
25 import java.io.IOException;
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("HMMER3"))
191 reply = FileFormat.HMMER3;
194 if (data.startsWith("LOCUS"))
196 reply = FileFormat.GenBank;
199 if (data.startsWith("ID "))
201 if (data.substring(2).trim().split(";").length == 7)
203 reply = FileFormat.Embl;
207 if (data.startsWith("H ") && !aaIndexHeaderRead)
209 aaIndexHeaderRead = true;
211 if (data.startsWith("D ") && aaIndexHeaderRead)
213 reply = FileFormat.ScoreMatrix;
216 if (data.startsWith("##GFF-VERSION"))
218 // GFF - possibly embedded in a Jalview features file!
219 reply = FileFormat.Features;
222 if (looksLikeFeatureData(data))
224 reply = FileFormat.Features;
227 if (data.indexOf("# STOCKHOLM") > -1)
229 reply = FileFormat.Stockholm;
232 if (data.indexOf("_ENTRY.ID") > -1
233 || data.indexOf("_AUDIT_AUTHOR.NAME") > -1
234 || data.indexOf("_ATOM_SITE.") > -1)
236 reply = FileFormat.MMCif;
239 // if (data.indexOf(">") > -1)
240 if (data.startsWith(">"))
242 // FASTA, PIR file or BLC file
243 boolean checkPIR = false, starterm = false;
244 if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
246 // watch for PIR file attributes
248 reply = FileFormat.PIR;
250 // could also be BLC file, read next line to confirm
251 data = source.nextLine();
253 if (data.indexOf(">") > -1)
255 reply = FileFormat.BLC;
259 // Is this a single line BLC file?
260 String data1 = source.nextLine();
261 String data2 = source.nextLine();
265 starterm = (data1 != null && data1.indexOf("*") > -1)
266 || (data2 != null && data2.indexOf("*") > -1);
268 if (data2 != null && (c1 = data.indexOf("*")) > -1)
270 if (c1 == 0 && c1 == data2.indexOf("*"))
272 reply = FileFormat.BLC;
276 reply = FileFormat.Fasta; // possibly a bad choice - may be
280 // otherwise can still possibly be a PIR file
284 reply = FileFormat.Fasta;
285 // TODO : AMSA File is indicated if there is annotation in the
286 // FASTA file - but FASTA will automatically generate this at the
294 // final check for PIR content. require
295 // >P1;title\n<blah>\nterminated sequence to occur at least once.
297 // TODO the PIR/fasta ambiguity may be the use case that is needed to
299 // a 'Parse as type XXX' parameter for the applet/application.
309 dta = source.nextLine();
310 } catch (IOException ex)
313 if (dta != null && dta.indexOf("*") > -1)
317 } while (dta != null && !starterm);
321 reply = FileFormat.PIR;
326 reply = FileFormat.Fasta; // probably a bad choice!
329 // read as a FASTA (probably)
332 if (data.indexOf("{\"") > -1)
334 reply = FileFormat.Json;
337 int lessThan = data.indexOf("<");
338 if ((lessThan > -1)) // possible Markup Language data i.e HTML,
341 String upper = data.toUpperCase(Locale.ROOT);
342 if (upper.substring(lessThan).startsWith("<HTML"))
344 reply = FileFormat.Html;
347 if (upper.substring(lessThan).startsWith("<RNAML"))
349 reply = FileFormat.Rnaml;
352 if (upper.substring(lessThan).startsWith("<BSML"))
354 reply = FileFormat.BSML;
359 if ((data.length() < 1) || (data.indexOf("#") == 0))
361 lineswereskipped = true;
365 if (data.indexOf("PILEUP") > -1)
367 reply = FileFormat.Pileup;
372 if ((data.indexOf("//") == 0) || ((data.indexOf("!!") > -1) && (data
373 .indexOf("!!") < data.indexOf("_MULTIPLE_ALIGNMENT "))))
375 reply = FileFormat.MSF;
379 else if (data.indexOf("CLUSTAL") > -1)
381 reply = FileFormat.Clustal;
386 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
388 reply = FileFormat.PDB;
391 else if (data.matches("\\s*\\d+\\s+\\d+\\s*"))
393 reply = FileFormat.Phylip;
398 if (!lineswereskipped && looksLikeJnetData(data))
400 reply = FileFormat.Jnet;
405 lineswereskipped = true; // this means there was some junk before any
406 // key file signature
414 source.reset(bytesRead); // so the file can be parsed from the mark
416 } catch (Exception ex)
418 System.err.println("File Identification failed!\n" + ex);
419 throw new FileFormatException(source.errormessage);
421 if (trimmedLength == 0)
424 "File Identification failed! - Empty file was read.");
425 throw new FileFormatException("EMPTY DATA FILE");
427 System.out.println("File format identified as " + reply.toString());
432 * Returns true if the data appears to be Jnet concise annotation format
437 protected boolean looksLikeJnetData(String data)
439 char firstChar = data.charAt(0);
440 int colonPos = data.indexOf(":");
441 int commaPos = data.indexOf(",");
442 boolean isJnet = firstChar != '*' && firstChar != ' ' && colonPos > -1
443 && commaPos > -1 && colonPos < commaPos;
444 // && data.indexOf(",")<data.indexOf(",", data.indexOf(","))) / ??
449 * Returns true if the data has at least 6 tab-delimited fields _and_ fields 4
450 * and 5 are integer (start/end)
455 protected boolean looksLikeFeatureData(String data)
461 String[] columns = data.split("\t");
462 if (columns.length < 6)
466 for (int col = 3; col < 5; col++)
470 Integer.parseInt(columns[col]);
471 } catch (NumberFormatException e)
484 public static void main(String[] args)
486 for (int i = 0; args != null && i < args.length; i++)
488 IdentifyFile ider = new IdentifyFile();
489 FileFormatI type = null;
492 type = ider.identify(args[i], DataSourceType.FILE);
493 } catch (FileFormatException e)
496 String.format("Error '%s' identifying file type for %s",
497 args[i], e.getMessage()));
499 System.out.println("Type of " + args[i] + " is " + type);
501 if (args == null || args.length == 0)
503 System.err.println("Usage: <Filename> [<Filename> ...]");