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