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.datamodel.Alignment;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.DBRefEntry;
26 import jalview.datamodel.DBRefSource;
27 import jalview.datamodel.PDBEntry;
28 import jalview.datamodel.Sequence;
29 import jalview.datamodel.SequenceFeature;
30 import jalview.datamodel.SequenceI;
31 import jalview.datamodel.UniprotEntry;
32 import jalview.datamodel.UniprotFile;
33 import jalview.util.DBRefUtils;
34 import jalview.ws.ebi.EBIFetchClient;
35 import jalview.ws.seqfetcher.DbSourceProxyImpl;
38 import java.io.FileReader;
39 import java.io.Reader;
41 import java.util.ArrayList;
42 import java.util.Vector;
44 import org.exolab.castor.mapping.Mapping;
45 import org.exolab.castor.xml.Unmarshaller;
47 import com.stevesoft.pat.Regex;
53 public class Uniprot extends DbSourceProxyImpl
55 private static final String BAR_DELIMITER = "|";
58 * Castor mapping loaded from uniprot_mapping.xml
60 private static Mapping map;
73 * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
76 public String getAccessionSeparator()
84 * @see jalview.ws.DbSourceProxy#getAccessionValidator()
87 public Regex getAccessionValidator()
89 return new Regex("([A-Z]+[0-9]+[A-Z0-9]+|[A-Z0-9]+_[A-Z0-9]+)");
95 * @see jalview.ws.DbSourceProxy#getDbSource()
98 public String getDbSource()
100 return DBRefSource.UNIPROT;
106 * @see jalview.ws.DbSourceProxy#getDbVersion()
109 public String getDbVersion()
111 return "0"; // we really don't know what version we're on.
115 * Reads a file containing the reply to the EBI Fetch Uniprot data query,
116 * unmarshals it to a UniprotFile object, and returns the list of UniprotEntry
117 * data models (mapped from <entry> elements)
122 public Vector<UniprotEntry> getUniprotEntries(Reader fileReader)
124 UniprotFile uni = new UniprotFile();
129 // 1. Load the mapping information from the file
130 map = new Mapping(uni.getClass().getClassLoader());
131 URL url = getClass().getResource("/uniprot_mapping.xml");
132 map.loadMapping(url);
135 // 2. Unmarshal the data
136 Unmarshaller unmar = new Unmarshaller(uni);
137 unmar.setIgnoreExtraElements(true);
138 unmar.setMapping(map);
139 if (fileReader != null)
141 uni = (UniprotFile) unmar.unmarshal(fileReader);
143 } catch (Exception e)
145 System.out.println("Error getUniprotEntries() " + e);
148 return uni.getUniprotEntries();
154 * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
157 public AlignmentI getSequenceRecords(String queries) throws Exception
162 queries = queries.toUpperCase().replaceAll(
163 "(UNIPROT\\|?|UNIPROT_|UNIREF\\d+_|UNIREF\\d+\\|?)", "");
164 AlignmentI al = null;
165 EBIFetchClient ebi = new EBIFetchClient();
166 // uniprotxml parameter required since december 2007
167 // uniprotkb dbname changed introduced december 2008
168 File file = ebi.fetchDataAsFile("uniprotkb:" + queries, "uniprotxml",
170 Vector<UniprotEntry> entries = getUniprotEntries(new FileReader(file));
174 ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
175 for (UniprotEntry entry : entries)
177 seqs.add(uniprotEntryToSequenceI(entry));
179 al = new Alignment(seqs.toArray(new SequenceI[0]));
184 } catch (Exception e)
195 * @return SequenceI instance created from the UniprotEntry instance
197 public SequenceI uniprotEntryToSequenceI(UniprotEntry entry){
198 String id = getUniprotEntryId(entry);
199 SequenceI sequence = new Sequence(id, entry.getUniprotSequence()
201 sequence.setDescription(getUniprotEntryDescription(entry));
203 final String dbVersion = getDbVersion();
204 ArrayList<DBRefEntry> dbRefs = new ArrayList<DBRefEntry>();
205 for (String accessionId : entry.getAccession())
207 DBRefEntry dbRef = new DBRefEntry(DBRefSource.UNIPROT, dbVersion,
210 // mark dbRef as a primary reference for this sequence
214 Vector<PDBEntry> onlyPdbEntries = new Vector<PDBEntry>();
215 for (PDBEntry pdb : entry.getDbReference())
217 DBRefEntry dbr = new DBRefEntry();
218 dbr.setSource(pdb.getType());
219 dbr.setAccessionId(pdb.getId());
220 dbr.setVersion(DBRefSource.UNIPROT + ":" + dbVersion);
222 if ("PDB".equals(pdb.getType()))
224 onlyPdbEntries.addElement(pdb);
226 if ("EMBL".equals(pdb.getType()))
228 // look for a CDS reference and add it, too.
229 String cdsId = (String) pdb.getProperty()
230 .get("protein sequence ID");
231 if (cdsId != null && cdsId.trim().length() > 0)
233 dbr = new DBRefEntry(DBRefSource.EMBLCDS, DBRefSource.UNIPROT
235 + dbVersion, cdsId.trim());
239 if (false) // "Ensembl".equals(pdb.getType()))
242 * <dbReference type="Ensembl" id="ENST00000321556">
243 * <molecule id="Q9BXM7-1"/>
244 * <property type="protein sequence ID" value="ENSP00000364204"/>
245 * <property type="gene ID" value="ENSG00000158828"/>
248 String cdsId = (String) pdb.getProperty()
249 .get("protein sequence ID");
250 if (cdsId != null && cdsId.trim().length() > 0)
252 // Only add the product ID
254 dbr = new DBRefEntry(DBRefSource.ENSEMBL, DBRefSource.UNIPROT
255 + ":" + dbVersion, cdsId.trim());
263 sequence.setPDBId(onlyPdbEntries);
264 if (entry.getFeature() != null)
266 for (SequenceFeature sf : entry.getFeature())
268 sf.setFeatureGroup("Uniprot");
269 sequence.addSequenceFeature(sf);
272 // we use setDBRefs to assign refs quickly.
273 sequence.setDBRefs(dbRefs.toArray(new DBRefEntry[0]));
274 // need to use ensurePrimaries to reify any refs that should become primary
276 DBRefUtils.ensurePrimaries(sequence); // promote any direct refs to primary
285 * @return protein name(s) delimited by a white space character
287 public static String getUniprotEntryDescription(UniprotEntry entry)
289 StringBuilder desc = new StringBuilder(32);
290 if (entry.getProtein() != null && entry.getProtein().getName() != null)
292 boolean first = true;
293 for (String nm : entry.getProtein().getName())
303 return desc.toString();
310 * @return The accession id(s) and name(s) delimited by '|'.
312 public static String getUniprotEntryId(UniprotEntry entry)
314 StringBuilder name = new StringBuilder(32);
315 // name.append("UniProt/Swiss-Prot");
316 // use 'canonicalised' name for optimal id matching
317 name.append(DBRefSource.UNIPROT);
318 for (String accessionId : entry.getAccession())
320 name.append(BAR_DELIMITER);
321 name.append(accessionId);
323 for (String n : entry.getName())
325 name.append(BAR_DELIMITER);
328 return name.toString();
334 * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
337 public boolean isValidReference(String accession)
339 // TODO: make the following a standard validator
340 return (accession == null || accession.length() < 2) ? false
341 : getAccessionValidator().search(accession);
345 * return LDHA_CHICK uniprot entry
348 public String getTestQuery()
354 public String getDbName()
356 return "Uniprot"; // getDbSource();