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;
32 public class IdentifyFile
34 public FileFormatI identify(File file, DataSourceType sourceType)
35 throws FileFormatException
37 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
38 FileParse parser = null;
41 parser = new FileParse(file, sourceType);
44 return identify(parser);
48 System.err.println("Error whilst identifying " + file);
49 e.printStackTrace(System.err);
50 emessage = e.getMessage();
54 throw new FileFormatException(parser.errormessage);
56 throw new FileFormatException(emessage);
60 * Identify a datasource's file content.
62 * @note Do not use this method for stream sources - create a FileParse object
68 * @throws FileFormatException
70 public FileFormatI identify(String file, DataSourceType sourceType)
71 throws FileFormatException
73 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
74 FileParse parser = null;
77 parser = new FileParse(file, sourceType);
80 return identify(parser);
84 System.err.println("Error whilst identifying " + file);
85 e.printStackTrace(System.err);
86 emessage = e.getMessage();
90 throw new FileFormatException(parser.errormessage);
92 throw new FileFormatException(emessage);
95 public FileFormatI identify(FileParse source) throws FileFormatException
97 return identify(source, true);
98 // preserves original behaviour prior to version 2.3
101 public FileFormatI identify(AlignmentFileReaderI file,
102 boolean closeSource) throws IOException
104 FileParse fp = new FileParse(file.getInFile(),
105 file.getDataSourceType());
106 return identify(fp, closeSource);
110 * Identify contents of source, closing it or resetting source to start
115 * @return (best guess at) file format
116 * @throws FileFormatException
118 public FileFormatI identify(FileParse source, boolean closeSource)
119 throws FileFormatException
121 FileFormatI reply = FileFormat.Pfam;
124 int trimmedLength = 0;
125 boolean lineswereskipped = false;
126 boolean isBinary = false; // true if length is non-zero and non-printable
127 // characters are encountered
135 boolean aaIndexHeaderRead = false;
137 while ((data = source.nextLine()) != null)
139 bytesRead += data.length();
140 trimmedLength += data.trim().length();
141 if (!lineswereskipped)
143 for (int i = 0; !isBinary && i < data.length(); i++)
145 char c = data.charAt(i);
146 isBinary = (c < 32 && c != '\t' && c != '\n' && c != '\r'
147 && c != 5 && c != 27); // nominal binary character filter
148 // excluding CR, LF, tab,DEL and ^E
149 // for certain blast ids
154 // jar files are special - since they contain all sorts of random
156 if (source.inFile != null)
158 String fileStr = source.inFile.getName();
159 // possibly a Jalview archive.
160 if (fileStr.lastIndexOf(".jar") > -1
161 || fileStr.lastIndexOf(".zip") > -1)
163 reply = FileFormat.Jalview;
166 if (!lineswereskipped && data.startsWith("PK"))
168 reply = FileFormat.Jalview; // archive.
172 data = data.toUpperCase();
174 if (data.startsWith(ScoreMatrixFile.SCOREMATRIX))
176 reply = FileFormat.ScoreMatrix;
179 if (data.startsWith("H ") && !aaIndexHeaderRead)
181 aaIndexHeaderRead = true;
183 if (data.startsWith("D ") && aaIndexHeaderRead)
185 reply = FileFormat.ScoreMatrix;
188 if (data.startsWith("##GFF-VERSION"))
190 // GFF - possibly embedded in a Jalview features file!
191 reply = FileFormat.Features;
194 if (looksLikeFeatureData(data))
196 reply = FileFormat.Features;
199 if (data.indexOf("# STOCKHOLM") > -1)
201 reply = FileFormat.Stockholm;
204 if (data.indexOf("_ENTRY.ID") > -1
205 || data.indexOf("_AUDIT_AUTHOR.NAME") > -1
206 || data.indexOf("_ATOM_SITE.") > -1)
208 reply = FileFormat.MMCif;
211 // if (data.indexOf(">") > -1)
212 if (data.startsWith(">"))
214 // FASTA, PIR file or BLC file
215 boolean checkPIR = false, starterm = false;
216 if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
218 // watch for PIR file attributes
220 reply = FileFormat.PIR;
222 // could also be BLC file, read next line to confirm
223 data = source.nextLine();
225 if (data.indexOf(">") > -1)
227 reply = FileFormat.BLC;
231 // Is this a single line BLC file?
232 String data1 = source.nextLine();
233 String data2 = source.nextLine();
237 starterm = (data1 != null && data1.indexOf("*") > -1)
238 || (data2 != null && data2.indexOf("*") > -1);
240 if (data2 != null && (c1 = data.indexOf("*")) > -1)
242 if (c1 == 0 && c1 == data2.indexOf("*"))
244 reply = FileFormat.BLC;
248 reply = FileFormat.Fasta; // possibly a bad choice - may be
252 // otherwise can still possibly be a PIR file
256 reply = FileFormat.Fasta;
257 // TODO : AMSA File is indicated if there is annotation in the
258 // FASTA file - but FASTA will automatically generate this at the
266 // final check for PIR content. require
267 // >P1;title\n<blah>\nterminated sequence to occur at least once.
269 // TODO the PIR/fasta ambiguity may be the use case that is needed to
271 // a 'Parse as type XXX' parameter for the applet/application.
281 dta = source.nextLine();
282 } catch (IOException ex)
285 if (dta != null && dta.indexOf("*") > -1)
289 } while (dta != null && !starterm);
293 reply = FileFormat.PIR;
298 reply = FileFormat.Fasta; // probably a bad choice!
301 // read as a FASTA (probably)
304 if (data.indexOf("{\"") > -1)
306 reply = FileFormat.Json;
309 int lessThan = data.indexOf("<");
310 if ((lessThan > -1)) // possible Markup Language data i.e HTML,
313 String upper = data.toUpperCase();
314 if (upper.substring(lessThan).startsWith("<HTML"))
316 reply = FileFormat.Html;
319 if (upper.substring(lessThan).startsWith("<RNAML"))
321 reply = FileFormat.Rnaml;
326 if ((data.length() < 1) || (data.indexOf("#") == 0))
328 lineswereskipped = true;
332 if (data.indexOf("PILEUP") > -1)
334 reply = FileFormat.Pileup;
339 if ((data.indexOf("//") == 0) || ((data.indexOf("!!") > -1) && (data
340 .indexOf("!!") < data.indexOf("_MULTIPLE_ALIGNMENT "))))
342 reply = FileFormat.MSF;
346 else if (data.indexOf("CLUSTAL") > -1)
348 reply = FileFormat.Clustal;
353 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
355 reply = FileFormat.PDB;
358 else if (data.matches("\\s*\\d+\\s+\\d+\\s*"))
360 reply = FileFormat.Phylip;
365 if (!lineswereskipped && looksLikeJnetData(data))
367 reply = FileFormat.Jnet;
372 lineswereskipped = true; // this means there was some junk before any
373 // key file signature
381 source.reset(bytesRead); // so the file can be parsed from the mark
383 } catch (Exception ex)
385 System.err.println("File Identification failed!\n" + ex);
386 throw new FileFormatException(source.errormessage);
388 if (trimmedLength == 0)
391 "File Identification failed! - Empty file was read.");
392 throw new FileFormatException("EMPTY DATA FILE");
394 System.out.println("File format identified as " + reply.toString());
399 * Returns true if the data appears to be Jnet concise annotation format
404 protected boolean looksLikeJnetData(String data)
406 char firstChar = data.charAt(0);
407 int colonPos = data.indexOf(":");
408 int commaPos = data.indexOf(",");
409 boolean isJnet = firstChar != '*' && firstChar != ' ' && colonPos > -1
410 && commaPos > -1 && colonPos < commaPos;
411 // && data.indexOf(",")<data.indexOf(",", data.indexOf(","))) / ??
416 * Returns true if the data has at least 6 tab-delimited fields _and_ fields 4
417 * and 5 are integer (start/end)
422 protected boolean looksLikeFeatureData(String data)
428 String[] columns = data.split("\t");
429 if (columns.length < 6)
433 for (int col = 3; col < 5; col++)
437 Integer.parseInt(columns[col]);
438 } catch (NumberFormatException e)
446 public static void main(String[] args)
448 for (int i = 0; args != null && i < args.length; i++)
450 IdentifyFile ider = new IdentifyFile();
451 FileFormatI type = null;
454 type = ider.identify(args[i], DataSourceType.FILE);
455 } catch (FileFormatException e)
458 String.format("Error '%s' identifying file type for %s",
459 args[i], e.getMessage()));
461 System.out.println("Type of " + args[i] + " is " + type);
463 if (args == null || args.length == 0)
465 System.err.println("Usage: <Filename> [<Filename> ...]");