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;
25 import java.io.IOException;
33 public class IdentifyFile
36 public FileFormatI identify(Object file, DataSourceType protocol)
37 throws FileFormatException
40 return (file instanceof File ? identify((File) file, protocol)
41 : identify((String) file, protocol));
45 public FileFormatI identify(File file, DataSourceType sourceType)
46 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 " + file);
61 e.printStackTrace(System.err);
62 emessage = e.getMessage();
66 throw new FileFormatException(parser.errormessage);
68 throw new FileFormatException(emessage);
72 * Identify a datasource's file content.
74 * @note Do not use this method for stream sources - create a FileParse object
80 * @throws FileFormatException
82 public FileFormatI identify(String file, DataSourceType sourceType)
83 throws FileFormatException
85 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
86 FileParse parser = null;
89 parser = new FileParse(file, sourceType);
92 return identify(parser);
96 System.err.println("Error whilst identifying " + file);
97 e.printStackTrace(System.err);
98 emessage = e.getMessage();
102 throw new FileFormatException(parser.errormessage);
104 throw new FileFormatException(emessage);
107 public FileFormatI identify(FileParse source) throws FileFormatException
109 return identify(source, true);
110 // preserves original behaviour prior to version 2.3
113 public FileFormatI identify(AlignmentFileReaderI file,
114 boolean closeSource) throws IOException
116 FileParse fp = new FileParse(file.getInFile(),
117 file.getDataSourceType());
118 return identify(fp, closeSource);
122 * Identify contents of source, closing it or resetting source to start
127 * @return (best guess at) file format
128 * @throws FileFormatException
130 public FileFormatI identify(FileParse source, boolean closeSource)
131 throws FileFormatException
133 FileFormatI reply = FileFormat.Pfam;
136 int trimmedLength = 0;
137 boolean lineswereskipped = false;
138 boolean isBinary = false; // true if length is non-zero and non-printable
139 // characters are encountered
147 boolean aaIndexHeaderRead = false;
149 while ((data = source.nextLine()) != null)
151 bytesRead += data.length();
152 trimmedLength += data.trim().length();
153 if (!lineswereskipped)
155 for (int i = 0; !isBinary && i < data.length(); i++)
157 char c = data.charAt(i);
158 isBinary = (c < 32 && c != '\t' && c != '\n' && c != '\r'
159 && c != 5 && c != 27); // nominal binary character filter
160 // excluding CR, LF, tab,DEL and ^E
161 // for certain blast ids
166 // jar files are special - since they contain all sorts of random
168 if (source.inFile != null)
170 String fileStr = source.inFile.getName();
171 if (fileStr.contains(".jar") || fileStr.contains(".zip")
172 || fileStr.contains(".jvp"))
174 // possibly a Jalview archive (but check further)
175 reply = FileFormat.Jalview;
178 if (!lineswereskipped && data.startsWith("PK"))
180 reply = FileFormat.Jalview; // archive
184 data = data.toUpperCase(Locale.ROOT);
186 if (data.startsWith(ScoreMatrixFile.SCOREMATRIX))
188 reply = FileFormat.ScoreMatrix;
191 if (data.startsWith("HMMER3"))
193 reply = FileFormat.HMMER3;
196 if (data.startsWith("LOCUS"))
198 reply = FileFormat.GenBank;
201 if (data.startsWith("ID "))
203 if (data.substring(2).trim().split(";").length == 7)
205 reply = FileFormat.Embl;
209 if (data.startsWith("H ") && !aaIndexHeaderRead)
211 aaIndexHeaderRead = true;
213 if (data.startsWith("D ") && aaIndexHeaderRead)
215 reply = FileFormat.ScoreMatrix;
218 if (data.startsWith("##GFF-VERSION"))
220 // GFF - possibly embedded in a Jalview features file!
221 reply = FileFormat.Features;
224 if (looksLikeFeatureData(data))
226 reply = FileFormat.Features;
229 if (data.indexOf("# STOCKHOLM") > -1)
231 reply = FileFormat.Stockholm;
234 if (data.indexOf("_ENTRY.ID") > -1
235 || data.indexOf("_AUDIT_AUTHOR.NAME") > -1
236 || data.indexOf("_ATOM_SITE.") > -1)
238 reply = FileFormat.MMCif;
241 // if (data.indexOf(">") > -1)
242 if (data.startsWith(">"))
244 // FASTA, PIR file or BLC file
245 boolean checkPIR = false, starterm = false;
246 if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
248 // watch for PIR file attributes
250 reply = FileFormat.PIR;
252 // could also be BLC file, read next line to confirm
253 data = source.nextLine();
255 if (data.indexOf(">") > -1)
257 reply = FileFormat.BLC;
261 // Is this a single line BLC file?
262 String data1 = source.nextLine();
263 String data2 = source.nextLine();
267 starterm = (data1 != null && data1.indexOf("*") > -1)
268 || (data2 != null && data2.indexOf("*") > -1);
270 if (data2 != null && (c1 = data.indexOf("*")) > -1)
272 if (c1 == 0 && c1 == data2.indexOf("*"))
274 reply = FileFormat.BLC;
278 reply = FileFormat.Fasta; // possibly a bad choice - may be
282 // otherwise can still possibly be a PIR file
286 reply = FileFormat.Fasta;
287 // TODO : AMSA File is indicated if there is annotation in the
288 // FASTA file - but FASTA will automatically generate this at the
296 // final check for PIR content. require
297 // >P1;title\n<blah>\nterminated sequence to occur at least once.
299 // TODO the PIR/fasta ambiguity may be the use case that is needed to
301 // a 'Parse as type XXX' parameter for the applet/application.
311 dta = source.nextLine();
312 } catch (IOException ex)
315 if (dta != null && dta.indexOf("*") > -1)
319 } while (dta != null && !starterm);
323 reply = FileFormat.PIR;
328 reply = FileFormat.Fasta; // probably a bad choice!
331 // read as a FASTA (probably)
334 if (data.indexOf("{\"") > -1)
336 reply = FileFormat.Json;
339 int lessThan = data.indexOf("<");
340 if ((lessThan > -1)) // possible Markup Language data i.e HTML,
343 String upper = data.toUpperCase(Locale.ROOT);
344 if (upper.substring(lessThan).startsWith("<HTML"))
346 reply = FileFormat.Html;
349 if (upper.substring(lessThan).startsWith("<RNAML"))
351 reply = FileFormat.Rnaml;
354 if (upper.substring(lessThan).startsWith("<BSML"))
356 reply = FileFormat.BSML;
361 if ((data.length() < 1) || (data.indexOf("#") == 0))
363 lineswereskipped = true;
367 if (data.indexOf("PILEUP") > -1)
369 reply = FileFormat.Pileup;
374 if ((data.indexOf("//") == 0) || ((data.indexOf("!!") > -1) && (data
375 .indexOf("!!") < data.indexOf("_MULTIPLE_ALIGNMENT "))))
377 reply = FileFormat.MSF;
381 else if (data.indexOf("CLUSTAL") > -1)
383 reply = FileFormat.Clustal;
388 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
390 reply = FileFormat.PDB;
393 else if (data.matches("\\s*\\d+\\s+\\d+\\s*"))
395 reply = FileFormat.Phylip;
400 if (!lineswereskipped && looksLikeJnetData(data))
402 reply = FileFormat.Jnet;
407 lineswereskipped = true; // this means there was some junk before any
408 // key file signature
416 source.reset(bytesRead); // so the file can be parsed from the mark
418 } catch (Exception ex)
420 System.err.println("File Identification failed!\n" + ex);
421 throw new FileFormatException(source.errormessage);
423 if (trimmedLength == 0)
426 "File Identification failed! - Empty file was read.");
427 throw new FileFormatException("EMPTY DATA FILE");
429 System.out.println("File format identified as " + reply.toString());
434 * Returns true if the data appears to be Jnet concise annotation format
439 protected boolean looksLikeJnetData(String data)
441 char firstChar = data.charAt(0);
442 int colonPos = data.indexOf(":");
443 int commaPos = data.indexOf(",");
444 boolean isJnet = firstChar != '*' && firstChar != ' ' && colonPos > -1
445 && commaPos > -1 && colonPos < commaPos;
446 // && data.indexOf(",")<data.indexOf(",", data.indexOf(","))) / ??
451 * Returns true if the data has at least 6 tab-delimited fields _and_ fields 4
452 * and 5 are integer (start/end)
457 protected boolean looksLikeFeatureData(String data)
463 String[] columns = data.split("\t");
464 if (columns.length < 6)
468 for (int col = 3; col < 5; col++)
472 Integer.parseInt(columns[col]);
473 } catch (NumberFormatException e)
486 public static void main(String[] args)
488 for (int i = 0; args != null && i < args.length; i++)
490 IdentifyFile ider = new IdentifyFile();
491 FileFormatI type = null;
494 type = ider.identify(args[i], DataSourceType.FILE);
495 } catch (FileFormatException e)
498 String.format("Error '%s' identifying file type for %s",
499 args[i], e.getMessage()));
501 System.out.println("Type of " + args[i] + " is " + type);
503 if (args == null || args.length == 0)
505 System.err.println("Usage: <Filename> [<Filename> ...]");