error message associated with an alignment file loading failure is passed back to...
[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       while ( (data = source.nextLine()) != null)
76       {
77         length+=data.length();
78         if (!lineswereskipped)
79         {
80           for (int i=0;!isBinary && i<data.length(); i++)
81           {
82             char c = data.charAt(i);
83             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 
84           }
85         }
86         if (isBinary)
87         {
88           // jar files are special - since they contain all sorts of random characters.
89           if (source.inFile!=null) 
90           {
91               String fileStr=source.inFile.getName();
92               // possibly a Jalview archive. 
93               if (fileStr.lastIndexOf(".jar")>-1 || fileStr.lastIndexOf(".zip")>-1) 
94               {
95                 reply = "Jalview";
96               }
97           } 
98           if (!lineswereskipped && data.startsWith("PK")) {
99             reply="Jalview"; // archive.
100             break;
101           }
102         }
103         data = data.toUpperCase();
104
105         if ( (data.indexOf("# STOCKHOLM") > -1))
106         {
107           reply = "STH";
108
109           break;
110         }
111
112         if ((data.length() < 1) || (data.indexOf("#") == 0))
113         {
114           lineswereskipped=true;
115           continue;
116         }
117
118         if (data.indexOf("PILEUP") > -1)
119         {
120           reply = "PileUp";
121
122           break;
123         }
124
125         if ( (data.indexOf("//") == 0) ||
126             ( (data.indexOf("!!") > -1) &&
127              (data.indexOf("!!") < data.indexOf(
128                  "_MULTIPLE_ALIGNMENT "))))
129         {
130           reply = "MSF";
131
132           break;
133         }
134         else if (data.indexOf("CLUSTAL") > -1)
135         {
136           reply = "CLUSTAL";
137
138           break;
139         }
140         else if ( (data.indexOf(">P1;") > -1) ||
141                  (data.indexOf(">DL;") > -1))
142         {
143           reply = "PIR";
144
145           break;
146         }
147         else if (data.indexOf(">") > -1)
148         {
149           // could be BLC file, read next line to confirm
150           data = source.nextLine();
151
152           if (data.indexOf(">") > -1)
153           {
154             reply = "BLC";
155           }
156           else
157           {
158             //Is this a single line BLC file?
159             source.nextLine();
160             String data2 = source.nextLine();
161             if (data2 != null
162                 && data.indexOf("*") > -1
163                 && data.indexOf("*") == data2.indexOf("*"))
164             {
165               reply = "BLC";
166             }
167             else
168             {
169                 reply = "FASTA";
170                 // TODO : AMSA File is indicated if there is annotation in the FASTA file - but FASTA will automatically generate this at the mo.
171             }
172           }
173           break;
174         }
175         else if (data.indexOf("HEADER") == 0 ||
176                  data.indexOf("ATOM") == 0)
177         {
178           reply = "PDB";
179           break;
180         }
181         else if (!lineswereskipped 
182                 && data.charAt(0)!='*' 
183                   && data.charAt(0)!=' ' 
184                     && data.indexOf(":") < data.indexOf(",")) //  && data.indexOf(",")<data.indexOf(",", data.indexOf(",")))
185         {
186           // file looks like a concise JNet file
187           reply = "JnetFile";
188           break;
189         }
190         
191         lineswereskipped=true; // this means there was some junk before any key file signature  
192       }
193       if (closeSource) {
194         source.close();
195       } else {
196         source.reset(); // so the file can be parsed from the beginning again.
197       }
198     }
199     catch (Exception ex)
200     {
201       System.err.println("File Identification failed!\n" + ex);
202       return source.errormessage;
203     }
204     if (length==0)
205     {
206       System.err.println("File Identification failed! - Empty file was read.");
207       return "EMPTY DATA FILE";
208     }
209     return reply;
210   }
211   public static void main(String[] args) {
212     for (int i=0; args!=null && i<args.length; i++)
213     {
214       IdentifyFile ider = new IdentifyFile();
215       String type = ider.Identify(args[i], AppletFormatAdapter.FILE);
216       System.out.println("Type of "+args[i]+" is "+type);
217     }
218     if (args==null || args.length==0)
219     {
220       System.err.println("Usage: <Filename> [<Filename> ...]");
221     }
222   }
223 }