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;
25 import java.io.FileInputStream;
26 import java.io.InputStream;
27 import java.util.ArrayList;
28 import java.util.List;
31 import com.stevesoft.pat.Regex;
33 import jalview.api.FeatureSettingsModelI;
34 import jalview.bin.Console;
35 import jalview.datamodel.AlignmentAnnotation;
36 import jalview.datamodel.AlignmentI;
37 import jalview.datamodel.ContactMatrixI;
38 import jalview.datamodel.DBRefEntry;
39 import jalview.datamodel.PDBEntry;
40 import jalview.datamodel.SequenceFeature;
41 import jalview.datamodel.SequenceI;
42 import jalview.io.DataSourceType;
43 import jalview.io.FileFormat;
44 import jalview.io.FileFormatI;
45 import jalview.io.FormatAdapter;
46 import jalview.io.PDBFeatureSettings;
47 import jalview.util.MessageManager;
48 import jalview.util.Platform;
49 import jalview.ws.datamodel.alphafold.PAEContactMatrix;
50 import jalview.ws.utils.UrlDownloadClient;
56 public class EBIAlfaFold extends EbiFileRetrievedProxy
58 private static final String SEPARATOR = "|";
60 private static final String COLON = ":";
62 private static final int PDB_ID_LENGTH = 4;
64 private static String AF_VERSION = "2";
74 * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
77 public String getAccessionSeparator()
85 * @see jalview.ws.DbSourceProxy#getAccessionValidator()
88 public Regex getAccessionValidator()
90 Regex validator = new Regex("(AF-[A-Z]+[0-9]+[A-Z0-9]+-F1)");
91 validator.setIgnoreCase(true);
98 * @see jalview.ws.DbSourceProxy#getDbSource()
101 public String getDbSource()
109 * @see jalview.ws.DbSourceProxy#getDbVersion()
112 public String getDbVersion()
117 public static String getAlphaFoldCifDownloadUrl(String id, String vnum)
119 if (vnum == null || vnum.length() == 0)
123 return "https://alphafold.ebi.ac.uk/files/" + id + "-model_v" + vnum
127 public static String getAlphaFoldPaeDownloadUrl(String id, String vnum)
129 if (vnum == null || vnum.length() == 0)
133 return "https://alphafold.ebi.ac.uk/files/" + id
134 + "-predicted_aligned_error_v" + vnum + ".json";
140 * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
143 public AlignmentI getSequenceRecords(String queries) throws Exception
145 return getSequenceRecords(queries, null);
148 public AlignmentI getSequenceRecords(String queries, String retrievalUrl)
151 AlignmentI pdbAlignment = null;
154 if (queries.indexOf(COLON) > -1)
156 chain = queries.substring(queries.indexOf(COLON) + 1);
157 id = queries.substring(0, queries.indexOf(COLON));
164 if (!isValidReference(id))
167 "(AFClient) Ignoring invalid alphafold query: '" + id + "'");
171 String alphaFoldCif = getAlphaFoldCifDownloadUrl(id, AF_VERSION);
172 if (retrievalUrl != null)
174 alphaFoldCif = retrievalUrl;
179 File tmpFile = File.createTempFile(id, ".cif");
180 Console.debug("Retrieving structure file for " + id + " from "
182 UrlDownloadClient.download(alphaFoldCif, tmpFile);
184 // may not need this check ?
185 file = tmpFile.getAbsolutePath();
191 pdbAlignment = importDownloadedStructureFromUrl(alphaFoldCif, tmpFile,
192 id, chain, getDbSource(), getDbVersion());
194 if (pdbAlignment == null || pdbAlignment.getHeight() < 1)
196 throw new Exception(MessageManager.formatMessage(
197 "exception.no_pdb_records_for_chain", new String[]
198 { id, ((chain == null) ? "' '" : chain) }));
200 retrieve_AlphaFold_pAE(id, pdbAlignment, retrievalUrl);
202 } catch (Exception ex) // Problem parsing PDB file
211 * get an alphafold pAE for the given id, and add it to sequence 0 in
212 * pdbAlignment (assuming it came from structurefile parser).
215 * @param pdbAlignment
216 * @param retrievalUrl
217 * - URL of .mmcif from EBI-AlphaFold - will be used to generate the
218 * pAE URL automatically
221 public static void retrieve_AlphaFold_pAE(String id,
222 AlignmentI pdbAlignment, String retrievalUrl) throws Exception
224 // import PAE as contact matrix - assume this will work if there was a
226 File pae = File.createTempFile(id, "pae_json");
227 String paeURL = getAlphaFoldPaeDownloadUrl(id, AF_VERSION);
229 if (retrievalUrl != null)
231 // manufacture the PAE url from a url like ...-model-vN.cif
232 paeURL = retrievalUrl.replace("model", "predicted_aligned_error")
233 .replace(".cif", ".json");
235 Console.debug("Downloading pae from " + paeURL + " to " + pae.toString()
240 UrlDownloadClient.download(paeURL, pae);
241 FileInputStream pae_input = new FileInputStream(pae);
243 if (!importPaeJSONAsContactMatrix(pdbAlignment, pae_input))
245 Console.warn("Couln't import contact matrix from " + paeURL
246 + " (stored in " + pae.toString() + ")");
248 } catch (Exception pae_ex)
250 Console.debug("Couldn't download PAE", pae_ex);
256 * parses the given pAE matrix and adds it to sequence 0 in the given
259 * @param pdbAlignment
261 * @return true if there was a pAE matrix added
264 public static boolean importPaeJSONAsContactMatrix(
265 AlignmentI pdbAlignment, InputStream pae_input) throws Exception
268 List<Object> pae_obj = (List<Object>) Platform.parseJSON(pae_input);
273 ContactMatrixI matrix = new PAEContactMatrix(
274 pdbAlignment.getSequenceAt(0),
275 (Map<String, Object>) pae_obj.get(0));
277 // THIS IS NOT GOING TO WORK !!!
278 // ?? StructureFile assumes sequenceI holds all data, so AlignmentAnnotation
279 // needs to hold the contact matrix data.
280 pdbAlignment.getSequenceAt(0)
281 .addAlignmentAnnotation(pdbAlignment.addContactList(matrix));
286 * general purpose structure importer - designed to yield alignment useful for
287 * transfer of annotation to associated sequences
289 * @param alphaFoldCif
298 public static AlignmentI importDownloadedStructureFromUrl(
299 String alphaFoldCif, File tmpFile, String id, String chain,
300 String dbSource, String dbVersion) throws Exception
302 String file = tmpFile.getAbsolutePath();
303 // todo get rid of Type and use FileFormatI instead?
304 FileFormatI fileFormat = FileFormat.MMCif;
305 AlignmentI pdbAlignment = new FormatAdapter().readFile(tmpFile,
306 DataSourceType.FILE, fileFormat);
307 if (pdbAlignment != null)
309 List<SequenceI> toremove = new ArrayList<SequenceI>();
310 for (SequenceI pdbcs : pdbAlignment.getSequences())
314 for (PDBEntry pid : pdbcs.getAllPDBEntries())
316 if (pid.getFile() == file)
318 chid = pid.getChainCode();
322 if (chain == null || (chid != null && (chid.equals(chain)
323 || chid.trim().equals(chain.trim())
324 || (chain.trim().length() == 0 && chid.equals("_")))))
326 // FIXME seems to result in 'PDB|1QIP|1qip|A' - 1QIP is redundant.
327 // TODO: suggest simplify naming to 1qip|A as default name defined
328 pdbcs.setName(id + SEPARATOR + pdbcs.getName());
329 // Might need to add more metadata to the PDBEntry object
332 * PDBEntry entry = new PDBEntry(); // Construct the PDBEntry
333 * entry.setId(id); if (entry.getProperty() == null)
334 * entry.setProperty(new Hashtable());
335 * entry.getProperty().put("chains", pdbchain.id + "=" +
336 * sq.getStart() + "-" + sq.getEnd());
337 * sq.getDatasetSequence().addPDBId(entry);
340 // We make a DBRefEtntry because we have obtained the PDB file from
343 // JBPNote - PDB DBRefEntry should also carry the chain and mapping
345 if (dbSource != null)
347 DBRefEntry dbentry = new DBRefEntry(dbSource,
349 dbVersion, (chid == null ? id : id + chid));
351 pdbcs.addDBRef(dbentry);
352 // update any feature groups
353 List<SequenceFeature> allsf = pdbcs.getFeatures()
355 List<SequenceFeature> newsf = new ArrayList<SequenceFeature>();
356 if (allsf != null && allsf.size() > 0)
358 for (SequenceFeature f : allsf)
360 if (file.equals(f.getFeatureGroup()))
362 f = new SequenceFeature(f, f.type, f.begin, f.end, id,
367 pdbcs.setSequenceFeatures(newsf);
373 // mark this sequence to be removed from the alignment
374 // - since it's not from the right chain
378 // now remove marked sequences
379 for (SequenceI pdbcs : toremove)
381 pdbAlignment.deleteSequence(pdbcs);
382 if (pdbcs.getAnnotation() != null)
384 for (AlignmentAnnotation aa : pdbcs.getAnnotation())
386 pdbAlignment.deleteAnnotation(aa);
397 * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
400 public boolean isValidReference(String accession)
402 Regex r = getAccessionValidator();
403 return r.search(accession.trim());
410 public String getTestQuery()
412 return "AF-O15552-F1";
416 public String getDbName()
418 return "ALPHAFOLD"; // getDbSource();
428 * Returns a descriptor for suitable feature display settings with
430 * <li>ResNums or insertions features visible</li>
431 * <li>insertions features coloured red</li>
432 * <li>ResNum features coloured by label</li>
433 * <li>Insertions displayed above (on top of) ResNums</li>
437 public FeatureSettingsModelI getFeatureColourScheme()
439 return new PDBFeatureSettings();