2421b82615c3fb81294c50a0183bfee2ba11d7eb
[jalview.git] / src / jalview / ws / dbsources / Pdb.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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.api.FeatureSettingsModelI;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.DBRefEntry;
27 import jalview.datamodel.DBRefSource;
28 import jalview.datamodel.PDBEntry;
29 import jalview.datamodel.SequenceI;
30 import jalview.io.FormatAdapter;
31 import jalview.io.PDBFeatureSettings;
32 import jalview.util.MessageManager;
33 import jalview.ws.ebi.EBIFetchClient;
34
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.Vector;
38
39 import com.stevesoft.pat.Regex;
40
41 /**
42  * @author JimP
43  * 
44  */
45 public class Pdb extends EbiFileRetrievedProxy
46 {
47   public Pdb()
48   {
49     super();
50   }
51
52   public static final String FEATURE_INSERTION = "INSERTION";
53
54   public static final String FEATURE_RES_NUM = "RESNUM";
55
56   private static String currentDefaultFomart = DBRefSource.MMCIF;
57
58   /*
59    * (non-Javadoc)
60    * 
61    * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
62    */
63   @Override
64   public String getAccessionSeparator()
65   {
66     // TODO Auto-generated method stub
67     return null;
68   }
69
70   /*
71    * (non-Javadoc)
72    * 
73    * @see jalview.ws.DbSourceProxy#getAccessionValidator()
74    */
75   @Override
76   public Regex getAccessionValidator()
77   {
78     return new Regex("([1-9][0-9A-Za-z]{3}):?([ _A-Za-z0-9]?)");
79   }
80
81   /*
82    * (non-Javadoc)
83    * 
84    * @see jalview.ws.DbSourceProxy#getDbSource()
85    */
86   @Override
87   public String getDbSource()
88   {
89     return DBRefSource.PDB;
90   }
91
92   /*
93    * (non-Javadoc)
94    * 
95    * @see jalview.ws.DbSourceProxy#getDbVersion()
96    */
97   @Override
98   public String getDbVersion()
99   {
100     return "0";
101   }
102
103   /*
104    * (non-Javadoc)
105    * 
106    * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
107    */
108   @Override
109   public AlignmentI getSequenceRecords(String queries) throws Exception
110   {
111     AlignmentI pdbAlignment = null;
112     Vector result = new Vector();
113     String chain = null;
114     String id = null;
115     if (queries.indexOf(":") > -1)
116     {
117       chain = queries.substring(queries.indexOf(":") + 1);
118       id = queries.substring(0, queries.indexOf(":"));
119     }
120     else
121     {
122       id = queries;
123     }
124     if (queries.length() > 4 && chain == null)
125     {
126       chain = queries.substring(4, 5);
127       id = queries.substring(0, 4);
128     }
129     if (!isValidReference(id))
130     {
131       System.err.println("Ignoring invalid pdb query: '" + id + "'");
132       stopQuery();
133       return null;
134     }
135     String ext = getCurrentDefaultFomart().equalsIgnoreCase("mmcif") ? ".cif"
136             : ".xml";
137     EBIFetchClient ebi = new EBIFetchClient();
138     file = ebi.fetchDataAsFile("pdb:" + id,
139             getCurrentDefaultFomart().toLowerCase(), "raw", ext)
140             .getAbsolutePath();
141     stopQuery();
142     if (file == null)
143     {
144       return null;
145     }
146     try
147     {
148
149       pdbAlignment = new FormatAdapter().readFile(file,
150               jalview.io.AppletFormatAdapter.FILE,
151               getCurrentDefaultFomart());
152       if (pdbAlignment != null)
153       {
154         List<SequenceI> toremove = new ArrayList<SequenceI>();
155         for (SequenceI pdbcs : pdbAlignment.getSequences())
156         {
157           String chid = null;
158           // Mapping map=null;
159           for (PDBEntry pid : pdbcs.getAllPDBEntries())
160           {
161             if (pid.getFile() == file)
162             {
163               chid = pid.getChainCode();
164
165             }
166             ;
167
168           }
169           if (chain == null
170                   || (chid != null && (chid.equals(chain)
171                           || chid.trim().equals(chain.trim()) || (chain
172                           .trim().length() == 0 && chid.equals("_")))))
173           {
174             pdbcs.setName(jalview.datamodel.DBRefSource.PDB + "|" + id
175                     + "|" + pdbcs.getName());
176             // Might need to add more metadata to the PDBEntry object
177             // like below
178             /*
179              * PDBEntry entry = new PDBEntry(); // Construct the PDBEntry
180              * entry.setId(id); if (entry.getProperty() == null)
181              * entry.setProperty(new Hashtable());
182              * entry.getProperty().put("chains", pdbchain.id + "=" +
183              * sq.getStart() + "-" + sq.getEnd());
184              * sq.getDatasetSequence().addPDBId(entry);
185              */
186             // Add PDB DB Refs
187             // We make a DBRefEtntry because we have obtained the PDB file from
188             // a
189             // verifiable source
190             // JBPNote - PDB DBRefEntry should also carry the chain and mapping
191             // information
192             DBRefEntry dbentry = new DBRefEntry(getDbSource(),
193                     getDbVersion(), (chid == null ? id : id + chid));
194             // dbentry.setMap()
195             pdbcs.addDBRef(dbentry);
196           }
197           else
198           {
199             // mark this sequence to be removed from the alignment
200             // - since it's not from the right chain
201             toremove.add(pdbcs);
202           }
203         }
204         // now remove marked sequences
205         for (SequenceI pdbcs : toremove)
206         {
207           pdbAlignment.deleteSequence(pdbcs);
208           if (pdbcs.getAnnotation() != null)
209           {
210             for (AlignmentAnnotation aa : pdbcs.getAnnotation())
211             {
212               pdbAlignment.deleteAnnotation(aa);
213             }
214           }
215         }
216       }
217
218       if (pdbAlignment == null || pdbAlignment.getHeight() < 1)
219       {
220         throw new Exception(MessageManager.formatMessage(
221                 "exception.no_pdb_records_for_chain", new String[] { id,
222                     ((chain == null) ? "' '" : chain) }));
223       }
224
225     } catch (Exception ex) // Problem parsing PDB file
226     {
227       stopQuery();
228       throw (ex);
229     }
230     return pdbAlignment;
231   }
232
233   /*
234    * (non-Javadoc)
235    * 
236    * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
237    */
238   @Override
239   public boolean isValidReference(String accession)
240   {
241     Regex r = getAccessionValidator();
242     return r.search(accession.trim());
243   }
244
245   /**
246    * obtain human glyoxalase chain A sequence
247    */
248   @Override
249   public String getTestQuery()
250   {
251     return "1QIPA";
252   }
253
254   @Override
255   public String getDbName()
256   {
257     return "PDB"; // getDbSource();
258   }
259
260   @Override
261   public int getTier()
262   {
263     return 0;
264   }
265
266   public static String getCurrentDefaultFomart()
267   {
268     return currentDefaultFomart;
269   }
270
271   public static void setCurrentDefaultFomart(String currentDefaultFomart)
272   {
273     Pdb.currentDefaultFomart = currentDefaultFomart;
274   }
275
276   /**
277    * Returns a descriptor for suitable feature display settings with
278    * <ul>
279    * <li>ResNums or insertions features visible</li>
280    * <li>insertions features coloured red</li>
281    * <li>ResNum features coloured by label</li>
282    * <li>Insertions displayed above (on top of) ResNums</li>
283    * </ul>
284    */
285   @Override
286   public FeatureSettingsModelI getFeatureColourScheme()
287   {
288     return new PDBFeatureSettings();
289   }
290 }