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