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