main method for running from command line and better binary file/archive detection
[jalview.git] / src / jalview / io / IdentifyFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.io;
20
21 import java.io.*;
22 import java.net.*;
23
24 /**
25  * DOCUMENT ME!
26  *
27  * @author $author$
28  * @version $Revision$
29  */
30 public class IdentifyFile
31 {
32   /**
33    * Identify a datasource's file content. 
34    * @note Do not use this method
35    * for stream sources - create a FileParse object instead. 
36    *
37    * @param file DOCUMENT ME!
38    * @param protocol DOCUMENT ME! 
39    * @return ID String
40    */
41   public String Identify(String file, String protocol)
42   {
43     FileParse parser = null;
44     try {
45       parser = new FileParse(file, protocol);
46       if (parser.isValid()) {
47         return Identify(parser);
48       }
49     } catch (Exception e) {
50       System.err.println("Error whilst identifying");
51       e.printStackTrace(System.err);
52     }
53     if (parser!=null)
54       return parser.errormessage;
55     return "UNIDENTIFIED FILE PARSING ERROR";
56   }
57   public String Identify(FileParse source) {
58     return Identify(source, true); // preserves original behaviour prior to version 2.3
59   }
60   /**
61    * Identify contents of source, closing it or resetting source to start afterwards.
62    * @param source
63    * @param closeSource
64    * @return filetype string
65    */
66   public String Identify(FileParse source, boolean closeSource) {
67     String reply = "PFAM";
68     String data;
69     int length=0;
70     boolean lineswereskipped=false;
71     boolean isBinary = false; // true if length is non-zero and non-printable characters are encountered
72     try {
73       while ( (data = source.nextLine()) != null)
74       {
75         length+=data.length();
76         if (!lineswereskipped)
77         {
78           for (int i=0;!isBinary && i<data.length(); i++)
79           {
80             char c = data.charAt(i);
81             isBinary = (c<32 && c!='\t' && c!='\n' && c!='\r' && c!=5 && c!=27); // nominal binary character filter excluding CR, LF, tab,DEL and ^E for certain blast ids 
82           }
83         }
84         if (isBinary)
85         {
86           // jar files are special - since they contain all sorts of random characters.
87           if (source.inFile!=null) 
88           {
89               String fileStr=source.inFile.getName();
90               // possibly a Jalview archive. 
91               if (fileStr.lastIndexOf(".jar")>-1 || fileStr.lastIndexOf(".zip")>-1) 
92               {
93                 reply = "Jalview";
94               }
95           } 
96           if (!lineswereskipped && data.startsWith("PK")) {
97             reply="Jalview"; // archive.
98             break;
99           }
100         }
101         data = data.toUpperCase();
102
103         if ( (data.indexOf("# STOCKHOLM") > -1))
104         {
105           reply = "STH";
106
107           break;
108         }
109
110         if ((data.length() < 1) || (data.indexOf("#") == 0))
111         {
112           lineswereskipped=true;
113           continue;
114         }
115
116         if (data.indexOf("PILEUP") > -1)
117         {
118           reply = "PileUp";
119
120           break;
121         }
122
123         if ( (data.indexOf("//") == 0) ||
124             ( (data.indexOf("!!") > -1) &&
125              (data.indexOf("!!") < data.indexOf(
126                  "_MULTIPLE_ALIGNMENT "))))
127         {
128           reply = "MSF";
129
130           break;
131         }
132         else if (data.indexOf("CLUSTAL") > -1)
133         {
134           reply = "CLUSTAL";
135
136           break;
137         }
138         else if ( (data.indexOf(">P1;") > -1) ||
139                  (data.indexOf(">DL;") > -1))
140         {
141           reply = "PIR";
142
143           break;
144         }
145         else if (data.indexOf(">") > -1)
146         {
147           // could be BLC file, read next line to confirm
148           data = source.nextLine();
149
150           if (data.indexOf(">") > -1)
151           {
152             reply = "BLC";
153           }
154           else
155           {
156             //Is this a single line BLC file?
157             source.nextLine();
158             String data2 = source.nextLine();
159             if (data2 != null
160                 && data.indexOf("*") > -1
161                 && data.indexOf("*") == data2.indexOf("*"))
162             {
163               reply = "BLC";
164             }
165             else
166             {
167                 reply = "FASTA";
168             }
169           }
170           break;
171         }
172         else if (data.indexOf("HEADER") == 0 ||
173                  data.indexOf("ATOM") == 0)
174         {
175           reply = "PDB";
176           break;
177         }
178         else if (!lineswereskipped 
179                 && data.charAt(0)!='*' 
180                   && data.charAt(0)!=' ' 
181                     && data.indexOf(":") < data.indexOf(",")) //  && data.indexOf(",")<data.indexOf(",", data.indexOf(",")))
182         {
183           // file looks like a concise JNet file
184           reply = "JnetFile";
185           break;
186         }
187         
188         lineswereskipped=true; // this means there was some junk before any key file signature  
189       }
190       if (closeSource) {
191         source.close();
192       } else {
193         source.reset(); // so the file can be parsed from the beginning again.
194       }
195     }
196     catch (Exception ex)
197     {
198       System.err.println("File Identification failed!\n" + ex);
199       return source.errormessage;
200     }
201     if (length==0)
202     {
203       System.err.println("File Identification failed! - Empty file was read.");
204       return "EMPTY DATA FILE";
205     }
206     return reply;
207   }
208   public static void main(String[] args) {
209     for (int i=0; args!=null && i<args.length; i++)
210     {
211       IdentifyFile ider = new IdentifyFile();
212       String type = ider.Identify(args[i], AppletFormatAdapter.FILE);
213       System.out.println("Type of "+args[i]+" is "+type);
214     }
215     if (args==null || args.length==0)
216     {
217       System.err.println("Usage: <Filename> [<Filename> ...]");
218     }
219   }
220 }