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.bin.Cache;
26 import jalview.datamodel.AlignmentAnnotation;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.DBRefEntry;
29 import jalview.datamodel.DBRefSource;
30 import jalview.datamodel.PDBEntry;
31 import jalview.datamodel.PDBEntry.Type;
32 import jalview.datamodel.SequenceI;
33 import jalview.io.DataSourceType;
34 import jalview.io.FileFormat;
35 import jalview.io.FileFormatI;
36 import jalview.io.FormatAdapter;
37 import jalview.io.PDBFeatureSettings;
38 import jalview.structure.StructureImportSettings;
39 import jalview.util.MessageManager;
40 import jalview.ws.ebi.EBIFetchClient;
43 import java.util.ArrayList;
44 import java.util.List;
45 import java.util.Locale;
47 import com.stevesoft.pat.Regex;
53 public class Pdb extends EbiFileRetrievedProxy
55 private static final String SEPARATOR = "|";
57 private static final String COLON = ":";
59 private static final int PDB_ID_LENGTH = 4;
69 * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
72 public String getAccessionSeparator()
80 * @see jalview.ws.DbSourceProxy#getAccessionValidator()
83 public Regex getAccessionValidator()
85 return new Regex("([1-9][0-9A-Za-z]{3}):?([ _A-Za-z0-9]?)");
91 * @see jalview.ws.DbSourceProxy#getDbSource()
94 public String getDbSource()
96 return DBRefSource.PDB;
102 * @see jalview.ws.DbSourceProxy#getDbVersion()
105 public String getDbVersion()
113 * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
116 public AlignmentI getSequenceRecords(String queries) throws Exception
118 updateConfiguration();
119 AlignmentI pdbAlignment = null;
122 if (queries.indexOf(COLON) > -1)
124 chain = queries.substring(queries.indexOf(COLON) + 1);
125 id = queries.substring(0, queries.indexOf(COLON));
133 * extract chain code if it is appended to the id and we
134 * don't already have one
136 if (queries.length() > PDB_ID_LENGTH && chain == null)
138 chain = queries.substring(PDB_ID_LENGTH, PDB_ID_LENGTH + 1);
139 id = queries.substring(0, PDB_ID_LENGTH);
142 if (!isValidReference(id))
145 .errPrintln("Ignoring invalid pdb query: '" + id + "'");
151 * ensure that an mmCIF format structure file is saved with extension.cif,
152 * because the Chimera "open" command recognises this extension
154 Type pdbFileFormat = StructureImportSettings
155 .getDefaultStructureFileFormat();
156 String ext = pdbFileFormat.getExtension();
157 String fetchFormat = pdbFileFormat.getFormat();
159 EBIFetchClient ebi = new EBIFetchClient();
160 File tmpFile = ebi.fetchDataAsFile("pdb:" + id, fetchFormat, ext);
161 file = tmpFile.getAbsolutePath();
169 // todo get rid of Type and use FileFormatI instead?
170 FileFormatI fileFormat = (pdbFileFormat == Type.PDB) ? FileFormat.PDB
172 pdbAlignment = new FormatAdapter().readFile(tmpFile,
173 DataSourceType.FILE, fileFormat);
174 if (pdbAlignment != null)
176 List<SequenceI> toremove = new ArrayList<SequenceI>();
177 for (SequenceI pdbcs : pdbAlignment.getSequences())
181 for (PDBEntry pid : pdbcs.getAllPDBEntries())
183 if (pid.getFile() == file)
185 chid = pid.getChainCode();
189 if (chain == null || (chid != null && (chid.equals(chain)
190 || chid.trim().equals(chain.trim())
191 || (chain.trim().length() == 0 && chid.equals("_")))))
193 // FIXME seems to result in 'PDB|1QIP|1qip|A' - 1QIP is redundant.
194 // TODO: suggest simplify naming to 1qip|A as default name defined
195 if (legacyJalviewPDBeName)
197 pdbcs.setName(jalview.datamodel.DBRefSource.PDB + SEPARATOR + id
198 + SEPARATOR + pdbcs.getName());
202 int sep_pos = pdbcs.getName().indexOf(SEPARATOR);
203 String cid = pdbcs.getName().substring(0,sep_pos);
204 cid = cid.toLowerCase(Locale.ROOT);
205 pdbcs.setName(cid+"_"+pdbcs.getName().substring(sep_pos+1));
208 // Might need to add more metadata to the PDBEntry object
211 * PDBEntry entry = new PDBEntry(); // Construct the PDBEntry
212 * entry.setId(id); if (entry.getProperty() == null)
213 * entry.setProperty(new Hashtable());
214 * entry.getProperty().put("chains", pdbchain.id + "=" +
215 * sq.getStart() + "-" + sq.getEnd());
216 * sq.getDatasetSequence().addPDBId(entry);
219 // We make a DBRefEtntry because we have obtained the PDB file from
222 // JBPNote - PDB DBRefEntry should also carry the chain and mapping
224 DBRefEntry dbentry = new DBRefEntry(getDbSource(),
225 getDbVersion(), (chid == null ? id : id + chid));
227 pdbcs.addDBRef(dbentry);
231 // mark this sequence to be removed from the alignment
232 // - since it's not from the right chain
236 // now remove marked sequences
237 for (SequenceI pdbcs : toremove)
239 pdbAlignment.deleteSequence(pdbcs);
240 if (pdbcs.getAnnotation() != null)
242 for (AlignmentAnnotation aa : pdbcs.getAnnotation())
244 pdbAlignment.deleteAnnotation(aa);
250 if (pdbAlignment == null || pdbAlignment.getHeight() < 1)
252 throw new Exception(MessageManager.formatMessage(
253 "exception.no_pdb_records_for_chain", new String[]
254 { id, ((chain == null) ? "' '" : chain) }));
257 } catch (Exception ex) // Problem parsing PDB file
265 boolean simpleChainName=false;
266 boolean legacyJalviewPDBeName=true;
268 private static String SIMPLE="SIMPLE";
269 private static String LEGACY="LEGACY";
272 * update the chain naming conventions based on the configuration
274 private void updateConfiguration()
276 final String nameConf = Cache.getDefault(Cache.PDB_CHAIN_NAME, "SIMPLE");
277 simpleChainName = SIMPLE.equals(nameConf);
278 legacyJalviewPDBeName = LEGACY.equals(nameConf);
284 * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
287 public boolean isValidReference(String accession)
289 Regex r = getAccessionValidator();
290 return r.search(accession.trim());
297 public String getTestQuery()
303 public String getDbName()
305 return "PDB"; // getDbSource();
315 * Returns a descriptor for suitable feature display settings with
317 * <li>ResNums or insertions features visible</li>
318 * <li>insertions features coloured red</li>
319 * <li>ResNum features coloured by label</li>
320 * <li>Insertions displayed above (on top of) ResNums</li>
324 public FeatureSettingsModelI getFeatureColourScheme()
326 return new PDBFeatureSettings();