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;
24 import jalview.bin.Cache;
25 import jalview.datamodel.Alignment;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.DBRefEntry;
28 import jalview.datamodel.DBRefSource;
29 import jalview.datamodel.PDBEntry;
30 import jalview.datamodel.Sequence;
31 import jalview.datamodel.SequenceFeature;
32 import jalview.datamodel.SequenceI;
33 import jalview.schemes.ResidueProperties;
34 import jalview.util.StringUtils;
35 import jalview.ws.seqfetcher.DbSourceProxyImpl;
36 import jalview.xml.binding.uniprot.DbReferenceType;
37 import jalview.xml.binding.uniprot.Entry;
38 import jalview.xml.binding.uniprot.FeatureType;
39 import jalview.xml.binding.uniprot.LocationType;
40 import jalview.xml.binding.uniprot.PositionType;
41 import jalview.xml.binding.uniprot.PropertyType;
43 import java.io.InputStream;
44 import java.net.HttpURLConnection;
46 import java.util.ArrayList;
47 import java.util.List;
48 import java.util.Vector;
50 import javax.xml.bind.JAXBContext;
51 import javax.xml.bind.JAXBElement;
52 import javax.xml.bind.JAXBException;
53 import javax.xml.stream.FactoryConfigurationError;
54 import javax.xml.stream.XMLInputFactory;
55 import javax.xml.stream.XMLStreamException;
56 import javax.xml.stream.XMLStreamReader;
58 import com.stevesoft.pat.Regex;
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 = "|";
73 private static Regex ACCESSION_REGEX;
83 private String getDomain()
85 return Cache.getDefault("UNIPROT_DOMAIN", DEFAULT_UNIPROT_DOMAIN);
91 * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
94 public String getAccessionSeparator()
102 * @see jalview.ws.DbSourceProxy#getAccessionValidator()
105 public Regex getAccessionValidator()
107 if (ACCESSION_REGEX == null)
109 ACCESSION_REGEX = Platform
110 .newRegex("([A-Z]+[0-9]+[A-Z0-9]+|[A-Z0-9]+_[A-Z0-9]+)");
112 return ACCESSION_REGEX;
118 * @see jalview.ws.DbSourceProxy#getDbSource()
121 public String getDbSource()
123 return DBRefSource.UNIPROT;
129 * @see jalview.ws.DbSourceProxy#getDbVersion()
132 public String getDbVersion()
134 return "0"; // we really don't know what version we're on.
140 * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
143 public AlignmentI getSequenceRecords(String queries) throws Exception
148 queries = queries.toUpperCase(Locale.ROOT).replaceAll(
149 "(UNIPROT\\|?|UNIPROT_|UNIREF\\d+_|UNIREF\\d+\\|?)", "");
150 AlignmentI al = null;
152 String downloadstring = getDomain() + "/uniprot/" + queries
155 URL url = new URL(downloadstring);
156 HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
157 // anything other than 200 means we don't have data
158 // TODO: JAL-3882 reuse the EnsemblRestClient's fair
159 // use/backoff logic to retry when the server tells us to go away
160 if (urlconn.getResponseCode() == 200)
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()]));
173 } catch (Exception e)
183 * Converts an Entry object (bound from Uniprot XML) to a Jalview Sequence
188 SequenceI uniprotEntryToSequence(Entry entry)
190 String id = getUniprotEntryId(entry);
192 * Sequence should not include any whitespace, but JAXB leaves these in
194 String seqString = entry.getSequence().getValue().replaceAll("\\s*",
197 SequenceI sequence = new Sequence(id,
199 sequence.setDescription(getUniprotEntryDescription(entry));
202 * add a 'self' DBRefEntry for each accession
204 final String dbVersion = getDbVersion();
205 List<DBRefEntry> dbRefs = new ArrayList<>();
206 boolean canonical=true;
207 for (String accessionId : entry.getAccession())
209 DBRefEntry dbRef = new DBRefEntry(DBRefSource.UNIPROT, dbVersion,
210 accessionId,null,canonical);
216 * add a DBRefEntry for each dbReference element in the XML;
217 * also add a PDBEntry if type="PDB";
218 * also add an EMBLCDS dbref if protein sequence id is given
219 * also add an Ensembl dbref " " " " " "
221 Vector<PDBEntry> pdbRefs = new Vector<>();
222 for (DbReferenceType dbref : entry.getDbReference())
224 String type = dbref.getType();
225 DBRefEntry dbr = new DBRefEntry(type,
226 DBRefSource.UNIPROT + ":" + dbVersion, dbref.getId());
228 if ("PDB".equals(type))
230 pdbRefs.add(new PDBEntry(dbr));
232 if ("EMBL".equals(type))
235 * e.g. Uniprot accession Q9BXM7 has
236 * <dbReference type="EMBL" id="M19359">
237 * <property type="protein sequence ID" value="AAA40981.1"/>
238 * <property type="molecule type" value="Genomic_DNA"/>
241 String cdsId = getProperty(dbref.getProperty(),
242 "protein sequence ID");
243 if (cdsId != null && cdsId.trim().length() > 0)
246 String[] vrs = cdsId.split("\\.");
247 String version = vrs.length > 1 ? vrs[1]
248 : DBRefSource.UNIPROT + ":" + dbVersion;
249 dbr = new DBRefEntry(DBRefSource.EMBLCDS, version, vrs[0]);
253 if ("Ensembl".equals(type))
256 * e.g. Uniprot accession Q9BXM7 has
257 * <dbReference type="Ensembl" id="ENST00000321556">
258 * <molecule id="Q9BXM7-1"/>
259 * <property type="protein sequence ID" value="ENSP00000364204"/>
260 * <property type="gene ID" value="ENSG00000158828"/>
263 String cdsId = getProperty(dbref.getProperty(),
264 "protein sequence ID");
265 if (cdsId != null && cdsId.trim().length() > 0)
267 dbr = new DBRefEntry(DBRefSource.ENSEMBL,
268 DBRefSource.UNIPROT + ":" + dbVersion, cdsId.trim());
275 * create features; they have either begin and end, or position, in XML
277 sequence.setPDBId(pdbRefs);
278 if (entry.getFeature() != null)
280 for (FeatureType uf : entry.getFeature())
282 LocationType location = uf.getLocation();
285 if (location.getPosition() != null)
287 start = location.getPosition().getPosition().intValue();
292 start = location.getBegin().getPosition().intValue();
293 end = location.getEnd().getPosition().intValue();
295 SequenceFeature sf = new SequenceFeature(uf.getType(),
296 getDescription(uf), start, end, "Uniprot");
297 sf.setStatus(uf.getStatus());
298 sequence.addSequenceFeature(sf);
301 for (DBRefEntry dbr : dbRefs)
303 sequence.addDBRef(dbr);
309 * A helper method that builds a sequence feature description
314 static String getDescription(FeatureType feature)
316 String orig = feature.getOriginal();
317 List<String> variants = feature.getVariation();
318 StringBuilder sb = new StringBuilder();
321 * append variant in standard format if present
323 * multiple variants are split over lines using <br>
325 boolean asHtml = false;
326 if (orig != null && !orig.isEmpty() && variants != null
327 && !variants.isEmpty())
330 for (String var : variants)
332 // TODO proper HGVS nomenclature for delins structural variations
333 // http://varnomen.hgvs.org/recommendations/protein/variant/delins/
334 // for now we are pragmatic - any orig/variant sequence longer than
335 // three characters is shown with single-character notation rather than
336 // three-letter notation
338 if (orig.length() < 4)
340 for (int c = 0, clen = orig.length(); c < clen; c++)
342 char origchar = orig.charAt(c);
343 String orig3 = ResidueProperties.aa2Triplet.get("" + origchar);
344 sb.append(orig3 == null ? origchar
345 : StringUtils.toSentenceCase(orig3));
353 LocationType location = feature.getLocation();
354 PositionType start = location.getPosition() == null
355 ? location.getBegin()
356 : location.getPosition();
357 sb.append(Integer.toString(start.getPosition().intValue()));
359 if (var.length() < 4)
361 for (int c = 0, clen = var.length(); c < clen; c++)
363 char varchar = var.charAt(c);
364 String var3 = ResidueProperties.aa2Triplet.get("" + varchar);
366 sb.append(var3 != null ? StringUtils.toSentenceCase(var3)
374 if (++p != variants.size())
376 sb.append("<br/> ");
385 String description = feature.getDescription();
386 if (description != null)
388 sb.append(description);
392 sb.insert(0, "<html>");
393 sb.append("</html>");
396 return sb.toString();
400 * A helper method that searches the list of properties for one with the given
401 * key, and if found returns the property value, else returns null
407 static String getProperty(List<PropertyType> properties, String key)
410 if (properties != null)
412 for (PropertyType prop : properties)
414 if (key.equals(prop.getType()))
416 value = prop.getValue();
425 * Extracts xml element entry/protein/recommendedName/fullName
430 static String getUniprotEntryDescription(Entry entry)
433 if (entry.getProtein() != null
434 && entry.getProtein().getRecommendedName() != null)
436 // fullName is mandatory if recommendedName is present
437 desc = entry.getProtein().getRecommendedName().getFullName()
444 * Constructs a sequence id by concatenating all entry/name elements with '|'
450 static String getUniprotEntryId(Entry entry)
452 StringBuilder name = new StringBuilder(32);
453 for (String n : entry.getName())
455 if (name.length() > 0)
457 name.append(BAR_DELIMITER);
461 return name.toString();
467 * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
470 public boolean isValidReference(String accession)
472 // TODO: make the following a standard validator
473 return (accession == null || accession.length() < 2) ? false
474 : getAccessionValidator().search(accession);
478 * return LDHA_CHICK uniprot entry
481 public String getTestQuery()
487 public String getDbName()
489 return "Uniprot"; // getDbSource();
499 * Reads the reply to the EBI Fetch Uniprot data query, unmarshals it to an
500 * Uniprot object, and returns the enclosed Entry objects, or null on any
506 public List<Entry> getUniprotEntries(InputStream is)
508 List<Entry> entries = null;
511 JAXBContext jc = JAXBContext
512 .newInstance("jalview.xml.binding.uniprot");
513 XMLStreamReader streamReader = XMLInputFactory.newInstance()
514 .createXMLStreamReader(is);
515 javax.xml.bind.Unmarshaller um = jc.createUnmarshaller();
516 JAXBElement<jalview.xml.binding.uniprot.Uniprot> uniprotElement =
517 um.unmarshal(streamReader, jalview.xml.binding.uniprot.Uniprot.class);
518 jalview.xml.binding.uniprot.Uniprot uniprot = uniprotElement.getValue();
520 if (uniprot != null && !uniprot.getEntry().isEmpty())
522 entries = uniprot.getEntry();
524 } catch (JAXBException | XMLStreamException
525 | FactoryConfigurationError e)
527 if (e instanceof javax.xml.bind.UnmarshalException && e.getCause()!=null && e.getCause() instanceof XMLStreamException && e.getCause().getMessage().contains("[row,col]:[1,1]"))
529 // trying to parse an empty stream