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