2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.ws.dbsources;
23 import java.util.Locale;
25 import jalview.bin.Cache;
26 import jalview.datamodel.Alignment;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.DBRefEntry;
29 import jalview.datamodel.DBRefSource;
30 import jalview.datamodel.PDBEntry;
31 import jalview.datamodel.Sequence;
32 import jalview.datamodel.SequenceFeature;
33 import jalview.datamodel.SequenceI;
34 import jalview.schemes.ResidueProperties;
35 import jalview.util.StringUtils;
36 import jalview.ws.seqfetcher.DbSourceProxyImpl;
37 import jalview.xml.binding.embl.ROOT;
38 import jalview.xml.binding.uniprot.DbReferenceType;
39 import jalview.xml.binding.uniprot.Entry;
40 import jalview.xml.binding.uniprot.FeatureType;
41 import jalview.xml.binding.uniprot.LocationType;
42 import jalview.xml.binding.uniprot.PositionType;
43 import jalview.xml.binding.uniprot.PropertyType;
45 import java.io.InputStream;
47 import java.net.URLConnection;
48 import java.util.ArrayList;
49 import java.util.List;
50 import java.util.Vector;
52 import javax.xml.bind.JAXBContext;
53 import javax.xml.bind.JAXBElement;
54 import javax.xml.bind.JAXBException;
55 import javax.xml.stream.FactoryConfigurationError;
56 import javax.xml.stream.XMLInputFactory;
57 import javax.xml.stream.XMLStreamException;
58 import javax.xml.stream.XMLStreamReader;
60 import com.stevesoft.pat.Regex;
63 * This class queries the Uniprot database for sequence data, unmarshals the
64 * returned XML, and converts it to Jalview Sequence records (including attached
65 * database references and sequence features)
70 public class TDBeacons extends DbSourceProxyImpl
72 private static final String DEFAULT_UNIPROT_DOMAIN = "https://www.uniprot.org";
74 private static final String BAR_DELIMITER = "|";
76 private static final String DEFAULT_THREEDBEACONS_DOMAIN = "https://wwwdev.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons-hub-api/uniprot/summary/";
86 private String getDomain()
88 return Cache.getDefault("UNIPROT_DOMAIN", DEFAULT_UNIPROT_DOMAIN);
89 // return Cache.getDefault("3DB_DOMAIN", DEFAULT_THREEDBEACONS_DOMAIN );
95 * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
98 public String getAccessionSeparator()
106 * @see jalview.ws.DbSourceProxy#getAccessionValidator()
109 public Regex getAccessionValidator()
111 return new Regex("([A-Z]+[0-9]+[A-Z0-9]+|[A-Z0-9]+_[A-Z0-9]+)");
117 * @see jalview.ws.DbSourceProxy#getDbSource()
120 public String getDbSource()
122 return "3d-beacons";// DBRefSource.UNIPROT;
128 * @see jalview.ws.DbSourceProxy#getDbVersion()
131 public String getDbVersion()
133 return "0"; // we really don't know what version we're on.
139 * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
142 public AlignmentI getSequenceRecords(String queries) throws Exception
147 queries = queries.toUpperCase(Locale.ROOT).replaceAll(
148 "(UNIPROT\\|?|UNIPROT_|UNIREF\\d+_|UNIREF\\d+\\|?)", "");
149 AlignmentI al = null;
151 String downloadstring = getDomain() + "/uniprot/" + queries + ".xml";
152 // String downloadstring = getDomain() + queries + ".json";
154 URL url = new URL(downloadstring);
155 URLConnection urlconn = url.openConnection();
156 InputStream istr = urlconn.getInputStream();
157 List<Entry> entries = getUniprotEntries(istr);
160 List<SequenceI> seqs = new ArrayList<>();
161 for (Entry entry : entries)
163 seqs.add(uniprotEntryToSequence(entry));
165 al = new Alignment(seqs.toArray(new SequenceI[seqs.size()]));
170 } catch (Exception e)
180 * Converts an Entry object (bound from Uniprot XML) to a Jalview Sequence
185 SequenceI uniprotEntryToSequence(Entry entry)
187 String id = getUniprotEntryId(entry);
189 * Sequence should not include any whitespace, but JAXB leaves these in
191 String seqString = entry.getSequence().getValue().replaceAll("\\s*",
194 SequenceI sequence = new Sequence(id, seqString);
195 sequence.setDescription(getUniprotEntryDescription(entry));
198 * add a 'self' DBRefEntry for each accession
200 final String dbVersion = getDbVersion();
201 List<DBRefEntry> dbRefs = new ArrayList<>();
202 for (String accessionId : entry.getAccession())
204 DBRefEntry dbRef = new DBRefEntry(DBRefSource.UNIPROT, dbVersion,
210 * add a DBRefEntry for each dbReference element in the XML;
211 * also add a PDBEntry if type="PDB";
212 * also add an EMBLCDS dbref if protein sequence id is given
213 * also add an Ensembl dbref " " " " " "
215 Vector<PDBEntry> pdbRefs = new Vector<>();
216 for (DbReferenceType dbref : entry.getDbReference())
218 String type = dbref.getType();
219 DBRefEntry dbr = new DBRefEntry(type,
220 DBRefSource.UNIPROT + ":" + dbVersion, dbref.getId());
222 if ("PDB".equals(type))
224 pdbRefs.add(new PDBEntry(dbr));
226 if ("EMBL".equals(type))
229 * e.g. Uniprot accession Q9BXM7 has
230 * <dbReference type="EMBL" id="M19359">
231 * <property type="protein sequence ID" value="AAA40981.1"/>
232 * <property type="molecule type" value="Genomic_DNA"/>
235 String cdsId = getProperty(dbref.getProperty(),
236 "protein sequence ID");
237 if (cdsId != null && cdsId.trim().length() > 0)
240 String[] vrs = cdsId.split("\\.");
241 String version = vrs.length > 1 ? vrs[1]
242 : DBRefSource.UNIPROT + ":" + dbVersion;
243 dbr = new DBRefEntry(DBRefSource.EMBLCDS, version, vrs[0]);
247 if ("Ensembl".equals(type))
250 * e.g. Uniprot accession Q9BXM7 has
251 * <dbReference type="Ensembl" id="ENST00000321556">
252 * <molecule id="Q9BXM7-1"/>
253 * <property type="protein sequence ID" value="ENSP00000364204"/>
254 * <property type="gene ID" value="ENSG00000158828"/>
257 String cdsId = getProperty(dbref.getProperty(),
258 "protein sequence ID");
259 if (cdsId != null && cdsId.trim().length() > 0)
261 dbr = new DBRefEntry(DBRefSource.ENSEMBL,
262 DBRefSource.UNIPROT + ":" + dbVersion, cdsId.trim());
269 * create features; they have either begin and end, or position, in XML
271 sequence.setPDBId(pdbRefs);
272 if (entry.getFeature() != null)
274 for (FeatureType uf : entry.getFeature())
276 LocationType location = uf.getLocation();
279 if (location.getPosition() != null)
281 start = location.getPosition().getPosition().intValue();
286 start = location.getBegin().getPosition().intValue();
287 end = location.getEnd().getPosition().intValue();
289 SequenceFeature sf = new SequenceFeature(uf.getType(),
290 getDescription(uf), start, end, "Uniprot");
291 sf.setStatus(uf.getStatus());
292 sequence.addSequenceFeature(sf);
295 for (DBRefEntry dbr : dbRefs)
297 sequence.addDBRef(dbr);
303 * A helper method that builds a sequence feature description
308 static String getDescription(FeatureType feature)
310 String orig = feature.getOriginal();
311 List<String> variants = feature.getVariation();
312 StringBuilder sb = new StringBuilder();
315 * append variant in standard format if present
317 * multiple variants are split over lines using <br>
319 boolean asHtml = false;
320 if (orig != null && !orig.isEmpty() && variants != null
321 && !variants.isEmpty())
324 for (String var : variants)
326 // TODO proper HGVS nomenclature for delins structural variations
327 // http://varnomen.hgvs.org/recommendations/protein/variant/delins/
328 // for now we are pragmatic - any orig/variant sequence longer than
329 // three characters is shown with single-character notation rather than
330 // three-letter notation
332 if (orig.length() < 4)
334 for (int c = 0, clen = orig.length(); c < clen; c++)
336 char origchar = orig.charAt(c);
337 String orig3 = ResidueProperties.aa2Triplet.get("" + origchar);
338 sb.append(orig3 == null ? origchar
339 : StringUtils.toSentenceCase(orig3));
347 LocationType location = feature.getLocation();
348 PositionType start = location.getPosition() == null
349 ? location.getBegin()
350 : location.getPosition();
351 sb.append(Integer.toString(start.getPosition().intValue()));
353 if (var.length() < 4)
355 for (int c = 0, clen = var.length(); c < clen; c++)
357 char varchar = var.charAt(c);
358 String var3 = ResidueProperties.aa2Triplet.get("" + varchar);
360 sb.append(var3 != null ? StringUtils.toSentenceCase(var3)
368 if (++p != variants.size())
370 sb.append("<br/> ");
379 String description = feature.getDescription();
380 if (description != null)
382 sb.append(description);
386 sb.insert(0, "<html>");
387 sb.append("</html>");
390 return sb.toString();
394 * A helper method that searches the list of properties for one with the given
395 * key, and if found returns the property value, else returns null
401 static String getProperty(List<PropertyType> properties, String key)
404 if (properties != null)
406 for (PropertyType prop : properties)
408 if (key.equals(prop.getType()))
410 value = prop.getValue();
419 * Extracts xml element entry/protein/recommendedName/fullName
424 static String getUniprotEntryDescription(Entry entry)
427 if (entry.getProtein() != null
428 && entry.getProtein().getRecommendedName() != null)
430 // fullName is mandatory if recommendedName is present
431 desc = entry.getProtein().getRecommendedName().getFullName()
438 * Constructs a sequence id by concatenating all entry/name elements with '|'
444 static String getUniprotEntryId(Entry entry)
446 StringBuilder name = new StringBuilder(32);
447 for (String n : entry.getName())
449 if (name.length() > 0)
451 name.append(BAR_DELIMITER);
455 return name.toString();
461 * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
464 public boolean isValidReference(String accession)
466 // TODO: make the following a standard validator
467 return (accession == null || accession.length() < 2) ? false
468 : getAccessionValidator().search(accession);
472 * return LDHA_CHICK uniprot entry
475 public String getTestQuery()
481 public String getDbName()
483 return "Uniprot"; // getDbSource();
493 * Reads the reply to the EBI Fetch Uniprot data query, unmarshals it to an
494 * Uniprot object, and returns the enclosed Entry objects, or null on any
500 public List<Entry> getUniprotEntries(InputStream is)
502 List<Entry> entries = null;
505 JAXBContext jc = JAXBContext
506 .newInstance("jalview.xml.binding.uniprot");
507 XMLStreamReader streamReader = XMLInputFactory.newInstance()
508 .createXMLStreamReader(is);
509 javax.xml.bind.Unmarshaller um = jc.createUnmarshaller();
510 JAXBElement<jalview.xml.binding.uniprot.Uniprot> uniprotElement = um
511 .unmarshal(streamReader,
512 jalview.xml.binding.uniprot.Uniprot.class);
513 jalview.xml.binding.uniprot.Uniprot uniprot = uniprotElement
516 if (uniprot != null && !uniprot.getEntry().isEmpty())
518 entries = uniprot.getEntry();
520 } catch (JAXBException | XMLStreamException
521 | FactoryConfigurationError e)