8951a6ef03c251f664c8c2a2e0b92dac2f217855
[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.util.Platform;
40 import jalview.ws.ebi.EBIFetchClient;
41
42 import java.io.File;
43 import java.util.ArrayList;
44 import java.util.List;
45
46 import com.stevesoft.pat.Regex;
47
48 /**
49  * @author JimP
50  * 
51  */
52 public class Pdb extends EbiFileRetrievedProxy
53 {
54   private static final String SEPARATOR = "|";
55
56   private static final String COLON = ":";
57
58   private static final int PDB_ID_LENGTH = 4;
59
60   private static Regex ACCESSION_REGEX;
61
62   public Pdb()
63   {
64     super();
65   }
66
67   /*
68    * (non-Javadoc)
69    * 
70    * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
71    */
72   @Override
73   public String getAccessionSeparator()
74   {
75     return null;
76   }
77
78   /*
79    * (non-Javadoc)
80    * 
81    * @see jalview.ws.DbSourceProxy#getAccessionValidator()
82    */
83   @Override
84   public Regex getAccessionValidator()
85   {
86     if (ACCESSION_REGEX == null)
87     {
88       ACCESSION_REGEX = Platform
89               .newRegex("([1-9][0-9A-Za-z]{3}):?([ _A-Za-z0-9]?)");
90     }
91     return ACCESSION_REGEX;
92   }
93
94   /*
95    * (non-Javadoc)
96    * 
97    * @see jalview.ws.DbSourceProxy#getDbSource()
98    */
99   @Override
100   public String getDbSource()
101   {
102     return DBRefSource.PDB;
103   }
104
105   /*
106    * (non-Javadoc)
107    * 
108    * @see jalview.ws.DbSourceProxy#getDbVersion()
109    */
110   @Override
111   public String getDbVersion()
112   {
113     return "0";
114   }
115
116   /*
117    * (non-Javadoc)
118    * 
119    * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
120    */
121   @Override
122   public AlignmentI getSequenceRecords(String queries) throws Exception
123   {
124     AlignmentI pdbAlignment = null;
125     String chain = null;
126     String id = null;
127     if (queries.indexOf(COLON) > -1)
128     {
129       chain = queries.substring(queries.indexOf(COLON) + 1);
130       id = queries.substring(0, queries.indexOf(COLON));
131     }
132     else
133     {
134       id = queries;
135     }
136
137     /*
138      * extract chain code if it is appended to the id and we
139      * don't already have one
140      */
141     if (queries.length() > PDB_ID_LENGTH && chain == null)
142     {
143       chain = queries.substring(PDB_ID_LENGTH, PDB_ID_LENGTH + 1);
144       id = queries.substring(0, PDB_ID_LENGTH);
145     }
146
147     if (!isValidReference(id))
148     {
149       System.err.println("Ignoring invalid pdb query: '" + id + "'");
150       stopQuery();
151       return null;
152     }
153
154     /*
155      * ensure that an mmCIF format structure file is saved with extension.cif,
156      * because the Chimera "open" command recognises this extension
157      */
158     Type pdbFileFormat = StructureImportSettings
159             .getDefaultStructureFileFormat();
160     String ext = pdbFileFormat.getExtension();
161     String fetchFormat = pdbFileFormat.getFormat();
162
163     EBIFetchClient ebi = new EBIFetchClient();
164     File tmpFile = ebi.fetchDataAsFile("pdb:" + id, fetchFormat, ext);
165     file = tmpFile.getAbsolutePath();
166     stopQuery();
167     if (file == null)
168     {
169       return null;
170     }
171     try
172     {
173       // todo get rid of Type and use FileFormatI instead?
174       FileFormatI fileFormat = (pdbFileFormat == Type.PDB) ? FileFormat.PDB
175               : FileFormat.MMCif;
176       pdbAlignment = new FormatAdapter().readFile(tmpFile, DataSourceType.FILE,
177               fileFormat);
178       if (pdbAlignment != null)
179       {
180         List<SequenceI> toremove = new ArrayList<SequenceI>();
181         for (SequenceI pdbcs : pdbAlignment.getSequences())
182         {
183           String chid = null;
184           // Mapping map=null;
185           for (PDBEntry pid : pdbcs.getAllPDBEntries())
186           {
187             if (pid.getFile() == file)
188             {
189               chid = pid.getChainCode();
190
191             }
192           }
193           if (chain == null || (chid != null && (chid.equals(chain)
194                   || chid.trim().equals(chain.trim())
195                   || (chain.trim().length() == 0 && chid.equals("_")))))
196           {
197             // FIXME seems to result in 'PDB|1QIP|1qip|A' - 1QIP is redundant.
198             // TODO: suggest simplify naming to 1qip|A as default name defined
199             pdbcs.setName(jalview.datamodel.DBRefSource.PDB + SEPARATOR + id
200                     + SEPARATOR + pdbcs.getName());
201             // Might need to add more metadata to the PDBEntry object
202             // like below
203             /*
204              * PDBEntry entry = new PDBEntry(); // Construct the PDBEntry
205              * entry.setId(id); if (entry.getProperty() == null)
206              * entry.setProperty(new Hashtable());
207              * entry.getProperty().put("chains", pdbchain.id + "=" +
208              * sq.getStart() + "-" + sq.getEnd());
209              * sq.getDatasetSequence().addPDBId(entry);
210              */
211             // Add PDB DB Refs
212             // We make a DBRefEtntry because we have obtained the PDB file from
213             // a
214             // verifiable source
215             // JBPNote - PDB DBRefEntry should also carry the chain and mapping
216             // information
217             DBRefEntry dbentry = new DBRefEntry(getDbSource(),
218                     getDbVersion(), (chid == null ? id : id + chid));
219             // dbentry.setMap()
220             pdbcs.addDBRef(dbentry);
221           }
222           else
223           {
224             // mark this sequence to be removed from the alignment
225             // - since it's not from the right chain
226             toremove.add(pdbcs);
227           }
228         }
229         // now remove marked sequences
230         for (SequenceI pdbcs : toremove)
231         {
232           pdbAlignment.deleteSequence(pdbcs);
233           if (pdbcs.getAnnotation() != null)
234           {
235             for (AlignmentAnnotation aa : pdbcs.getAnnotation())
236             {
237               pdbAlignment.deleteAnnotation(aa);
238             }
239           }
240         }
241       }
242
243       if (pdbAlignment == null || pdbAlignment.getHeight() < 1)
244       {
245         throw new Exception(MessageManager.formatMessage(
246                 "exception.no_pdb_records_for_chain", new String[]
247                 { id, ((chain == null) ? "' '" : chain) }));
248       }
249
250     } catch (Exception ex) // Problem parsing PDB file
251     {
252       stopQuery();
253       throw (ex);
254     }
255     return pdbAlignment;
256   }
257
258   /*
259    * (non-Javadoc)
260    * 
261    * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
262    */
263   @Override
264   public boolean isValidReference(String accession)
265   {
266     Regex r = getAccessionValidator();
267     return r.search(accession.trim());
268   }
269
270   /**
271    * human glyoxalase
272    */
273   @Override
274   public String getTestQuery()
275   {
276     return "1QIP";
277   }
278
279   @Override
280   public String getDbName()
281   {
282     return "PDB"; // getDbSource();
283   }
284
285   @Override
286   public int getTier()
287   {
288     return 0;
289   }
290
291   /**
292    * Returns a descriptor for suitable feature display settings with
293    * <ul>
294    * <li>ResNums or insertions features visible</li>
295    * <li>insertions features coloured red</li>
296    * <li>ResNum features coloured by label</li>
297    * <li>Insertions displayed above (on top of) ResNums</li>
298    * </ul>
299    */
300   @Override
301   public FeatureSettingsModelI getFeatureColourScheme()
302   {
303     return new PDBFeatureSettings();
304   }
305 }