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