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
33 public static final String FeaturesFile = "GFF or Jalview features";
36 * Identify a datasource's file content.
38 * @note Do not use this method for stream sources - create a FileParse object
44 * @throws FileFormatException
46 public FileFormatI identify(String file, DataSourceType sourceType)
47 throws FileFormatException
49 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
50 FileParse parser = null;
53 parser = new FileParse(file, sourceType);
56 return identify(parser);
60 System.err.println("Error whilst identifying");
61 e.printStackTrace(System.err);
62 emessage = e.getMessage();
66 throw new FileFormatException(parser.errormessage);
68 throw new FileFormatException(emessage);
71 public FileFormatI identify(FileParse source) throws FileFormatException
73 return identify(source, true);
74 // preserves original behaviour prior to version 2.3
78 * Identify contents of source, closing it or resetting source to start
83 * @return (best guess at) file format
84 * @throws FileFormatException
86 public FileFormatI identify(FileParse source, boolean closeSource)
87 throws FileFormatException
89 FileFormatI reply = FileFormat.Pfam;
92 int trimmedLength = 0;
93 boolean lineswereskipped = false;
94 boolean isBinary = false; // true if length is non-zero and non-printable
95 // characters are encountered
102 while ((data = source.nextLine()) != null)
104 bytesRead += data.length();
105 trimmedLength += data.trim().length();
106 if (!lineswereskipped)
108 for (int i = 0; !isBinary && i < data.length(); i++)
110 char c = data.charAt(i);
111 isBinary = (c < 32 && c != '\t' && c != '\n' && c != '\r'
112 && c != 5 && c != 27); // nominal binary character filter
113 // excluding CR, LF, tab,DEL and ^E
114 // for certain blast ids
119 // jar files are special - since they contain all sorts of random
121 if (source.inFile != null)
123 String fileStr = source.inFile.getName();
124 // possibly a Jalview archive.
125 if (fileStr.lastIndexOf(".jar") > -1
126 || fileStr.lastIndexOf(".zip") > -1)
128 reply = FileFormat.Jalview;
131 if (!lineswereskipped && data.startsWith("PK"))
133 reply = FileFormat.Jalview; // archive.
137 data = data.toUpperCase();
139 if (data.startsWith("##GFF-VERSION"))
141 // GFF - possibly embedded in a Jalview features file!
142 reply = FileFormat.Features;
145 if (looksLikeFeatureData(data))
147 reply = FileFormat.Features;
150 if (data.indexOf("# STOCKHOLM") > -1)
152 reply = FileFormat.Stockholm;
155 if (data.indexOf("_ENTRY.ID") > -1
156 || data.indexOf("_AUDIT_AUTHOR.NAME") > -1
157 || data.indexOf("_ATOM_SITE.") > -1)
159 reply = FileFormat.MMCif;
162 // if (data.indexOf(">") > -1)
163 if (data.startsWith(">"))
165 // FASTA, PIR file or BLC file
166 boolean checkPIR = false, starterm = false;
167 if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
169 // watch for PIR file attributes
171 reply = FileFormat.PIR;
173 // could also be BLC file, read next line to confirm
174 data = source.nextLine();
176 if (data.indexOf(">") > -1)
178 reply = FileFormat.BLC;
182 // Is this a single line BLC file?
183 String data1 = source.nextLine();
184 String data2 = source.nextLine();
188 starterm = (data1 != null && data1.indexOf("*") > -1)
189 || (data2 != null && data2.indexOf("*") > -1);
191 if (data2 != null && (c1 = data.indexOf("*")) > -1)
193 if (c1 == 0 && c1 == data2.indexOf("*"))
195 reply = FileFormat.BLC;
199 reply = FileFormat.Fasta; // possibly a bad choice - may be
203 // otherwise can still possibly be a PIR file
207 reply = FileFormat.Fasta;
208 // TODO : AMSA File is indicated if there is annotation in the
209 // FASTA file - but FASTA will automatically generate this at the
217 // final check for PIR content. require
218 // >P1;title\n<blah>\nterminated sequence to occur at least once.
220 // TODO the PIR/fasta ambiguity may be the use case that is needed to
222 // a 'Parse as type XXX' parameter for the applet/application.
232 dta = source.nextLine();
233 } catch (IOException ex)
236 if (dta != null && dta.indexOf("*") > -1)
240 } while (dta != null && !starterm);
244 reply = FileFormat.PIR;
249 reply = FileFormat.Fasta; // probably a bad choice!
252 // read as a FASTA (probably)
255 int lessThan = data.indexOf("<");
256 if ((lessThan > -1)) // possible Markup Language data i.e HTML,
259 String upper = data.toUpperCase();
260 if (upper.substring(lessThan).startsWith("<HTML"))
262 reply = FileFormat.Html;
265 if (upper.substring(lessThan).startsWith("<RNAML"))
267 reply = FileFormat.Rnaml;
272 if (data.indexOf("{\"") > -1)
274 reply = FileFormat.Json;
277 if ((data.length() < 1) || (data.indexOf("#") == 0))
279 lineswereskipped = true;
283 if (data.indexOf("PILEUP") > -1)
285 reply = FileFormat.Pileup;
290 if ((data.indexOf("//") == 0)
291 || ((data.indexOf("!!") > -1) && (data.indexOf("!!") < data
292 .indexOf("_MULTIPLE_ALIGNMENT "))))
294 reply = FileFormat.MSF;
298 else if (data.indexOf("CLUSTAL") > -1)
300 reply = FileFormat.Clustal;
305 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
307 reply = FileFormat.PDB;
310 else if (data.matches("\\s*\\d+\\s+\\d+\\s*"))
312 reply = FileFormat.Phylip;
317 if (!lineswereskipped && looksLikeJnetData(data))
319 reply = FileFormat.Jnet;
324 lineswereskipped = true; // this means there was some junk before any
325 // key file signature
333 source.reset(bytesRead); // so the file can be parsed from the mark
335 } catch (Exception ex)
337 System.err.println("File Identification failed!\n" + ex);
338 throw new FileFormatException(source.errormessage);
340 if (trimmedLength == 0)
343 .println("File Identification failed! - Empty file was read.");
344 throw new FileFormatException("EMPTY DATA FILE");
350 * Returns true if the data appears to be Jnet concise annotation format
355 protected boolean looksLikeJnetData(String data)
357 char firstChar = data.charAt(0);
358 int colonPos = data.indexOf(":");
359 int commaPos = data.indexOf(",");
360 boolean isJnet = firstChar != '*' && firstChar != ' ' && colonPos > -1
361 && commaPos > -1 && colonPos < commaPos;
362 // && data.indexOf(",")<data.indexOf(",", data.indexOf(","))) / ??
367 * Returns true if the data has at least 6 tab-delimited fields _and_
368 * fields 4 and 5 are integer (start/end)
372 protected boolean looksLikeFeatureData(String data)
378 String[] columns = data.split("\t");
379 if (columns.length < 6) {
382 for (int col = 3; col < 5; col++)
385 Integer.parseInt(columns[col]);
386 } catch (NumberFormatException e) {
393 public static void main(String[] args)
395 for (int i = 0; args != null && i < args.length; i++)
397 IdentifyFile ider = new IdentifyFile();
398 FileFormatI type = null;
401 type = ider.identify(args[i], DataSourceType.FILE);
402 } catch (FileFormatException e)
404 System.err.println(String.format(
405 "Error '%s' identifying file type for %s", args[i],
408 System.out.println("Type of " + args[i] + " is " + type);
410 if (args == null || args.length == 0)
412 System.err.println("Usage: <Filename> [<Filename> ...]");