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.uniprot.DbReferenceType;
38 import jalview.xml.binding.uniprot.Entry;
39 import jalview.xml.binding.uniprot.FeatureType;
40 import jalview.xml.binding.uniprot.LocationType;
41 import jalview.xml.binding.uniprot.PositionType;
42 import jalview.xml.binding.uniprot.PropertyType;
44 import java.io.InputStream;
45 import java.net.HttpURLConnection;
47 import java.util.ArrayList;
48 import java.util.List;
49 import java.util.Vector;
51 import javax.xml.bind.JAXBContext;
52 import javax.xml.bind.JAXBElement;
53 import javax.xml.bind.JAXBException;
54 import javax.xml.stream.FactoryConfigurationError;
55 import javax.xml.stream.XMLInputFactory;
56 import javax.xml.stream.XMLStreamException;
57 import javax.xml.stream.XMLStreamReader;
59 import com.stevesoft.pat.Regex;
62 * This class queries the Uniprot database for sequence data, unmarshals the
63 * returned XML, and converts it to Jalview Sequence records (including attached
64 * database references and sequence features)
69 public class Uniprot extends DbSourceProxyImpl
71 private static final String DEFAULT_UNIPROT_DOMAIN = "https://www.uniprot.org";
73 private static final String BAR_DELIMITER = "|";
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 return new Regex("([A-Z]+[0-9]+[A-Z0-9]+|[A-Z0-9]+_[A-Z0-9]+)");
113 * @see jalview.ws.DbSourceProxy#getDbSource()
116 public String getDbSource()
118 return DBRefSource.UNIPROT;
124 * @see jalview.ws.DbSourceProxy#getDbVersion()
127 public String getDbVersion()
129 return "0"; // we really don't know what version we're on.
135 * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
138 public AlignmentI getSequenceRecords(String queries) throws Exception
143 queries = queries.toUpperCase(Locale.ROOT).replaceAll(
144 "(UNIPROT\\|?|UNIPROT_|UNIREF\\d+_|UNIREF\\d+\\|?)", "");
145 AlignmentI al = null;
147 String downloadstring = getDomain() + "/uniprot/" + queries
150 URL url = new URL(downloadstring);
151 HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
152 // anything other than 200 means we don't have data
153 // TODO: JAL-3882 reuse the EnsemblRestClient's fair
154 // use/backoff logic to retry when the server tells us to go away
155 if (urlconn.getResponseCode() == 200)
157 InputStream istr = urlconn.getInputStream();
158 List<Entry> entries = getUniprotEntries(istr);
161 List<SequenceI> seqs = new ArrayList<>();
162 for (Entry entry : entries)
164 seqs.add(uniprotEntryToSequence(entry));
166 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 boolean canonical=true;
206 for (String accessionId : entry.getAccession())
208 DBRefEntry dbRef = new DBRefEntry(DBRefSource.UNIPROT, dbVersion,
209 accessionId,null,canonical);
215 * add a DBRefEntry for each dbReference element in the XML;
216 * also add a PDBEntry if type="PDB";
217 * also add an EMBLCDS dbref if protein sequence id is given
218 * also add an Ensembl dbref " " " " " "
220 Vector<PDBEntry> pdbRefs = new Vector<>();
221 for (DbReferenceType dbref : entry.getDbReference())
223 String type = dbref.getType();
224 DBRefEntry dbr = new DBRefEntry(type,
225 DBRefSource.UNIPROT + ":" + dbVersion, dbref.getId());
227 if ("PDB".equals(type))
229 pdbRefs.add(new PDBEntry(dbr));
231 if ("EMBL".equals(type))
234 * e.g. Uniprot accession Q9BXM7 has
235 * <dbReference type="EMBL" id="M19359">
236 * <property type="protein sequence ID" value="AAA40981.1"/>
237 * <property type="molecule type" value="Genomic_DNA"/>
240 String cdsId = getProperty(dbref.getProperty(),
241 "protein sequence ID");
242 if (cdsId != null && cdsId.trim().length() > 0)
245 String[] vrs = cdsId.split("\\.");
246 String version = vrs.length > 1 ? vrs[1]
247 : DBRefSource.UNIPROT + ":" + dbVersion;
248 dbr = new DBRefEntry(DBRefSource.EMBLCDS, version, vrs[0]);
252 if ("Ensembl".equals(type))
255 * e.g. Uniprot accession Q9BXM7 has
256 * <dbReference type="Ensembl" id="ENST00000321556">
257 * <molecule id="Q9BXM7-1"/>
258 * <property type="protein sequence ID" value="ENSP00000364204"/>
259 * <property type="gene ID" value="ENSG00000158828"/>
262 String cdsId = getProperty(dbref.getProperty(),
263 "protein sequence ID");
264 if (cdsId != null && cdsId.trim().length() > 0)
266 dbr = new DBRefEntry(DBRefSource.ENSEMBL,
267 DBRefSource.UNIPROT + ":" + dbVersion, cdsId.trim());
274 * create features; they have either begin and end, or position, in XML
276 sequence.setPDBId(pdbRefs);
277 if (entry.getFeature() != null)
279 for (FeatureType uf : entry.getFeature())
281 LocationType location = uf.getLocation();
284 if (location.getPosition() != null)
286 start = location.getPosition().getPosition().intValue();
291 start = location.getBegin().getPosition().intValue();
292 end = location.getEnd().getPosition().intValue();
294 SequenceFeature sf = new SequenceFeature(uf.getType(),
295 getDescription(uf), start, end, "Uniprot");
296 sf.setStatus(uf.getStatus());
297 sequence.addSequenceFeature(sf);
300 for (DBRefEntry dbr : dbRefs)
302 sequence.addDBRef(dbr);
308 * A helper method that builds a sequence feature description
313 static String getDescription(FeatureType feature)
315 String orig = feature.getOriginal();
316 List<String> variants = feature.getVariation();
317 StringBuilder sb = new StringBuilder();
320 * append variant in standard format if present
322 * multiple variants are split over lines using <br>
324 boolean asHtml = false;
325 if (orig != null && !orig.isEmpty() && variants != null
326 && !variants.isEmpty())
329 for (String var : variants)
331 // TODO proper HGVS nomenclature for delins structural variations
332 // http://varnomen.hgvs.org/recommendations/protein/variant/delins/
333 // for now we are pragmatic - any orig/variant sequence longer than
334 // three characters is shown with single-character notation rather than
335 // three-letter notation
337 if (orig.length() < 4)
339 for (int c = 0, clen = orig.length(); c < clen; c++)
341 char origchar = orig.charAt(c);
342 String orig3 = ResidueProperties.aa2Triplet.get("" + origchar);
343 sb.append(orig3 == null ? origchar
344 : StringUtils.toSentenceCase(orig3));
352 LocationType location = feature.getLocation();
353 PositionType start = location.getPosition() == null
354 ? location.getBegin()
355 : location.getPosition();
356 sb.append(Integer.toString(start.getPosition().intValue()));
358 if (var.length() < 4)
360 for (int c = 0, clen = var.length(); c < clen; c++)
362 char varchar = var.charAt(c);
363 String var3 = ResidueProperties.aa2Triplet.get("" + varchar);
365 sb.append(var3 != null ? StringUtils.toSentenceCase(var3)
373 if (++p != variants.size())
375 sb.append("<br/> ");
384 String description = feature.getDescription();
385 if (description != null)
387 sb.append(description);
391 sb.insert(0, "<html>");
392 sb.append("</html>");
395 return sb.toString();
399 * A helper method that searches the list of properties for one with the given
400 * key, and if found returns the property value, else returns null
406 static String getProperty(List<PropertyType> properties, String key)
409 if (properties != null)
411 for (PropertyType prop : properties)
413 if (key.equals(prop.getType()))
415 value = prop.getValue();
424 * Extracts xml element entry/protein/recommendedName/fullName
429 static String getUniprotEntryDescription(Entry entry)
432 if (entry.getProtein() != null
433 && entry.getProtein().getRecommendedName() != null)
435 // fullName is mandatory if recommendedName is present
436 desc = entry.getProtein().getRecommendedName().getFullName()
443 * Constructs a sequence id by concatenating all entry/name elements with '|'
449 static String getUniprotEntryId(Entry entry)
451 StringBuilder name = new StringBuilder(32);
452 for (String n : entry.getName())
454 if (name.length() > 0)
456 name.append(BAR_DELIMITER);
460 return name.toString();
466 * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
469 public boolean isValidReference(String accession)
471 // TODO: make the following a standard validator
472 return (accession == null || accession.length() < 2) ? false
473 : getAccessionValidator().search(accession);
477 * return LDHA_CHICK uniprot entry
480 public String getTestQuery()
486 public String getDbName()
488 return "Uniprot"; // getDbSource();
498 * Reads the reply to the EBI Fetch Uniprot data query, unmarshals it to an
499 * Uniprot object, and returns the enclosed Entry objects, or null on any
505 public List<Entry> getUniprotEntries(InputStream is)
507 List<Entry> entries = null;
510 JAXBContext jc = JAXBContext
511 .newInstance("jalview.xml.binding.uniprot");
512 XMLStreamReader streamReader = XMLInputFactory.newInstance()
513 .createXMLStreamReader(is);
514 javax.xml.bind.Unmarshaller um = jc.createUnmarshaller();
515 JAXBElement<jalview.xml.binding.uniprot.Uniprot> uniprotElement =
516 um.unmarshal(streamReader, jalview.xml.binding.uniprot.Uniprot.class);
517 jalview.xml.binding.uniprot.Uniprot uniprot = uniprotElement.getValue();
519 if (uniprot != null && !uniprot.getEntry().isEmpty())
521 entries = uniprot.getEntry();
523 } catch (JAXBException | XMLStreamException
524 | FactoryConfigurationError e)
526 if (e instanceof javax.xml.bind.UnmarshalException && e.getCause()!=null && e.getCause() instanceof XMLStreamException && e.getCause().getMessage().contains("[row,col]:[1,1]"))
528 // trying to parse an empty stream