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