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