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.SequenceFeature;
32 import jalview.datamodel.SequenceI;
33 import jalview.datamodel.features.SequenceFeaturesI;
34 import jalview.io.DataSourceType;
35 import jalview.io.FileFormat;
36 import jalview.io.FileFormatI;
37 import jalview.io.FormatAdapter;
38 import jalview.io.PDBFeatureSettings;
39 import jalview.structure.StructureImportSettings;
40 import jalview.util.HttpUtils;
41 import jalview.util.MessageManager;
42 import jalview.ws.ebi.EBIFetchClient;
43 import jalview.ws.utils.UrlDownloadClient;
46 import java.util.ArrayList;
47 import java.util.List;
49 import com.stevesoft.pat.Regex;
55 public class EBIAlfaFold extends EbiFileRetrievedProxy
57 private static final String SEPARATOR = "|";
59 private static final String COLON = ":";
61 private static final int PDB_ID_LENGTH = 4;
71 * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
74 public String getAccessionSeparator()
82 * @see jalview.ws.DbSourceProxy#getAccessionValidator()
85 public Regex getAccessionValidator()
87 Regex validator = new Regex("(AF-[A-Z]+[0-9]+[A-Z0-9]+-F1)");
88 validator.setIgnoreCase(true);
95 * @see jalview.ws.DbSourceProxy#getDbSource()
98 public String getDbSource()
106 * @see jalview.ws.DbSourceProxy#getDbVersion()
109 public String getDbVersion()
114 public static String getAlphaFoldCifDownloadUrl(String id)
116 return "https://alphafold.ebi.ac.uk/files/" + id + "-model_v1.cif";
122 * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
125 public AlignmentI getSequenceRecords(String queries) throws Exception
127 return getSequenceRecords(queries, null);
130 public AlignmentI getSequenceRecords(String queries, String retrievalUrl)
133 AlignmentI pdbAlignment = null;
136 if (queries.indexOf(COLON) > -1)
138 chain = queries.substring(queries.indexOf(COLON) + 1);
139 id = queries.substring(0, queries.indexOf(COLON));
146 if (!isValidReference(id))
149 "(AFClient) Ignoring invalid pdb query: '" + id + "'");
153 String alphaFoldCif = getAlphaFoldCifDownloadUrl(id);
154 if (retrievalUrl != null)
156 alphaFoldCif = retrievalUrl;
161 File tmpFile = File.createTempFile(id, ".cif");
162 UrlDownloadClient.download(alphaFoldCif, tmpFile);
164 // may not need this check ?
165 file = tmpFile.getAbsolutePath();
171 pdbAlignment = importDownloadedStructureFromUrl(alphaFoldCif, tmpFile,
172 id, chain, getDbSource(), getDbVersion());
174 if (pdbAlignment == null || pdbAlignment.getHeight() < 1)
176 throw new Exception(MessageManager.formatMessage(
177 "exception.no_pdb_records_for_chain", new String[]
178 { id, ((chain == null) ? "' '" : chain) }));
181 } catch (Exception ex) // Problem parsing PDB file
190 * general purpose structure importer - designed to yield alignment useful for
191 * transfer of annotation to associated sequences
193 * @param alphaFoldCif
202 public static AlignmentI importDownloadedStructureFromUrl(
203 String alphaFoldCif, File tmpFile, String id, String chain,
204 String dbSource, String dbVersion) throws Exception
206 String file = tmpFile.getAbsolutePath();
207 // todo get rid of Type and use FileFormatI instead?
208 FileFormatI fileFormat = FileFormat.MMCif;
209 AlignmentI pdbAlignment = new FormatAdapter().readFile(tmpFile,
210 DataSourceType.FILE, fileFormat);
211 if (pdbAlignment != null)
213 List<SequenceI> toremove = new ArrayList<SequenceI>();
214 for (SequenceI pdbcs : pdbAlignment.getSequences())
218 for (PDBEntry pid : pdbcs.getAllPDBEntries())
220 if (pid.getFile() == file)
222 chid = pid.getChainCode();
226 if (chain == null || (chid != null && (chid.equals(chain)
227 || chid.trim().equals(chain.trim())
228 || (chain.trim().length() == 0 && chid.equals("_")))))
230 // FIXME seems to result in 'PDB|1QIP|1qip|A' - 1QIP is redundant.
231 // TODO: suggest simplify naming to 1qip|A as default name defined
232 pdbcs.setName(id + SEPARATOR + pdbcs.getName());
233 // Might need to add more metadata to the PDBEntry object
236 * PDBEntry entry = new PDBEntry(); // Construct the PDBEntry
237 * entry.setId(id); if (entry.getProperty() == null)
238 * entry.setProperty(new Hashtable());
239 * entry.getProperty().put("chains", pdbchain.id + "=" +
240 * sq.getStart() + "-" + sq.getEnd());
241 * sq.getDatasetSequence().addPDBId(entry);
244 // We make a DBRefEtntry because we have obtained the PDB file from
247 // JBPNote - PDB DBRefEntry should also carry the chain and mapping
249 if (dbSource != null)
251 DBRefEntry dbentry = new DBRefEntry(dbSource,
253 dbVersion, (chid == null ? id : id + chid));
255 pdbcs.addDBRef(dbentry);
256 // update any feature groups
257 List<SequenceFeature> allsf = pdbcs.getFeatures()
259 List<SequenceFeature> newsf = new ArrayList<SequenceFeature>();
260 if (allsf != null && allsf.size() > 0)
262 for (SequenceFeature f : allsf)
264 if (file.equals(f.getFeatureGroup()))
266 f = new SequenceFeature(f, f.type, f.begin, f.end, id,
271 pdbcs.setSequenceFeatures(newsf);
277 // mark this sequence to be removed from the alignment
278 // - since it's not from the right chain
282 // now remove marked sequences
283 for (SequenceI pdbcs : toremove)
285 pdbAlignment.deleteSequence(pdbcs);
286 if (pdbcs.getAnnotation() != null)
288 for (AlignmentAnnotation aa : pdbcs.getAnnotation())
290 pdbAlignment.deleteAnnotation(aa);
301 * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
304 public boolean isValidReference(String accession)
306 Regex r = getAccessionValidator();
307 return r.search(accession.trim());
314 public String getTestQuery()
320 public String getDbName()
322 return "PDB"; // getDbSource();
332 * Returns a descriptor for suitable feature display settings with
334 * <li>ResNums or insertions features visible</li>
335 * <li>insertions features coloured red</li>
336 * <li>ResNum features coloured by label</li>
337 * <li>Insertions displayed above (on top of) ResNums</li>
341 public FeatureSettingsModelI getFeatureColourScheme()
343 return new PDBFeatureSettings();