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
35 public FileFormatI identify(Object file, DataSourceType protocol) throws FileFormatException
38 return (file instanceof File ? identify((File) file, protocol) : identify((String) file, protocol));
42 public FileFormatI identify(File file, DataSourceType sourceType)
43 throws FileFormatException
46 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
47 FileParse parser = null;
50 parser = new FileParse(file, sourceType);
53 return identify(parser);
57 System.err.println("Error whilst identifying " + file);
58 e.printStackTrace(System.err);
59 emessage = e.getMessage();
63 throw new FileFormatException(parser.errormessage);
65 throw new FileFormatException(emessage);
69 * Identify a datasource's file content.
71 * @note Do not use this method for stream sources - create a FileParse object
77 * @throws FileFormatException
79 public FileFormatI identify(String file, DataSourceType sourceType)
80 throws FileFormatException
82 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
83 FileParse parser = null;
86 parser = new FileParse(file, sourceType);
89 return identify(parser);
93 System.err.println("Error whilst identifying " + file);
94 e.printStackTrace(System.err);
95 emessage = e.getMessage();
99 throw new FileFormatException(parser.errormessage);
101 throw new FileFormatException(emessage);
104 public FileFormatI identify(FileParse source) throws FileFormatException
106 return identify(source, true);
107 // preserves original behaviour prior to version 2.3
110 public FileFormatI identify(AlignmentFileReaderI file,
111 boolean closeSource) throws IOException
113 FileParse fp = new FileParse(file.getInFile(),
114 file.getDataSourceType());
115 return identify(fp, closeSource);
119 * Identify contents of source, closing it or resetting source to start
124 * @return (best guess at) file format
125 * @throws FileFormatException
127 public FileFormatI identify(FileParse source, boolean closeSource)
128 throws FileFormatException
130 FileFormatI reply = FileFormat.Pfam;
133 int trimmedLength = 0;
134 boolean lineswereskipped = false;
135 boolean isBinary = false; // true if length is non-zero and non-printable
136 // characters are encountered
144 boolean aaIndexHeaderRead = false;
146 while ((data = source.nextLine()) != null)
148 bytesRead += data.length();
149 trimmedLength += data.trim().length();
150 if (!lineswereskipped)
152 for (int i = 0; !isBinary && i < data.length(); i++)
154 int c = data.charAt(i);
155 isBinary = (c < 32 && c != '\t' && c != '\n' && c != '\r'
156 && c != 5 && c != 27); // nominal binary character filter
157 // excluding CR, LF, tab,DEL and ^E
158 // for certain blast ids
163 // jar files are special - since they contain all sorts of random
165 if (source.inFile != null)
167 String fileStr = source.inFile.getName();
168 if (fileStr.contains(".jar") || fileStr.contains(".zip")
169 || fileStr.contains(".jvp"))
171 // possibly a Jalview archive (but check further)
172 reply = FileFormat.Jalview;
176 if (!lineswereskipped && data.startsWith("PK"))
178 reply = FileFormat.Jalview; // archive
182 if (data.startsWith("<?xml"))
184 data = source.nextLine();
185 if (data.startsWith("<Bsml>"))
187 reply = FileFormat.BSML;
191 data = data.toUpperCase();
193 if (data.startsWith(ScoreMatrixFile.SCOREMATRIX))
195 reply = FileFormat.ScoreMatrix;
198 if (data.startsWith("H ") && !aaIndexHeaderRead)
200 aaIndexHeaderRead = true;
202 if (data.startsWith("D ") && aaIndexHeaderRead)
204 reply = FileFormat.ScoreMatrix;
207 if (data.startsWith("##GFF-VERSION"))
209 // GFF - possibly embedded in a Jalview features file!
210 reply = FileFormat.Features;
213 if (looksLikeFeatureData(data))
215 reply = FileFormat.Features;
218 if (data.indexOf("# STOCKHOLM") > -1)
220 reply = FileFormat.Stockholm;
223 if (data.indexOf("_ENTRY.ID") > -1
224 || data.indexOf("_AUDIT_AUTHOR.NAME") > -1
225 || data.indexOf("_ATOM_SITE.") > -1)
227 reply = FileFormat.MMCif;
230 // if (data.indexOf(">") > -1)
231 if (data.startsWith(">"))
233 // FASTA, PIR file or BLC file
234 boolean checkPIR = false, starterm = false;
235 if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
237 // watch for PIR file attributes
239 reply = FileFormat.PIR;
241 // could also be BLC file, read next line to confirm
242 data = source.nextLine();
244 if (data.indexOf(">") > -1)
246 reply = FileFormat.BLC;
250 // Is this a single line BLC file?
251 String data1 = source.nextLine();
252 String data2 = source.nextLine();
256 starterm = (data1 != null && data1.indexOf("*") > -1)
257 || (data2 != null && data2.indexOf("*") > -1);
259 if (data2 != null && (c1 = data.indexOf("*")) > -1)
261 if (c1 == 0 && c1 == data2.indexOf("*"))
263 reply = FileFormat.BLC;
267 reply = FileFormat.Fasta; // possibly a bad choice - may be
271 // otherwise can still possibly be a PIR file
275 reply = FileFormat.Fasta;
276 // TODO : AMSA File is indicated if there is annotation in the
277 // FASTA file - but FASTA will automatically generate this at the
285 // final check for PIR content. require
286 // >P1;title\n<blah>\nterminated sequence to occur at least once.
288 // TODO the PIR/fasta ambiguity may be the use case that is needed to
290 // a 'Parse as type XXX' parameter for the applet/application.
300 dta = source.nextLine();
301 } catch (IOException ex)
304 if (dta != null && dta.indexOf("*") > -1)
308 } while (dta != null && !starterm);
312 reply = FileFormat.PIR;
317 reply = FileFormat.Fasta; // probably a bad choice!
320 // read as a FASTA (probably)
323 if (data.indexOf("{\"") > -1)
325 reply = FileFormat.Json;
328 int lessThan = data.indexOf("<");
329 if ((lessThan > -1)) // possible Markup Language data i.e HTML,
332 String upper = data.toUpperCase();
333 if (upper.substring(lessThan).startsWith("<HTML"))
335 reply = FileFormat.Html;
338 if (upper.substring(lessThan).startsWith("<RNAML"))
340 reply = FileFormat.Rnaml;
345 if ((data.length() < 1) || (data.indexOf("#") == 0))
347 lineswereskipped = true;
351 if (data.indexOf("PILEUP") > -1)
353 reply = FileFormat.Pileup;
358 if ((data.indexOf("//") == 0) || ((data.indexOf("!!") > -1) && (data
359 .indexOf("!!") < data.indexOf("_MULTIPLE_ALIGNMENT "))))
361 reply = FileFormat.MSF;
365 else if (data.indexOf("CLUSTAL") > -1)
367 reply = FileFormat.Clustal;
372 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
374 reply = FileFormat.PDB;
377 else if (data.matches("\\s*\\d+\\s+\\d+\\s*"))
379 reply = FileFormat.Phylip;
384 if (!lineswereskipped && looksLikeJnetData(data))
386 reply = FileFormat.Jnet;
391 lineswereskipped = true; // this means there was some junk before any
392 // key file signature
400 source.reset(bytesRead); // so the file can be parsed from the mark
402 } catch (Exception ex)
404 System.err.println("File Identification failed!\n" + ex);
405 throw new FileFormatException(source.errormessage);
407 if (trimmedLength == 0)
410 "File Identification failed! - Empty file was read.");
411 throw new FileFormatException("EMPTY DATA FILE");
413 System.out.println("File format identified as " + reply.toString());
418 * Returns true if the data appears to be Jnet concise annotation format
423 protected boolean looksLikeJnetData(String data)
425 char firstChar = data.charAt(0);
426 int colonPos = data.indexOf(":");
427 int commaPos = data.indexOf(",");
428 boolean isJnet = firstChar != '*' && firstChar != ' ' && colonPos > -1
429 && commaPos > -1 && colonPos < commaPos;
430 // && data.indexOf(",")<data.indexOf(",", data.indexOf(","))) / ??
435 * Returns true if the data has at least 6 tab-delimited fields _and_ fields 4
436 * and 5 are integer (start/end)
441 protected boolean looksLikeFeatureData(String data)
447 String[] columns = data.split("\t");
448 if (columns.length < 6)
452 for (int col = 3; col < 5; col++)
456 Integer.parseInt(columns[col]);
457 } catch (NumberFormatException e)
470 public static void main(String[] args)
472 for (int i = 0; args != null && i < args.length; i++)
474 IdentifyFile ider = new IdentifyFile();
475 FileFormatI type = null;
478 type = ider.identify(args[i], DataSourceType.FILE);
479 } catch (FileFormatException e)
482 String.format("Error '%s' identifying file type for %s",
483 args[i], e.getMessage()));
485 System.out.println("Type of " + args[i] + " is " + type);
487 if (args == null || args.length == 0)
489 System.err.println("Usage: <Filename> [<Filename> ...]");