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
108 boolean aaIndexHeaderRead = false;
110 while ((data = source.nextLine()) != null)
112 bytesRead += data.length();
113 trimmedLength += data.trim().length();
114 if (!lineswereskipped)
116 for (int i = 0; !isBinary && i < data.length(); i++)
118 char c = data.charAt(i);
119 isBinary = (c < 32 && c != '\t' && c != '\n' && c != '\r'
120 && c != 5 && c != 27); // nominal binary character filter
121 // excluding CR, LF, tab,DEL and ^E
122 // for certain blast ids
127 // jar files are special - since they contain all sorts of random
129 if (source.inFile != null)
131 String fileStr = source.inFile.getName();
132 // possibly a Jalview archive.
133 if (fileStr.lastIndexOf(".jar") > -1
134 || fileStr.lastIndexOf(".zip") > -1)
136 reply = FileFormat.Jalview;
139 if (!lineswereskipped && data.startsWith("PK"))
141 reply = FileFormat.Jalview; // archive.
145 data = data.toUpperCase();
147 if (data.startsWith(ScoreMatrixFile.SCOREMATRIX))
149 reply = FileFormat.ScoreMatrix;
152 if (data.startsWith("HMMER3"))
154 reply = FileFormat.HMMER3;
157 if (data.startsWith("H ") && !aaIndexHeaderRead)
159 aaIndexHeaderRead = true;
161 if (data.startsWith("D ") && aaIndexHeaderRead)
163 reply = FileFormat.ScoreMatrix;
166 if (data.startsWith("##GFF-VERSION"))
168 // GFF - possibly embedded in a Jalview features file!
169 reply = FileFormat.Features;
172 if (looksLikeFeatureData(data))
174 reply = FileFormat.Features;
177 if (data.indexOf("# STOCKHOLM") > -1)
179 reply = FileFormat.Stockholm;
182 if (data.indexOf("_ENTRY.ID") > -1
183 || data.indexOf("_AUDIT_AUTHOR.NAME") > -1
184 || data.indexOf("_ATOM_SITE.") > -1)
186 reply = FileFormat.MMCif;
189 // if (data.indexOf(">") > -1)
190 if (data.startsWith(">"))
192 // FASTA, PIR file or BLC file
193 boolean checkPIR = false, starterm = false;
194 if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
196 // watch for PIR file attributes
198 reply = FileFormat.PIR;
200 // could also be BLC file, read next line to confirm
201 data = source.nextLine();
203 if (data.indexOf(">") > -1)
205 reply = FileFormat.BLC;
209 // Is this a single line BLC file?
210 String data1 = source.nextLine();
211 String data2 = source.nextLine();
215 starterm = (data1 != null && data1.indexOf("*") > -1)
216 || (data2 != null && data2.indexOf("*") > -1);
218 if (data2 != null && (c1 = data.indexOf("*")) > -1)
220 if (c1 == 0 && c1 == data2.indexOf("*"))
222 reply = FileFormat.BLC;
226 reply = FileFormat.Fasta; // possibly a bad choice - may be
230 // otherwise can still possibly be a PIR file
234 reply = FileFormat.Fasta;
235 // TODO : AMSA File is indicated if there is annotation in the
236 // FASTA file - but FASTA will automatically generate this at the
244 // final check for PIR content. require
245 // >P1;title\n<blah>\nterminated sequence to occur at least once.
247 // TODO the PIR/fasta ambiguity may be the use case that is needed to
249 // a 'Parse as type XXX' parameter for the applet/application.
259 dta = source.nextLine();
260 } catch (IOException ex)
263 if (dta != null && dta.indexOf("*") > -1)
267 } while (dta != null && !starterm);
271 reply = FileFormat.PIR;
276 reply = FileFormat.Fasta; // probably a bad choice!
279 // read as a FASTA (probably)
282 int lessThan = data.indexOf("<");
283 if ((lessThan > -1)) // possible Markup Language data i.e HTML,
286 String upper = data.toUpperCase();
287 if (upper.substring(lessThan).startsWith("<HTML"))
289 reply = FileFormat.Html;
292 if (upper.substring(lessThan).startsWith("<RNAML"))
294 reply = FileFormat.Rnaml;
299 if (data.indexOf("{\"") > -1)
301 reply = FileFormat.Json;
304 if ((data.length() < 1) || (data.indexOf("#") == 0))
306 lineswereskipped = true;
310 if (data.indexOf("PILEUP") > -1)
312 reply = FileFormat.Pileup;
317 if ((data.indexOf("//") == 0)
318 || ((data.indexOf("!!") > -1) && (data.indexOf("!!") < data
319 .indexOf("_MULTIPLE_ALIGNMENT "))))
321 reply = FileFormat.MSF;
325 else if (data.indexOf("CLUSTAL") > -1)
327 reply = FileFormat.Clustal;
332 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
334 reply = FileFormat.PDB;
337 else if (data.matches("\\s*\\d+\\s+\\d+\\s*"))
339 reply = FileFormat.Phylip;
344 if (!lineswereskipped && looksLikeJnetData(data))
346 reply = FileFormat.Jnet;
351 lineswereskipped = true; // this means there was some junk before any
352 // key file signature
360 source.reset(bytesRead); // so the file can be parsed from the mark
362 } catch (Exception ex)
364 System.err.println("File Identification failed!\n" + ex);
365 throw new FileFormatException(source.errormessage);
367 if (trimmedLength == 0)
370 .println("File Identification failed! - Empty file was read.");
371 throw new FileFormatException("EMPTY DATA FILE");
373 System.out.println("File format identified as " + reply.toString());
378 * Returns true if the data appears to be Jnet concise annotation format
383 protected boolean looksLikeJnetData(String data)
385 char firstChar = data.charAt(0);
386 int colonPos = data.indexOf(":");
387 int commaPos = data.indexOf(",");
388 boolean isJnet = firstChar != '*' && firstChar != ' ' && colonPos > -1
389 && commaPos > -1 && colonPos < commaPos;
390 // && data.indexOf(",")<data.indexOf(",", data.indexOf(","))) / ??
395 * Returns true if the data has at least 6 tab-delimited fields _and_ fields 4
396 * and 5 are integer (start/end)
401 protected boolean looksLikeFeatureData(String data)
407 String[] columns = data.split("\t");
408 if (columns.length < 6)
412 for (int col = 3; col < 5; col++)
416 Integer.parseInt(columns[col]);
417 } catch (NumberFormatException e)
425 public static void main(String[] args)
427 for (int i = 0; args != null && i < args.length; i++)
429 IdentifyFile ider = new IdentifyFile();
430 FileFormatI type = null;
433 type = ider.identify(args[i], DataSourceType.FILE);
434 } catch (FileFormatException e)
436 System.err.println(String.format(
437 "Error '%s' identifying file type for %s", args[i],
440 System.out.println("Type of " + args[i] + " is " + type);
442 if (args == null || args.length == 0)
444 System.err.println("Usage: <Filename> [<Filename> ...]");