JAL-1620 version bump and release notes
[jalview.git] / src / jalview / ws / dbsources / Pdb.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
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
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ws.dbsources;
22
23 import jalview.datamodel.Alignment;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.DBRefEntry;
26 import jalview.datamodel.DBRefSource;
27 import jalview.datamodel.PDBEntry;
28 import jalview.datamodel.SequenceI;
29
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Vector;
33
34 import MCview.PDBChain;
35 import MCview.PDBfile;
36
37 import com.stevesoft.pat.Regex;
38
39 import jalview.datamodel.AlignmentI;
40 import jalview.io.FormatAdapter;
41 import jalview.util.MessageManager;
42 import jalview.ws.ebi.EBIFetchClient;
43 import jalview.ws.seqfetcher.DbSourceProxy;
44
45 /**
46  * @author JimP
47  * 
48  */
49 public class Pdb extends EbiFileRetrievedProxy implements DbSourceProxy
50 {
51   public Pdb()
52   {
53     super();
54     addDbSourceProperty(DBRefSource.PROTSEQDB);
55   }
56
57   /*
58    * (non-Javadoc)
59    * 
60    * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
61    */
62   public String getAccessionSeparator()
63   {
64     // TODO Auto-generated method stub
65     return null;
66   }
67
68   /*
69    * (non-Javadoc)
70    * 
71    * @see jalview.ws.DbSourceProxy#getAccessionValidator()
72    */
73   public Regex getAccessionValidator()
74   {
75     return new Regex("([1-9][0-9A-Za-z]{3}):?([ _A-Za-z0-9]?)");
76   }
77
78   /*
79    * (non-Javadoc)
80    * 
81    * @see jalview.ws.DbSourceProxy#getDbSource()
82    */
83   public String getDbSource()
84   {
85     return DBRefSource.PDB;
86   }
87
88   /*
89    * (non-Javadoc)
90    * 
91    * @see jalview.ws.DbSourceProxy#getDbVersion()
92    */
93   public String getDbVersion()
94   {
95     return "0";
96   }
97
98   /*
99    * (non-Javadoc)
100    * 
101    * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
102    */
103   public AlignmentI getSequenceRecords(String queries) throws Exception
104   {
105     AlignmentI pdbfile = null;
106     Vector result = new Vector();
107     String chain = null;
108     String id = null;
109     if (queries.indexOf(":") > -1)
110     {
111       chain = queries.substring(queries.indexOf(":") + 1);
112       id = queries.substring(0, queries.indexOf(":"));
113     }
114     else
115     {
116       id = queries;
117     }
118     if (queries.length() > 4 && chain == null)
119     {
120       chain = queries.substring(4, 5);
121       id = queries.substring(0, 4);
122     }
123     if (!isValidReference(id))
124     {
125       System.err.println("Ignoring invalid pdb query: '" + id + "'");
126       stopQuery();
127       return null;
128     }
129     EBIFetchClient ebi = new EBIFetchClient();
130     file = ebi.fetchDataAsFile("pdb:" + id, "pdb", "raw").getAbsolutePath();
131     stopQuery();
132     if (file == null)
133     {
134       return null;
135     }
136     try
137     {
138
139       pdbfile = new FormatAdapter().readFile(file,
140               jalview.io.AppletFormatAdapter.FILE, "PDB");
141       if (pdbfile != null)
142       {
143         List<SequenceI> toremove = new ArrayList<SequenceI>();
144         for (SequenceI pdbcs : pdbfile.getSequences())
145         {
146           String chid = null;
147           // Mapping map=null;
148           for (PDBEntry pid : (Vector<PDBEntry>) pdbcs.getPDBId())
149           {
150             if (pid.getFile() == file)
151             {
152               chid = (String) pid.getProperty().get("CHAIN");
153
154             }
155             ;
156
157           }
158           if (chain == null
159                   || (chid != null && (chid.equals(chain)
160                           || chid.trim().equals(chain.trim()) || (chain
161                           .trim().length() == 0 && chid.equals("_")))))
162           {
163             pdbcs.setName(jalview.datamodel.DBRefSource.PDB + "|" + id
164                     + "|" + pdbcs.getName());
165             // Might need to add more metadata to the PDBEntry object
166             // like below
167             /*
168              * PDBEntry entry = new PDBEntry(); // Construct the PDBEntry
169              * entry.setId(id); if (entry.getProperty() == null)
170              * entry.setProperty(new Hashtable());
171              * entry.getProperty().put("chains", pdbchain.id + "=" +
172              * sq.getStart() + "-" + sq.getEnd());
173              * sq.getDatasetSequence().addPDBId(entry);
174              */
175             // Add PDB DB Refs
176             // We make a DBRefEtntry because we have obtained the PDB file from
177             // a
178             // verifiable source
179             // JBPNote - PDB DBRefEntry should also carry the chain and mapping
180             // information
181             DBRefEntry dbentry = new DBRefEntry(getDbSource(),
182                     getDbVersion(), (chid == null ? id : id + chid));
183             // dbentry.setMap()
184             pdbcs.addDBRef(dbentry);
185           }
186           else
187           {
188             // mark this sequence to be removed from the alignment
189             // - since it's not from the right chain
190             toremove.add(pdbcs);
191           }
192         }
193         // now remove marked sequences
194         for (SequenceI pdbcs : toremove)
195         {
196           pdbfile.deleteSequence(pdbcs);
197           if (pdbcs.getAnnotation()!=null)
198           {
199             for (AlignmentAnnotation aa: pdbcs.getAnnotation())
200             {
201               pdbfile.deleteAnnotation(aa);
202             }
203           }
204         }
205       }
206
207       if (pdbfile == null || pdbfile.getHeight() < 1)
208       {
209         throw new Exception(MessageManager.formatMessage("exception.no_pdb_records_for_chain", new String[]{id, ((chain == null) ? "' '" : chain)}));
210       }
211
212     } catch (Exception ex) // Problem parsing PDB file
213     {
214       stopQuery();
215       throw (ex);
216     }
217     return pdbfile;
218   }
219
220   /*
221    * (non-Javadoc)
222    * 
223    * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
224    */
225   public boolean isValidReference(String accession)
226   {
227     Regex r = getAccessionValidator();
228     return r.search(accession.trim());
229   }
230
231   /**
232    * obtain human glyoxalase chain A sequence
233    */
234   public String getTestQuery()
235   {
236     return "1QIPA";
237   }
238
239   public String getDbName()
240   {
241     return "PDB"; // getDbSource();
242   }
243
244   @Override
245   public int getTier()
246   {
247     return 0;
248   }
249 }