merge from 2_4_Release branch
[jalview.git] / src / jalview / ws / dbsources / Pdb.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)\r
3  * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  * \r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  * \r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  * \r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 package jalview.ws.dbsources;\r
20 \r
21 import jalview.datamodel.Alignment;\r
22 import jalview.datamodel.DBRefEntry;\r
23 import jalview.datamodel.DBRefSource;\r
24 import jalview.datamodel.SequenceI;\r
25 \r
26 import java.io.BufferedInputStream;\r
27 import java.io.InputStream;\r
28 import java.io.InputStreamReader;\r
29 import java.util.Hashtable;\r
30 import java.util.Vector;\r
31 \r
32 import MCview.PDBChain;\r
33 import MCview.PDBfile;\r
34 \r
35 import com.stevesoft.pat.Regex;\r
36 \r
37 import jalview.datamodel.AlignmentI;\r
38 import jalview.io.FileParse;\r
39 import jalview.ws.ebi.EBIFetchClient;\r
40 import jalview.ws.seqfetcher.DbSourceProxy;\r
41 import jalview.ws.seqfetcher.DbSourceProxyImpl;\r
42 \r
43 /**\r
44  * @author JimP\r
45  * \r
46  */\r
47 public class Pdb extends EbiFileRetrievedProxy implements DbSourceProxy\r
48 {\r
49   public Pdb()\r
50   {\r
51     super();\r
52     addDbSourceProperty(DBRefSource.PROTSEQDB);\r
53   }\r
54 \r
55   /*\r
56    * (non-Javadoc)\r
57    * \r
58    * @see jalview.ws.DbSourceProxy#getAccessionSeparator()\r
59    */\r
60   public String getAccessionSeparator()\r
61   {\r
62     // TODO Auto-generated method stub\r
63     return null;\r
64   }\r
65 \r
66   /*\r
67    * (non-Javadoc)\r
68    * \r
69    * @see jalview.ws.DbSourceProxy#getAccessionValidator()\r
70    */\r
71   public Regex getAccessionValidator()\r
72   {\r
73     return new Regex("([1-9][0-9A-Za-z]{3}):?([ _A-Za-z0-9]?)");\r
74   }\r
75 \r
76   /*\r
77    * (non-Javadoc)\r
78    * \r
79    * @see jalview.ws.DbSourceProxy#getDbSource()\r
80    */\r
81   public String getDbSource()\r
82   {\r
83     return DBRefSource.PDB;\r
84   }\r
85 \r
86   /*\r
87    * (non-Javadoc)\r
88    * \r
89    * @see jalview.ws.DbSourceProxy#getDbVersion()\r
90    */\r
91   public String getDbVersion()\r
92   {\r
93     return "0";\r
94   }\r
95 \r
96   /*\r
97    * (non-Javadoc)\r
98    * \r
99    * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])\r
100    */\r
101   public AlignmentI getSequenceRecords(String queries) throws Exception\r
102   {\r
103 \r
104     Vector result = new Vector();\r
105     String chain = null;\r
106     String id = null;\r
107     if (queries.indexOf(":") > -1)\r
108     {\r
109       chain = queries.substring(queries.indexOf(":") + 1);\r
110       id = queries.substring(0, queries.indexOf(":"));\r
111     }\r
112     else\r
113     {\r
114       id = queries;\r
115     }\r
116     if (queries.length() > 4 && chain == null)\r
117     {\r
118       chain = queries.substring(4, 5);\r
119       id = queries.substring(0, 4);\r
120     }\r
121     if (!isValidReference(id))\r
122     {\r
123       System.err.println("Ignoring invalid pdb query: '" + id + "'");\r
124       stopQuery();\r
125       return null;\r
126     }\r
127     EBIFetchClient ebi = new EBIFetchClient();\r
128     file = ebi.fetchDataAsFile("pdb:" + id, "pdb", "raw").getAbsolutePath();\r
129     stopQuery();\r
130     if (file == null)\r
131     {\r
132       return null;\r
133     }\r
134     try\r
135     {\r
136 \r
137       PDBfile pdbfile = new PDBfile(file,\r
138               jalview.io.AppletFormatAdapter.FILE);\r
139       for (int i = 0; i < pdbfile.chains.size(); i++)\r
140       {\r
141         if (chain == null\r
142                 || ((PDBChain) pdbfile.chains.elementAt(i)).id\r
143                         .toUpperCase().equals(chain))\r
144         {\r
145           PDBChain pdbchain = (PDBChain) pdbfile.chains.elementAt(i);\r
146           // Get the Chain's Sequence - who's dataset includes any special\r
147           // features added from the PDB file\r
148           SequenceI sq = pdbchain.sequence;\r
149           // Specially formatted name for the PDB chain sequences retrieved from\r
150           // the PDB\r
151           sq.setName(jalview.datamodel.DBRefSource.PDB + "|" + id + "|"\r
152                   + sq.getName());\r
153           // Might need to add more metadata to the PDBEntry object\r
154           // like below\r
155           /*\r
156            * PDBEntry entry = new PDBEntry(); // Construct the PDBEntry\r
157            * entry.setId(id); if (entry.getProperty() == null)\r
158            * entry.setProperty(new Hashtable());\r
159            * entry.getProperty().put("chains", pdbchain.id + "=" + sq.getStart() +\r
160            * "-" + sq.getEnd()); sq.getDatasetSequence().addPDBId(entry);\r
161            */\r
162           // Add PDB DB Refs\r
163           // We make a DBRefEtntry because we have obtained the PDB file from a\r
164           // verifiable source\r
165           // JBPNote - PDB DBRefEntry should also carry the chain and mapping\r
166           // information\r
167           DBRefEntry dbentry = new DBRefEntry(getDbSource(),\r
168                   getDbVersion(), id + pdbchain.id);\r
169           sq.addDBRef(dbentry);\r
170           // and add seuqence to the retrieved set\r
171           result.addElement(sq.deriveSequence());\r
172         }\r
173       }\r
174 \r
175       if (result.size() < 1)\r
176       {\r
177         throw new Exception("No PDB Records for " + id + " chain "\r
178                 + ((chain == null) ? "' '" : chain));\r
179       }\r
180     } catch (Exception ex) // Problem parsing PDB file\r
181     {\r
182       stopQuery();\r
183       throw (ex);\r
184     }\r
185 \r
186     SequenceI[] results = new SequenceI[result.size()];\r
187     for (int i = 0, j = result.size(); i < j; i++)\r
188     {\r
189       results[i] = (SequenceI) result.elementAt(i);\r
190       result.setElementAt(null, i);\r
191     }\r
192     return new Alignment(results);\r
193   }\r
194 \r
195   /*\r
196    * (non-Javadoc)\r
197    * \r
198    * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)\r
199    */\r
200   public boolean isValidReference(String accession)\r
201   {\r
202     Regex r = getAccessionValidator();\r
203     return r.search(accession.trim());\r
204   }\r
205 \r
206   /**\r
207    * obtain human glyoxalase chain A sequence\r
208    */\r
209   public String getTestQuery()\r
210   {\r
211     return "1QIPA";\r
212   }\r
213 \r
214   public String getDbName()\r
215   {\r
216     return "PDB"; // getDbSource();\r
217   }\r
218 \r
219 }\r