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.FileNotFoundException;
25 import java.io.IOException;
26 import java.util.Locale;
28 import jalview.bin.Console;
36 public class IdentifyFile
39 private static final String XMLHEADER = "<?XML VERSION=\"1.0\" ENCODING=\"UTF-8\" STANDALONE=\"YES\"?>";
41 public FileFormatI identify(Object file, DataSourceType protocol)
42 throws FileFormatException, FileNotFoundException
45 return (file instanceof File ? identify((File) file, protocol)
46 : identify((String) file, protocol));
50 public FileFormatI identify(File file, DataSourceType sourceType)
51 throws FileFormatException
54 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
55 FileParse parser = null;
58 parser = new FileParse(file, sourceType);
61 return identify(parser);
65 Console.error("Error whilst identifying " + file, e);
66 emessage = e.getMessage();
70 throw new FileFormatException(parser.errormessage);
72 throw new FileFormatException(emessage);
76 * Identify a datasource's file content.
78 * @note Do not use this method for stream sources - create a FileParse object
84 * @throws FileFormatException
86 public FileFormatI identify(String file, DataSourceType sourceType)
87 throws FileFormatException, FileNotFoundException
89 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
90 FileParse parser = null;
91 FileNotFoundException fnf = null;
94 parser = new FileParse(file, sourceType);
97 return identify(parser);
99 } catch (FileNotFoundException e)
102 emessage = "Could not find '" + file + "'";
103 Console.error("Could not find '" + file + "'", e);
104 } catch (IOException e)
106 Console.error("Error whilst trying to read " + file, e);
107 } catch (Exception e)
109 Console.error("Error whilst identifying " + file, e);
110 emessage = e.getMessage();
114 throw new FileFormatException(parser.errormessage);
120 throw new FileFormatException(emessage);
123 public FileFormatI identify(FileParse source) throws FileFormatException
125 return identify(source, true);
126 // preserves original behaviour prior to version 2.3
129 public FileFormatI identify(AlignmentFileReaderI file,
130 boolean closeSource) throws IOException
132 FileParse fp = new FileParse(file.getInFile(),
133 file.getDataSourceType());
134 return identify(fp, closeSource);
138 * Identify contents of source, closing it or resetting source to start
143 * @return (best guess at) file format
144 * @throws FileFormatException
146 public FileFormatI identify(FileParse source, boolean closeSource)
147 throws FileFormatException
149 FileFormatI reply = FileFormat.Pfam;
152 int trimmedLength = 0;
153 boolean isXml = false; // set true if first line is XMLHEADER
154 boolean lineswereskipped = false;
155 boolean isBinary = false; // true if length is non-zero and non-printable
156 // characters are encountered
164 boolean aaIndexHeaderRead = false;
166 while ((data = source.nextLine()) != null)
168 bytesRead += data.length();
169 trimmedLength += data.trim().length();
170 if (!lineswereskipped)
172 for (int i = 0; !isBinary && i < data.length(); i++)
174 char c = data.charAt(i);
175 isBinary = (c < 32 && c != '\t' && c != '\n' && c != '\r'
176 && c != 5 && c != 27); // nominal binary character filter
177 // excluding CR, LF, tab,DEL and ^E
178 // for certain blast ids
183 // jar files are special - since they contain all sorts of random
185 if (source.inFile != null)
187 String fileStr = source.inFile.getName();
188 if (fileStr.contains(".jar") || fileStr.contains(".zip")
189 || fileStr.contains(".jvp"))
191 // possibly a Jalview archive (but check further)
192 reply = FileFormat.Jalview;
195 if (!lineswereskipped && data.startsWith("PK"))
197 reply = FileFormat.Jalview; // archive
201 data = data.toUpperCase(Locale.ROOT);
203 if (data.startsWith(ScoreMatrixFile.SCOREMATRIX))
205 reply = FileFormat.ScoreMatrix;
208 if (data.startsWith(XMLHEADER) && !lineswereskipped)
212 if (data.startsWith("LOCUS"))
214 reply = FileFormat.GenBank;
217 if (data.startsWith("ID "))
219 if (data.substring(2).trim().split(";").length == 7)
221 reply = FileFormat.Embl;
225 if (data.startsWith("H ") && !aaIndexHeaderRead)
227 aaIndexHeaderRead = true;
229 if (data.startsWith("D ") && aaIndexHeaderRead)
231 reply = FileFormat.ScoreMatrix;
234 if (data.startsWith("##GFF-VERSION"))
236 // GFF - possibly embedded in a Jalview features file!
237 reply = FileFormat.Features;
240 if (looksLikeFeatureData(data))
242 reply = FileFormat.Features;
245 if (data.indexOf("# STOCKHOLM") > -1)
247 reply = FileFormat.Stockholm;
250 if (data.indexOf("_ENTRY.ID") > -1
251 || data.indexOf("_AUDIT_AUTHOR.NAME") > -1
252 || data.indexOf("_ATOM_SITE.") > -1)
254 reply = FileFormat.MMCif;
257 // if (data.indexOf(">") > -1)
258 if (data.startsWith(">"))
260 // FASTA, PIR file or BLC file
261 boolean checkPIR = false, starterm = false;
262 if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
264 // watch for PIR file attributes
266 reply = FileFormat.PIR;
268 // could also be BLC file, read next line to confirm
269 data = source.nextLine();
271 if (data.indexOf(">") > -1)
273 reply = FileFormat.BLC;
277 // Is this a single line BLC file?
278 String data1 = source.nextLine();
279 String data2 = source.nextLine();
283 starterm = (data1 != null && data1.indexOf("*") > -1)
284 || (data2 != null && data2.indexOf("*") > -1);
286 if (data2 != null && (c1 = data.indexOf("*")) > -1)
288 if (c1 == 0 && c1 == data2.indexOf("*"))
290 reply = FileFormat.BLC;
294 reply = FileFormat.Fasta; // possibly a bad choice - may be
298 // otherwise can still possibly be a PIR file
302 reply = FileFormat.Fasta;
303 // TODO : AMSA File is indicated if there is annotation in the
304 // FASTA file - but FASTA will automatically generate this at the
312 // final check for PIR content. require
313 // >P1;title\n<blah>\nterminated sequence to occur at least once.
315 // TODO the PIR/fasta ambiguity may be the use case that is needed to
317 // a 'Parse as type XXX' parameter for the applet/application.
327 dta = source.nextLine();
328 } catch (IOException ex)
331 if (dta != null && dta.indexOf("*") > -1)
335 } while (dta != null && !starterm);
339 reply = FileFormat.PIR;
344 reply = FileFormat.Fasta; // probably a bad choice!
347 // read as a FASTA (probably)
350 if (data.indexOf("{\"") > -1)
352 reply = FileFormat.Json;
355 int lessThan = data.indexOf("<");
356 if ((lessThan > -1)) // possible Markup Language data i.e HTML,
359 String upper = data.toUpperCase(Locale.ROOT);
360 if (upper.substring(lessThan).startsWith("<HTML"))
362 reply = FileFormat.Html;
365 if (upper.substring(lessThan).startsWith("<RNAML"))
367 reply = FileFormat.Rnaml;
370 if (isXml && data.contains(
371 "<NS2:JALVIEWUSERCOLOURS SCHEMENAME=\"SEQUENCE FEATURES\" XMLNS:NS2=\"WWW.JALVIEW.ORG/COLOURS\">"))
373 reply = FileFormat.FeatureSettings;
378 if ((data.length() < 1) || (data.indexOf("#") == 0))
380 lineswereskipped = true;
384 if (data.indexOf("PILEUP") > -1)
386 reply = FileFormat.Pileup;
391 if ((data.indexOf("//") == 0) || ((data.indexOf("!!") > -1) && (data
392 .indexOf("!!") < data.indexOf("_MULTIPLE_ALIGNMENT "))))
394 reply = FileFormat.MSF;
398 else if (data.indexOf("CLUSTAL") > -1)
400 reply = FileFormat.Clustal;
405 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
407 reply = FileFormat.PDB;
410 else if (data.matches("\\s*\\d+\\s+\\d+\\s*"))
412 reply = FileFormat.Phylip;
417 if (!lineswereskipped && looksLikeJnetData(data))
419 reply = FileFormat.Jnet;
424 lineswereskipped = true; // this means there was some junk before any
425 // key file signature
433 source.reset(bytesRead); // so the file can be parsed from the mark
435 } catch (Exception ex)
437 Console.error("File Identification failed!\n" + ex);
438 throw new FileFormatException(source.errormessage);
440 if (trimmedLength == 0)
442 Console.error("File Identification failed! - Empty file was read.");
443 throw new FileFormatException("EMPTY DATA FILE");
445 Console.debug("File format identified as " + reply.toString());
450 * Returns true if the data appears to be Jnet concise annotation format
455 protected boolean looksLikeJnetData(String data)
457 char firstChar = data.charAt(0);
458 int colonPos = data.indexOf(":");
459 int commaPos = data.indexOf(",");
460 boolean isJnet = firstChar != '*' && firstChar != ' ' && colonPos > -1
461 && commaPos > -1 && colonPos < commaPos;
462 // && data.indexOf(",")<data.indexOf(",", data.indexOf(","))) / ??
467 * Returns true if the data has at least 6 tab-delimited fields _and_ fields 4
468 * and 5 are integer (start/end)
473 protected boolean looksLikeFeatureData(String data)
479 String[] columns = data.split("\t");
480 if (columns.length < 6)
484 for (int col = 3; col < 5; col++)
488 Integer.parseInt(columns[col]);
489 } catch (NumberFormatException e)
502 public static void main(String[] args)
504 for (int i = 0; args != null && i < args.length; i++)
506 IdentifyFile ider = new IdentifyFile();
507 FileFormatI type = null;
510 type = ider.identify(args[i], DataSourceType.FILE);
511 } catch (FileNotFoundException e)
513 Console.error(String.format("Error '%s' fetching file %s", args[i],
515 } catch (FileFormatException e)
518 String.format("Error '%s' identifying file type for %s",
519 args[i], e.getMessage()));
521 Console.debug("Type of " + args[i] + " is " + type);
523 if (args == null || args.length == 0)
525 Console.error("Usage: <Filename> [<Filename> ...]");