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
107 while ((data = source.nextLine()) != null)
109 bytesRead += data.length();
110 trimmedLength += data.trim().length();
111 if (!lineswereskipped)
113 for (int i = 0; !isBinary && i < data.length(); i++)
115 char c = data.charAt(i);
116 isBinary = (c < 32 && c != '\t' && c != '\n' && c != '\r'
117 && c != 5 && c != 27); // nominal binary character filter
118 // excluding CR, LF, tab,DEL and ^E
119 // for certain blast ids
124 // jar files are special - since they contain all sorts of random
126 if (source.inFile != null)
128 String fileStr = source.inFile.getName();
129 // possibly a Jalview archive.
130 if (fileStr.lastIndexOf(".jar") > -1
131 || fileStr.lastIndexOf(".zip") > -1)
133 reply = FileFormat.Jalview;
136 if (!lineswereskipped && data.startsWith("PK"))
138 reply = FileFormat.Jalview; // archive.
142 data = data.toUpperCase();
144 if (data.startsWith("##GFF-VERSION"))
146 // GFF - possibly embedded in a Jalview features file!
147 reply = FileFormat.Features;
150 if (looksLikeFeatureData(data))
152 reply = FileFormat.Features;
155 if (data.indexOf("# STOCKHOLM") > -1)
157 reply = FileFormat.Stockholm;
160 if (data.indexOf("_ENTRY.ID") > -1
161 || data.indexOf("_AUDIT_AUTHOR.NAME") > -1
162 || data.indexOf("_ATOM_SITE.") > -1)
164 reply = FileFormat.MMCif;
167 // if (data.indexOf(">") > -1)
168 if (data.startsWith(">"))
170 // FASTA, PIR file or BLC file
171 boolean checkPIR = false, starterm = false;
172 if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
174 // watch for PIR file attributes
176 reply = FileFormat.PIR;
178 // could also be BLC file, read next line to confirm
179 data = source.nextLine();
181 if (data.indexOf(">") > -1)
183 reply = FileFormat.BLC;
187 // Is this a single line BLC file?
188 String data1 = source.nextLine();
189 String data2 = source.nextLine();
193 starterm = (data1 != null && data1.indexOf("*") > -1)
194 || (data2 != null && data2.indexOf("*") > -1);
196 if (data2 != null && (c1 = data.indexOf("*")) > -1)
198 if (c1 == 0 && c1 == data2.indexOf("*"))
200 reply = FileFormat.BLC;
204 reply = FileFormat.Fasta; // possibly a bad choice - may be
208 // otherwise can still possibly be a PIR file
212 reply = FileFormat.Fasta;
213 // TODO : AMSA File is indicated if there is annotation in the
214 // FASTA file - but FASTA will automatically generate this at the
222 // final check for PIR content. require
223 // >P1;title\n<blah>\nterminated sequence to occur at least once.
225 // TODO the PIR/fasta ambiguity may be the use case that is needed to
227 // a 'Parse as type XXX' parameter for the applet/application.
237 dta = source.nextLine();
238 } catch (IOException ex)
241 if (dta != null && dta.indexOf("*") > -1)
245 } while (dta != null && !starterm);
249 reply = FileFormat.PIR;
254 reply = FileFormat.Fasta; // probably a bad choice!
257 // read as a FASTA (probably)
260 int lessThan = data.indexOf("<");
261 if ((lessThan > -1)) // possible Markup Language data i.e HTML,
264 String upper = data.toUpperCase();
265 if (upper.substring(lessThan).startsWith("<HTML"))
267 reply = FileFormat.Html;
270 if (upper.substring(lessThan).startsWith("<RNAML"))
272 reply = FileFormat.Rnaml;
277 if (data.indexOf("{\"") > -1)
279 reply = FileFormat.Json;
282 if ((data.length() < 1) || (data.indexOf("#") == 0))
284 lineswereskipped = true;
288 if (data.indexOf("PILEUP") > -1)
290 reply = FileFormat.Pileup;
295 if ((data.indexOf("//") == 0)
296 || ((data.indexOf("!!") > -1) && (data.indexOf("!!") < data
297 .indexOf("_MULTIPLE_ALIGNMENT "))))
299 reply = FileFormat.MSF;
303 else if (data.indexOf("CLUSTAL") > -1)
305 reply = FileFormat.Clustal;
310 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
312 reply = FileFormat.PDB;
315 else if (data.matches("\\s*\\d+\\s+\\d+\\s*"))
317 reply = FileFormat.Phylip;
322 if (!lineswereskipped && looksLikeJnetData(data))
324 reply = FileFormat.Jnet;
329 lineswereskipped = true; // this means there was some junk before any
330 // key file signature
338 source.reset(bytesRead); // so the file can be parsed from the mark
340 } catch (Exception ex)
342 System.err.println("File Identification failed!\n" + ex);
343 throw new FileFormatException(source.errormessage);
345 if (trimmedLength == 0)
348 .println("File Identification failed! - Empty file was read.");
349 throw new FileFormatException("EMPTY DATA FILE");
351 System.out.println("File format identified as " + reply.toString());
356 * Returns true if the data appears to be Jnet concise annotation format
361 protected boolean looksLikeJnetData(String data)
363 char firstChar = data.charAt(0);
364 int colonPos = data.indexOf(":");
365 int commaPos = data.indexOf(",");
366 boolean isJnet = firstChar != '*' && firstChar != ' ' && colonPos > -1
367 && commaPos > -1 && colonPos < commaPos;
368 // && data.indexOf(",")<data.indexOf(",", data.indexOf(","))) / ??
373 * Returns true if the data has at least 6 tab-delimited fields _and_ fields 4
374 * and 5 are integer (start/end)
379 protected boolean looksLikeFeatureData(String data)
385 String[] columns = data.split("\t");
386 if (columns.length < 6)
390 for (int col = 3; col < 5; col++)
394 Integer.parseInt(columns[col]);
395 } catch (NumberFormatException e)
403 public static void main(String[] args)
405 for (int i = 0; args != null && i < args.length; i++)
407 IdentifyFile ider = new IdentifyFile();
408 FileFormatI type = null;
411 type = ider.identify(args[i], DataSourceType.FILE);
412 } catch (FileFormatException e)
414 System.err.println(String.format(
415 "Error '%s' identifying file type for %s", args[i],
418 System.out.println("Type of " + args[i] + " is " + type);
420 if (args == null || args.length == 0)
422 System.err.println("Usage: <Filename> [<Filename> ...]");