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.io.InputStream;
25 import java.net.URLConnection;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Vector;
30 import javax.xml.bind.JAXBContext;
31 import javax.xml.bind.JAXBElement;
32 import javax.xml.bind.JAXBException;
33 import javax.xml.stream.FactoryConfigurationError;
34 import javax.xml.stream.XMLInputFactory;
35 import javax.xml.stream.XMLStreamException;
36 import javax.xml.stream.XMLStreamReader;
38 import com.stevesoft.pat.Regex;
40 import jalview.bin.Cache;
41 import jalview.datamodel.Alignment;
42 import jalview.datamodel.AlignmentI;
43 import jalview.datamodel.DBRefEntry;
44 import jalview.datamodel.DBRefSource;
45 import jalview.datamodel.PDBEntry;
46 import jalview.datamodel.Sequence;
47 import jalview.datamodel.SequenceFeature;
48 import jalview.datamodel.SequenceI;
49 import jalview.schemes.ResidueProperties;
50 import jalview.util.Platform;
51 import jalview.util.StringUtils;
52 import jalview.ws.seqfetcher.DbSourceProxyImpl;
53 import jalview.xml.binding.uniprot.DbReferenceType;
54 import jalview.xml.binding.uniprot.Entry;
55 import jalview.xml.binding.uniprot.FeatureType;
56 import jalview.xml.binding.uniprot.LocationType;
57 import jalview.xml.binding.uniprot.PositionType;
58 import jalview.xml.binding.uniprot.PropertyType;
61 * This class queries the Uniprot database for sequence data, unmarshals the
62 * returned XML, and converts it to Jalview Sequence records (including attached
63 * database references and sequence features)
68 public class Uniprot extends DbSourceProxyImpl
70 private static final String DEFAULT_UNIPROT_DOMAIN = "https://www.uniprot.org";
72 private static final String BAR_DELIMITER = "|";
74 private static Regex ACCESSION_REGEX;
84 private String getDomain()
86 return Cache.getDefault("UNIPROT_DOMAIN", DEFAULT_UNIPROT_DOMAIN);
92 * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
95 public String getAccessionSeparator()
103 * @see jalview.ws.DbSourceProxy#getAccessionValidator()
106 public Regex getAccessionValidator()
108 if (ACCESSION_REGEX == null)
110 ACCESSION_REGEX = Platform
111 .newRegex("([A-Z]+[0-9]+[A-Z0-9]+|[A-Z0-9]+_[A-Z0-9]+)");
113 return ACCESSION_REGEX;
119 * @see jalview.ws.DbSourceProxy#getDbSource()
122 public String getDbSource()
124 return DBRefSource.UNIPROT;
130 * @see jalview.ws.DbSourceProxy#getDbVersion()
133 public String getDbVersion()
135 return "0"; // we really don't know what version we're on.
141 * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
144 public AlignmentI getSequenceRecords(String queries) throws Exception
149 queries = queries.toUpperCase().replaceAll(
150 "(UNIPROT\\|?|UNIPROT_|UNIREF\\d+_|UNIREF\\d+\\|?)", "");
151 AlignmentI al = null;
153 String downloadstring = getDomain() + "/uniprot/" + queries
156 URL url = new URL(downloadstring);
157 URLConnection urlconn = url.openConnection();
158 InputStream istr = urlconn.getInputStream();
159 List<Entry> entries = getUniprotEntries(istr);
162 List<SequenceI> seqs = new ArrayList<>();
163 for (Entry entry : entries)
165 seqs.add(uniprotEntryToSequence(entry));
167 al = new Alignment(seqs.toArray(new SequenceI[seqs.size()]));
172 } catch (Exception e)
182 * Converts an Entry object (bound from Uniprot XML) to a Jalview Sequence
187 SequenceI uniprotEntryToSequence(Entry entry)
189 String id = getUniprotEntryId(entry);
191 * Sequence should not include any whitespace, but JAXB leaves these in
193 String seqString = entry.getSequence().getValue().replaceAll("\\s*",
196 SequenceI sequence = new Sequence(id,
198 sequence.setDescription(getUniprotEntryDescription(entry));
201 * add a 'self' DBRefEntry for each accession
203 final String dbVersion = getDbVersion();
204 List<DBRefEntry> dbRefs = new ArrayList<>();
205 for (String accessionId : entry.getAccession())
207 DBRefEntry dbRef = new DBRefEntry(DBRefSource.UNIPROT, dbVersion,
213 * add a DBRefEntry for each dbReference element in the XML;
214 * also add a PDBEntry if type="PDB";
215 * also add an EMBLCDS dbref if protein sequence id is given
216 * also add an Ensembl dbref " " " " " "
218 Vector<PDBEntry> pdbRefs = new Vector<>();
219 for (DbReferenceType dbref : entry.getDbReference())
221 String type = dbref.getType();
222 DBRefEntry dbr = new DBRefEntry(type,
223 DBRefSource.UNIPROT + ":" + dbVersion, dbref.getId());
225 if ("PDB".equals(type))
227 pdbRefs.add(new PDBEntry(dbr));
229 if ("EMBL".equals(type))
232 * e.g. Uniprot accession Q9BXM7 has
233 * <dbReference type="EMBL" id="M19359">
234 * <property type="protein sequence ID" value="AAA40981.1"/>
235 * <property type="molecule type" value="Genomic_DNA"/>
238 String cdsId = getProperty(dbref.getProperty(),
239 "protein sequence ID");
240 if (cdsId != null && cdsId.trim().length() > 0)
243 String[] vrs = cdsId.split("\\.");
244 String version = vrs.length > 1 ? vrs[1]
245 : DBRefSource.UNIPROT + ":" + dbVersion;
246 dbr = new DBRefEntry(DBRefSource.EMBLCDS, version, vrs[0]);
250 if ("Ensembl".equals(type))
253 * e.g. Uniprot accession Q9BXM7 has
254 * <dbReference type="Ensembl" id="ENST00000321556">
255 * <molecule id="Q9BXM7-1"/>
256 * <property type="protein sequence ID" value="ENSP00000364204"/>
257 * <property type="gene ID" value="ENSG00000158828"/>
260 String cdsId = getProperty(dbref.getProperty(),
261 "protein sequence ID");
262 if (cdsId != null && cdsId.trim().length() > 0)
264 dbr = new DBRefEntry(DBRefSource.ENSEMBL,
265 DBRefSource.UNIPROT + ":" + dbVersion, cdsId.trim());
272 * create features; they have either begin and end, or position, in XML
274 sequence.setPDBId(pdbRefs);
275 if (entry.getFeature() != null)
277 for (FeatureType uf : entry.getFeature())
279 LocationType location = uf.getLocation();
282 if (location.getPosition() != null)
284 start = location.getPosition().getPosition().intValue();
289 start = location.getBegin().getPosition().intValue();
290 end = location.getEnd().getPosition().intValue();
292 SequenceFeature sf = new SequenceFeature(uf.getType(),
293 getDescription(uf), start, end, "Uniprot");
294 sf.setStatus(uf.getStatus());
295 sequence.addSequenceFeature(sf);
298 for (DBRefEntry dbr : dbRefs)
300 sequence.addDBRef(dbr);
306 * A helper method that builds a sequence feature description
311 static String getDescription(FeatureType feature)
313 String orig = feature.getOriginal();
314 List<String> variants = feature.getVariation();
315 StringBuilder sb = new StringBuilder();
318 * append variant in standard format if present
320 * multiple variants are split over lines using <br>
322 boolean asHtml = false;
323 if (orig != null && !orig.isEmpty() && variants != null
324 && !variants.isEmpty())
327 for (String var : variants)
329 // TODO proper HGVS nomenclature for delins structural variations
330 // http://varnomen.hgvs.org/recommendations/protein/variant/delins/
331 // for now we are pragmatic - any orig/variant sequence longer than
332 // three characters is shown with single-character notation rather than
333 // three-letter notation
335 if (orig.length() < 4)
337 for (int c = 0, clen = orig.length(); c < clen; c++)
339 char origchar = orig.charAt(c);
340 String orig3 = ResidueProperties.aa2Triplet.get("" + origchar);
341 sb.append(orig3 == null ? origchar
342 : StringUtils.toSentenceCase(orig3));
350 LocationType location = feature.getLocation();
351 PositionType start = location.getPosition() == null
352 ? location.getBegin()
353 : location.getPosition();
354 sb.append(Integer.toString(start.getPosition().intValue()));
356 if (var.length() < 4)
358 for (int c = 0, clen = var.length(); c < clen; c++)
360 char varchar = var.charAt(c);
361 String var3 = ResidueProperties.aa2Triplet.get("" + varchar);
363 sb.append(var3 != null ? StringUtils.toSentenceCase(var3)
371 if (++p != variants.size())
373 sb.append("<br/> ");
382 String description = feature.getDescription();
383 if (description != null)
385 sb.append(description);
389 sb.insert(0, "<html>");
390 sb.append("</html>");
393 return sb.toString();
397 * A helper method that searches the list of properties for one with the given
398 * key, and if found returns the property value, else returns null
404 static String getProperty(List<PropertyType> properties, String key)
407 if (properties != null)
409 for (PropertyType prop : properties)
411 if (key.equals(prop.getType()))
413 value = prop.getValue();
422 * Extracts xml element entry/protein/recommendedName/fullName
427 static String getUniprotEntryDescription(Entry entry)
430 if (entry.getProtein() != null
431 && entry.getProtein().getRecommendedName() != null)
433 // fullName is mandatory if recommendedName is present
434 desc = entry.getProtein().getRecommendedName().getFullName()
441 * Constructs a sequence id by concatenating all entry/name elements with '|'
447 static String getUniprotEntryId(Entry entry)
449 StringBuilder name = new StringBuilder(32);
450 for (String n : entry.getName())
452 if (name.length() > 0)
454 name.append(BAR_DELIMITER);
458 return name.toString();
464 * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
467 public boolean isValidReference(String accession)
469 // TODO: make the following a standard validator
470 return (accession == null || accession.length() < 2) ? false
471 : getAccessionValidator().search(accession);
475 * return LDHA_CHICK uniprot entry
478 public String getTestQuery()
484 public String getDbName()
486 return "Uniprot"; // getDbSource();
496 * Reads the reply to the EBI Fetch Uniprot data query, unmarshals it to an
497 * Uniprot object, and returns the enclosed Entry objects, or null on any
503 public List<Entry> getUniprotEntries(InputStream is)
505 List<Entry> entries = null;
508 JAXBContext jc = JAXBContext
509 .newInstance("jalview.xml.binding.uniprot");
510 XMLStreamReader streamReader = XMLInputFactory.newInstance()
511 .createXMLStreamReader(is);
512 javax.xml.bind.Unmarshaller um = jc.createUnmarshaller();
513 JAXBElement<jalview.xml.binding.uniprot.Uniprot> uniprotElement =
514 um.unmarshal(streamReader, jalview.xml.binding.uniprot.Uniprot.class);
515 jalview.xml.binding.uniprot.Uniprot uniprot = uniprotElement.getValue();
517 if (uniprot != null && !uniprot.getEntry().isEmpty())
519 entries = uniprot.getEntry();
521 } catch (JAXBException | XMLStreamException
522 | FactoryConfigurationError e)