3 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
4 * Copyright (C) $$Year-Rel$$ The Jalview Authors
6 * This file is part of Jalview.
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.
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.
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.
22 package jalview.ws.dbsources;
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;
43 import java.util.ArrayList;
44 import java.util.List;
46 import com.stevesoft.pat.Regex;
52 public class Pdb extends EbiFileRetrievedProxy
54 private static final String SEPARATOR = "|";
56 private static final String COLON = ":";
58 private static final int PDB_ID_LENGTH = 4;
60 private static Regex ACCESSION_REGEX;
70 * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
73 public String getAccessionSeparator()
81 * @see jalview.ws.DbSourceProxy#getAccessionValidator()
84 public Regex getAccessionValidator()
86 if (ACCESSION_REGEX == null)
88 ACCESSION_REGEX = Platform
89 .newRegex("([1-9][0-9A-Za-z]{3}):?([ _A-Za-z0-9]?)");
91 return ACCESSION_REGEX;
97 * @see jalview.ws.DbSourceProxy#getDbSource()
100 public String getDbSource()
102 return DBRefSource.PDB;
108 * @see jalview.ws.DbSourceProxy#getDbVersion()
111 public String getDbVersion()
119 * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
122 public AlignmentI getSequenceRecords(String queries) throws Exception
124 AlignmentI pdbAlignment = null;
127 if (queries.indexOf(COLON) > -1)
129 chain = queries.substring(queries.indexOf(COLON) + 1);
130 id = queries.substring(0, queries.indexOf(COLON));
138 * extract chain code if it is appended to the id and we
139 * don't already have one
141 if (queries.length() > PDB_ID_LENGTH && chain == null)
143 chain = queries.substring(PDB_ID_LENGTH, PDB_ID_LENGTH + 1);
144 id = queries.substring(0, PDB_ID_LENGTH);
147 if (!isValidReference(id))
149 System.err.println("Ignoring invalid pdb query: '" + id + "'");
155 * ensure that an mmCIF format structure file is saved with extension.cif,
156 * because the Chimera "open" command recognises this extension
158 Type pdbFileFormat = StructureImportSettings
159 .getDefaultStructureFileFormat();
160 String ext = pdbFileFormat.getExtension();
161 String fetchFormat = pdbFileFormat.getFormat();
163 EBIFetchClient ebi = new EBIFetchClient();
164 File tmpFile = ebi.fetchDataAsFile("pdb:" + id, fetchFormat, ext);
165 file = tmpFile.getAbsolutePath();
173 // todo get rid of Type and use FileFormatI instead?
174 FileFormatI fileFormat = (pdbFileFormat == Type.PDB) ? FileFormat.PDB
176 pdbAlignment = new FormatAdapter().readFile(tmpFile,
177 DataSourceType.FILE, fileFormat);
178 if (pdbAlignment != null)
180 List<SequenceI> toremove = new ArrayList<SequenceI>();
181 for (SequenceI pdbcs : pdbAlignment.getSequences())
185 for (PDBEntry pid : pdbcs.getAllPDBEntries())
187 if (pid.getFile() == file)
189 chid = pid.getChainCode();
193 if (chain == null || (chid != null && (chid.equals(chain)
194 || chid.trim().equals(chain.trim())
195 || (chain.trim().length() == 0 && chid.equals("_")))))
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
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);
212 // We make a DBRefEtntry because we have obtained the PDB file from
215 // JBPNote - PDB DBRefEntry should also carry the chain and mapping
217 DBRefEntry dbentry = new DBRefEntry(getDbSource(),
218 getDbVersion(), (chid == null ? id : id + chid));
220 pdbcs.addDBRef(dbentry);
224 // mark this sequence to be removed from the alignment
225 // - since it's not from the right chain
229 // now remove marked sequences
230 for (SequenceI pdbcs : toremove)
232 pdbAlignment.deleteSequence(pdbcs);
233 if (pdbcs.getAnnotation() != null)
235 for (AlignmentAnnotation aa : pdbcs.getAnnotation())
237 pdbAlignment.deleteAnnotation(aa);
243 if (pdbAlignment == null || pdbAlignment.getHeight() < 1)
245 throw new Exception(MessageManager.formatMessage(
246 "exception.no_pdb_records_for_chain", new String[]
247 { id, ((chain == null) ? "' '" : chain) }));
250 } catch (Exception ex) // Problem parsing PDB file
261 * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
264 public boolean isValidReference(String accession)
266 Regex r = getAccessionValidator();
267 return r.search(accession.trim());
274 public String getTestQuery()
280 public String getDbName()
282 return "PDB"; // getDbSource();
292 * Returns a descriptor for suitable feature display settings with
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>
301 public FeatureSettingsModelI getFeatureColourScheme()
303 return new PDBFeatureSettings();