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,
76 boolean closeSource) throws IOException
78 FileParse fp = new FileParse(file.getInFile(),
79 file.getDataSourceType());
80 return identify(fp, closeSource);
84 * Identify contents of source, closing it or resetting source to start
89 * @return (best guess at) file format
90 * @throws FileFormatException
92 public FileFormatI identify(FileParse source, boolean closeSource)
93 throws FileFormatException
95 FileFormatI reply = FileFormat.Pfam;
98 int trimmedLength = 0;
99 boolean lineswereskipped = false;
100 boolean isBinary = false; // true if length is non-zero and non-printable
101 // characters are encountered
109 boolean aaIndexHeaderRead = false;
111 while ((data = source.nextLine()) != null)
113 bytesRead += data.length();
114 trimmedLength += data.trim().length();
115 if (!lineswereskipped)
117 for (int i = 0; !isBinary && i < data.length(); i++)
119 char c = data.charAt(i);
120 isBinary = (c < 32 && c != '\t' && c != '\n' && c != '\r'
121 && c != 5 && c != 27); // nominal binary character filter
122 // excluding CR, LF, tab,DEL and ^E
123 // for certain blast ids
128 // jar files are special - since they contain all sorts of random
130 if (source.inFile != null)
132 String fileStr = source.inFile.getName();
133 // possibly a Jalview archive.
134 if (fileStr.lastIndexOf(".jar") > -1
135 || fileStr.lastIndexOf(".zip") > -1)
137 reply = FileFormat.Jalview;
140 if (!lineswereskipped && data.startsWith("PK"))
142 reply = FileFormat.Jalview; // archive.
146 data = data.toUpperCase();
148 if (data.startsWith(ScoreMatrixFile.SCOREMATRIX))
150 reply = FileFormat.ScoreMatrix;
153 if (data.startsWith("LOCUS"))
155 reply = FileFormat.GenBank;
158 if (data.startsWith("ID "))
160 if (data.substring(2).trim().split(";").length == 7)
162 reply = FileFormat.Embl;
166 if (data.startsWith("H ") && !aaIndexHeaderRead)
168 aaIndexHeaderRead = true;
170 if (data.startsWith("D ") && aaIndexHeaderRead)
172 reply = FileFormat.ScoreMatrix;
175 if (data.startsWith("##GFF-VERSION"))
177 // GFF - possibly embedded in a Jalview features file!
178 reply = FileFormat.Features;
181 if (looksLikeFeatureData(data))
183 reply = FileFormat.Features;
186 if (data.indexOf("# STOCKHOLM") > -1)
188 reply = FileFormat.Stockholm;
191 if (data.indexOf("_ENTRY.ID") > -1
192 || data.indexOf("_AUDIT_AUTHOR.NAME") > -1
193 || data.indexOf("_ATOM_SITE.") > -1)
195 reply = FileFormat.MMCif;
198 // if (data.indexOf(">") > -1)
199 if (data.startsWith(">"))
201 // FASTA, PIR file or BLC file
202 boolean checkPIR = false, starterm = false;
203 if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
205 // watch for PIR file attributes
207 reply = FileFormat.PIR;
209 // could also be BLC file, read next line to confirm
210 data = source.nextLine();
212 if (data.indexOf(">") > -1)
214 reply = FileFormat.BLC;
218 // Is this a single line BLC file?
219 String data1 = source.nextLine();
220 String data2 = source.nextLine();
224 starterm = (data1 != null && data1.indexOf("*") > -1)
225 || (data2 != null && data2.indexOf("*") > -1);
227 if (data2 != null && (c1 = data.indexOf("*")) > -1)
229 if (c1 == 0 && c1 == data2.indexOf("*"))
231 reply = FileFormat.BLC;
235 reply = FileFormat.Fasta; // possibly a bad choice - may be
239 // otherwise can still possibly be a PIR file
243 reply = FileFormat.Fasta;
244 // TODO : AMSA File is indicated if there is annotation in the
245 // FASTA file - but FASTA will automatically generate this at the
253 // final check for PIR content. require
254 // >P1;title\n<blah>\nterminated sequence to occur at least once.
256 // TODO the PIR/fasta ambiguity may be the use case that is needed to
258 // a 'Parse as type XXX' parameter for the applet/application.
268 dta = source.nextLine();
269 } catch (IOException ex)
272 if (dta != null && dta.indexOf("*") > -1)
276 } while (dta != null && !starterm);
280 reply = FileFormat.PIR;
285 reply = FileFormat.Fasta; // probably a bad choice!
288 // read as a FASTA (probably)
291 if (data.indexOf("{\"") > -1)
293 reply = FileFormat.Json;
296 int lessThan = data.indexOf("<");
297 if ((lessThan > -1)) // possible Markup Language data i.e HTML,
300 String upper = data.toUpperCase();
301 if (upper.substring(lessThan).startsWith("<HTML"))
303 reply = FileFormat.Html;
306 if (upper.substring(lessThan).startsWith("<RNAML"))
308 reply = FileFormat.Rnaml;
313 if ((data.length() < 1) || (data.indexOf("#") == 0))
315 lineswereskipped = true;
319 if (data.indexOf("PILEUP") > -1)
321 reply = FileFormat.Pileup;
326 if ((data.indexOf("//") == 0) || ((data.indexOf("!!") > -1) && (data
327 .indexOf("!!") < data.indexOf("_MULTIPLE_ALIGNMENT "))))
329 reply = FileFormat.MSF;
333 else if (data.indexOf("CLUSTAL") > -1)
335 reply = FileFormat.Clustal;
340 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
342 reply = FileFormat.PDB;
345 else if (data.matches("\\s*\\d+\\s+\\d+\\s*"))
347 reply = FileFormat.Phylip;
352 if (!lineswereskipped && looksLikeJnetData(data))
354 reply = FileFormat.Jnet;
359 lineswereskipped = true; // this means there was some junk before any
360 // key file signature
368 source.reset(bytesRead); // so the file can be parsed from the mark
370 } catch (Exception ex)
372 System.err.println("File Identification failed!\n" + ex);
373 throw new FileFormatException(source.errormessage);
375 if (trimmedLength == 0)
378 "File Identification failed! - Empty file was read.");
379 throw new FileFormatException("EMPTY DATA FILE");
381 System.out.println("File format identified as " + reply.toString());
386 * Returns true if the data appears to be Jnet concise annotation format
391 protected boolean looksLikeJnetData(String data)
393 char firstChar = data.charAt(0);
394 int colonPos = data.indexOf(":");
395 int commaPos = data.indexOf(",");
396 boolean isJnet = firstChar != '*' && firstChar != ' ' && colonPos > -1
397 && commaPos > -1 && colonPos < commaPos;
398 // && data.indexOf(",")<data.indexOf(",", data.indexOf(","))) / ??
403 * Returns true if the data has at least 6 tab-delimited fields _and_ fields 4
404 * and 5 are integer (start/end)
409 protected boolean looksLikeFeatureData(String data)
415 String[] columns = data.split("\t");
416 if (columns.length < 6)
420 for (int col = 3; col < 5; col++)
424 Integer.parseInt(columns[col]);
425 } catch (NumberFormatException e)
433 public static void main(String[] args)
435 for (int i = 0; args != null && i < args.length; i++)
437 IdentifyFile ider = new IdentifyFile();
438 FileFormatI type = null;
441 type = ider.identify(args[i], DataSourceType.FILE);
442 } catch (FileFormatException e)
445 String.format("Error '%s' identifying file type for %s",
446 args[i], e.getMessage()));
448 System.out.println("Type of " + args[i] + " is " + type);
450 if (args == null || args.length == 0)
452 System.err.println("Usage: <Filename> [<Filename> ...]");