ensure mark is set before identification is started.
[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     String emessage = "UNIDENTIFIED FILE PARSING ERROR";
44     FileParse parser = null;
45     try {
46       parser = new FileParse(file, protocol);
47       if (parser.isValid()) {
48         return Identify(parser);
49       }
50     } catch (Exception e) {
51       System.err.println("Error whilst identifying");
52       e.printStackTrace(System.err);
53       emessage = e.getMessage();
54     }
55     if (parser!=null)
56       return parser.errormessage;
57     return emessage;
58   }
59   public String Identify(FileParse source) {
60     return Identify(source, true); // preserves original behaviour prior to version 2.3
61   }
62   /**
63    * Identify contents of source, closing it or resetting source to start afterwards.
64    * @param source
65    * @param closeSource
66    * @return filetype string
67    */
68   public String Identify(FileParse source, boolean closeSource) {
69     String reply = "PFAM";
70     String data;
71     int length=0;
72     boolean lineswereskipped=false;
73     boolean isBinary = false; // true if length is non-zero and non-printable characters are encountered
74     try {
75       if (!closeSource)
76       {
77         source.mark();
78       }
79       while ( (data = source.nextLine()) != null)
80       {
81         length+=data.length();
82         if (!lineswereskipped)
83         {
84           for (int i=0;!isBinary && i<data.length(); i++)
85           {
86             char c = data.charAt(i);
87             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 
88           }
89         }
90         if (isBinary)
91         {
92           // jar files are special - since they contain all sorts of random characters.
93           if (source.inFile!=null) 
94           {
95               String fileStr=source.inFile.getName();
96               // possibly a Jalview archive. 
97               if (fileStr.lastIndexOf(".jar")>-1 || fileStr.lastIndexOf(".zip")>-1) 
98               {
99                 reply = "Jalview";
100               }
101           } 
102           if (!lineswereskipped && data.startsWith("PK")) {
103             reply="Jalview"; // archive.
104             break;
105           }
106         }
107         data = data.toUpperCase();
108
109         if ( (data.indexOf("# STOCKHOLM") > -1))
110         {
111           reply = "STH";
112
113           break;
114         }
115
116         if ((data.length() < 1) || (data.indexOf("#") == 0))
117         {
118           lineswereskipped=true;
119           continue;
120         }
121
122         if (data.indexOf("PILEUP") > -1)
123         {
124           reply = "PileUp";
125
126           break;
127         }
128
129         if ( (data.indexOf("//") == 0) ||
130             ( (data.indexOf("!!") > -1) &&
131              (data.indexOf("!!") < data.indexOf(
132                  "_MULTIPLE_ALIGNMENT "))))
133         {
134           reply = "MSF";
135
136           break;
137         }
138         else if (data.indexOf("CLUSTAL") > -1)
139         {
140           reply = "CLUSTAL";
141
142           break;
143         }
144         else if ( (data.indexOf(">P1;") > -1) ||
145                  (data.indexOf(">DL;") > -1))
146         {
147           reply = "PIR";
148
149           break;
150         }
151         else if (data.indexOf(">") > -1)
152         {
153           // could be BLC file, read next line to confirm
154           data = source.nextLine();
155
156           if (data.indexOf(">") > -1)
157           {
158             reply = "BLC";
159           }
160           else
161           {
162             //Is this a single line BLC file?
163             source.nextLine();
164             String data2 = source.nextLine();
165             if (data2 != null
166                 && data.indexOf("*") > -1
167                 && data.indexOf("*") == data2.indexOf("*"))
168             {
169               reply = "BLC";
170             }
171             else
172             {
173                 reply = "FASTA";
174                 // TODO : AMSA File is indicated if there is annotation in the FASTA file - but FASTA will automatically generate this at the mo.
175             }
176           }
177           break;
178         }
179         else if (data.indexOf("HEADER") == 0 ||
180                  data.indexOf("ATOM") == 0)
181         {
182           reply = "PDB";
183           break;
184         }
185         else if (!lineswereskipped 
186                 && data.charAt(0)!='*' 
187                   && data.charAt(0)!=' ' 
188                     && data.indexOf(":") < data.indexOf(",")) //  && data.indexOf(",")<data.indexOf(",", data.indexOf(",")))
189         {
190           // file looks like a concise JNet file
191           reply = "JnetFile";
192           break;
193         }
194         
195         lineswereskipped=true; // this means there was some junk before any key file signature  
196       }
197       if (closeSource) {
198         source.close();
199       } else {
200         source.reset(); // so the file can be parsed from the beginning again.
201       }
202     }
203     catch (Exception ex)
204     {
205       System.err.println("File Identification failed!\n" + ex);
206       return source.errormessage;
207     }
208     if (length==0)
209     {
210       System.err.println("File Identification failed! - Empty file was read.");
211       return "EMPTY DATA FILE";
212     }
213     return reply;
214   }
215   public static void main(String[] args) {
216     for (int i=0; args!=null && i<args.length; i++)
217     {
218       IdentifyFile ider = new IdentifyFile();
219       String type = ider.Identify(args[i], AppletFormatAdapter.FILE);
220       System.out.println("Type of "+args[i]+" is "+type);
221     }
222     if (args==null || args.length==0)
223     {
224       System.err.println("Usage: <Filename> [<Filename> ...]");
225     }
226   }
227 }