Clover icon

Coverage Report

  1. Project Clover database Fri Nov 28 2025 17:48:58 GMT
  2. Package jalview.io

File IdentifyFile.java

 

Coverage histogram

../../img/srcFileCovDistChart8.png
21% of files have more coverage

Code metrics

114
199
12
1
554
430
102
0.51
16.58
12
8.5

Classes

Class Line # Actions
IdentifyFile 36 199 102
0.781538578.2%
 

Contributing tests

This file is covered by 208 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
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.
11    *
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.
16    *
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.
20    */
21    package jalview.io;
22   
23    import java.io.File;
24    import java.io.FileNotFoundException;
25    import java.io.IOException;
26    import java.util.Locale;
27   
28    import jalview.bin.Console;
29   
30    /**
31    * DOCUMENT ME!
32    *
33    * @author $author$
34    * @version $Revision$
35    */
 
36    public class IdentifyFile
37    {
38   
39    private static final String XMLHEADER = "<?XML VERSION=\"1.0\" ENCODING=\"UTF-8\" STANDALONE=\"YES\"?>";
40   
 
41  2 toggle public FileFormatI identify(Object file, DataSourceType protocol)
42    throws FileFormatException, FileNotFoundException
43    {
44  2 return identify(file, protocol, false);
45    }
46   
 
47  13 toggle public FileFormatI identify(Object file, DataSourceType protocol,
48    boolean checkForAnnotations)
49    throws FileFormatException, FileNotFoundException
50    {
51    // BH 2018
52  13 return (file instanceof File
53    ? identify((File) file, protocol, checkForAnnotations)
54    : identify((String) file, protocol, checkForAnnotations));
55   
56    }
57   
 
58  6 toggle public FileFormatI identify(File file, DataSourceType sourceType)
59    throws FileFormatException
60    {
61  6 return identify(file, sourceType, false);
62    }
63   
 
64  6 toggle public FileFormatI identify(File file, DataSourceType sourceType,
65    boolean checkForAnnotations) throws FileFormatException
66    {
67    // BH 2018
68  6 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
69  6 FileParse parser = null;
70  6 try
71    {
72  6 parser = new FileParse(file, sourceType);
73  6 if (parser.isValid())
74    {
75  6 return identify(parser, true, checkForAnnotations);
76    }
77    } catch (Exception e)
78    {
79  0 Console.error("Error whilst identifying " + file, e);
80  0 emessage = e.getMessage();
81    }
82  0 if (parser != null)
83    {
84  0 throw new FileFormatException(parser.errormessage);
85    }
86  0 throw new FileFormatException(emessage);
87    }
88   
 
89  431 toggle public FileFormatI identify(String file, DataSourceType sourceType)
90    throws FileFormatException, FileNotFoundException
91    {
92  431 return identify(file, sourceType, false);
93    }
94   
95    /**
96    * Identify a datasource's file content.
97    *
98    * @note Do not use this method for stream sources - create a FileParse object
99    * instead.
100    *
101    * @param file
102    * @param sourceType
103    * @return
104    * @throws FileFormatException
105    */
 
106  444 toggle public FileFormatI identify(String file, DataSourceType sourceType,
107    boolean checkForAnnotations)
108    throws FileFormatException, FileNotFoundException
109    {
110  444 String emessage = "UNIDENTIFIED FILE PARSING ERROR";
111  444 FileParse parser = null;
112  444 FileNotFoundException fnf = null;
113  444 try
114    {
115  444 parser = new FileParse(file, sourceType);
116  444 if (parser.isValid())
117    {
118  444 return identify(parser);
119    }
120    } catch (FileNotFoundException e)
121    {
122  0 fnf = e;
123  0 emessage = "Could not find '" + file + "'";
124  0 Console.error("Could not find '" + file + "'", e);
125    } catch (IOException e)
126    {
127  0 Console.error("Error whilst trying to read " + file, e);
128    } catch (Exception e)
129    {
130  0 Console.error("Error whilst identifying " + file, e);
131  0 emessage = e.getMessage();
132    }
133  0 if (parser != null)
134    {
135  0 throw new FileFormatException(parser.errormessage);
136    }
137  0 if (fnf != null)
138    {
139  0 throw (fnf);
140    }
141  0 throw new FileFormatException(emessage);
142    }
143   
 
144  450 toggle public FileFormatI identify(FileParse source) throws FileFormatException
145    {
146  450 return identify(source, true, false);
147    // preserves original behaviour prior to version 2.3
148    }
149   
 
150  0 toggle public FileFormatI identify(AlignmentFileReaderI file,
151    boolean closeSource) throws IOException
152    {
153  0 FileParse fp = new FileParse(file.getInFile(),
154    file.getDataSourceType());
155  0 return identify(fp, closeSource, false);
156    }
157   
158    /**
159    * Identify contents of source, closing it or resetting source to start
160    * afterwards.
161    *
162    * @param source
163    * @param closeSource
164    * @return (best guess at) file format
165    * @throws FileFormatException
166    */
 
167  456 toggle public FileFormatI identify(FileParse source, boolean closeSource,
168    boolean checkForAnnotations) throws FileFormatException
169    {
170  456 FileFormatI reply = FileFormat.Pfam;
171  456 String data;
172  456 int bytesRead = 0;
173  456 int trimmedLength = 0;
174  456 boolean isXml = false; // set true if first line is XMLHEADER
175  456 boolean lineswereskipped = false;
176  456 boolean isBinary = false; // true if length is non-zero and non-printable
177    // characters are encountered
178   
179  456 try
180    {
181  456 if (!closeSource)
182    {
183  0 source.mark();
184    }
185  456 boolean aaIndexHeaderRead = false;
186   
187  ? while ((data = source.nextLine()) != null)
188    {
189  707 bytesRead += data.length();
190  707 trimmedLength += data.trim().length();
191  707 if (!lineswereskipped)
192    {
193  63800 for (int i = 0; !isBinary && i < data.length(); i++)
194    {
195  63344 char c = data.charAt(i);
196  63344 isBinary = (c < 32 && c != '\t' && c != '\n' && c != '\r'
197    && c != 5 && c != 27); // nominal binary character filter
198    // excluding CR, LF, tab,DEL and ^E
199    // for certain blast ids
200    }
201    }
202  707 if (isBinary)
203    {
204    // jar files are special - since they contain all sorts of random
205    // characters.
206  35 if (source.inFile != null)
207    {
208  35 String fileStr = source.inFile.getName();
209  35 if (fileStr.contains(".jar") || fileStr.contains(".zip")
210    || fileStr.contains(".jvp"))
211    {
212    // possibly a Jalview archive (but check further)
213  30 reply = FileFormat.Jalview;
214    }
215    }
216  35 if (!lineswereskipped && data.startsWith("PK"))
217    {
218  35 reply = FileFormat.Jalview; // archive
219  35 break;
220    }
221    }
222  672 data = data.toUpperCase(Locale.ROOT);
223   
224  672 if (data.startsWith(ScoreMatrixFile.SCOREMATRIX))
225    {
226  1 reply = FileFormat.ScoreMatrix;
227  1 break;
228    }
229  671 if (data.startsWith(XMLHEADER) && !lineswereskipped)
230    {
231  2 isXml = true;
232    }
233  671 if (data.startsWith("LOCUS"))
234    {
235  1 reply = FileFormat.GenBank;
236  1 break;
237    }
238  670 if (data.startsWith("ID "))
239    {
240  1 if (data.substring(2).trim().split(";").length == 7)
241    {
242  1 reply = FileFormat.Embl;
243  1 break;
244    }
245    }
246  669 if (data.startsWith("H ") && !aaIndexHeaderRead)
247    {
248  1 aaIndexHeaderRead = true;
249    }
250  669 if (data.startsWith("D ") && aaIndexHeaderRead)
251    {
252  1 reply = FileFormat.ScoreMatrix;
253  1 break;
254    }
255  668 if (data.startsWith("##GFF-VERSION"))
256    {
257    // GFF - possibly embedded in a Jalview features file!
258  6 reply = FileFormat.Features;
259  6 break;
260    }
261  662 if (looksLikeFeatureData(data))
262    {
263  5 reply = FileFormat.Features;
264  5 break;
265    }
266  657 if (data.indexOf("# STOCKHOLM") > -1)
267    {
268  8 reply = FileFormat.Stockholm;
269  8 break;
270    }
271  649 if (data.indexOf("_ENTRY.ID") > -1
272    || data.indexOf("_AUDIT_AUTHOR.NAME") > -1
273    || data.indexOf("_ATOM_SITE.") > -1)
274    {
275  1 reply = FileFormat.MMCif;
276  1 break;
277    }
278    // if (data.indexOf(">") > -1)
279  648 if (data.startsWith(">"))
280    {
281    // FASTA, PIR file or BLC file
282  351 boolean checkPIR = false, starterm = false;
283  351 if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
284    {
285    // watch for PIR file attributes
286  1 checkPIR = true;
287  1 reply = FileFormat.PIR;
288    }
289    // could also be BLC file, read next line to confirm
290  351 data = source.nextLine();
291   
292  351 if (data.indexOf(">") > -1)
293    {
294  1 reply = FileFormat.BLC;
295    }
296    else
297    {
298    // Is this a single line BLC file?
299  350 String data1 = source.nextLine();
300  350 String data2 = source.nextLine();
301  350 int c1;
302  350 if (checkPIR)
303    {
304  1 starterm = (data1 != null && data1.indexOf("*") > -1)
305    || (data2 != null && data2.indexOf("*") > -1);
306    }
307  ? if (data2 != null && (c1 = data.indexOf("*")) > -1)
308    {
309  1 if (c1 == 0 && c1 == data2.indexOf("*"))
310    {
311  0 reply = FileFormat.BLC;
312    }
313    else
314    {
315  1 reply = FileFormat.Fasta; // possibly a bad choice - may be
316    // recognised as PIR
317    }
318    // otherwise can still possibly be a PIR file
319    }
320    else
321    {
322  349 reply = FileFormat.Fasta;
323    // TODO : AMSA File is indicated if there is annotation in the
324    // FASTA file - but FASTA will automatically generate this at the
325    // mo.
326  349 if (!checkPIR)
327    {
328  348 break;
329    }
330    }
331    }
332    // final check for PIR content. require
333    // >P1;title\n<blah>\nterminated sequence to occur at least once.
334   
335    // TODO the PIR/fasta ambiguity may be the use case that is needed to
336    // have
337    // a 'Parse as type XXX' parameter for the applet/application.
338  3 if (checkPIR)
339    {
340  1 String dta = null;
341  1 if (!starterm)
342    {
343  1 do
344    {
345  1 try
346    {
347  1 dta = source.nextLine();
348    } catch (IOException ex)
349    {
350    }
351  1 if (dta != null && dta.indexOf("*") > -1)
352    {
353  1 starterm = true;
354    }
355  1 } while (dta != null && !starterm);
356    }
357  1 if (starterm)
358    {
359  1 reply = FileFormat.PIR;
360  1 break;
361    }
362    else
363    {
364  0 reply = FileFormat.Fasta; // probably a bad choice!
365    }
366    }
367    // read as a FASTA (probably)
368  2 break;
369    }
370  297 if (data.indexOf("{\"") > -1)
371    {
372  1 reply = FileFormat.Json;
373  1 break;
374    }
375  296 int lessThan = data.indexOf("<");
376  296 if ((lessThan > -1)) // possible Markup Language data i.e HTML, RNAML,
377    // XML
378    {
379  22 String upper = data.toUpperCase(Locale.ROOT);
380  22 if (upper.substring(lessThan).startsWith("<HTML"))
381    {
382  6 reply = FileFormat.Html;
383  6 break;
384    }
385  16 if (upper.substring(lessThan).startsWith("<RNAML"))
386    {
387  2 reply = FileFormat.Rnaml;
388  2 break;
389    }
390  14 if (isXml && data.contains(
391    "<NS2:JALVIEWUSERCOLOURS SCHEMENAME=\"SEQUENCE FEATURES\" XMLNS:NS2=\"WWW.JALVIEW.ORG/COLOURS\">"))
392    {
393  2 reply = FileFormat.FeatureSettings;
394  2 break;
395    }
396    }
397   
398  286 if ((data.length() < 1) || (data.indexOf("#") == 0))
399    {
400  51 lineswereskipped = true;
401  51 continue;
402    }
403   
404  235 if (data.indexOf("PILEUP") > -1)
405    {
406  1 reply = FileFormat.Pileup;
407   
408  1 break;
409    }
410   
411  234 if ((data.indexOf("//") == 0) || ((data.indexOf("!!") > -1) && (data
412    .indexOf("!!") < data.indexOf("_MULTIPLE_ALIGNMENT "))))
413    {
414  1 reply = FileFormat.MSF;
415   
416  1 break;
417    }
418  233 else if (data.indexOf("CLUSTAL") > -1)
419    {
420  1 reply = FileFormat.Clustal;
421   
422  1 break;
423    }
424   
425  232 else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
426    {
427  19 reply = FileFormat.PDB;
428  19 break;
429    }
430  213 else if (data.matches("\\s*\\d+\\s+\\d+\\s*"))
431    {
432  1 reply = FileFormat.Phylip;
433  1 break;
434    }
435  212 else if (checkForAnnotations && !lineswereskipped
436    && data.equals(AnnotationFile.JALVIEW_ANNOTATION))
437    {
438  0 reply = FileFormat.JalviewAnnotation;
439    }
440    else
441    {
442  212 if (!lineswereskipped && looksLikeJnetData(data))
443    {
444  0 reply = FileFormat.Jnet;
445  0 break;
446    }
447    }
448   
449  212 lineswereskipped = true; // this means there was some junk before any
450    // key file signature
451    }
452  456 if (closeSource)
453    {
454  456 source.close();
455    }
456    else
457    {
458  0 source.reset(bytesRead); // so the file can be parsed from the mark
459    }
460    } catch (Exception ex)
461    {
462  0 Console.error("File Identification failed!\n" + ex);
463  0 throw new FileFormatException(source.errormessage);
464    }
465  456 if (trimmedLength == 0)
466    {
467  0 Console.error("File Identification failed! - Empty file was read.");
468  0 throw new FileFormatException("EMPTY DATA FILE");
469    }
470  456 Console.debug("File format identified as " + reply.toString());
471  456 return reply;
472    }
473   
474    /**
475    * Returns true if the data appears to be Jnet concise annotation format
476    *
477    * @param data
478    * @return
479    */
 
480  25 toggle protected boolean looksLikeJnetData(String data)
481    {
482  25 char firstChar = data.charAt(0);
483  25 int colonPos = data.indexOf(":");
484  25 int commaPos = data.indexOf(",");
485  25 boolean isJnet = firstChar != '*' && firstChar != ' ' && colonPos > -1
486    && commaPos > -1 && colonPos < commaPos;
487    // && data.indexOf(",")<data.indexOf(",", data.indexOf(","))) / ??
488  25 return isJnet;
489    }
490   
491    /**
492    * Returns true if the data has at least 6 tab-delimited fields _and_ fields 4
493    * and 5 are integer (start/end)
494    *
495    * @param data
496    * @return
497    */
 
498  669 toggle protected boolean looksLikeFeatureData(String data)
499    {
500  669 if (data == null)
501    {
502  1 return false;
503    }
504  668 String[] columns = data.split("\t");
505  668 if (columns.length < 6)
506    {
507  659 return false;
508    }
509  24 for (int col = 3; col < 5; col++)
510    {
511  17 try
512    {
513  17 Integer.parseInt(columns[col]);
514    } catch (NumberFormatException e)
515    {
516  2 return false;
517    }
518    }
519  7 return true;
520    }
521   
522    /**
523    *
524    * @param args
525    * @j2sIgnore
526    */
 
527  0 toggle public static void main(String[] args)
528    {
529  0 for (int i = 0; args != null && i < args.length; i++)
530    {
531  0 IdentifyFile ider = new IdentifyFile();
532  0 FileFormatI type = null;
533  0 try
534    {
535  0 type = ider.identify(args[i], DataSourceType.FILE);
536    } catch (FileNotFoundException e)
537    {
538  0 Console.error(String.format("Error '%s' fetching file %s", args[i],
539    e.getMessage()));
540    } catch (FileFormatException e)
541    {
542  0 Console.error(
543    String.format("Error '%s' identifying file type for %s",
544    args[i], e.getMessage()));
545    }
546  0 Console.debug("Type of " + args[i] + " is " + type);
547    }
548  0 if (args == null || args.length == 0)
549    {
550  0 Console.error("Usage: <Filename> [<Filename> ...]");
551    }
552    }
553   
554    }