linear interpolation between sparse annotation element values.
[jalview.git] / src / jalview / io / IdentifyFile.java
1 /*
2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 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
23 import java.net.*;
24
25
26 /**
27  * DOCUMENT ME!
28  *
29  * @author $author$
30  * @version $Revision$
31  */
32 public class IdentifyFile
33 {
34     /**
35      * DOCUMENT ME!
36      *
37      * @param file DOCUMENT ME!
38      * @param protocol DOCUMENT ME!
39      *
40      * @return DOCUMENT ME!
41      */
42     public String Identify(String file, String protocol)
43     {
44         String reply = "PFAM";
45         String error =  "FILE NOT FOUND";
46         try
47         {
48             BufferedReader reader = null;
49
50             if (protocol.equals(AppletFormatAdapter.FILE))
51             {
52               reader = new BufferedReader(new FileReader(file));
53             }
54             else if (protocol.equals(AppletFormatAdapter.URL))
55             {
56               error = "URL NOT FOUND";
57               URL url = new URL(file);
58               reader = new BufferedReader(new InputStreamReader(
59                   url.openStream()));
60
61             }
62             else if (protocol.equals(AppletFormatAdapter.PASTE))
63             {
64               reader = new BufferedReader(new StringReader(file));
65             }
66             else if (protocol.equals(AppletFormatAdapter.CLASSLOADER))
67             {
68               java.io.InputStream is = getClass().getResourceAsStream("/" +
69                   file);
70               reader = new BufferedReader(new java.io.InputStreamReader(is));
71             }
72
73             String data;
74
75             while ((data = reader.readLine()) != null)
76             {
77                 data = data.toUpperCase();
78
79                 if ( (data.indexOf("# STOCKHOLM") > -1))
80                 {
81                   reply = "STH";
82
83                   break;
84                 }
85
86                 if ((data.indexOf("#") == 0) || (data.length() < 1))
87                 {
88                     continue;
89                 }
90
91                 if (data.indexOf("PILEUP") > -1)
92                 {
93                     reply = "PileUp";
94
95                     break;
96                 }
97
98                 if ((data.indexOf("//") == 0) ||
99                         ((data.indexOf("!!") > -1) &&
100                         (data.indexOf("!!") < data.indexOf(
101                             "_MULTIPLE_ALIGNMENT "))))
102                 {
103                     reply = "MSF";
104
105                     break;
106                 }
107                 else if (data.indexOf("CLUSTAL") > -1)
108                 {
109                     reply = "CLUSTAL";
110
111                     break;
112                 }
113                 else if ((data.indexOf(">P1;") > -1) ||
114                         (data.indexOf(">DL;") > -1))
115                 {
116                     reply = "PIR";
117
118                     break;
119                 }
120                 else if (data.indexOf(">") > -1)
121                 {
122                     // could be BLC file, read next line to confirm
123                     data = reader.readLine();
124
125                     if (data.indexOf(">") > -1 || data.indexOf("*") >-1 )
126                     {
127                         reply = "BLC";
128                     }
129                     else
130                     {
131                         reply = "FASTA";
132                     }
133
134                     break;
135                   }
136                   else if (data.indexOf("HEADER") > -1 ||
137                            data.indexOf("ATOM") > -1)
138                   {
139                     reply = "PDB";
140                     break;
141                   }
142                   else if (data.indexOf(":") < data.indexOf(",")) //  && data.indexOf(",")<data.indexOf(",", data.indexOf(",")))
143                   {
144                     // file looks like a concise JNet file
145                     reply = "JnetFile";
146                     break;
147                   }
148             }
149
150             reader.close();
151         }
152         catch (Exception ex)
153         {
154             System.err.println("File Identification failed!\n" + ex);
155             return error;
156         }
157
158         return reply;
159     }
160 }