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 jalview.bin.Cache;
24 import jalview.datamodel.Alignment;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.DBRefEntry;
27 import jalview.datamodel.DBRefSource;
28 import jalview.datamodel.PDBEntry;
29 import jalview.datamodel.Sequence;
30 import jalview.datamodel.SequenceFeature;
31 import jalview.datamodel.SequenceI;
32 import jalview.schemes.ResidueProperties;
33 import jalview.util.StringUtils;
34 import jalview.ws.seqfetcher.DbSourceProxyImpl;
35 import jalview.xml.binding.uniprot.DbReferenceType;
36 import jalview.xml.binding.uniprot.Entry;
37 import jalview.xml.binding.uniprot.FeatureType;
38 import jalview.xml.binding.uniprot.LocationType;
39 import jalview.xml.binding.uniprot.PositionType;
40 import jalview.xml.binding.uniprot.PropertyType;
42 import java.io.InputStream;
44 import java.net.URLConnection;
45 import java.util.ArrayList;
46 import java.util.List;
47 import java.util.Vector;
49 import javax.xml.bind.JAXBContext;
50 import javax.xml.bind.JAXBElement;
51 import javax.xml.bind.JAXBException;
52 import javax.xml.stream.FactoryConfigurationError;
53 import javax.xml.stream.XMLInputFactory;
54 import javax.xml.stream.XMLStreamException;
55 import javax.xml.stream.XMLStreamReader;
57 import com.stevesoft.pat.Regex;
60 * This class queries the Uniprot database for sequence data, unmarshals the
61 * returned XML, and converts it to Jalview Sequence records (including attached
62 * database references and sequence features)
67 public class Uniprot extends DbSourceProxyImpl
69 private static final String DEFAULT_UNIPROT_DOMAIN = "https://www.uniprot.org";
71 private static final String BAR_DELIMITER = "|";
81 private String getDomain()
83 return Cache.getDefault("UNIPROT_DOMAIN", DEFAULT_UNIPROT_DOMAIN);
89 * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
92 public String getAccessionSeparator()
100 * @see jalview.ws.DbSourceProxy#getAccessionValidator()
103 public Regex getAccessionValidator()
105 return new Regex("([A-Z]+[0-9]+[A-Z0-9]+|[A-Z0-9]+_[A-Z0-9]+)");
111 * @see jalview.ws.DbSourceProxy#getDbSource()
114 public String getDbSource()
116 return DBRefSource.UNIPROT;
122 * @see jalview.ws.DbSourceProxy#getDbVersion()
125 public String getDbVersion()
127 return "0"; // we really don't know what version we're on.
133 * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
136 public AlignmentI getSequenceRecords(String queries) throws Exception
141 queries = queries.toUpperCase().replaceAll(
142 "(UNIPROT\\|?|UNIPROT_|UNIREF\\d+_|UNIREF\\d+\\|?)", "");
143 AlignmentI al = null;
145 String downloadstring = getDomain() + "/uniprot/" + queries
148 URL url = new URL(downloadstring);
149 URLConnection urlconn = url.openConnection();
150 InputStream istr = urlconn.getInputStream();
151 List<Entry> entries = getUniprotEntries(istr);
154 List<SequenceI> seqs = new ArrayList<>();
155 for (Entry entry : entries)
157 seqs.add(uniprotEntryToSequence(entry));
159 al = new Alignment(seqs.toArray(new SequenceI[seqs.size()]));
164 } catch (Exception e)
174 * Converts an Entry object (bound from Uniprot XML) to a Jalview Sequence
179 SequenceI uniprotEntryToSequence(Entry entry)
181 String id = getUniprotEntryId(entry);
183 * Sequence should not include any whitespace, but JAXB leaves these in
185 String seqString = entry.getSequence().getValue().replaceAll("\\s*",
188 SequenceI sequence = new Sequence(id,
190 sequence.setDescription(getUniprotEntryDescription(entry));
193 * add a 'self' DBRefEntry for each accession
195 final String dbVersion = getDbVersion();
196 List<DBRefEntry> dbRefs = new ArrayList<>();
197 for (String accessionId : entry.getAccession())
199 DBRefEntry dbRef = new DBRefEntry(DBRefSource.UNIPROT, dbVersion,
205 * add a DBRefEntry for each dbReference element in the XML;
206 * also add a PDBEntry if type="PDB";
207 * also add an EMBLCDS dbref if protein sequence id is given
208 * also add an Ensembl dbref " " " " " "
210 Vector<PDBEntry> pdbRefs = new Vector<>();
211 for (DbReferenceType dbref : entry.getDbReference())
213 String type = dbref.getType();
214 DBRefEntry dbr = new DBRefEntry(type,
215 DBRefSource.UNIPROT + ":" + dbVersion, dbref.getId());
217 if ("PDB".equals(type))
219 pdbRefs.add(new PDBEntry(dbr));
221 if ("EMBL".equals(type))
224 * e.g. Uniprot accession Q9BXM7 has
225 * <dbReference type="EMBL" id="M19359">
226 * <property type="protein sequence ID" value="AAA40981.1"/>
227 * <property type="molecule type" value="Genomic_DNA"/>
230 String cdsId = getProperty(dbref.getProperty(),
231 "protein sequence ID");
232 if (cdsId != null && cdsId.trim().length() > 0)
235 String[] vrs = cdsId.split("\\.");
236 String version = vrs.length > 1 ? vrs[1]
237 : DBRefSource.UNIPROT + ":" + dbVersion;
238 dbr = new DBRefEntry(DBRefSource.EMBLCDS, version, vrs[0]);
242 if ("Ensembl".equals(type))
245 * e.g. Uniprot accession Q9BXM7 has
246 * <dbReference type="Ensembl" id="ENST00000321556">
247 * <molecule id="Q9BXM7-1"/>
248 * <property type="protein sequence ID" value="ENSP00000364204"/>
249 * <property type="gene ID" value="ENSG00000158828"/>
252 String cdsId = getProperty(dbref.getProperty(),
253 "protein sequence ID");
254 if (cdsId != null && cdsId.trim().length() > 0)
256 dbr = new DBRefEntry(DBRefSource.ENSEMBL,
257 DBRefSource.UNIPROT + ":" + dbVersion, cdsId.trim());
264 * create features; they have either begin and end, or position, in XML
266 sequence.setPDBId(pdbRefs);
267 if (entry.getFeature() != null)
269 for (FeatureType uf : entry.getFeature())
271 LocationType location = uf.getLocation();
274 if (location.getPosition() != null)
276 start = location.getPosition().getPosition().intValue();
281 start = location.getBegin().getPosition().intValue();
282 end = location.getEnd().getPosition().intValue();
284 SequenceFeature sf = new SequenceFeature(uf.getType(),
285 getDescription(uf), start, end, "Uniprot");
286 sf.setStatus(uf.getStatus());
287 sequence.addSequenceFeature(sf);
290 for (DBRefEntry dbr : dbRefs)
292 sequence.addDBRef(dbr);
298 * A helper method that builds a sequence feature description
303 static String getDescription(FeatureType feature)
305 String orig = feature.getOriginal();
306 List<String> variants = feature.getVariation();
307 StringBuilder sb = new StringBuilder();
310 * append variant in standard format if present
312 * multiple variants are split over lines using <br>
314 boolean asHtml = false;
315 if (orig != null && !orig.isEmpty() && variants != null
316 && !variants.isEmpty())
319 for (String var : variants)
321 // TODO proper HGVS nomenclature for delins structural variations
322 // http://varnomen.hgvs.org/recommendations/protein/variant/delins/
323 // for now we are pragmatic - any orig/variant sequence longer than
324 // three characters is shown with single-character notation rather than
325 // three-letter notation
327 if (orig.length() < 4)
329 for (int c = 0, clen = orig.length(); c < clen; c++)
331 char origchar = orig.charAt(c);
332 String orig3 = ResidueProperties.aa2Triplet.get("" + origchar);
333 sb.append(orig3 == null ? origchar
334 : StringUtils.toSentenceCase(orig3));
342 LocationType location = feature.getLocation();
343 PositionType start = location.getPosition() == null
344 ? location.getBegin()
345 : location.getPosition();
346 sb.append(Integer.toString(start.getPosition().intValue()));
348 if (var.length() < 4)
350 for (int c = 0, clen = var.length(); c < clen; c++)
352 char varchar = var.charAt(c);
353 String var3 = ResidueProperties.aa2Triplet.get("" + varchar);
355 sb.append(var3 != null ? StringUtils.toSentenceCase(var3)
363 if (++p != variants.size())
365 sb.append("<br/> ");
374 String description = feature.getDescription();
375 if (description != null)
377 sb.append(description);
381 sb.insert(0, "<html>");
382 sb.append("</html>");
385 return sb.toString();
389 * A helper method that searches the list of properties for one with the given
390 * key, and if found returns the property value, else returns null
396 static String getProperty(List<PropertyType> properties, String key)
399 if (properties != null)
401 for (PropertyType prop : properties)
403 if (key.equals(prop.getType()))
405 value = prop.getValue();
414 * Extracts xml element entry/protein/recommendedName/fullName
419 static String getUniprotEntryDescription(Entry entry)
422 if (entry.getProtein() != null
423 && entry.getProtein().getRecommendedName() != null)
425 // fullName is mandatory if recommendedName is present
426 desc = entry.getProtein().getRecommendedName().getFullName()
433 * Constructs a sequence id by concatenating all entry/name elements with '|'
439 static String getUniprotEntryId(Entry entry)
441 StringBuilder name = new StringBuilder(32);
442 for (String n : entry.getName())
444 if (name.length() > 0)
446 name.append(BAR_DELIMITER);
450 return name.toString();
456 * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
459 public boolean isValidReference(String accession)
461 // TODO: make the following a standard validator
462 return (accession == null || accession.length() < 2) ? false
463 : getAccessionValidator().search(accession);
467 * return LDHA_CHICK uniprot entry
470 public String getTestQuery()
476 public String getDbName()
478 return "Uniprot"; // getDbSource();
488 * Reads the reply to the EBI Fetch Uniprot data query, unmarshals it to an
489 * Uniprot object, and returns the enclosed Entry objects, or null on any
495 public List<Entry> getUniprotEntries(InputStream is)
497 List<Entry> entries = null;
500 JAXBContext jc = JAXBContext
501 .newInstance("jalview.xml.binding.uniprot");
502 XMLStreamReader streamReader = XMLInputFactory.newInstance()
503 .createXMLStreamReader(is);
504 javax.xml.bind.Unmarshaller um = jc.createUnmarshaller();
505 JAXBElement<jalview.xml.binding.uniprot.Uniprot> uniprotElement =
506 um.unmarshal(streamReader, jalview.xml.binding.uniprot.Uniprot.class);
507 jalview.xml.binding.uniprot.Uniprot uniprot = uniprotElement.getValue();
509 if (uniprot != null && !uniprot.getEntry().isEmpty())
511 entries = uniprot.getEntry();
513 } catch (JAXBException | XMLStreamException
514 | FactoryConfigurationError e)