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.datamodel.xdb.uniprot.UniprotEntry;
33 import jalview.datamodel.xdb.uniprot.UniprotFeature;
34 import jalview.datamodel.xdb.uniprot.UniprotFile;
35 import jalview.ws.seqfetcher.DbSourceProxyImpl;
37 import java.io.InputStream;
38 import java.io.InputStreamReader;
39 import java.io.Reader;
41 import java.net.URLConnection;
42 import java.util.ArrayList;
43 import java.util.Vector;
45 import org.exolab.castor.mapping.Mapping;
46 import org.exolab.castor.xml.Unmarshaller;
48 import com.stevesoft.pat.Regex;
54 public class Uniprot extends DbSourceProxyImpl
56 private static final String DEFAULT_UNIPROT_DOMAIN = "https://www.uniprot.org";
58 private static final String BAR_DELIMITER = "|";
61 * Castor mapping loaded from uniprot_mapping.xml
63 private static Mapping map;
73 private String getDomain()
75 return Cache.getDefault("UNIPROT_DOMAIN", DEFAULT_UNIPROT_DOMAIN);
81 * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
84 public String getAccessionSeparator()
92 * @see jalview.ws.DbSourceProxy#getAccessionValidator()
95 public Regex getAccessionValidator()
97 return new Regex("([A-Z]+[0-9]+[A-Z0-9]+|[A-Z0-9]+_[A-Z0-9]+)");
103 * @see jalview.ws.DbSourceProxy#getDbSource()
106 public String getDbSource()
108 return DBRefSource.UNIPROT;
114 * @see jalview.ws.DbSourceProxy#getDbVersion()
117 public String getDbVersion()
119 return "0"; // we really don't know what version we're on.
123 * Reads a file containing the reply to the EBI Fetch Uniprot data query,
124 * unmarshals it to a UniprotFile object, and returns the list of UniprotEntry
125 * data models (mapped from <entry> elements)
130 public Vector<UniprotEntry> getUniprotEntries(Reader fileReader)
132 UniprotFile uni = new UniprotFile();
137 // 1. Load the mapping information from the file
138 map = new Mapping(uni.getClass().getClassLoader());
139 URL url = getClass().getResource("/uniprot_mapping.xml");
140 map.loadMapping(url);
143 // 2. Unmarshal the data
144 Unmarshaller unmar = new Unmarshaller(uni);
145 unmar.setIgnoreExtraElements(true);
146 unmar.setMapping(map);
147 if (fileReader != null)
149 uni = (UniprotFile) unmar.unmarshal(fileReader);
151 } catch (Exception e)
153 System.out.println("Error getUniprotEntries() " + e);
156 return uni.getUniprotEntries();
162 * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
165 public AlignmentI getSequenceRecords(String queries) throws Exception
170 queries = queries.toUpperCase().replaceAll(
171 "(UNIPROT\\|?|UNIPROT_|UNIREF\\d+_|UNIREF\\d+\\|?)", "");
172 AlignmentI al = null;
174 String downloadstring = getDomain() + "/uniprot/" + queries
177 URLConnection urlconn = null;
179 url = new URL(downloadstring);
180 urlconn = url.openConnection();
181 InputStream istr = urlconn.getInputStream();
182 Vector<UniprotEntry> entries = getUniprotEntries(
183 new InputStreamReader(istr, "UTF-8"));
187 ArrayList<SequenceI> seqs = new ArrayList<>();
188 for (UniprotEntry entry : entries)
190 seqs.add(uniprotEntryToSequenceI(entry));
192 al = new Alignment(seqs.toArray(new SequenceI[0]));
197 } catch (Exception e)
210 * @return SequenceI instance created from the UniprotEntry instance
212 public SequenceI uniprotEntryToSequenceI(UniprotEntry entry)
214 String id = getUniprotEntryId(entry);
215 SequenceI sequence = new Sequence(id,
216 entry.getUniprotSequence().getContent());
217 sequence.setDescription(getUniprotEntryDescription(entry));
219 final String dbVersion = getDbVersion();
220 ArrayList<DBRefEntry> dbRefs = new ArrayList<>();
221 for (String accessionId : entry.getAccession())
223 DBRefEntry dbRef = new DBRefEntry(DBRefSource.UNIPROT, dbVersion,
226 // mark dbRef as a primary reference for this sequence
230 Vector<PDBEntry> onlyPdbEntries = new Vector<>();
231 for (PDBEntry pdb : entry.getDbReference())
233 DBRefEntry dbr = new DBRefEntry();
234 dbr.setSource(pdb.getType());
235 dbr.setAccessionId(pdb.getId());
236 dbr.setVersion(DBRefSource.UNIPROT + ":" + dbVersion);
238 if ("PDB".equals(pdb.getType()))
240 onlyPdbEntries.addElement(pdb);
242 if ("EMBL".equals(pdb.getType()))
244 // look for a CDS reference and add it, too.
245 String cdsId = (String) pdb.getProperty("protein sequence ID");
246 if (cdsId != null && cdsId.trim().length() > 0)
249 String[] vrs = cdsId.split("\\.");
250 dbr = new DBRefEntry(DBRefSource.EMBLCDS, vrs.length > 1 ? vrs[1]
251 : DBRefSource.UNIPROT + ":" + dbVersion, vrs[0]);
255 if ("Ensembl".equals(pdb.getType()))
258 * <dbReference type="Ensembl" id="ENST00000321556">
259 * <molecule id="Q9BXM7-1"/>
260 * <property type="protein sequence ID" value="ENSP00000364204"/>
261 * <property type="gene ID" value="ENSG00000158828"/>
264 String cdsId = (String) pdb.getProperty("protein sequence ID");
265 if (cdsId != null && cdsId.trim().length() > 0)
267 dbr = new DBRefEntry(DBRefSource.ENSEMBL,
268 DBRefSource.UNIPROT + ":" + dbVersion, cdsId.trim());
275 sequence.setPDBId(onlyPdbEntries);
276 if (entry.getFeature() != null)
278 for (UniprotFeature uf : entry.getFeature())
280 SequenceFeature copy = new SequenceFeature(uf.getType(),
281 uf.getDescription(), uf.getBegin(), uf.getEnd(), "Uniprot");
282 copy.setStatus(uf.getStatus());
283 sequence.addSequenceFeature(copy);
286 for (DBRefEntry dbr : dbRefs)
288 sequence.addDBRef(dbr);
297 * @return protein name(s) delimited by a white space character
299 public static String getUniprotEntryDescription(UniprotEntry entry)
301 StringBuilder desc = new StringBuilder(32);
302 if (entry.getProtein() != null && entry.getProtein().getName() != null)
304 boolean first = true;
305 for (String nm : entry.getProtein().getName())
315 return desc.toString();
322 * @return The accession id(s) and name(s) delimited by '|'.
324 public static String getUniprotEntryId(UniprotEntry entry)
326 StringBuilder name = new StringBuilder(32);
327 for (String n : entry.getName())
329 if (name.length() > 0)
331 name.append(BAR_DELIMITER);
335 return name.toString();
341 * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
344 public boolean isValidReference(String accession)
346 // TODO: make the following a standard validator
347 return (accession == null || accession.length() < 2) ? false
348 : getAccessionValidator().search(accession);
352 * return LDHA_CHICK uniprot entry
355 public String getTestQuery()
361 public String getDbName()
363 return "Uniprot"; // getDbSource();