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.bin.Console;
27 import jalview.datamodel.AlignmentAnnotation;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.ContactMatrix;
30 import jalview.datamodel.ContactMatrixI;
31 import jalview.datamodel.DBRefEntry;
32 import jalview.datamodel.DBRefSource;
33 import jalview.datamodel.PDBEntry;
34 import jalview.datamodel.PDBEntry.Type;
35 import jalview.datamodel.SequenceFeature;
36 import jalview.datamodel.SequenceI;
37 import jalview.datamodel.features.SequenceFeaturesI;
38 import jalview.io.DataSourceType;
39 import jalview.io.FileFormat;
40 import jalview.io.FileFormatI;
41 import jalview.io.FormatAdapter;
42 import jalview.io.PDBFeatureSettings;
43 import jalview.javascript.json.JSON;
44 import jalview.structure.StructureImportSettings;
45 import jalview.util.HttpUtils;
46 import jalview.util.MessageManager;
47 import jalview.util.Platform;
48 import jalview.ws.datamodel.alphafold.PAEContactMatrix;
49 import jalview.ws.ebi.EBIFetchClient;
50 import jalview.ws.utils.UrlDownloadClient;
52 import java.io.BufferedReader;
54 import java.io.FileInputStream;
55 import java.util.ArrayList;
56 import java.util.List;
59 import org.jmol.adapter.readers.simple.JSONReader;
61 import com.stevesoft.pat.Regex;
67 public class EBIAlfaFold extends EbiFileRetrievedProxy
69 private static final String SEPARATOR = "|";
71 private static final String COLON = ":";
73 private static final int PDB_ID_LENGTH = 4;
83 * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
86 public String getAccessionSeparator()
94 * @see jalview.ws.DbSourceProxy#getAccessionValidator()
97 public Regex getAccessionValidator()
99 Regex validator = new Regex("(AF-[A-Z]+[0-9]+[A-Z0-9]+-F1)");
100 validator.setIgnoreCase(true);
107 * @see jalview.ws.DbSourceProxy#getDbSource()
110 public String getDbSource()
118 * @see jalview.ws.DbSourceProxy#getDbVersion()
121 public String getDbVersion()
126 public static String getAlphaFoldCifDownloadUrl(String id)
128 return "https://alphafold.ebi.ac.uk/files/" + id + "-model_v1.cif";
131 public static String getAlphaFoldPaeDownloadUrl(String id)
133 return "https://alphafold.ebi.ac.uk/files/" + id
134 + "-predicted_aligned_error_v1.json";
140 * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
143 public AlignmentI getSequenceRecords(String queries) throws Exception
145 return getSequenceRecords(queries, null);
147 public AlignmentI getSequenceRecords(String queries, String retrievalUrl) throws Exception
149 AlignmentI pdbAlignment = null;
152 if (queries.indexOf(COLON) > -1)
154 chain = queries.substring(queries.indexOf(COLON) + 1);
155 id = queries.substring(0, queries.indexOf(COLON));
162 if (!isValidReference(id))
165 "(AFClient) Ignoring invalid alphafold query: '" + id + "'");
169 String alphaFoldCif = getAlphaFoldCifDownloadUrl(id);
170 if (retrievalUrl != null)
172 alphaFoldCif = retrievalUrl;
177 File tmpFile = File.createTempFile(id, ".cif");
178 Console.debug("Retrieving structure file for "+id+" from "+alphaFoldCif);
179 UrlDownloadClient.download(alphaFoldCif, tmpFile);
181 // may not need this check ?
182 file = tmpFile.getAbsolutePath();
188 pdbAlignment = importDownloadedStructureFromUrl(alphaFoldCif, tmpFile, id, chain, getDbSource(),getDbVersion());
191 if (pdbAlignment == null || pdbAlignment.getHeight() < 1)
193 throw new Exception(MessageManager.formatMessage(
194 "exception.no_pdb_records_for_chain", new String[]
195 { id, ((chain == null) ? "' '" : chain) }));
198 // import PAE as contact matrix - assume this will work if there was a
200 File pae = File.createTempFile(id, "pae_json");
201 String paeURL = getAlphaFoldPaeDownloadUrl(id);
203 if (retrievalUrl!=null) {
204 // manufacture the PAE url from a url like ...-model-vN.cif
205 paeURL = retrievalUrl.replace("model","predicted_aligned_error").replace(".cif",".json");
207 Console.debug("Downloading pae from " + paeURL
208 + " to " + pae.toString() + "");
211 UrlDownloadClient.download(paeURL, pae);
212 if (!importPaeJSONAsContactMatrix(pdbAlignment, pae))
214 Console.warn("Couln't import contact matrix from " + paeURL
215 + " (stored in " + pae.toString() + ")");
217 } catch (Exception pae_ex) {
218 Console.debug("Couldn't download PAE",pae_ex);
221 } catch (Exception ex) // Problem parsing PDB file
229 private boolean importPaeJSONAsContactMatrix(AlignmentI pdbAlignment,
230 File pae) throws Exception
232 FileInputStream pae_input = new FileInputStream(pae);
234 List<Object> pae_obj = (List<Object>) Platform
235 .parseJSON(pae_input);
240 ContactMatrixI matrix = new PAEContactMatrix(
241 pdbAlignment.getSequenceAt(0), (Map<String, Object>)pae_obj.get(0));
243 pdbAlignment.getSequenceAt(0).addAlignmentAnnotation(pdbAlignment.addContactList(matrix));
248 * general purpose structure importer - designed to yield alignment useful for transfer of annotation to associated sequences
249 * @param alphaFoldCif
258 public static AlignmentI importDownloadedStructureFromUrl(String alphaFoldCif,
259 File tmpFile, String id, String chain, String dbSource, String dbVersion) throws Exception
261 String file = tmpFile.getAbsolutePath();
262 // todo get rid of Type and use FileFormatI instead?
263 FileFormatI fileFormat = FileFormat.MMCif;
264 AlignmentI pdbAlignment = new FormatAdapter().readFile(tmpFile,
265 DataSourceType.FILE, fileFormat);
266 if (pdbAlignment != null)
268 List<SequenceI> toremove = new ArrayList<SequenceI>();
269 for (SequenceI pdbcs : pdbAlignment.getSequences())
273 for (PDBEntry pid : pdbcs.getAllPDBEntries())
275 if (pid.getFile() == file)
277 chid = pid.getChainCode();
281 if (chain == null || (chid != null && (chid.equals(chain)
282 || chid.trim().equals(chain.trim())
283 || (chain.trim().length() == 0 && chid.equals("_")))))
285 // FIXME seems to result in 'PDB|1QIP|1qip|A' - 1QIP is redundant.
286 // TODO: suggest simplify naming to 1qip|A as default name defined
287 pdbcs.setName(id + SEPARATOR + pdbcs.getName());
288 // Might need to add more metadata to the PDBEntry object
291 * PDBEntry entry = new PDBEntry(); // Construct the PDBEntry
292 * entry.setId(id); if (entry.getProperty() == null)
293 * entry.setProperty(new Hashtable());
294 * entry.getProperty().put("chains", pdbchain.id + "=" +
295 * sq.getStart() + "-" + sq.getEnd());
296 * sq.getDatasetSequence().addPDBId(entry);
299 // We make a DBRefEtntry because we have obtained the PDB file from
302 // JBPNote - PDB DBRefEntry should also carry the chain and mapping
304 if (dbSource != null)
306 DBRefEntry dbentry = new DBRefEntry(dbSource,
308 dbVersion, (chid == null ? id : id + chid));
310 pdbcs.addDBRef(dbentry);
311 // update any feature groups
312 List<SequenceFeature> allsf = pdbcs.getFeatures().getAllFeatures();
313 List<SequenceFeature> newsf = new ArrayList<SequenceFeature>();
314 if (allsf!=null && allsf.size()>0)
316 for (SequenceFeature f:allsf)
318 if (file.equals(f.getFeatureGroup()))
320 f = new SequenceFeature(f, f.type, f.begin, f.end, id, f.score);
324 pdbcs.setSequenceFeatures(newsf);
330 // mark this sequence to be removed from the alignment
331 // - since it's not from the right chain
335 // now remove marked sequences
336 for (SequenceI pdbcs : toremove)
338 pdbAlignment.deleteSequence(pdbcs);
339 if (pdbcs.getAnnotation() != null)
341 for (AlignmentAnnotation aa : pdbcs.getAnnotation())
343 pdbAlignment.deleteAnnotation(aa);
354 * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
357 public boolean isValidReference(String accession)
359 Regex r = getAccessionValidator();
360 return r.search(accession.trim());
367 public String getTestQuery()
369 return "AF-O15552-F1";
373 public String getDbName()
375 return "ALPHAFOLD"; // getDbSource();
385 * Returns a descriptor for suitable feature display settings with
387 * <li>ResNums or insertions features visible</li>
388 * <li>insertions features coloured red</li>
389 * <li>ResNum features coloured by label</li>
390 * <li>Insertions displayed above (on top of) ResNums</li>
394 public FeatureSettingsModelI getFeatureColourScheme()
396 return new PDBFeatureSettings();