68dda8862ff7240c56d012e4b47ef6f41445e1d1
[jalview.git] / src / jalview / ws / dbsources / Pdb.java
1
2 /*
3  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
4  * Copyright (C) $$Year-Rel$$ The Jalview Authors
5  * 
6  * This file is part of Jalview.
7  * 
8  * Jalview is free software: you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License 
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *  
13  * Jalview is distributed in the hope that it will be useful, but 
14  * WITHOUT ANY WARRANTY; without even the implied warranty 
15  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
16  * PURPOSE.  See the GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
20  * The Jalview Authors are detailed in the 'AUTHORS' file.
21  */
22 package jalview.ws.dbsources;
23
24 import jalview.api.FeatureSettingsModelI;
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.DBRefEntry;
28 import jalview.datamodel.DBRefSource;
29 import jalview.datamodel.PDBEntry;
30 import jalview.datamodel.PDBEntry.Type;
31 import jalview.datamodel.SequenceI;
32 import jalview.io.DataSourceType;
33 import jalview.io.FileFormat;
34 import jalview.io.FileFormatI;
35 import jalview.io.FormatAdapter;
36 import jalview.io.PDBFeatureSettings;
37 import jalview.structure.StructureImportSettings;
38 import jalview.util.MessageManager;
39 import jalview.ws.ebi.EBIFetchClient;
40
41 import java.util.ArrayList;
42 import java.util.List;
43
44 import com.stevesoft.pat.Regex;
45
46 /**
47  * @author JimP
48  * 
49  */
50 public class Pdb extends EbiFileRetrievedProxy
51 {
52   private static final String SEPARATOR = "|";
53
54   private static final String COLON = ":";
55
56   private static final int PDB_ID_LENGTH = 4;
57
58   public Pdb()
59   {
60     super();
61   }
62
63   /*
64    * (non-Javadoc)
65    * 
66    * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
67    */
68   @Override
69   public String getAccessionSeparator()
70   {
71     return null;
72   }
73
74   /*
75    * (non-Javadoc)
76    * 
77    * @see jalview.ws.DbSourceProxy#getAccessionValidator()
78    */
79   @Override
80   public Regex getAccessionValidator()
81   {
82     return new Regex("([1-9][0-9A-Za-z]{3}):?([ _A-Za-z0-9]?)");
83   }
84
85   /*
86    * (non-Javadoc)
87    * 
88    * @see jalview.ws.DbSourceProxy#getDbSource()
89    */
90   @Override
91   public String getDbSource()
92   {
93     return DBRefSource.PDB;
94   }
95
96   /*
97    * (non-Javadoc)
98    * 
99    * @see jalview.ws.DbSourceProxy#getDbVersion()
100    */
101   @Override
102   public String getDbVersion()
103   {
104     return "0";
105   }
106
107   /*
108    * (non-Javadoc)
109    * 
110    * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
111    */
112   @Override
113   public AlignmentI getSequenceRecords(String queries) throws Exception
114   {
115     AlignmentI pdbAlignment = null;
116     String chain = null;
117     String id = null;
118     if (queries.indexOf(COLON) > -1)
119     {
120       chain = queries.substring(queries.indexOf(COLON) + 1);
121       id = queries.substring(0, queries.indexOf(COLON));
122     }
123     else
124     {
125       id = queries;
126     }
127
128     /*
129      * extract chain code if it is appended to the id and we
130      * don't already have one
131      */
132     if (queries.length() > PDB_ID_LENGTH && chain == null)
133     {
134       chain = queries.substring(PDB_ID_LENGTH, PDB_ID_LENGTH + 1);
135       id = queries.substring(0, PDB_ID_LENGTH);
136     }
137
138     if (!isValidReference(id))
139     {
140       System.err.println("Ignoring invalid pdb query: '" + id + "'");
141       stopQuery();
142       return null;
143     }
144     Type pdbFileFormat = StructureImportSettings
145             .getDefaultStructureFileFormat();
146     String ext = "." + pdbFileFormat.getExtension();
147     EBIFetchClient ebi = new EBIFetchClient();
148     file = ebi.fetchDataAsFile("pdb:" + id, pdbFileFormat.getFormat(), ext)
149             .getAbsolutePath();
150     stopQuery();
151     if (file == null)
152     {
153       return null;
154     }
155     try
156     {
157       // todo get rid of Type and use FileFormatI instead?
158       // todo get rid of "ext" and just use "tmp"?
159       FileFormatI fileFormat = pdbFileFormat == Type.PDB ? FileFormat.PDB
160               : FileFormat.MMCif;
161       pdbAlignment = new FormatAdapter().readFile(file,
162               DataSourceType.FILE, fileFormat);
163       if (pdbAlignment != null)
164       {
165         List<SequenceI> toremove = new ArrayList<SequenceI>();
166         for (SequenceI pdbcs : pdbAlignment.getSequences())
167         {
168           String chid = null;
169           // Mapping map=null;
170           for (PDBEntry pid : pdbcs.getAllPDBEntries())
171           {
172             if (pid.getFile() == file)
173             {
174               chid = pid.getChainCode();
175
176             }
177           }
178           if (chain == null
179                   || (chid != null && (chid.equals(chain)
180                           || chid.trim().equals(chain.trim()) || (chain
181                           .trim().length() == 0 && chid.equals("_")))))
182           {
183             // FIXME seems to result in 'PDB|1QIP|1qip|A' - 1QIP is redundant.
184             // TODO: suggest simplify naming to 1qip|A as default name defined
185             pdbcs.setName(jalview.datamodel.DBRefSource.PDB + SEPARATOR
186                     + id + SEPARATOR + pdbcs.getName());
187             // Might need to add more metadata to the PDBEntry object
188             // like below
189             /*
190              * PDBEntry entry = new PDBEntry(); // Construct the PDBEntry
191              * entry.setId(id); if (entry.getProperty() == null)
192              * entry.setProperty(new Hashtable());
193              * entry.getProperty().put("chains", pdbchain.id + "=" +
194              * sq.getStart() + "-" + sq.getEnd());
195              * sq.getDatasetSequence().addPDBId(entry);
196              */
197             // Add PDB DB Refs
198             // We make a DBRefEtntry because we have obtained the PDB file from
199             // a
200             // verifiable source
201             // JBPNote - PDB DBRefEntry should also carry the chain and mapping
202             // information
203             DBRefEntry dbentry = new DBRefEntry(getDbSource(),
204                     getDbVersion(), (chid == null ? id : id + chid));
205             // dbentry.setMap()
206             pdbcs.addDBRef(dbentry);
207           }
208           else
209           {
210             // mark this sequence to be removed from the alignment
211             // - since it's not from the right chain
212             toremove.add(pdbcs);
213           }
214         }
215         // now remove marked sequences
216         for (SequenceI pdbcs : toremove)
217         {
218           pdbAlignment.deleteSequence(pdbcs);
219           if (pdbcs.getAnnotation() != null)
220           {
221             for (AlignmentAnnotation aa : pdbcs.getAnnotation())
222             {
223               pdbAlignment.deleteAnnotation(aa);
224             }
225           }
226         }
227       }
228
229       if (pdbAlignment == null || pdbAlignment.getHeight() < 1)
230       {
231         throw new Exception(MessageManager.formatMessage(
232                 "exception.no_pdb_records_for_chain", new String[] { id,
233                     ((chain == null) ? "' '" : chain) }));
234       }
235
236     } catch (Exception ex) // Problem parsing PDB file
237     {
238       stopQuery();
239       throw (ex);
240     }
241     return pdbAlignment;
242   }
243
244   /*
245    * (non-Javadoc)
246    * 
247    * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
248    */
249   @Override
250   public boolean isValidReference(String accession)
251   {
252     Regex r = getAccessionValidator();
253     return r.search(accession.trim());
254   }
255
256   /**
257    * human glyoxalase
258    */
259   @Override
260   public String getTestQuery()
261   {
262     return "1QIP";
263   }
264
265   @Override
266   public String getDbName()
267   {
268     return "PDB"; // getDbSource();
269   }
270
271   @Override
272   public int getTier()
273   {
274     return 0;
275   }
276
277   /**
278    * Returns a descriptor for suitable feature display settings with
279    * <ul>
280    * <li>ResNums or insertions features visible</li>
281    * <li>insertions features coloured red</li>
282    * <li>ResNum features coloured by label</li>
283    * <li>Insertions displayed above (on top of) ResNums</li>
284    * </ul>
285    */
286   @Override
287   public FeatureSettingsModelI getFeatureColourScheme()
288   {
289     return new PDBFeatureSettings();
290   }
291 }