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;
26 import java.io.IOException;
34 public class IdentifyFile
37 public FileFormatI identify(Object file, DataSourceType protocol)
38 throws FileFormatException
41 return (file instanceof File ? identify((File) file, protocol)
42 : identify((String) file, protocol));
46 public FileFormatI identify(File file, DataSourceType sourceType)
47 throws FileFormatException
50 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
51 FileParse parser = null;
54 parser = new FileParse(file, sourceType);
57 return identify(parser);
61 System.err.println("Error whilst identifying " + file);
62 e.printStackTrace(System.err);
63 emessage = e.getMessage();
67 throw new FileFormatException(parser.errormessage);
69 throw new FileFormatException(emessage);
73 * Identify a datasource's file content.
75 * @note Do not use this method for stream sources - create a FileParse object
81 * @throws FileFormatException
83 public FileFormatI identify(String file, DataSourceType sourceType)
84 throws FileFormatException
86 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
87 FileParse parser = null;
90 parser = new FileParse(file, sourceType);
93 return identify(parser);
97 System.err.println("Error whilst identifying " + file);
98 e.printStackTrace(System.err);
99 emessage = e.getMessage();
103 throw new FileFormatException(parser.errormessage);
105 throw new FileFormatException(emessage);
108 public FileFormatI identify(FileParse source) throws FileFormatException
110 return identify(source, true);
111 // preserves original behaviour prior to version 2.3
114 public FileFormatI identify(AlignmentFileReaderI file,
115 boolean closeSource) throws IOException
117 FileParse fp = new FileParse(file.getInFile(),
118 file.getDataSourceType());
119 return identify(fp, closeSource);
123 * Identify contents of source, closing it or resetting source to start
128 * @return (best guess at) file format
129 * @throws FileFormatException
131 public FileFormatI identify(FileParse source, boolean closeSource)
132 throws FileFormatException
134 FileFormatI reply = FileFormat.Pfam;
137 int trimmedLength = 0;
138 boolean lineswereskipped = false;
139 boolean isBinary = false; // true if length is non-zero and non-printable
140 // characters are encountered
148 boolean aaIndexHeaderRead = false;
150 while ((data = source.nextLine()) != null)
152 bytesRead += data.length();
153 trimmedLength += data.trim().length();
154 if (!lineswereskipped)
156 for (int i = 0; !isBinary && i < data.length(); i++)
158 char c = data.charAt(i);
159 isBinary = (c < 32 && c != '\t' && c != '\n' && c != '\r'
160 && c != 5 && c != 27); // nominal binary character filter
161 // excluding CR, LF, tab,DEL and ^E
162 // for certain blast ids
167 // jar files are special - since they contain all sorts of random
169 if (source.inFile != null)
171 String fileStr = source.inFile.getName();
172 if (fileStr.contains(".jar") || fileStr.contains(".zip")
173 || fileStr.contains(".jvp"))
175 // possibly a Jalview archive (but check further)
176 reply = FileFormat.Jalview;
179 if (!lineswereskipped && data.startsWith("PK"))
181 reply = FileFormat.Jalview; // archive
185 data = data.toUpperCase(Locale.ROOT);
187 if (data.startsWith(ScoreMatrixFile.SCOREMATRIX))
189 reply = FileFormat.ScoreMatrix;
192 if (data.startsWith("LOCUS"))
194 reply = FileFormat.GenBank;
197 if (data.startsWith("ID "))
199 if (data.substring(2).trim().split(";").length == 7)
201 reply = FileFormat.Embl;
205 if (data.startsWith("H ") && !aaIndexHeaderRead)
207 aaIndexHeaderRead = true;
209 if (data.startsWith("D ") && aaIndexHeaderRead)
211 reply = FileFormat.ScoreMatrix;
214 if (data.startsWith("##GFF-VERSION"))
216 // GFF - possibly embedded in a Jalview features file!
217 reply = FileFormat.Features;
220 if (looksLikeFeatureData(data))
222 reply = FileFormat.Features;
225 if (data.indexOf("# STOCKHOLM") > -1)
227 reply = FileFormat.Stockholm;
230 if (data.indexOf("_ENTRY.ID") > -1
231 || data.indexOf("_AUDIT_AUTHOR.NAME") > -1
232 || data.indexOf("_ATOM_SITE.") > -1)
234 reply = FileFormat.MMCif;
237 // if (data.indexOf(">") > -1)
238 if (data.startsWith(">"))
240 // FASTA, PIR file or BLC file
241 boolean checkPIR = false, starterm = false;
242 if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
244 // watch for PIR file attributes
246 reply = FileFormat.PIR;
248 // could also be BLC file, read next line to confirm
249 data = source.nextLine();
251 if (data.indexOf(">") > -1)
253 reply = FileFormat.BLC;
257 // Is this a single line BLC file?
258 String data1 = source.nextLine();
259 String data2 = source.nextLine();
263 starterm = (data1 != null && data1.indexOf("*") > -1)
264 || (data2 != null && data2.indexOf("*") > -1);
266 if (data2 != null && (c1 = data.indexOf("*")) > -1)
268 if (c1 == 0 && c1 == data2.indexOf("*"))
270 reply = FileFormat.BLC;
274 reply = FileFormat.Fasta; // possibly a bad choice - may be
278 // otherwise can still possibly be a PIR file
282 reply = FileFormat.Fasta;
283 // TODO : AMSA File is indicated if there is annotation in the
284 // FASTA file - but FASTA will automatically generate this at the
292 // final check for PIR content. require
293 // >P1;title\n<blah>\nterminated sequence to occur at least once.
295 // TODO the PIR/fasta ambiguity may be the use case that is needed to
297 // a 'Parse as type XXX' parameter for the applet/application.
307 dta = source.nextLine();
308 } catch (IOException ex)
311 if (dta != null && dta.indexOf("*") > -1)
315 } while (dta != null && !starterm);
319 reply = FileFormat.PIR;
324 reply = FileFormat.Fasta; // probably a bad choice!
327 // read as a FASTA (probably)
330 if (data.indexOf("{\"") > -1)
332 reply = FileFormat.Json;
335 int lessThan = data.indexOf("<");
336 if ((lessThan > -1)) // possible Markup Language data i.e HTML,
339 String upper = data.toUpperCase(Locale.ROOT);
340 if (upper.substring(lessThan).startsWith("<HTML"))
342 reply = FileFormat.Html;
345 if (upper.substring(lessThan).startsWith("<RNAML"))
347 reply = FileFormat.Rnaml;
352 if ((data.length() < 1) || (data.indexOf("#") == 0))
354 lineswereskipped = true;
358 if (data.indexOf("PILEUP") > -1)
360 reply = FileFormat.Pileup;
365 if ((data.indexOf("//") == 0) || ((data.indexOf("!!") > -1) && (data
366 .indexOf("!!") < data.indexOf("_MULTIPLE_ALIGNMENT "))))
368 reply = FileFormat.MSF;
372 else if (data.indexOf("CLUSTAL") > -1)
374 reply = FileFormat.Clustal;
379 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
381 reply = FileFormat.PDB;
384 else if (data.matches("\\s*\\d+\\s+\\d+\\s*"))
386 reply = FileFormat.Phylip;
391 if (!lineswereskipped && looksLikeJnetData(data))
393 reply = FileFormat.Jnet;
398 lineswereskipped = true; // this means there was some junk before any
399 // key file signature
407 source.reset(bytesRead); // so the file can be parsed from the mark
409 } catch (Exception ex)
411 System.err.println("File Identification failed!\n" + ex);
412 throw new FileFormatException(source.errormessage);
414 if (trimmedLength == 0)
417 "File Identification failed! - Empty file was read.");
418 throw new FileFormatException("EMPTY DATA FILE");
420 System.out.println("File format identified as " + reply.toString());
425 * Returns true if the data appears to be Jnet concise annotation format
430 protected boolean looksLikeJnetData(String data)
432 char firstChar = data.charAt(0);
433 int colonPos = data.indexOf(":");
434 int commaPos = data.indexOf(",");
435 boolean isJnet = firstChar != '*' && firstChar != ' ' && colonPos > -1
436 && commaPos > -1 && colonPos < commaPos;
437 // && data.indexOf(",")<data.indexOf(",", data.indexOf(","))) / ??
442 * Returns true if the data has at least 6 tab-delimited fields _and_ fields 4
443 * and 5 are integer (start/end)
448 protected boolean looksLikeFeatureData(String data)
454 String[] columns = data.split("\t");
455 if (columns.length < 6)
459 for (int col = 3; col < 5; col++)
463 Integer.parseInt(columns[col]);
464 } catch (NumberFormatException e)
477 public static void main(String[] args)
479 for (int i = 0; args != null && i < args.length; i++)
481 IdentifyFile ider = new IdentifyFile();
482 FileFormatI type = null;
485 type = ider.identify(args[i], DataSourceType.FILE);
486 } catch (FileFormatException e)
489 String.format("Error '%s' identifying file type for %s",
490 args[i], e.getMessage()));
492 System.out.println("Type of " + args[i] + " is " + type);
494 if (args == null || args.length == 0)
496 System.err.println("Usage: <Filename> [<Filename> ...]");