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.List;
33 import org.json.simple.parser.ParseException;
35 import com.stevesoft.pat.Regex;
37 import jalview.api.FeatureSettingsModelI;
38 import jalview.bin.Console;
39 import jalview.datamodel.AlignmentAnnotation;
40 import jalview.datamodel.AlignmentI;
41 import jalview.datamodel.ContactMatrixI;
42 import jalview.datamodel.DBRefEntry;
43 import jalview.datamodel.PDBEntry;
44 import jalview.datamodel.SequenceFeature;
45 import jalview.datamodel.SequenceI;
46 import jalview.io.DataSourceType;
47 import jalview.io.FileFormat;
48 import jalview.io.FileFormatI;
49 import jalview.io.FormatAdapter;
50 import jalview.io.PDBFeatureSettings;
51 import jalview.util.MessageManager;
52 import jalview.util.Platform;
53 import jalview.ws.datamodel.alphafold.PAEContactMatrix;
54 import jalview.ws.utils.UrlDownloadClient;
60 public class EBIAlfaFold extends EbiFileRetrievedProxy
62 private static final String SEPARATOR = "|";
64 private static final String COLON = ":";
66 private static final int PDB_ID_LENGTH = 4;
68 private static String AF_VERSION = "3";
78 * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
81 public String getAccessionSeparator()
89 * @see jalview.ws.DbSourceProxy#getAccessionValidator()
92 public Regex getAccessionValidator()
94 Regex validator = new Regex("(AF-[A-Z]+[0-9]+[A-Z0-9]+-F1)");
95 validator.setIgnoreCase(true);
102 * @see jalview.ws.DbSourceProxy#getDbSource()
105 public String getDbSource()
113 * @see jalview.ws.DbSourceProxy#getDbVersion()
116 public String getDbVersion()
121 public static String getAlphaFoldCifDownloadUrl(String id, String vnum)
123 if (vnum == null || vnum.length() == 0)
127 return "https://alphafold.ebi.ac.uk/files/" + id + "-model_v" + vnum
131 public static String getAlphaFoldPaeDownloadUrl(String id, String vnum)
133 if (vnum == null || vnum.length() == 0)
137 return "https://alphafold.ebi.ac.uk/files/" + id
138 + "-predicted_aligned_error_v" + vnum + ".json";
144 * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
147 public AlignmentI getSequenceRecords(String queries) throws Exception
149 return getSequenceRecords(queries, null);
152 public AlignmentI getSequenceRecords(String queries, String retrievalUrl)
155 AlignmentI pdbAlignment = null;
158 if (queries.indexOf(COLON) > -1)
160 chain = queries.substring(queries.indexOf(COLON) + 1);
161 id = queries.substring(0, queries.indexOf(COLON));
168 if (!isValidReference(id))
171 "(AFClient) Ignoring invalid alphafold query: '" + id + "'");
175 String alphaFoldCif = getAlphaFoldCifDownloadUrl(id, AF_VERSION);
176 if (retrievalUrl != null)
178 alphaFoldCif = retrievalUrl;
183 File tmpFile = File.createTempFile(id, ".cif");
184 Console.debug("Retrieving structure file for " + id + " from "
186 UrlDownloadClient.download(alphaFoldCif, tmpFile);
188 // may not need this check ?
189 file = tmpFile.getAbsolutePath();
195 pdbAlignment = importDownloadedStructureFromUrl(alphaFoldCif, tmpFile,
196 id, chain, getDbSource(), getDbVersion());
198 if (pdbAlignment == null || pdbAlignment.getHeight() < 1)
200 throw new Exception(MessageManager.formatMessage(
201 "exception.no_pdb_records_for_chain", new String[]
202 { id, ((chain == null) ? "' '" : chain) }));
204 // done during structure retrieval
205 // retrieve_AlphaFold_pAE(id, pdbAlignment, retrievalUrl);
207 } catch (Exception ex) // Problem parsing PDB file
216 * get an alphafold pAE for the given id, and add it to sequence 0 in
217 * pdbAlignment (assuming it came from structurefile parser).
220 * @param pdbAlignment
221 * @param retrievalUrl
222 * - URL of .mmcif from EBI-AlphaFold - will be used to generate the
223 * pAE URL automatically
224 * @throws IOException
227 public static void retrieve_AlphaFold_pAE(String id,
228 AlignmentI pdbAlignment, String retrievalUrl) throws IOException
230 // import PAE as contact matrix - assume this will work if there was a
232 String paeURL = getAlphaFoldPaeDownloadUrl(id, AF_VERSION);
234 if (retrievalUrl != null)
236 // manufacture the PAE url from a url like ...-model-vN.cif
237 paeURL = retrievalUrl.replace("model", "predicted_aligned_error")
238 .replace(".cif", ".json");
244 pae = File.createTempFile(id == null ? "af_pae" : id, "pae_json");
245 } catch (IOException e)
249 Console.debug("Downloading pae from " + paeURL + " to " + pae.toString()
251 UrlDownloadClient.download(paeURL, pae);
252 addAlphaFoldPAE(pdbAlignment, pae);
255 public static void addAlphaFoldPAE(AlignmentI pdbAlignment, File pae)
257 FileInputStream pae_input = null;
260 pae_input = new FileInputStream(pae);
261 } catch (FileNotFoundException e)
264 "Could not find pAE file '" + pae.getAbsolutePath() + "'", e);
269 if (!importPaeJSONAsContactMatrix(pdbAlignment, pae_input))
271 Console.warn("Could not import contact matrix from '"
272 + pae.getAbsolutePath() + "'");
274 } catch (IOException e1)
276 Console.error("Error when importing pAE file '"
277 + pae.getAbsolutePath() + "'", e1);
278 } catch (ParseException e2)
281 "Error when parsing pAE file '" + pae.getAbsolutePath() + "'",
288 * parses the given pAE matrix and adds it to sequence 0 in the given
291 * @param pdbAlignment
293 * @return true if there was a pAE matrix added
294 * @throws ParseException
295 * @throws IOException
298 public static boolean importPaeJSONAsContactMatrix(
299 AlignmentI pdbAlignment, InputStream pae_input)
300 throws IOException, ParseException
303 List<Object> pae_obj = (List<Object>) Platform.parseJSON(pae_input);
308 ContactMatrixI matrix = new PAEContactMatrix(
309 pdbAlignment.getSequenceAt(0),
310 (Map<String, Object>) pae_obj.get(0));
312 AlignmentAnnotation cmannot = pdbAlignment.getSequenceAt(0)
313 .addContactList(matrix);
314 pdbAlignment.addAnnotation(cmannot);
319 * general purpose structure importer - designed to yield alignment useful for
320 * transfer of annotation to associated sequences
322 * @param alphaFoldCif
331 public static AlignmentI importDownloadedStructureFromUrl(
332 String alphaFoldCif, File tmpFile, String id, String chain,
333 String dbSource, String dbVersion) throws Exception
335 String file = tmpFile.getAbsolutePath();
336 // todo get rid of Type and use FileFormatI instead?
337 FileFormatI fileFormat = FileFormat.MMCif;
338 AlignmentI pdbAlignment = new FormatAdapter().readFile(tmpFile,
339 DataSourceType.FILE, fileFormat);
340 if (pdbAlignment != null)
342 List<SequenceI> toremove = new ArrayList<SequenceI>();
343 for (SequenceI pdbcs : pdbAlignment.getSequences())
347 for (PDBEntry pid : pdbcs.getAllPDBEntries())
349 if (pid.getFile() == file)
351 chid = pid.getChainCode();
355 if (chain == null || (chid != null && (chid.equals(chain)
356 || chid.trim().equals(chain.trim())
357 || (chain.trim().length() == 0 && chid.equals("_")))))
359 // FIXME seems to result in 'PDB|1QIP|1qip|A' - 1QIP is redundant.
360 // TODO: suggest simplify naming to 1qip|A as default name defined
361 pdbcs.setName(id + SEPARATOR + pdbcs.getName());
362 // Might need to add more metadata to the PDBEntry object
365 * PDBEntry entry = new PDBEntry(); // Construct the PDBEntry
366 * entry.setId(id); if (entry.getProperty() == null)
367 * entry.setProperty(new Hashtable());
368 * entry.getProperty().put("chains", pdbchain.id + "=" +
369 * sq.getStart() + "-" + sq.getEnd());
370 * sq.getDatasetSequence().addPDBId(entry);
373 // We make a DBRefEtntry because we have obtained the PDB file from
376 // JBPNote - PDB DBRefEntry should also carry the chain and mapping
378 if (dbSource != null)
380 DBRefEntry dbentry = new DBRefEntry(dbSource,
382 dbVersion, (chid == null ? id : id + chid));
384 pdbcs.addDBRef(dbentry);
385 // update any feature groups
386 List<SequenceFeature> allsf = pdbcs.getFeatures()
388 List<SequenceFeature> newsf = new ArrayList<SequenceFeature>();
389 if (allsf != null && allsf.size() > 0)
391 for (SequenceFeature f : allsf)
393 if (file.equals(f.getFeatureGroup()))
395 f = new SequenceFeature(f, f.type, f.begin, f.end, id,
400 pdbcs.setSequenceFeatures(newsf);
406 // mark this sequence to be removed from the alignment
407 // - since it's not from the right chain
411 // now remove marked sequences
412 for (SequenceI pdbcs : toremove)
414 pdbAlignment.deleteSequence(pdbcs);
415 if (pdbcs.getAnnotation() != null)
417 for (AlignmentAnnotation aa : pdbcs.getAnnotation())
419 pdbAlignment.deleteAnnotation(aa);
430 * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
433 public boolean isValidReference(String accession)
435 Regex r = getAccessionValidator();
436 return r.search(accession.trim());
443 public String getTestQuery()
445 return "AF-O15552-F1";
449 public String getDbName()
451 return "ALPHAFOLD"; // getDbSource();
461 * Returns a descriptor for suitable feature display settings with
463 * <li>ResNums or insertions features visible</li>
464 * <li>insertions features coloured red</li>
465 * <li>ResNum features coloured by label</li>
466 * <li>Insertions displayed above (on top of) ResNums</li>
470 public FeatureSettingsModelI getFeatureColourScheme()
472 return new PDBFeatureSettings();