b06edc560056e357c7d97cac5c4aa17937b60279
[jalview.git] / src / jalview / io / IdentifyFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.io;
19
20 import java.io.*;
21
22 /**
23  * DOCUMENT ME!
24  * 
25  * @author $author$
26  * @version $Revision$
27  */
28 public class IdentifyFile
29 {
30   /**
31    * Identify a datasource's file content.
32    * 
33    * @note Do not use this method for stream sources - create a FileParse object
34    *       instead.
35    * 
36    * @param file
37    *          DOCUMENT ME!
38    * @param protocol
39    *          DOCUMENT ME!
40    * @return ID String
41    */
42   public String Identify(String file, String protocol)
43   {
44     String emessage = "UNIDENTIFIED FILE PARSING ERROR";
45     FileParse parser = null;
46     try
47     {
48       parser = new FileParse(file, protocol);
49       if (parser.isValid())
50       {
51         return Identify(parser);
52       }
53     } catch (Exception e)
54     {
55       System.err.println("Error whilst identifying");
56       e.printStackTrace(System.err);
57       emessage = e.getMessage();
58     }
59     if (parser != null)
60       return parser.errormessage;
61     return emessage;
62   }
63
64   public String Identify(FileParse source)
65   {
66     return Identify(source, true); // preserves original behaviour prior to
67     // version 2.3
68   }
69
70   /**
71    * Identify contents of source, closing it or resetting source to start
72    * afterwards.
73    * 
74    * @param source
75    * @param closeSource
76    * @return filetype string
77    */
78   public String Identify(FileParse source, boolean closeSource)
79   {
80     String reply = "PFAM";
81     String data;
82     int length = 0;
83     boolean lineswereskipped = false;
84     boolean isBinary = false; // true if length is non-zero and non-printable
85     // characters are encountered
86     try
87     {
88       if (!closeSource)
89       {
90         source.mark();
91       }
92       while ((data = source.nextLine()) != null)
93       {
94         length += data.length();
95         if (!lineswereskipped)
96         {
97           for (int i = 0; !isBinary && i < data.length(); i++)
98           {
99             char c = data.charAt(i);
100             isBinary = (c < 32 && c != '\t' && c != '\n' && c != '\r'
101                     && c != 5 && c != 27); // nominal binary character filter
102             // excluding CR, LF, tab,DEL and ^E
103             // for certain blast ids
104           }
105         }
106         if (isBinary)
107         {
108           // jar files are special - since they contain all sorts of random
109           // characters.
110           if (source.inFile != null)
111           {
112             String fileStr = source.inFile.getName();
113             // possibly a Jalview archive.
114             if (fileStr.lastIndexOf(".jar") > -1
115                     || fileStr.lastIndexOf(".zip") > -1)
116             {
117               reply = "Jalview";
118             }
119           }
120           if (!lineswereskipped && data.startsWith("PK"))
121           {
122             reply = "Jalview"; // archive.
123             break;
124           }
125         }
126         data = data.toUpperCase();
127
128         if ((data.indexOf("# STOCKHOLM") > -1))
129         {
130           reply = "STH";
131
132           break;
133         }
134
135         if ((data.length() < 1) || (data.indexOf("#") == 0))
136         {
137           lineswereskipped = true;
138           continue;
139         }
140
141         if (data.indexOf("PILEUP") > -1)
142         {
143           reply = "PileUp";
144
145           break;
146         }
147
148         if ((data.indexOf("//") == 0)
149                 || ((data.indexOf("!!") > -1) && (data.indexOf("!!") < data
150                         .indexOf("_MULTIPLE_ALIGNMENT "))))
151         {
152           reply = "MSF";
153
154           break;
155         }
156         else if (data.indexOf("CLUSTAL") > -1)
157         {
158           reply = "CLUSTAL";
159
160           break;
161         }
162         else if (data.indexOf(">") > -1)
163         {
164           // FASTA, PIR file or BLC file
165           boolean checkPIR = false, starterm = false;
166           if ((data.indexOf(">P1;") > -1) || (data.indexOf(">DL;") > -1))
167           {
168             // watch for PIR file attributes
169             checkPIR = true;
170             reply = "PIR";
171           }
172           // could also be BLC file, read next line to confirm
173           data = source.nextLine();
174
175           if (data.indexOf(">") > -1)
176           {
177             reply = "BLC";
178           }
179           else
180           {
181             // Is this a single line BLC file?
182             String data1 = source.nextLine();
183             String data2 = source.nextLine();
184             if (checkPIR)
185             {
186               starterm = (data1 != null && data1.indexOf("*") > -1)
187                       || (data2 != null && data2.indexOf("*") > -1);
188             }
189             if (data2 != null && data.indexOf("*") > -1)
190             {
191               if (data.indexOf("*") == data2.indexOf("*"))
192               {
193                 reply = "BLC";
194               }
195               // otherwise can still possibly be a PIR file
196             }
197             else
198             {
199               reply = "FASTA";
200               // TODO : AMSA File is indicated if there is annotation in the
201               // FASTA file - but FASTA will automatically generate this at the
202               // mo.
203               if (!checkPIR)
204               {
205                 break;
206               }
207             }
208           }
209           // final check for PIR content. require
210           // >P1;title\n<blah>\nterminated sequence to occur at least once.
211
212           // TODO the PIR/fasta ambiguity may be the use case that is needed to
213           // have
214           // a 'Parse as type XXX' parameter for the applet/application.
215           if (checkPIR)
216           {
217             String dta = null;
218             if (!starterm)
219             {
220               do
221               {
222                 try
223                 {
224                   dta = source.nextLine();
225                 } catch (IOException ex)
226                 {
227                 }
228                 ;
229                 if (dta != null && dta.indexOf("*") > -1)
230                 {
231                   starterm = true;
232                 }
233               } while (dta != null && !starterm);
234             }
235             if (starterm)
236             {
237               reply = "PIR";
238               break;
239             }
240             else
241             {
242               reply = "FASTA"; // probably a bad choice!
243             }
244           }
245           // read as a FASTA (probably)
246           break;
247         }
248         else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
249         {
250           reply = "PDB";
251           break;
252         }
253         /*
254          * // TODO comment out SimpleBLAST identification for Jalview 2.4.1 else
255          * if (!lineswereskipped && data.indexOf("BLAST")<4) { reply =
256          * "SimpleBLAST"; break;
257          * 
258          * } // end comments for Jalview 2.4.1
259          */
260         else if (!lineswereskipped && data.charAt(0) != '*'
261                 && data.charAt(0) != ' '
262                 && data.indexOf(":") < data.indexOf(",")) // &&
263         // data.indexOf(",")<data.indexOf(",",
264         // data.indexOf(",")))
265         {
266           // file looks like a concise JNet file
267           reply = "JnetFile";
268           break;
269         }
270
271         lineswereskipped = true; // this means there was some junk before any
272         // key file signature
273       }
274       if (closeSource)
275       {
276         source.close();
277       }
278       else
279       {
280         source.reset(); // so the file can be parsed from the beginning again.
281       }
282     } catch (Exception ex)
283     {
284       System.err.println("File Identification failed!\n" + ex);
285       return source.errormessage;
286     }
287     if (length == 0)
288     {
289       System.err
290               .println("File Identification failed! - Empty file was read.");
291       return "EMPTY DATA FILE";
292     }
293     return reply;
294   }
295
296   public static void main(String[] args)
297   {
298     for (int i = 0; args != null && i < args.length; i++)
299     {
300       IdentifyFile ider = new IdentifyFile();
301       String type = ider.Identify(args[i], AppletFormatAdapter.FILE);
302       System.out.println("Type of " + args[i] + " is " + type);
303     }
304     if (args == null || args.length == 0)
305     {
306       System.err.println("Usage: <Filename> [<Filename> ...]");
307     }
308   }
309 }