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
34 * Identify a datasource's file content.
36 * @note Do not use this method for stream sources - create a FileParse object
42 * @throws FileFormatException
44 public FileFormatI identify(String file, DataSourceType sourceType)
45 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");
59 e.printStackTrace(System.err);
60 emessage = e.getMessage();
64 throw new FileFormatException(parser.errormessage);
66 throw new FileFormatException(emessage);
69 public FileFormatI identify(FileParse source) throws FileFormatException
71 return identify(source, true);
72 // preserves original behaviour prior to version 2.3
75 public FileFormatI identify(AlignmentFileReaderI file, boolean closeSource)
78 FileParse fp = new FileParse(file.getInFile(), file.getDataSourceType());
79 return identify(fp, closeSource);
83 * Identify contents of source, closing it or resetting source to start
88 * @return (best guess at) file format
89 * @throws FileFormatException
91 public FileFormatI identify(FileParse source, boolean closeSource)
92 throws FileFormatException
94 FileFormatI reply = FileFormat.Pfam;
97 int trimmedLength = 0;
98 boolean lineswereskipped = false;
99 boolean isBinary = false; // true if length is non-zero and non-printable
100 // characters are encountered
107 while ((data = source.nextLine()) != null)
109 bytesRead += data.length();
110 trimmedLength += data.trim().length();
111 if (!lineswereskipped)
113 for (int i = 0; !isBinary && i < data.length(); i++)
115 char c = data.charAt(i);
116 isBinary = (c < 32 && c != '\t' && c != '\n' && c != '\r'
117 && c != 5 && c != 27); // nominal binary character filter
118 // excluding CR, LF, tab,DEL and ^E
119 // for certain blast ids
124 // jar files are special - since they contain all sorts of random
126 if (source.inFile != null)
128 String fileStr = source.inFile.getName();
129 // possibly a Jalview archive.
130 if (fileStr.lastIndexOf(".jar") > -1
131 || fileStr.lastIndexOf(".zip") > -1)
133 reply = FileFormat.Jalview;
136 if (!lineswereskipped && data.startsWith("PK"))
138 reply = FileFormat.Jalview; // archive.
142 data = data.toUpperCase();
144 if (data.startsWith(ScoreMatrixFile.SCOREMATRIX))
146 reply = FileFormat.ScoreMatrix;
149 if (data.startsWith("##GFF-VERSION"))
151 // GFF - possibly embedded in a Jalview features file!
152 reply = FileFormat.Features;
155 if (looksLikeFeatureData(data))
157 reply = FileFormat.Features;
160 if (data.indexOf("# STOCKHOLM") > -1)
162 reply = FileFormat.Stockholm;
165 if (data.indexOf("_ENTRY.ID") > -1
166 || data.indexOf("_AUDIT_AUTHOR.NAME") > -1
167 || data.indexOf("_ATOM_SITE.") > -1)
169 reply = FileFormat.MMCif;
172 // if (data.indexOf(">") > -1)
173 if (data.startsWith(">"))
175 // FASTA, PIR file or BLC file
176 boolean checkPIR = false, starterm = false;
177 if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
179 // watch for PIR file attributes
181 reply = FileFormat.PIR;
183 // could also be BLC file, read next line to confirm
184 data = source.nextLine();
186 if (data.indexOf(">") > -1)
188 reply = FileFormat.BLC;
192 // Is this a single line BLC file?
193 String data1 = source.nextLine();
194 String data2 = source.nextLine();
198 starterm = (data1 != null && data1.indexOf("*") > -1)
199 || (data2 != null && data2.indexOf("*") > -1);
201 if (data2 != null && (c1 = data.indexOf("*")) > -1)
203 if (c1 == 0 && c1 == data2.indexOf("*"))
205 reply = FileFormat.BLC;
209 reply = FileFormat.Fasta; // possibly a bad choice - may be
213 // otherwise can still possibly be a PIR file
217 reply = FileFormat.Fasta;
218 // TODO : AMSA File is indicated if there is annotation in the
219 // FASTA file - but FASTA will automatically generate this at the
227 // final check for PIR content. require
228 // >P1;title\n<blah>\nterminated sequence to occur at least once.
230 // TODO the PIR/fasta ambiguity may be the use case that is needed to
232 // a 'Parse as type XXX' parameter for the applet/application.
242 dta = source.nextLine();
243 } catch (IOException ex)
246 if (dta != null && dta.indexOf("*") > -1)
250 } while (dta != null && !starterm);
254 reply = FileFormat.PIR;
259 reply = FileFormat.Fasta; // probably a bad choice!
262 // read as a FASTA (probably)
265 int lessThan = data.indexOf("<");
266 if ((lessThan > -1)) // possible Markup Language data i.e HTML,
269 String upper = data.toUpperCase();
270 if (upper.substring(lessThan).startsWith("<HTML"))
272 reply = FileFormat.Html;
275 if (upper.substring(lessThan).startsWith("<RNAML"))
277 reply = FileFormat.Rnaml;
282 if (data.indexOf("{\"") > -1)
284 reply = FileFormat.Json;
287 if ((data.length() < 1) || (data.indexOf("#") == 0))
289 lineswereskipped = true;
293 if (data.indexOf("PILEUP") > -1)
295 reply = FileFormat.Pileup;
300 if ((data.indexOf("//") == 0)
301 || ((data.indexOf("!!") > -1) && (data.indexOf("!!") < data
302 .indexOf("_MULTIPLE_ALIGNMENT "))))
304 reply = FileFormat.MSF;
308 else if (data.indexOf("CLUSTAL") > -1)
310 reply = FileFormat.Clustal;
315 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
317 reply = FileFormat.PDB;
320 else if (data.matches("\\s*\\d+\\s+\\d+\\s*"))
322 reply = FileFormat.Phylip;
327 if (!lineswereskipped && looksLikeJnetData(data))
329 reply = FileFormat.Jnet;
334 lineswereskipped = true; // this means there was some junk before any
335 // key file signature
343 source.reset(bytesRead); // so the file can be parsed from the mark
345 } catch (Exception ex)
347 System.err.println("File Identification failed!\n" + ex);
348 throw new FileFormatException(source.errormessage);
350 if (trimmedLength == 0)
353 .println("File Identification failed! - Empty file was read.");
354 throw new FileFormatException("EMPTY DATA FILE");
356 System.out.println("File format identified as " + reply.toString());
361 * Returns true if the data appears to be Jnet concise annotation format
366 protected boolean looksLikeJnetData(String data)
368 char firstChar = data.charAt(0);
369 int colonPos = data.indexOf(":");
370 int commaPos = data.indexOf(",");
371 boolean isJnet = firstChar != '*' && firstChar != ' ' && colonPos > -1
372 && commaPos > -1 && colonPos < commaPos;
373 // && data.indexOf(",")<data.indexOf(",", data.indexOf(","))) / ??
378 * Returns true if the data has at least 6 tab-delimited fields _and_ fields 4
379 * and 5 are integer (start/end)
384 protected boolean looksLikeFeatureData(String data)
390 String[] columns = data.split("\t");
391 if (columns.length < 6)
395 for (int col = 3; col < 5; col++)
399 Integer.parseInt(columns[col]);
400 } catch (NumberFormatException e)
408 public static void main(String[] args)
410 for (int i = 0; args != null && i < args.length; i++)
412 IdentifyFile ider = new IdentifyFile();
413 FileFormatI type = null;
416 type = ider.identify(args[i], DataSourceType.FILE);
417 } catch (FileFormatException e)
419 System.err.println(String.format(
420 "Error '%s' identifying file type for %s", args[i],
423 System.out.println("Type of " + args[i] + " is " + type);
425 if (args == null || args.length == 0)
427 System.err.println("Usage: <Filename> [<Filename> ...]");