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.SequenceI;
32 import jalview.io.DataSourceType;
33 import jalview.io.FileFormat;
34 import jalview.io.FileFormatI;
35 import jalview.io.FormatAdapter;
36 import jalview.io.PDBFeatureSettings;
37 import jalview.structure.StructureImportSettings;
38 import jalview.util.HttpUtils;
39 import jalview.util.MessageManager;
40 import jalview.ws.ebi.EBIFetchClient;
41 import jalview.ws.utils.UrlDownloadClient;
44 import java.util.ArrayList;
45 import java.util.List;
47 import com.stevesoft.pat.Regex;
53 public class EBIAlfaFold extends EbiFileRetrievedProxy
55 private static final String SEPARATOR = "|";
57 private static final String COLON = ":";
59 private static final int PDB_ID_LENGTH = 4;
69 * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
72 public String getAccessionSeparator()
80 * @see jalview.ws.DbSourceProxy#getAccessionValidator()
83 public Regex getAccessionValidator()
85 Regex validator = new Regex("(AF-[A-Z]+[0-9]+[A-Z0-9]+-F1)");
86 validator.setIgnoreCase(true);
93 * @see jalview.ws.DbSourceProxy#getDbSource()
96 public String getDbSource()
104 * @see jalview.ws.DbSourceProxy#getDbVersion()
107 public String getDbVersion()
112 public static String getAlphaFoldCifDownloadUrl(String id)
114 return "https://alphafold.ebi.ac.uk/files/" + id + "-model_v1.cif";
120 * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
123 public AlignmentI getSequenceRecords(String queries) throws Exception
125 AlignmentI pdbAlignment = null;
128 if (queries.indexOf(COLON) > -1)
130 chain = queries.substring(queries.indexOf(COLON) + 1);
131 id = queries.substring(0, queries.indexOf(COLON));
138 if (!isValidReference(id))
141 "(AFClient) Ignoring invalid pdb query: '" + id + "'");
145 String alphaFoldCif = getAlphaFoldCifDownloadUrl(id);
149 File tmpFile = File.createTempFile(id, ".cif");
150 UrlDownloadClient.download(alphaFoldCif, tmpFile);
152 // may not need this check ?
153 file = tmpFile.getAbsolutePath();
159 pdbAlignment = importDownloadedStructureFromUrl(alphaFoldCif, tmpFile, id, chain, getDbSource(),getDbVersion());
162 if (pdbAlignment == null || pdbAlignment.getHeight() < 1)
164 throw new Exception(MessageManager.formatMessage(
165 "exception.no_pdb_records_for_chain", new String[]
166 { id, ((chain == null) ? "' '" : chain) }));
169 } catch (Exception ex) // Problem parsing PDB file
178 * general purpose structure importer - designed to yield alignment useful for transfer of annotation to associated sequences
179 * @param alphaFoldCif
188 public static AlignmentI importDownloadedStructureFromUrl(String alphaFoldCif,
189 File tmpFile, String id, String chain, String dbSource, String dbVersion) throws Exception
191 String file = tmpFile.getAbsolutePath();
192 // todo get rid of Type and use FileFormatI instead?
193 FileFormatI fileFormat = FileFormat.MMCif;
194 AlignmentI pdbAlignment = new FormatAdapter().readFile(tmpFile,
195 DataSourceType.FILE, fileFormat);
196 if (pdbAlignment != null)
198 List<SequenceI> toremove = new ArrayList<SequenceI>();
199 for (SequenceI pdbcs : pdbAlignment.getSequences())
203 for (PDBEntry pid : pdbcs.getAllPDBEntries())
205 if (pid.getFile() == file)
207 chid = pid.getChainCode();
211 if (chain == null || (chid != null && (chid.equals(chain)
212 || chid.trim().equals(chain.trim())
213 || (chain.trim().length() == 0 && chid.equals("_")))))
215 // FIXME seems to result in 'PDB|1QIP|1qip|A' - 1QIP is redundant.
216 // TODO: suggest simplify naming to 1qip|A as default name defined
217 pdbcs.setName(id + SEPARATOR + pdbcs.getName());
218 // Might need to add more metadata to the PDBEntry object
221 * PDBEntry entry = new PDBEntry(); // Construct the PDBEntry
222 * entry.setId(id); if (entry.getProperty() == null)
223 * entry.setProperty(new Hashtable());
224 * entry.getProperty().put("chains", pdbchain.id + "=" +
225 * sq.getStart() + "-" + sq.getEnd());
226 * sq.getDatasetSequence().addPDBId(entry);
229 // We make a DBRefEtntry because we have obtained the PDB file from
232 // JBPNote - PDB DBRefEntry should also carry the chain and mapping
234 if (dbSource != null)
236 DBRefEntry dbentry = new DBRefEntry(dbSource,
238 dbVersion, (chid == null ? id : id + chid));
240 pdbcs.addDBRef(dbentry);
245 // mark this sequence to be removed from the alignment
246 // - since it's not from the right chain
250 // now remove marked sequences
251 for (SequenceI pdbcs : toremove)
253 pdbAlignment.deleteSequence(pdbcs);
254 if (pdbcs.getAnnotation() != null)
256 for (AlignmentAnnotation aa : pdbcs.getAnnotation())
258 pdbAlignment.deleteAnnotation(aa);
269 * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
272 public boolean isValidReference(String accession)
274 Regex r = getAccessionValidator();
275 return r.search(accession.trim());
282 public String getTestQuery()
288 public String getDbName()
290 return "PDB"; // getDbSource();
300 * Returns a descriptor for suitable feature display settings with
302 * <li>ResNums or insertions features visible</li>
303 * <li>insertions features coloured red</li>
304 * <li>ResNum features coloured by label</li>
305 * <li>Insertions displayed above (on top of) ResNums</li>
309 public FeatureSettingsModelI getFeatureColourScheme()
311 return new PDBFeatureSettings();