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.util.Locale;
26 import java.io.IOException;
34 public class IdentifyFile
37 public FileFormatI identify(Object file, DataSourceType protocol) throws FileFormatException
40 return (file instanceof File ? identify((File) file, protocol) : identify((String) file, protocol));
44 public FileFormatI identify(File file, DataSourceType sourceType)
45 throws FileFormatException
48 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
49 FileParse parser = null;
52 parser = new FileParse(file, sourceType);
55 return identify(parser);
59 System.err.println("Error whilst identifying " + file);
60 e.printStackTrace(System.err);
61 emessage = e.getMessage();
65 throw new FileFormatException(parser.errormessage);
67 throw new FileFormatException(emessage);
71 * Identify a datasource's file content.
73 * @note Do not use this method for stream sources - create a FileParse object
79 * @throws FileFormatException
81 public FileFormatI identify(String file, DataSourceType sourceType)
82 throws FileFormatException
84 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
85 FileParse parser = null;
88 parser = new FileParse(file, sourceType);
91 return identify(parser);
95 System.err.println("Error whilst identifying " + file);
96 e.printStackTrace(System.err);
97 emessage = e.getMessage();
101 throw new FileFormatException(parser.errormessage);
103 throw new FileFormatException(emessage);
106 public FileFormatI identify(FileParse source) throws FileFormatException
108 return identify(source, true);
109 // preserves original behaviour prior to version 2.3
112 public FileFormatI identify(AlignmentFileReaderI file,
113 boolean closeSource) throws IOException
115 FileParse fp = new FileParse(file.getInFile(),
116 file.getDataSourceType());
117 return identify(fp, closeSource);
121 * Identify contents of source, closing it or resetting source to start
126 * @return (best guess at) file format
127 * @throws FileFormatException
129 public FileFormatI identify(FileParse source, boolean closeSource)
130 throws FileFormatException
132 FileFormatI reply = FileFormat.Pfam;
135 int trimmedLength = 0;
136 boolean lineswereskipped = false;
137 boolean isBinary = false; // true if length is non-zero and non-printable
138 // characters are encountered
146 boolean aaIndexHeaderRead = false;
148 while ((data = source.nextLine()) != null)
150 bytesRead += data.length();
151 trimmedLength += data.trim().length();
152 if (!lineswereskipped)
154 for (int i = 0; !isBinary && i < data.length(); i++)
156 char c = data.charAt(i);
157 isBinary = (c < 32 && c != '\t' && c != '\n' && c != '\r'
158 && c != 5 && c != 27); // nominal binary character filter
159 // excluding CR, LF, tab,DEL and ^E
160 // for certain blast ids
165 // jar files are special - since they contain all sorts of random
167 if (source.inFile != null || source.getDataName() != null)
169 String fileStr = source.inFile == null ? source.getDataName()
170 : source.inFile.getName();
171 if (fileStr.contains(".jar")
172 || fileStr.contains(".zip") || fileStr.contains(".jvp"))
174 // possibly a Jalview archive (but check further)
175 reply = FileFormat.Jalview;
176 // TODO shouldn't there be a break here?
178 else if (fileStr.lastIndexOf(".bam") > -1)
180 reply = FileFormat.Bam;
184 if (!lineswereskipped && data.startsWith("PK"))
186 reply = FileFormat.Jalview; // archive
190 data = data.toUpperCase(Locale.ROOT);
192 if (data.startsWith(ScoreMatrixFile.SCOREMATRIX))
194 reply = FileFormat.ScoreMatrix;
197 if (data.startsWith("LOCUS"))
199 reply = FileFormat.GenBank;
202 if (data.startsWith("ID "))
204 if (data.substring(2).trim().split(";").length == 7)
206 reply = FileFormat.Embl;
210 if (data.startsWith("@HD\t"))
212 reply = FileFormat.Bam;
215 if (data.startsWith("H ") && !aaIndexHeaderRead)
217 aaIndexHeaderRead = true;
219 if (data.startsWith("D ") && aaIndexHeaderRead)
221 reply = FileFormat.ScoreMatrix;
224 if (data.startsWith("##GFF-VERSION"))
226 // GFF - possibly embedded in a Jalview features file!
227 reply = FileFormat.Features;
230 if (looksLikeFeatureData(data))
232 reply = FileFormat.Features;
235 if (data.indexOf("# STOCKHOLM") > -1)
237 reply = FileFormat.Stockholm;
240 if (data.indexOf("_ENTRY.ID") > -1
241 || data.indexOf("_AUDIT_AUTHOR.NAME") > -1
242 || data.indexOf("_ATOM_SITE.") > -1)
244 reply = FileFormat.MMCif;
247 // if (data.indexOf(">") > -1)
248 if (data.startsWith(">"))
250 // FASTA, PIR file or BLC file
251 boolean checkPIR = false, starterm = false;
252 if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
254 // watch for PIR file attributes
256 reply = FileFormat.PIR;
258 // could also be BLC file, read next line to confirm
259 data = source.nextLine();
261 if (data.indexOf(">") > -1)
263 reply = FileFormat.BLC;
267 // Is this a single line BLC file?
268 String data1 = source.nextLine();
269 String data2 = source.nextLine();
273 starterm = (data1 != null && data1.indexOf("*") > -1)
274 || (data2 != null && data2.indexOf("*") > -1);
276 if (data2 != null && (c1 = data.indexOf("*")) > -1)
278 if (c1 == 0 && c1 == data2.indexOf("*"))
280 reply = FileFormat.BLC;
284 reply = FileFormat.Fasta; // possibly a bad choice - may be
288 // otherwise can still possibly be a PIR file
292 reply = FileFormat.Fasta;
293 // TODO : AMSA File is indicated if there is annotation in the
294 // FASTA file - but FASTA will automatically generate this at the
302 // final check for PIR content. require
303 // >P1;title\n<blah>\nterminated sequence to occur at least once.
305 // TODO the PIR/fasta ambiguity may be the use case that is needed to
307 // a 'Parse as type XXX' parameter for the applet/application.
317 dta = source.nextLine();
318 } catch (IOException ex)
321 if (dta != null && dta.indexOf("*") > -1)
325 } while (dta != null && !starterm);
329 reply = FileFormat.PIR;
334 reply = FileFormat.Fasta; // probably a bad choice!
337 // read as a FASTA (probably)
340 if (data.indexOf("{\"") > -1)
342 reply = FileFormat.Json;
345 int lessThan = data.indexOf("<");
346 if ((lessThan > -1)) // possible Markup Language data i.e HTML,
349 String upper = data.toUpperCase(Locale.ROOT);
350 if (upper.substring(lessThan).startsWith("<HTML"))
352 reply = FileFormat.Html;
355 if (upper.substring(lessThan).startsWith("<RNAML"))
357 reply = FileFormat.Rnaml;
362 if ((data.length() < 1) || (data.indexOf("#") == 0))
364 lineswereskipped = true;
368 if (data.indexOf("PILEUP") > -1)
370 reply = FileFormat.Pileup;
375 if ((data.indexOf("//") == 0) || ((data.indexOf("!!") > -1) && (data
376 .indexOf("!!") < data.indexOf("_MULTIPLE_ALIGNMENT "))))
378 reply = FileFormat.MSF;
382 else if (data.indexOf("CLUSTAL") > -1)
384 reply = FileFormat.Clustal;
389 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
391 reply = FileFormat.PDB;
394 else if (data.matches("\\s*\\d+\\s+\\d+\\s*"))
396 reply = FileFormat.Phylip;
401 if (!lineswereskipped && looksLikeJnetData(data))
403 reply = FileFormat.Jnet;
408 lineswereskipped = true; // this means there was some junk before any
409 // key file signature
417 source.reset(bytesRead); // so the file can be parsed from the mark
419 } catch (Exception ex)
421 System.err.println("File Identification failed!\n" + ex);
422 throw new FileFormatException(source.errormessage);
424 if (trimmedLength == 0)
427 "File Identification failed! - Empty file was read.");
428 throw new FileFormatException("EMPTY DATA FILE");
430 System.out.println("File format identified as " + reply.toString());
435 * Returns true if the data appears to be Jnet concise annotation format
440 protected boolean looksLikeJnetData(String data)
442 char firstChar = data.charAt(0);
443 int colonPos = data.indexOf(":");
444 int commaPos = data.indexOf(",");
445 boolean isJnet = firstChar != '*' && firstChar != ' ' && colonPos > -1
446 && commaPos > -1 && colonPos < commaPos;
447 // && data.indexOf(",")<data.indexOf(",", data.indexOf(","))) / ??
452 * Returns true if the data has at least 6 tab-delimited fields _and_ fields 4
453 * and 5 are integer (start/end)
458 protected boolean looksLikeFeatureData(String data)
464 String[] columns = data.split("\t");
465 if (columns.length < 6)
469 for (int col = 3; col < 5; col++)
473 Integer.parseInt(columns[col]);
474 } catch (NumberFormatException e)
487 public static void main(String[] args)
489 for (int i = 0; args != null && i < args.length; i++)
491 IdentifyFile ider = new IdentifyFile();
492 FileFormatI type = null;
495 type = ider.identify(args[i], DataSourceType.FILE);
496 } catch (FileFormatException e)
499 String.format("Error '%s' identifying file type for %s",
500 args[i], e.getMessage()));
502 System.out.println("Type of " + args[i] + " is " + type);
504 if (args == null || args.length == 0)
506 System.err.println("Usage: <Filename> [<Filename> ...]");