refactored to allow seeks on a FileParse datasource so IdentifyFile can be applied...
[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     try {
70       while ( (data = source.nextLine()) != null)
71       {
72         data = data.toUpperCase();
73
74         if ( (data.indexOf("# STOCKHOLM") > -1))
75         {
76           reply = "STH";
77
78           break;
79         }
80
81         if ( (data.indexOf("#") == 0) || (data.length() < 1))
82         {
83           continue;
84         }
85
86         if (data.indexOf("PILEUP") > -1)
87         {
88           reply = "PileUp";
89
90           break;
91         }
92
93         if ( (data.indexOf("//") == 0) ||
94             ( (data.indexOf("!!") > -1) &&
95              (data.indexOf("!!") < data.indexOf(
96                  "_MULTIPLE_ALIGNMENT "))))
97         {
98           reply = "MSF";
99
100           break;
101         }
102         else if (data.indexOf("CLUSTAL") > -1)
103         {
104           reply = "CLUSTAL";
105
106           break;
107         }
108         else if ( (data.indexOf(">P1;") > -1) ||
109                  (data.indexOf(">DL;") > -1))
110         {
111           reply = "PIR";
112
113           break;
114         }
115         else if (data.indexOf(">") > -1)
116         {
117           // could be BLC file, read next line to confirm
118           data = source.nextLine();
119
120           if (data.indexOf(">") > -1)
121           {
122             reply = "BLC";
123           }
124           else
125           {
126             //Is this a single line BLC file?
127             source.nextLine();
128             String data2 = source.nextLine();
129             if (data2 != null
130                 && data.indexOf("*") > -1
131                 && data.indexOf("*") == data2.indexOf("*"))
132             {
133               reply = "BLC";
134             }
135             else
136             {
137               reply = "FASTA";
138             }
139           }
140
141           break;
142         }
143         else if (data.indexOf("HEADER") == 0 ||
144                  data.indexOf("ATOM") == 0)
145         {
146           reply = "PDB";
147           break;
148         }
149         else if (data.indexOf(":") < data.indexOf(",")) //  && data.indexOf(",")<data.indexOf(",", data.indexOf(",")))
150         {
151           // file looks like a concise JNet file
152           reply = "JnetFile";
153           break;
154         }
155         else  if (source.inFile!=null) 
156         {
157             String fileStr=source.inFile.getName();
158             // possibly a Jalview archive. 
159             if (fileStr.lastIndexOf(".jar")>-1 || fileStr.lastIndexOf(".zip")>-1) 
160             {
161               reply = "Jalview";
162             }
163         } else if (data.startsWith("PK")) {
164           reply="Jalview"; // archive.
165           break;
166         }
167       }
168       if (closeSource) {
169         source.close();
170       } else {
171         source.reset(); // so the file can be parsed from the beginning again.
172       }
173     }
174     catch (Exception ex)
175     {
176       System.err.println("File Identification failed!\n" + ex);
177       return source.errormessage;
178     }
179
180     return reply;
181   }
182 }