applied copyright 2008
[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     super();\r
51     addDbSourceProperty(DBRefSource.PROTSEQDB);\r
52   }\r
53 \r
54   /* (non-Javadoc)\r
55    * @see jalview.ws.DbSourceProxy#getAccessionSeparator()\r
56    */\r
57   public String getAccessionSeparator()\r
58   {\r
59     // TODO Auto-generated method stub\r
60     return null;\r
61   }\r
62 \r
63   /* (non-Javadoc)\r
64    * @see jalview.ws.DbSourceProxy#getAccessionValidator()\r
65    */\r
66   public Regex getAccessionValidator()\r
67   {\r
68     return new Regex("([1-9][0-9A-Za-z]{3}):?([ _A-Za-z0-9]?)");\r
69   }\r
70 \r
71   /* (non-Javadoc)\r
72    * @see jalview.ws.DbSourceProxy#getDbSource()\r
73    */\r
74   public String getDbSource()\r
75   {\r
76     return DBRefSource.PDB;\r
77   }\r
78 \r
79   /* (non-Javadoc)\r
80    * @see jalview.ws.DbSourceProxy#getDbVersion()\r
81    */\r
82   public String getDbVersion()\r
83   {\r
84     return "0";\r
85   }\r
86   /* (non-Javadoc)\r
87    * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])\r
88    */\r
89   public AlignmentI getSequenceRecords(String queries) throws Exception\r
90   {\r
91 \r
92     Vector result = new Vector();\r
93     String chain = null;\r
94     String id = null;\r
95     if (queries.indexOf(":") > -1)\r
96     {\r
97       chain = queries.substring(queries.indexOf(":") + 1);\r
98       id = queries.substring(0, queries.indexOf(":"));\r
99     }\r
100     else\r
101     {\r
102       id = queries;\r
103     }\r
104     if (queries.length() > 4 && chain == null)\r
105     {\r
106       chain = queries.substring(4,5);\r
107       id = queries.substring(0, 4);\r
108     }\r
109     if (!isValidReference(id))\r
110     {\r
111       System.err.println("Ignoring invalid pdb query: '"+id+"'");\r
112       stopQuery();\r
113       return null;\r
114     }\r
115     EBIFetchClient ebi = new EBIFetchClient();\r
116     file = ebi.fetchDataAsFile("pdb:" + id, "pdb", "raw")\r
117             .getAbsolutePath();\r
118     stopQuery();\r
119     if (file == null)\r
120     {\r
121       return null;\r
122     }\r
123     try\r
124     {\r
125       \r
126       PDBfile pdbfile = new PDBfile(file,\r
127               jalview.io.AppletFormatAdapter.FILE);\r
128       for (int i = 0; i < pdbfile.chains.size(); i++)\r
129       {\r
130         if (chain == null\r
131                 || ((PDBChain) pdbfile.chains.elementAt(i)).id\r
132                         .toUpperCase().equals(chain))\r
133         {\r
134           PDBChain pdbchain = (PDBChain) pdbfile.chains.elementAt(i);\r
135           // Get the Chain's Sequence - who's dataset includes any special features added from the PDB file\r
136           SequenceI sq = pdbchain.sequence;\r
137           // Specially formatted name for the PDB chain sequences retrieved from the PDB\r
138           sq.setName(jalview.datamodel.DBRefSource.PDB+"|" + id + "|" + sq.getName());\r
139           // Might need to add more metadata to the PDBEntry object\r
140           // like below\r
141           /*\r
142            * PDBEntry entry = new PDBEntry();\r
143            // Construct the PDBEntry\r
144            entry.setId(id);\r
145            if (entry.getProperty() == null)\r
146            entry.setProperty(new Hashtable());\r
147            entry.getProperty().put("chains",\r
148            pdbchain.id\r
149            + "=" + sq.getStart()\r
150            + "-" + sq.getEnd());\r
151            sq.getDatasetSequence().addPDBId(entry);\r
152            */\r
153           // Add PDB DB Refs\r
154           // We make a DBRefEtntry because we have obtained the PDB file from a verifiable source\r
155           // JBPNote - PDB DBRefEntry should also carry the chain and mapping information\r
156           DBRefEntry dbentry = new DBRefEntry(getDbSource(),\r
157                   getDbVersion(), id + pdbchain.id);\r
158           sq.addDBRef(dbentry);\r
159           // and add seuqence to the retrieved set\r
160           result.addElement(sq.deriveSequence());\r
161         }\r
162       }\r
163 \r
164       if (result.size() < 1)\r
165       {\r
166         throw new Exception("No PDB Records for " + id + " chain "\r
167                 + ((chain == null) ? "' '" : chain));\r
168       }\r
169     } catch (Exception ex) // Problem parsing PDB file\r
170     {\r
171       stopQuery();\r
172       throw (ex);\r
173     }\r
174 \r
175     SequenceI[] results = new SequenceI[result.size()];\r
176     for (int i = 0, j = result.size(); i < j; i++)\r
177     {\r
178       results[i] = (SequenceI) result.elementAt(i);\r
179       result.setElementAt(null, i);\r
180     }\r
181     return new Alignment(results);\r
182   }\r
183 \r
184   /* (non-Javadoc)\r
185    * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)\r
186    */\r
187   public boolean isValidReference(String accession)\r
188   {\r
189     Regex r = getAccessionValidator();\r
190     return r.search(accession.trim());\r
191   }\r
192 \r
193   /**\r
194    * obtain human glyoxalase chain A sequence\r
195    */\r
196   public String getTestQuery()\r
197   {\r
198     return "1QIPA";\r
199   }\r
200 \r
201   public String getDbName()\r
202   {\r
203     return "PDB"; // getDbSource();\r
204   }\r
205 \r
206 }\r