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.gui.Preferences;
33 import jalview.schemes.ResidueProperties;
34 import jalview.util.Platform;
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;
46 import java.net.URLConnection;
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 = "|";
75 private static Regex ACCESSION_REGEX;
85 private String getDomain()
87 return Cache.getDefault(Preferences.UNIPROT_DOMAIN, DEFAULT_UNIPROT_DOMAIN);
93 * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
96 public String getAccessionSeparator()
104 * @see jalview.ws.DbSourceProxy#getAccessionValidator()
107 public Regex getAccessionValidator()
109 if (ACCESSION_REGEX == null)
111 ACCESSION_REGEX = Platform
112 .newRegex("([A-Z]+[0-9]+[A-Z0-9]+|[A-Z0-9]+_[A-Z0-9]+)", null);
114 return ACCESSION_REGEX;
120 * @see jalview.ws.DbSourceProxy#getDbSource()
123 public String getDbSource()
125 return DBRefSource.UNIPROT;
131 * @see jalview.ws.DbSourceProxy#getDbVersion()
134 public String getDbVersion()
136 return "0"; // we really don't know what version we're on.
142 * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
145 public AlignmentI getSequenceRecords(String queries) throws Exception
150 queries = queries.toUpperCase().replaceAll(
151 "(UNIPROT\\|?|UNIPROT_|UNIREF\\d+_|UNIREF\\d+\\|?)", "");
152 AlignmentI al = null;
154 String downloadstring = getDomain() + "/uniprot/" + queries
157 URL url = new URL(downloadstring);
158 URLConnection urlconn = url.openConnection();
159 InputStream istr = urlconn.getInputStream();
160 List<Entry> entries = getUniprotEntries(istr);
163 List<SequenceI> seqs = new ArrayList<>();
164 for (Entry entry : entries)
166 seqs.add(uniprotEntryToSequence(entry));
168 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 for (String accessionId : entry.getAccession())
208 DBRefEntry dbRef = new DBRefEntry(DBRefSource.UNIPROT, dbVersion,
214 * add a DBRefEntry for each dbReference element in the XML;
215 * also add a PDBEntry if type="PDB";
216 * also add an EMBLCDS dbref if protein sequence id is given
217 * also add an Ensembl dbref " " " " " "
219 Vector<PDBEntry> pdbRefs = new Vector<>();
220 for (DbReferenceType dbref : entry.getDbReference())
222 String type = dbref.getType();
223 DBRefEntry dbr = new DBRefEntry(type,
224 DBRefSource.UNIPROT + ":" + dbVersion, dbref.getId());
226 if ("PDB".equals(type))
228 pdbRefs.add(new PDBEntry(dbr));
230 if ("EMBL".equals(type))
233 * e.g. Uniprot accession Q9BXM7 has
234 * <dbReference type="EMBL" id="M19359">
235 * <property type="protein sequence ID" value="AAA40981.1"/>
236 * <property type="molecule type" value="Genomic_DNA"/>
239 String cdsId = getProperty(dbref.getProperty(),
240 "protein sequence ID");
241 if (cdsId != null && cdsId.trim().length() > 0)
244 String[] vrs = cdsId.split("\\.");
245 String version = vrs.length > 1 ? vrs[1]
246 : DBRefSource.UNIPROT + ":" + dbVersion;
247 dbr = new DBRefEntry(DBRefSource.EMBLCDS, version, vrs[0]);
251 if ("Ensembl".equals(type))
254 * e.g. Uniprot accession Q9BXM7 has
255 * <dbReference type="Ensembl" id="ENST00000321556">
256 * <molecule id="Q9BXM7-1"/>
257 * <property type="protein sequence ID" value="ENSP00000364204"/>
258 * <property type="gene ID" value="ENSG00000158828"/>
261 String cdsId = getProperty(dbref.getProperty(),
262 "protein sequence ID");
263 if (cdsId != null && cdsId.trim().length() > 0)
265 dbr = new DBRefEntry(DBRefSource.ENSEMBL,
266 DBRefSource.UNIPROT + ":" + dbVersion, cdsId.trim());
273 * create features; they have either begin and end, or position, in XML
275 sequence.setPDBId(pdbRefs);
276 if (entry.getFeature() != null)
278 for (FeatureType uf : entry.getFeature())
280 LocationType location = uf.getLocation();
283 if (location.getPosition() != null)
285 start = location.getPosition().getPosition().intValue();
290 start = location.getBegin().getPosition().intValue();
291 end = location.getEnd().getPosition().intValue();
293 SequenceFeature sf = new SequenceFeature(uf.getType(),
294 getDescription(uf), start, end, "Uniprot");
295 sf.setStatus(uf.getStatus());
296 sequence.addSequenceFeature(sf);
299 for (DBRefEntry dbr : dbRefs)
301 sequence.addDBRef(dbr);
307 * A helper method that builds a sequence feature description
312 static String getDescription(FeatureType feature)
314 String orig = feature.getOriginal();
315 List<String> variants = feature.getVariation();
316 StringBuilder sb = new StringBuilder();
319 * append variant in standard format if present
321 * multiple variants are split over lines using <br>
323 boolean asHtml = false;
324 if (orig != null && !orig.isEmpty() && variants != null
325 && !variants.isEmpty())
328 for (String var : variants)
330 // TODO proper HGVS nomenclature for delins structural variations
331 // http://varnomen.hgvs.org/recommendations/protein/variant/delins/
332 // for now we are pragmatic - any orig/variant sequence longer than
333 // three characters is shown with single-character notation rather than
334 // three-letter notation
336 if (orig.length() < 4)
338 for (int c = 0, clen = orig.length(); c < clen; c++)
340 char origchar = orig.charAt(c);
341 String orig3 = ResidueProperties.aa2Triplet.get("" + origchar);
342 sb.append(orig3 == null ? origchar
343 : StringUtils.toSentenceCase(orig3));
351 LocationType location = feature.getLocation();
352 PositionType start = location.getPosition() == null
353 ? location.getBegin()
354 : location.getPosition();
355 sb.append(Integer.toString(start.getPosition().intValue()));
357 if (var.length() < 4)
359 for (int c = 0, clen = var.length(); c < clen; c++)
361 char varchar = var.charAt(c);
362 String var3 = ResidueProperties.aa2Triplet.get("" + varchar);
364 sb.append(var3 != null ? StringUtils.toSentenceCase(var3)
372 if (++p != variants.size())
374 sb.append("<br/> ");
383 String description = feature.getDescription();
384 if (description != null)
386 sb.append(description);
390 sb.insert(0, "<html>");
391 sb.append("</html>");
394 return sb.toString();
398 * A helper method that searches the list of properties for one with the given
399 * key, and if found returns the property value, else returns null
405 static String getProperty(List<PropertyType> properties, String key)
408 if (properties != null)
410 for (PropertyType prop : properties)
412 if (key.equals(prop.getType()))
414 value = prop.getValue();
423 * Extracts xml element entry/protein/recommendedName/fullName
428 static String getUniprotEntryDescription(Entry entry)
431 if (entry.getProtein() != null
432 && entry.getProtein().getRecommendedName() != null)
434 // fullName is mandatory if recommendedName is present
435 desc = entry.getProtein().getRecommendedName().getFullName()
442 * Constructs a sequence id by concatenating all entry/name elements with '|'
448 static String getUniprotEntryId(Entry entry)
450 StringBuilder name = new StringBuilder(32);
451 for (String n : entry.getName())
453 if (name.length() > 0)
455 name.append(BAR_DELIMITER);
459 return name.toString();
465 * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
468 public boolean isValidReference(String accession)
470 // TODO: make the following a standard validator
471 return (accession == null || accession.length() < 2) ? false
472 : getAccessionValidator().search(accession);
476 * return LDHA_CHICK uniprot entry
479 public String getTestQuery()
485 public String getDbName()
487 return "Uniprot"; // getDbSource();
497 * Reads the reply to the EBI Fetch Uniprot data query, unmarshals it to an
498 * Uniprot object, and returns the enclosed Entry objects, or null on any
504 public List<Entry> getUniprotEntries(InputStream is)
506 List<Entry> entries = null;
509 JAXBContext jc = JAXBContext
510 .newInstance("jalview.xml.binding.uniprot");
511 XMLStreamReader streamReader = XMLInputFactory.newInstance()
512 .createXMLStreamReader(is);
513 javax.xml.bind.Unmarshaller um = jc.createUnmarshaller();
514 JAXBElement<jalview.xml.binding.uniprot.Uniprot> uniprotElement =
515 um.unmarshal(streamReader, jalview.xml.binding.uniprot.Uniprot.class);
516 jalview.xml.binding.uniprot.Uniprot uniprot = uniprotElement.getValue();
518 if (uniprot != null && !uniprot.getEntry().isEmpty())
520 entries = uniprot.getEntry();
522 } catch (JAXBException | XMLStreamException
523 | FactoryConfigurationError e)