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