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;
24 import java.util.Locale;
32 public class IdentifyFile
35 * Identify a datasource's file content.
37 * @note Do not use this method for stream sources - create a FileParse object
43 * @throws FileFormatException
45 public FileFormatI identify(String file, DataSourceType sourceType)
46 throws FileFormatException
48 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
49 FileParse parser = null;
52 parser = new FileParse(file, sourceType);
55 return identify(parser);
59 System.err.println("Error whilst identifying");
60 e.printStackTrace(System.err);
61 emessage = e.getMessage();
65 throw new FileFormatException(parser.errormessage);
67 throw new FileFormatException(emessage);
70 public FileFormatI identify(FileParse source) throws FileFormatException
72 return identify(source, true);
73 // preserves original behaviour prior to version 2.3
76 public FileFormatI identify(AlignmentFileReaderI file,
77 boolean closeSource) throws IOException
79 FileParse fp = new FileParse(file.getInFile(),
80 file.getDataSourceType());
81 return identify(fp, closeSource);
85 * Identify contents of source, closing it or resetting source to start
90 * @return (best guess at) file format
91 * @throws FileFormatException
93 public FileFormatI identify(FileParse source, boolean closeSource)
94 throws FileFormatException
96 FileFormatI reply = FileFormat.Pfam;
99 int trimmedLength = 0;
100 boolean lineswereskipped = false;
101 boolean isBinary = false; // true if length is non-zero and non-printable
102 // characters are encountered
110 boolean aaIndexHeaderRead = false;
112 while ((data = source.nextLine()) != null)
114 bytesRead += data.length();
115 trimmedLength += data.trim().length();
116 if (!lineswereskipped)
118 for (int i = 0; !isBinary && i < data.length(); i++)
120 char c = data.charAt(i);
121 isBinary = (c < 32 && c != '\t' && c != '\n' && c != '\r'
122 && c != 5 && c != 27); // nominal binary character filter
123 // excluding CR, LF, tab,DEL and ^E
124 // for certain blast ids
129 // jar files are special - since they contain all sorts of random
131 if (source.inFile != null)
133 String fileStr = source.inFile.getName();
134 // possibly a Jalview archive.
135 if (fileStr.lastIndexOf(".jar") > -1
136 || fileStr.lastIndexOf(".zip") > -1)
138 reply = FileFormat.Jalview;
141 if (!lineswereskipped && data.startsWith("PK"))
143 reply = FileFormat.Jalview; // archive.
147 data = data.toUpperCase(Locale.ROOT);
149 if (data.startsWith(ScoreMatrixFile.SCOREMATRIX))
151 reply = FileFormat.ScoreMatrix;
154 if (data.startsWith("H ") && !aaIndexHeaderRead)
156 aaIndexHeaderRead = true;
158 if (data.startsWith("D ") && aaIndexHeaderRead)
160 reply = FileFormat.ScoreMatrix;
163 if (data.startsWith("##GFF-VERSION"))
165 // GFF - possibly embedded in a Jalview features file!
166 reply = FileFormat.Features;
169 if (looksLikeFeatureData(data))
171 reply = FileFormat.Features;
174 if (data.indexOf("# STOCKHOLM") > -1)
176 reply = FileFormat.Stockholm;
179 if (data.indexOf("_ENTRY.ID") > -1
180 || data.indexOf("_AUDIT_AUTHOR.NAME") > -1
181 || data.indexOf("_ATOM_SITE.") > -1)
183 reply = FileFormat.MMCif;
186 // if (data.indexOf(">") > -1)
187 if (data.startsWith(">"))
189 // FASTA, PIR file or BLC file
190 boolean checkPIR = false, starterm = false;
191 if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
193 // watch for PIR file attributes
195 reply = FileFormat.PIR;
197 // could also be BLC file, read next line to confirm
198 data = source.nextLine();
200 if (data.indexOf(">") > -1)
202 reply = FileFormat.BLC;
206 // Is this a single line BLC file?
207 String data1 = source.nextLine();
208 String data2 = source.nextLine();
212 starterm = (data1 != null && data1.indexOf("*") > -1)
213 || (data2 != null && data2.indexOf("*") > -1);
215 if (data2 != null && (c1 = data.indexOf("*")) > -1)
217 if (c1 == 0 && c1 == data2.indexOf("*"))
219 reply = FileFormat.BLC;
223 reply = FileFormat.Fasta; // possibly a bad choice - may be
227 // otherwise can still possibly be a PIR file
231 reply = FileFormat.Fasta;
232 // TODO : AMSA File is indicated if there is annotation in the
233 // FASTA file - but FASTA will automatically generate this at the
241 // final check for PIR content. require
242 // >P1;title\n<blah>\nterminated sequence to occur at least once.
244 // TODO the PIR/fasta ambiguity may be the use case that is needed to
246 // a 'Parse as type XXX' parameter for the applet/application.
256 dta = source.nextLine();
257 } catch (IOException ex)
260 if (dta != null && dta.indexOf("*") > -1)
264 } while (dta != null && !starterm);
268 reply = FileFormat.PIR;
273 reply = FileFormat.Fasta; // probably a bad choice!
276 // read as a FASTA (probably)
279 if (data.indexOf("{\"") > -1)
281 reply = FileFormat.Json;
284 int lessThan = data.indexOf("<");
285 if ((lessThan > -1)) // possible Markup Language data i.e HTML,
288 String upper = data.toUpperCase(Locale.ROOT);
289 if (upper.substring(lessThan).startsWith("<HTML"))
291 reply = FileFormat.Html;
294 if (upper.substring(lessThan).startsWith("<RNAML"))
296 reply = FileFormat.Rnaml;
301 if ((data.length() < 1) || (data.indexOf("#") == 0))
303 lineswereskipped = true;
307 if (data.indexOf("PILEUP") > -1)
309 reply = FileFormat.Pileup;
314 if ((data.indexOf("//") == 0) || ((data.indexOf("!!") > -1) && (data
315 .indexOf("!!") < data.indexOf("_MULTIPLE_ALIGNMENT "))))
317 reply = FileFormat.MSF;
321 else if (data.indexOf("CLUSTAL") > -1)
323 reply = FileFormat.Clustal;
328 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
330 reply = FileFormat.PDB;
333 else if (data.matches("\\s*\\d+\\s+\\d+\\s*"))
335 reply = FileFormat.Phylip;
340 if (!lineswereskipped && looksLikeJnetData(data))
342 reply = FileFormat.Jnet;
347 lineswereskipped = true; // this means there was some junk before any
348 // key file signature
356 source.reset(bytesRead); // so the file can be parsed from the mark
358 } catch (Exception ex)
360 System.err.println("File Identification failed!\n" + ex);
361 throw new FileFormatException(source.errormessage);
363 if (trimmedLength == 0)
366 "File Identification failed! - Empty file was read.");
367 throw new FileFormatException("EMPTY DATA FILE");
369 System.out.println("File format identified as " + reply.toString());
374 * Returns true if the data appears to be Jnet concise annotation format
379 protected boolean looksLikeJnetData(String data)
381 char firstChar = data.charAt(0);
382 int colonPos = data.indexOf(":");
383 int commaPos = data.indexOf(",");
384 boolean isJnet = firstChar != '*' && firstChar != ' ' && colonPos > -1
385 && commaPos > -1 && colonPos < commaPos;
386 // && data.indexOf(",")<data.indexOf(",", data.indexOf(","))) / ??
391 * Returns true if the data has at least 6 tab-delimited fields _and_ fields 4
392 * and 5 are integer (start/end)
397 protected boolean looksLikeFeatureData(String data)
403 String[] columns = data.split("\t");
404 if (columns.length < 6)
408 for (int col = 3; col < 5; col++)
412 Integer.parseInt(columns[col]);
413 } catch (NumberFormatException e)
421 public static void main(String[] args)
423 for (int i = 0; args != null && i < args.length; i++)
425 IdentifyFile ider = new IdentifyFile();
426 FileFormatI type = null;
429 type = ider.identify(args[i], DataSourceType.FILE);
430 } catch (FileFormatException e)
433 String.format("Error '%s' identifying file type for %s",
434 args[i], e.getMessage()));
436 System.out.println("Type of " + args[i] + " is " + type);
438 if (args == null || args.length == 0)
440 System.err.println("Usage: <Filename> [<Filename> ...]");