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.FileNotFoundException;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.ArrayList;
30 import java.util.Date;
31 import java.util.HashMap;
32 import java.util.List;
35 import org.json.simple.JSONArray;
36 import org.json.simple.JSONObject;
37 import org.json.simple.parser.ParseException;
39 import com.stevesoft.pat.Regex;
41 import jalview.api.FeatureSettingsModelI;
42 import jalview.bin.Console;
43 import jalview.datamodel.AlignedCodonFrame;
44 import jalview.datamodel.AlignmentAnnotation;
45 import jalview.datamodel.AlignmentI;
46 import jalview.datamodel.ContactMatrixI;
47 import jalview.datamodel.DBRefEntry;
48 import jalview.datamodel.PDBEntry;
49 import jalview.datamodel.SequenceFeature;
50 import jalview.datamodel.SequenceI;
51 import jalview.gui.Desktop;
52 import jalview.io.DataSourceType;
53 import jalview.io.FileFormat;
54 import jalview.io.FileFormatI;
55 import jalview.io.FormatAdapter;
56 import jalview.io.PDBFeatureSettings;
57 import jalview.structure.StructureMapping;
58 import jalview.structure.StructureSelectionManager;
59 import jalview.util.MessageManager;
60 import jalview.util.Platform;
61 import jalview.ws.datamodel.alphafold.PAEContactMatrix;
62 import jalview.ws.utils.UrlDownloadClient;
68 public class EBIAlfaFold extends EbiFileRetrievedProxy
70 private static final String SEPARATOR = "|";
72 private static final String COLON = ":";
74 private static final int PDB_ID_LENGTH = 4;
76 private static String AF_VERSION = "3";
86 * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
89 public String getAccessionSeparator()
97 * @see jalview.ws.DbSourceProxy#getAccessionValidator()
100 public Regex getAccessionValidator()
102 Regex validator = new Regex("(AF-[A-Z]+[0-9]+[A-Z0-9]+-F1)");
103 validator.setIgnoreCase(true);
110 * @see jalview.ws.DbSourceProxy#getDbSource()
113 public String getDbSource()
121 * @see jalview.ws.DbSourceProxy#getDbVersion()
124 public String getDbVersion()
129 public static String getAlphaFoldCifDownloadUrl(String id, String vnum)
131 if (vnum == null || vnum.length() == 0)
135 return "https://alphafold.ebi.ac.uk/files/" + id + "-model_v" + vnum
139 public static String getAlphaFoldPaeDownloadUrl(String id, String vnum)
141 if (vnum == null || vnum.length() == 0)
145 return "https://alphafold.ebi.ac.uk/files/" + id
146 + "-predicted_aligned_error_v" + vnum + ".json";
152 * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
155 public AlignmentI getSequenceRecords(String queries) throws Exception
157 return getSequenceRecords(queries, null);
160 public AlignmentI getSequenceRecords(String queries, String retrievalUrl)
163 AlignmentI pdbAlignment = null;
166 if (queries.indexOf(COLON) > -1)
168 chain = queries.substring(queries.indexOf(COLON) + 1);
169 id = queries.substring(0, queries.indexOf(COLON));
176 if (!isValidReference(id))
179 "(AFClient) Ignoring invalid alphafold query: '" + id + "'");
183 String alphaFoldCif = getAlphaFoldCifDownloadUrl(id, AF_VERSION);
184 if (retrievalUrl != null)
186 alphaFoldCif = retrievalUrl;
191 File tmpFile = File.createTempFile(id, ".cif");
192 Console.debug("Retrieving structure file for " + id + " from "
194 UrlDownloadClient.download(alphaFoldCif, tmpFile);
196 // may not need this check ?
197 file = tmpFile.getAbsolutePath();
203 pdbAlignment = importDownloadedStructureFromUrl(alphaFoldCif, tmpFile,
204 id, chain, getDbSource(), getDbVersion());
206 if (pdbAlignment == null || pdbAlignment.getHeight() < 1)
208 throw new Exception(MessageManager.formatMessage(
209 "exception.no_pdb_records_for_chain", new String[]
210 { id, ((chain == null) ? "' '" : chain) }));
212 // done during structure retrieval
213 // retrieve_AlphaFold_pAE(id, pdbAlignment, retrievalUrl);
215 } catch (Exception ex) // Problem parsing PDB file
224 * get an alphafold pAE for the given id and return the File object of the
225 * downloaded (temp) file
228 * @param pdbAlignment
229 * @param retrievalUrl
230 * - URL of .mmcif from EBI-AlphaFold - will be used to generate the
231 * pAE URL automatically
232 * @throws IOException
235 public static File fetchAlphaFoldPAE(String id, String retrievalUrl)
238 // import PAE as contact matrix - assume this will work if there was a
240 String paeURL = getAlphaFoldPaeDownloadUrl(id, AF_VERSION);
242 if (retrievalUrl != null)
244 // manufacture the PAE url from a url like ...-model-vN.cif
245 paeURL = retrievalUrl.replace("model", "predicted_aligned_error")
246 .replace(".cif", ".json");
250 File pae = paeDownloadCache.get(paeURL);
251 if (pae != null && pae.exists() && (new Date().getTime()
252 - pae.lastModified()) < PAE_CACHE_STALE_TIME)
255 "Using existing file in PAE cache for '" + paeURL + "'");
261 pae = File.createTempFile(id == null ? "af_pae" : id, "pae_json");
262 } catch (IOException e)
266 Console.debug("Downloading pae from " + paeURL + " to " + pae.toString()
270 UrlDownloadClient.download(paeURL, pae);
271 } catch (IOException e)
275 // cache and it if successful
276 paeDownloadCache.put(paeURL, pae);
281 * get an alphafold pAE for the given id, and add it to sequence 0 in
282 * pdbAlignment (assuming it came from structurefile parser).
285 * @param pdbAlignment
286 * @param retrievalUrl
287 * - URL of .mmcif from EBI-AlphaFold - will be used to generate the
288 * pAE URL automatically
289 * @throws IOException
292 public static void retrieve_AlphaFold_pAE(String id,
293 AlignmentI pdbAlignment, String retrievalUrl) throws IOException
295 File pae = fetchAlphaFoldPAE(id, retrievalUrl);
296 addAlphaFoldPAE(pdbAlignment, pae, 0, null, false, false);
299 public static void addAlphaFoldPAE(AlignmentI pdbAlignment, File pae,
300 int index, String id, boolean isStruct, boolean isStructId)
302 FileInputStream paeInput = null;
305 paeInput = new FileInputStream(pae);
306 } catch (FileNotFoundException e)
309 "Could not find pAE file '" + pae.getAbsolutePath() + "'", e);
315 StructureSelectionManager ssm = StructureSelectionManager
316 .getStructureSelectionManager(Desktop.instance);
319 String structFilename = isStructId ? ssm.findFileForPDBId(id) : id;
320 addPAEToStructure(ssm, structFilename, pae);
326 // attach to sequence?!
329 if (!importPaeJSONAsContactMatrixToSequence(pdbAlignment, paeInput,
332 Console.warn("Could not import contact matrix from '"
333 + pae.getAbsolutePath() + "' to sequence.");
335 } catch (IOException e1)
337 Console.error("Error when importing pAE file '"
338 + pae.getAbsolutePath() + "'", e1);
339 } catch (ParseException e2)
341 Console.error("Error when parsing pAE file '"
342 + pae.getAbsolutePath() + "'", e2);
348 public static void addPAEToStructure(StructureSelectionManager ssm,
349 String structFilename, File pae)
351 FileInputStream paeInput = null;
354 paeInput = new FileInputStream(pae);
355 } catch (FileNotFoundException e)
358 "Could not find pAE file '" + pae.getAbsolutePath() + "'", e);
363 ssm = StructureSelectionManager
364 .getStructureSelectionManager(Desktop.instance);
368 StructureMapping[] smArray = ssm.getMapping(structFilename);
372 if (!importPaeJSONAsContactMatrixToStructure(smArray, paeInput))
374 Console.warn("Could not import contact matrix from '"
375 + pae.getAbsolutePath() + "' to structure.");
377 } catch (IOException e1)
379 Console.error("Error when importing pAE file '"
380 + pae.getAbsolutePath() + "'", e1);
381 } catch (ParseException e2)
383 Console.error("Error when parsing pAE file '"
384 + pae.getAbsolutePath() + "'", e2);
390 * parses the given pAE matrix and adds it to sequence 0 in the given
393 * @param pdbAlignment
395 * @return true if there was a pAE matrix added
396 * @throws ParseException
397 * @throws IOException
400 public static boolean importPaeJSONAsContactMatrixToSequence(
401 AlignmentI pdbAlignment, InputStream pae_input)
402 throws IOException, ParseException
404 return importPaeJSONAsContactMatrixToSequence(pdbAlignment, pae_input,
408 public static boolean importPaeJSONAsContactMatrixToSequence(
409 AlignmentI pdbAlignment, File paeFile, int index, String seqId)
410 throws FileNotFoundException, IOException, ParseException
412 return importPaeJSONAsContactMatrixToSequence(pdbAlignment,
413 new FileInputStream(paeFile), index, seqId);
416 public static boolean importPaeJSONAsContactMatrixToSequence(
417 AlignmentI pdbAlignment, InputStream pae_input, int index,
418 String seqId) throws IOException, ParseException
420 SequenceI sequence = null;
423 int seqToGet = index > 0 ? index : 0;
424 sequence = pdbAlignment.getSequenceAt(seqToGet);
426 if (sequence == null)
428 SequenceI[] sequences = pdbAlignment.findSequenceMatch(seqId);
429 if (sequences == null || sequences.length < 1)
431 Console.warn("Could not find sequence with id '" + seqId
432 + "' to attach pAE matrix to. Ignoring matrix.");
437 sequence = sequences[0]; // just use the first sequence with this seqId
440 if (sequence == null)
444 return importPaeJSONAsContactMatrixToSequence(pdbAlignment, pae_input,
448 public static boolean importPaeJSONAsContactMatrixToSequence(
449 AlignmentI pdbAlignment, File pae_input, SequenceI sequence)
450 throws IOException, ParseException
452 return importPaeJSONAsContactMatrixToSequence(pdbAlignment,
453 new FileInputStream(pae_input), sequence);
456 public static boolean importPaeJSONAsContactMatrixToSequence(
457 AlignmentI pdbAlignment, InputStream pae_input,
458 SequenceI sequence) throws IOException, ParseException
460 JSONObject paeDict = parseJSONtoPAEContactMatrix(pae_input);
463 Console.debug("JSON file did not parse properly.");
466 ContactMatrixI matrix = new PAEContactMatrix(sequence,
467 (Map<String, Object>) paeDict);
469 AlignmentAnnotation cmannot = sequence.addContactList(matrix);
470 pdbAlignment.addAnnotation(cmannot);
475 public static JSONObject parseJSONtoPAEContactMatrix(
476 InputStream pae_input) throws IOException, ParseException
478 Object paeJson = Platform.parseJSON(pae_input);
479 JSONObject paeDict = null;
480 if (paeJson instanceof JSONObject)
482 paeDict = (JSONObject) paeJson;
484 else if (paeJson instanceof JSONArray)
486 JSONArray jsonArray = (JSONArray) paeJson;
487 if (jsonArray.size() > 0)
488 paeDict = (JSONObject) jsonArray.get(0);
494 public static boolean importPaeJSONAsContactMatrixToStructure(
495 StructureMapping[] smArray, InputStream paeInput)
496 throws IOException, ParseException
498 boolean someDone = false;
499 for (StructureMapping sm : smArray)
501 boolean thisDone = importPaeJSONAsContactMatrixToStructure(sm,
503 someDone |= thisDone;
508 public static boolean importPaeJSONAsContactMatrixToStructure(
509 StructureMapping sm, InputStream paeInput)
510 throws IOException, ParseException
512 JSONObject pae_obj = parseJSONtoPAEContactMatrix(paeInput);
515 Console.debug("JSON file did not parse properly.");
519 ContactMatrixI matrix = new PAEContactMatrix(sm.getSequence(),
520 (Map<String, Object>) pae_obj);
522 AlignmentAnnotation cmannot = sm.getSequence().addContactList(matrix);
523 // sm.getSequence().addAlignmentAnnotation(cmannot);
524 sm.transfer(cmannot);
527 StructureSelectionManager ssm = StructureSelectionManager
528 .getStructureSelectionManager(Desktop.instance);
529 List<AlignedCodonFrame> acfList = ssm.getSequenceMappings();
535 * general purpose structure importer - designed to yield alignment useful for
536 * transfer of annotation to associated sequences
538 * @param alphaFoldCif
547 public static AlignmentI importDownloadedStructureFromUrl(
548 String alphaFoldCif, File tmpFile, String id, String chain,
549 String dbSource, String dbVersion) throws Exception
551 String file = tmpFile.getAbsolutePath();
552 // todo get rid of Type and use FileFormatI instead?
553 FileFormatI fileFormat = FileFormat.MMCif;
554 AlignmentI pdbAlignment = new FormatAdapter().readFile(tmpFile,
555 DataSourceType.FILE, fileFormat);
556 if (pdbAlignment != null)
558 List<SequenceI> toremove = new ArrayList<SequenceI>();
559 for (SequenceI pdbcs : pdbAlignment.getSequences())
563 for (PDBEntry pid : pdbcs.getAllPDBEntries())
565 if (pid.getFile() == file)
567 chid = pid.getChainCode();
571 if (chain == null || (chid != null && (chid.equals(chain)
572 || chid.trim().equals(chain.trim())
573 || (chain.trim().length() == 0 && chid.equals("_")))))
575 // FIXME seems to result in 'PDB|1QIP|1qip|A' - 1QIP is redundant.
576 // TODO: suggest simplify naming to 1qip|A as default name defined
577 pdbcs.setName(id + SEPARATOR + pdbcs.getName());
578 // Might need to add more metadata to the PDBEntry object
581 * PDBEntry entry = new PDBEntry(); // Construct the PDBEntry
582 * entry.setId(id); if (entry.getProperty() == null)
583 * entry.setProperty(new Hashtable());
584 * entry.getProperty().put("chains", pdbchain.id + "=" +
585 * sq.getStart() + "-" + sq.getEnd());
586 * sq.getDatasetSequence().addPDBId(entry);
589 // We make a DBRefEtntry because we have obtained the PDB file from
592 // JBPNote - PDB DBRefEntry should also carry the chain and mapping
594 if (dbSource != null)
596 DBRefEntry dbentry = new DBRefEntry(dbSource,
598 dbVersion, (chid == null ? id : id + chid));
600 pdbcs.addDBRef(dbentry);
601 // update any feature groups
602 List<SequenceFeature> allsf = pdbcs.getFeatures()
604 List<SequenceFeature> newsf = new ArrayList<SequenceFeature>();
605 if (allsf != null && allsf.size() > 0)
607 for (SequenceFeature f : allsf)
609 if (file.equals(f.getFeatureGroup()))
611 f = new SequenceFeature(f, f.type, f.begin, f.end, id,
616 pdbcs.setSequenceFeatures(newsf);
622 // mark this sequence to be removed from the alignment
623 // - since it's not from the right chain
627 // now remove marked sequences
628 for (SequenceI pdbcs : toremove)
630 pdbAlignment.deleteSequence(pdbcs);
631 if (pdbcs.getAnnotation() != null)
633 for (AlignmentAnnotation aa : pdbcs.getAnnotation())
635 pdbAlignment.deleteAnnotation(aa);
646 * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
649 public boolean isValidReference(String accession)
651 Regex r = getAccessionValidator();
652 return r.search(accession.trim());
659 public String getTestQuery()
661 return "AF-O15552-F1";
665 public String getDbName()
667 return "ALPHAFOLD"; // getDbSource();
677 * Returns a descriptor for suitable feature display settings with
679 * <li>ResNums or insertions features visible</li>
680 * <li>insertions features coloured red</li>
681 * <li>ResNum features coloured by label</li>
682 * <li>Insertions displayed above (on top of) ResNums</li>
686 public FeatureSettingsModelI getFeatureColourScheme()
688 return new PDBFeatureSettings();
692 private static final long PAE_CACHE_STALE_TIME = 1 * 86400000;
694 private static Map<String, File> paeDownloadCache = new HashMap<>();