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.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;
45 import java.net.URLConnection;
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 = "|";
82 private String getDomain()
84 return Cache.getDefault(Preferences.UNIPROT_DOMAIN, DEFAULT_UNIPROT_DOMAIN);
90 * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
93 public String getAccessionSeparator()
101 * @see jalview.ws.DbSourceProxy#getAccessionValidator()
104 public Regex getAccessionValidator()
106 return new Regex("([A-Z]+[0-9]+[A-Z0-9]+|[A-Z0-9]+_[A-Z0-9]+)");
112 * @see jalview.ws.DbSourceProxy#getDbSource()
115 public String getDbSource()
117 return DBRefSource.UNIPROT;
123 * @see jalview.ws.DbSourceProxy#getDbVersion()
126 public String getDbVersion()
128 return "0"; // we really don't know what version we're on.
134 * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
137 public AlignmentI getSequenceRecords(String queries) throws Exception
142 queries = queries.toUpperCase().replaceAll(
143 "(UNIPROT\\|?|UNIPROT_|UNIREF\\d+_|UNIREF\\d+\\|?)", "");
144 AlignmentI al = null;
146 String downloadstring = getDomain() + "/uniprot/" + queries
149 URL url = new URL(downloadstring);
150 URLConnection urlconn = url.openConnection();
151 InputStream istr = urlconn.getInputStream();
152 List<Entry> entries = getUniprotEntries(istr);
155 List<SequenceI> seqs = new ArrayList<>();
156 for (Entry entry : entries)
158 seqs.add(uniprotEntryToSequence(entry));
160 al = new Alignment(seqs.toArray(new SequenceI[seqs.size()]));
165 } catch (Exception e)
175 * Converts an Entry object (bound from Uniprot XML) to a Jalview Sequence
180 SequenceI uniprotEntryToSequence(Entry entry)
182 String id = getUniprotEntryId(entry);
184 * Sequence should not include any whitespace, but JAXB leaves these in
186 String seqString = entry.getSequence().getValue().replaceAll("\\s*",
189 SequenceI sequence = new Sequence(id,
191 sequence.setDescription(getUniprotEntryDescription(entry));
194 * add a 'self' DBRefEntry for each accession
196 final String dbVersion = getDbVersion();
197 List<DBRefEntry> dbRefs = new ArrayList<>();
198 for (String accessionId : entry.getAccession())
200 DBRefEntry dbRef = new DBRefEntry(DBRefSource.UNIPROT, dbVersion,
206 * add a DBRefEntry for each dbReference element in the XML;
207 * also add a PDBEntry if type="PDB";
208 * also add an EMBLCDS dbref if protein sequence id is given
209 * also add an Ensembl dbref " " " " " "
211 Vector<PDBEntry> pdbRefs = new Vector<>();
212 for (DbReferenceType dbref : entry.getDbReference())
214 String type = dbref.getType();
215 DBRefEntry dbr = new DBRefEntry(type,
216 DBRefSource.UNIPROT + ":" + dbVersion, dbref.getId());
218 if ("PDB".equals(type))
220 pdbRefs.add(new PDBEntry(dbr));
222 if ("EMBL".equals(type))
225 * e.g. Uniprot accession Q9BXM7 has
226 * <dbReference type="EMBL" id="M19359">
227 * <property type="protein sequence ID" value="AAA40981.1"/>
228 * <property type="molecule type" value="Genomic_DNA"/>
231 String cdsId = getProperty(dbref.getProperty(),
232 "protein sequence ID");
233 if (cdsId != null && cdsId.trim().length() > 0)
236 String[] vrs = cdsId.split("\\.");
237 String version = vrs.length > 1 ? vrs[1]
238 : DBRefSource.UNIPROT + ":" + dbVersion;
239 dbr = new DBRefEntry(DBRefSource.EMBLCDS, version, vrs[0]);
243 if ("Ensembl".equals(type))
246 * e.g. Uniprot accession Q9BXM7 has
247 * <dbReference type="Ensembl" id="ENST00000321556">
248 * <molecule id="Q9BXM7-1"/>
249 * <property type="protein sequence ID" value="ENSP00000364204"/>
250 * <property type="gene ID" value="ENSG00000158828"/>
253 String cdsId = getProperty(dbref.getProperty(),
254 "protein sequence ID");
255 if (cdsId != null && cdsId.trim().length() > 0)
257 dbr = new DBRefEntry(DBRefSource.ENSEMBL,
258 DBRefSource.UNIPROT + ":" + dbVersion, cdsId.trim());
265 * create features; they have either begin and end, or position, in XML
267 sequence.setPDBId(pdbRefs);
268 if (entry.getFeature() != null)
270 for (FeatureType uf : entry.getFeature())
272 LocationType location = uf.getLocation();
275 if (location.getPosition() != null)
277 start = location.getPosition().getPosition().intValue();
282 start = location.getBegin().getPosition().intValue();
283 end = location.getEnd().getPosition().intValue();
285 SequenceFeature sf = new SequenceFeature(uf.getType(),
286 getDescription(uf), start, end, "Uniprot");
287 sf.setStatus(uf.getStatus());
288 sequence.addSequenceFeature(sf);
291 for (DBRefEntry dbr : dbRefs)
293 sequence.addDBRef(dbr);
299 * A helper method that builds a sequence feature description
304 static String getDescription(FeatureType feature)
306 String orig = feature.getOriginal();
307 List<String> variants = feature.getVariation();
308 StringBuilder sb = new StringBuilder();
311 * append variant in standard format if present
313 * multiple variants are split over lines using <br>
315 boolean asHtml = false;
316 if (orig != null && !orig.isEmpty() && variants != null
317 && !variants.isEmpty())
320 for (String var : variants)
322 // TODO proper HGVS nomenclature for delins structural variations
323 // http://varnomen.hgvs.org/recommendations/protein/variant/delins/
324 // for now we are pragmatic - any orig/variant sequence longer than
325 // three characters is shown with single-character notation rather than
326 // three-letter notation
328 if (orig.length() < 4)
330 for (int c = 0, clen = orig.length(); c < clen; c++)
332 char origchar = orig.charAt(c);
333 String orig3 = ResidueProperties.aa2Triplet.get("" + origchar);
334 sb.append(orig3 == null ? origchar
335 : StringUtils.toSentenceCase(orig3));
343 LocationType location = feature.getLocation();
344 PositionType start = location.getPosition() == null
345 ? location.getBegin()
346 : location.getPosition();
347 sb.append(Integer.toString(start.getPosition().intValue()));
349 if (var.length() < 4)
351 for (int c = 0, clen = var.length(); c < clen; c++)
353 char varchar = var.charAt(c);
354 String var3 = ResidueProperties.aa2Triplet.get("" + varchar);
356 sb.append(var3 != null ? StringUtils.toSentenceCase(var3)
364 if (++p != variants.size())
366 sb.append("<br/> ");
375 String description = feature.getDescription();
376 if (description != null)
378 sb.append(description);
382 sb.insert(0, "<html>");
383 sb.append("</html>");
386 return sb.toString();
390 * A helper method that searches the list of properties for one with the given
391 * key, and if found returns the property value, else returns null
397 static String getProperty(List<PropertyType> properties, String key)
400 if (properties != null)
402 for (PropertyType prop : properties)
404 if (key.equals(prop.getType()))
406 value = prop.getValue();
415 * Extracts xml element entry/protein/recommendedName/fullName
420 static String getUniprotEntryDescription(Entry entry)
423 if (entry.getProtein() != null
424 && entry.getProtein().getRecommendedName() != null)
426 // fullName is mandatory if recommendedName is present
427 desc = entry.getProtein().getRecommendedName().getFullName()
434 * Constructs a sequence id by concatenating all entry/name elements with '|'
440 static String getUniprotEntryId(Entry entry)
442 StringBuilder name = new StringBuilder(32);
443 for (String n : entry.getName())
445 if (name.length() > 0)
447 name.append(BAR_DELIMITER);
451 return name.toString();
457 * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
460 public boolean isValidReference(String accession)
462 // TODO: make the following a standard validator
463 return (accession == null || accession.length() < 2) ? false
464 : getAccessionValidator().search(accession);
468 * return LDHA_CHICK uniprot entry
471 public String getTestQuery()
477 public String getDbName()
479 return "Uniprot"; // getDbSource();
489 * Reads the reply to the EBI Fetch Uniprot data query, unmarshals it to an
490 * Uniprot object, and returns the enclosed Entry objects, or null on any
496 public List<Entry> getUniprotEntries(InputStream is)
498 List<Entry> entries = null;
501 JAXBContext jc = JAXBContext
502 .newInstance("jalview.xml.binding.uniprot");
503 XMLStreamReader streamReader = XMLInputFactory.newInstance()
504 .createXMLStreamReader(is);
505 javax.xml.bind.Unmarshaller um = jc.createUnmarshaller();
506 JAXBElement<jalview.xml.binding.uniprot.Uniprot> uniprotElement =
507 um.unmarshal(streamReader, jalview.xml.binding.uniprot.Uniprot.class);
508 jalview.xml.binding.uniprot.Uniprot uniprot = uniprotElement.getValue();
510 if (uniprot != null && !uniprot.getEntry().isEmpty())
512 entries = uniprot.getEntry();
514 } catch (JAXBException | XMLStreamException
515 | FactoryConfigurationError e)