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;
25 import java.util.Locale;
27 import jalview.bin.Console;
35 public class IdentifyFile
38 private static final String XMLHEADER = "<?XML VERSION=\"1.0\" ENCODING=\"UTF-8\" STANDALONE=\"YES\"?>";
40 public FileFormatI identify(Object file, DataSourceType protocol)
41 throws FileFormatException
44 return (file instanceof File ? identify((File) file, protocol)
45 : identify((String) file, protocol));
49 public FileFormatI identify(File file, DataSourceType sourceType)
50 throws FileFormatException
53 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
54 FileParse parser = null;
57 parser = new FileParse(file, sourceType);
60 return identify(parser);
64 Console.error("Error whilst identifying " + file, e);
65 emessage = e.getMessage();
69 throw new FileFormatException(parser.errormessage);
71 throw new FileFormatException(emessage);
75 * Identify a datasource's file content.
77 * @note Do not use this method for stream sources - create a FileParse object
83 * @throws FileFormatException
85 public FileFormatI identify(String file, DataSourceType sourceType)
86 throws FileFormatException
88 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
89 FileParse parser = null;
92 parser = new FileParse(file, sourceType);
95 return identify(parser);
99 Console.error("Error whilst identifying " + file, e);
100 emessage = e.getMessage();
104 throw new FileFormatException(parser.errormessage);
106 throw new FileFormatException(emessage);
109 public FileFormatI identify(FileParse source) throws FileFormatException
111 return identify(source, true);
112 // preserves original behaviour prior to version 2.3
115 public FileFormatI identify(AlignmentFileReaderI file,
116 boolean closeSource) throws IOException
118 FileParse fp = new FileParse(file.getInFile(),
119 file.getDataSourceType());
120 return identify(fp, closeSource);
124 * Identify contents of source, closing it or resetting source to start
129 * @return (best guess at) file format
130 * @throws FileFormatException
132 public FileFormatI identify(FileParse source, boolean closeSource)
133 throws FileFormatException
135 FileFormatI reply = FileFormat.Pfam;
138 int trimmedLength = 0;
139 boolean isXml = false; // set true if first line is XMLHEADER
140 boolean lineswereskipped = false;
141 boolean isBinary = false; // true if length is non-zero and non-printable
142 // characters are encountered
150 boolean aaIndexHeaderRead = false;
152 while ((data = source.nextLine()) != null)
154 bytesRead += data.length();
155 trimmedLength += data.trim().length();
156 if (!lineswereskipped)
158 for (int i = 0; !isBinary && i < data.length(); i++)
160 char c = data.charAt(i);
161 isBinary = (c < 32 && c != '\t' && c != '\n' && c != '\r'
162 && c != 5 && c != 27); // nominal binary character filter
163 // excluding CR, LF, tab,DEL and ^E
164 // for certain blast ids
169 // jar files are special - since they contain all sorts of random
171 if (source.inFile != null)
173 String fileStr = source.inFile.getName();
174 if (fileStr.contains(".jar") || fileStr.contains(".zip")
175 || fileStr.contains(".jvp"))
177 // possibly a Jalview archive (but check further)
178 reply = FileFormat.Jalview;
181 if (!lineswereskipped && data.startsWith("PK"))
183 reply = FileFormat.Jalview; // archive
187 data = data.toUpperCase(Locale.ROOT);
189 if (data.startsWith(ScoreMatrixFile.SCOREMATRIX))
191 reply = FileFormat.ScoreMatrix;
194 if (data.startsWith(XMLHEADER) && !lineswereskipped)
198 if (data.startsWith("LOCUS"))
200 reply = FileFormat.GenBank;
203 if (data.startsWith("ID "))
205 if (data.substring(2).trim().split(";").length == 7)
207 reply = FileFormat.Embl;
211 if (data.startsWith("H ") && !aaIndexHeaderRead)
213 aaIndexHeaderRead = true;
215 if (data.startsWith("D ") && aaIndexHeaderRead)
217 reply = FileFormat.ScoreMatrix;
220 if (data.startsWith("##GFF-VERSION"))
222 // GFF - possibly embedded in a Jalview features file!
223 reply = FileFormat.Features;
226 if (looksLikeFeatureData(data))
228 reply = FileFormat.Features;
231 if (data.indexOf("# STOCKHOLM") > -1)
233 reply = FileFormat.Stockholm;
236 if (data.indexOf("_ENTRY.ID") > -1
237 || data.indexOf("_AUDIT_AUTHOR.NAME") > -1
238 || data.indexOf("_ATOM_SITE.") > -1)
240 reply = FileFormat.MMCif;
243 // if (data.indexOf(">") > -1)
244 if (data.startsWith(">"))
246 // FASTA, PIR file or BLC file
247 boolean checkPIR = false, starterm = false;
248 if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
250 // watch for PIR file attributes
252 reply = FileFormat.PIR;
254 // could also be BLC file, read next line to confirm
255 data = source.nextLine();
257 if (data.indexOf(">") > -1)
259 reply = FileFormat.BLC;
263 // Is this a single line BLC file?
264 String data1 = source.nextLine();
265 String data2 = source.nextLine();
269 starterm = (data1 != null && data1.indexOf("*") > -1)
270 || (data2 != null && data2.indexOf("*") > -1);
272 if (data2 != null && (c1 = data.indexOf("*")) > -1)
274 if (c1 == 0 && c1 == data2.indexOf("*"))
276 reply = FileFormat.BLC;
280 reply = FileFormat.Fasta; // possibly a bad choice - may be
284 // otherwise can still possibly be a PIR file
288 reply = FileFormat.Fasta;
289 // TODO : AMSA File is indicated if there is annotation in the
290 // FASTA file - but FASTA will automatically generate this at the
298 // final check for PIR content. require
299 // >P1;title\n<blah>\nterminated sequence to occur at least once.
301 // TODO the PIR/fasta ambiguity may be the use case that is needed to
303 // a 'Parse as type XXX' parameter for the applet/application.
313 dta = source.nextLine();
314 } catch (IOException ex)
317 if (dta != null && dta.indexOf("*") > -1)
321 } while (dta != null && !starterm);
325 reply = FileFormat.PIR;
330 reply = FileFormat.Fasta; // probably a bad choice!
333 // read as a FASTA (probably)
336 if (data.indexOf("{\"") > -1)
338 reply = FileFormat.Json;
341 int lessThan = data.indexOf("<");
342 if ((lessThan > -1)) // possible Markup Language data i.e HTML,
345 String upper = data.toUpperCase(Locale.ROOT);
346 if (upper.substring(lessThan).startsWith("<HTML"))
348 reply = FileFormat.Html;
351 if (upper.substring(lessThan).startsWith("<RNAML"))
353 reply = FileFormat.Rnaml;
356 if (isXml && data.contains(
357 "<NS2:JALVIEWUSERCOLOURS SCHEMENAME=\"SEQUENCE FEATURES\" XMLNS:NS2=\"WWW.JALVIEW.ORG/COLOURS\">"))
359 reply = FileFormat.FeatureSettings;
364 if ((data.length() < 1) || (data.indexOf("#") == 0))
366 lineswereskipped = true;
370 if (data.indexOf("PILEUP") > -1)
372 reply = FileFormat.Pileup;
377 if ((data.indexOf("//") == 0) || ((data.indexOf("!!") > -1) && (data
378 .indexOf("!!") < data.indexOf("_MULTIPLE_ALIGNMENT "))))
380 reply = FileFormat.MSF;
384 else if (data.indexOf("CLUSTAL") > -1)
386 reply = FileFormat.Clustal;
391 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
393 reply = FileFormat.PDB;
396 else if (data.matches("\\s*\\d+\\s+\\d+\\s*"))
398 reply = FileFormat.Phylip;
403 if (!lineswereskipped && looksLikeJnetData(data))
405 reply = FileFormat.Jnet;
410 lineswereskipped = true; // this means there was some junk before any
411 // key file signature
419 source.reset(bytesRead); // so the file can be parsed from the mark
421 } catch (Exception ex)
423 Console.error("File Identification failed!\n" + ex);
424 throw new FileFormatException(source.errormessage);
426 if (trimmedLength == 0)
428 Console.error("File Identification failed! - Empty file was read.");
429 throw new FileFormatException("EMPTY DATA FILE");
431 Console.debug("File format identified as " + reply.toString());
436 * Returns true if the data appears to be Jnet concise annotation format
441 protected boolean looksLikeJnetData(String data)
443 char firstChar = data.charAt(0);
444 int colonPos = data.indexOf(":");
445 int commaPos = data.indexOf(",");
446 boolean isJnet = firstChar != '*' && firstChar != ' ' && colonPos > -1
447 && commaPos > -1 && colonPos < commaPos;
448 // && data.indexOf(",")<data.indexOf(",", data.indexOf(","))) / ??
453 * Returns true if the data has at least 6 tab-delimited fields _and_ fields 4
454 * and 5 are integer (start/end)
459 protected boolean looksLikeFeatureData(String data)
465 String[] columns = data.split("\t");
466 if (columns.length < 6)
470 for (int col = 3; col < 5; col++)
474 Integer.parseInt(columns[col]);
475 } catch (NumberFormatException e)
488 public static void main(String[] args)
490 for (int i = 0; args != null && i < args.length; i++)
492 IdentifyFile ider = new IdentifyFile();
493 FileFormatI type = null;
496 type = ider.identify(args[i], DataSourceType.FILE);
497 } catch (FileFormatException e)
500 String.format("Error '%s' identifying file type for %s",
501 args[i], e.getMessage()));
503 Console.debug("Type of " + args[i] + " is " + type);
505 if (args == null || args.length == 0)
507 Console.error("Usage: <Filename> [<Filename> ...]");