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.SequenceFeature;
32 import jalview.datamodel.SequenceI;
33 import jalview.datamodel.features.SequenceFeaturesI;
34 import jalview.io.DataSourceType;
35 import jalview.io.FileFormat;
36 import jalview.io.FileFormatI;
37 import jalview.io.FormatAdapter;
38 import jalview.io.PDBFeatureSettings;
39 import jalview.structure.StructureImportSettings;
40 import jalview.util.HttpUtils;
41 import jalview.util.MessageManager;
42 import jalview.ws.ebi.EBIFetchClient;
43 import jalview.ws.utils.UrlDownloadClient;
46 import java.util.ArrayList;
47 import java.util.List;
49 import com.stevesoft.pat.Regex;
55 public class EBIAlfaFold extends EbiFileRetrievedProxy
57 private static final String SEPARATOR = "|";
59 private static final String COLON = ":";
61 private static final int PDB_ID_LENGTH = 4;
71 * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
74 public String getAccessionSeparator()
82 * @see jalview.ws.DbSourceProxy#getAccessionValidator()
85 public Regex getAccessionValidator()
87 Regex validator = new Regex("(AF-[A-Z]+[0-9]+[A-Z0-9]+-F1)");
88 validator.setIgnoreCase(true);
95 * @see jalview.ws.DbSourceProxy#getDbSource()
98 public String getDbSource()
106 * @see jalview.ws.DbSourceProxy#getDbVersion()
109 public String getDbVersion()
114 public static String getAlphaFoldCifDownloadUrl(String id)
116 return "https://alphafold.ebi.ac.uk/files/" + id + "-model_v1.cif";
122 * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
125 public AlignmentI getSequenceRecords(String queries) throws Exception
127 return getSequenceRecords(queries, null);
129 public AlignmentI getSequenceRecords(String queries, String retrievalUrl) throws Exception
131 AlignmentI pdbAlignment = null;
134 if (queries.indexOf(COLON) > -1)
136 chain = queries.substring(queries.indexOf(COLON) + 1);
137 id = queries.substring(0, queries.indexOf(COLON));
144 if (!isValidReference(id))
147 "(AFClient) Ignoring invalid pdb query: '" + id + "'");
151 String alphaFoldCif = getAlphaFoldCifDownloadUrl(id);
152 if (retrievalUrl != null)
154 alphaFoldCif = retrievalUrl;
159 File tmpFile = File.createTempFile(id, ".cif");
160 UrlDownloadClient.download(alphaFoldCif, tmpFile);
162 // may not need this check ?
163 file = tmpFile.getAbsolutePath();
169 pdbAlignment = importDownloadedStructureFromUrl(alphaFoldCif, tmpFile, id, chain, getDbSource(),getDbVersion());
172 if (pdbAlignment == null || pdbAlignment.getHeight() < 1)
174 throw new Exception(MessageManager.formatMessage(
175 "exception.no_pdb_records_for_chain", new String[]
176 { id, ((chain == null) ? "' '" : chain) }));
179 } catch (Exception ex) // Problem parsing PDB file
188 * general purpose structure importer - designed to yield alignment useful for transfer of annotation to associated sequences
189 * @param alphaFoldCif
198 public static AlignmentI importDownloadedStructureFromUrl(String alphaFoldCif,
199 File tmpFile, String id, String chain, String dbSource, String dbVersion) throws Exception
201 String file = tmpFile.getAbsolutePath();
202 // todo get rid of Type and use FileFormatI instead?
203 FileFormatI fileFormat = FileFormat.MMCif;
204 AlignmentI pdbAlignment = new FormatAdapter().readFile(tmpFile,
205 DataSourceType.FILE, fileFormat);
206 if (pdbAlignment != null)
208 List<SequenceI> toremove = new ArrayList<SequenceI>();
209 for (SequenceI pdbcs : pdbAlignment.getSequences())
213 for (PDBEntry pid : pdbcs.getAllPDBEntries())
215 if (pid.getFile() == file)
217 chid = pid.getChainCode();
221 if (chain == null || (chid != null && (chid.equals(chain)
222 || chid.trim().equals(chain.trim())
223 || (chain.trim().length() == 0 && chid.equals("_")))))
225 // FIXME seems to result in 'PDB|1QIP|1qip|A' - 1QIP is redundant.
226 // TODO: suggest simplify naming to 1qip|A as default name defined
227 pdbcs.setName(id + SEPARATOR + pdbcs.getName());
228 // Might need to add more metadata to the PDBEntry object
231 * PDBEntry entry = new PDBEntry(); // Construct the PDBEntry
232 * entry.setId(id); if (entry.getProperty() == null)
233 * entry.setProperty(new Hashtable());
234 * entry.getProperty().put("chains", pdbchain.id + "=" +
235 * sq.getStart() + "-" + sq.getEnd());
236 * sq.getDatasetSequence().addPDBId(entry);
239 // We make a DBRefEtntry because we have obtained the PDB file from
242 // JBPNote - PDB DBRefEntry should also carry the chain and mapping
244 if (dbSource != null)
246 DBRefEntry dbentry = new DBRefEntry(dbSource,
248 dbVersion, (chid == null ? id : id + chid));
250 pdbcs.addDBRef(dbentry);
251 // update any feature groups
252 List<SequenceFeature> allsf = pdbcs.getFeatures().getAllFeatures();
253 List<SequenceFeature> newsf = new ArrayList<SequenceFeature>();
254 if (allsf!=null && allsf.size()>0)
256 for (SequenceFeature f:allsf)
258 if (file.equals(f.getFeatureGroup()))
260 f = new SequenceFeature(f, f.type, f.begin, f.end, id, f.score);
264 pdbcs.setSequenceFeatures(newsf);
270 // mark this sequence to be removed from the alignment
271 // - since it's not from the right chain
275 // now remove marked sequences
276 for (SequenceI pdbcs : toremove)
278 pdbAlignment.deleteSequence(pdbcs);
279 if (pdbcs.getAnnotation() != null)
281 for (AlignmentAnnotation aa : pdbcs.getAnnotation())
283 pdbAlignment.deleteAnnotation(aa);
294 * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
297 public boolean isValidReference(String accession)
299 Regex r = getAccessionValidator();
300 return r.search(accession.trim());
307 public String getTestQuery()
313 public String getDbName()
315 return "PDB"; // getDbSource();
325 * Returns a descriptor for suitable feature display settings with
327 * <li>ResNums or insertions features visible</li>
328 * <li>insertions features coloured red</li>
329 * <li>ResNum features coloured by label</li>
330 * <li>Insertions displayed above (on top of) ResNums</li>
334 public FeatureSettingsModelI getFeatureColourScheme()
336 return new PDBFeatureSettings();