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.AlignmentI;
24 import jalview.datamodel.DBRefEntry;
25 import jalview.datamodel.DBRefSource;
26 import jalview.datamodel.PDBEntry;
27 import jalview.datamodel.SequenceFeature;
28 import jalview.datamodel.SequenceI;
29 import jalview.datamodel.UniprotEntry;
30 import jalview.datamodel.UniprotFile;
31 import jalview.ws.ebi.EBIFetchClient;
32 import jalview.ws.seqfetcher.DbSourceProxy;
33 import jalview.ws.seqfetcher.DbSourceProxyImpl;
36 import java.io.FileReader;
37 import java.io.Reader;
38 import java.util.Vector;
40 import org.exolab.castor.xml.Unmarshaller;
42 import com.stevesoft.pat.Regex;
48 public class Uniprot extends DbSourceProxyImpl implements DbSourceProxy
51 private static final String BAR_DELIMITER = "|";
53 private static final String NEWLINE = "\n";
55 private static org.exolab.castor.mapping.Mapping map;
63 addDbSourceProperty(DBRefSource.SEQDB, DBRefSource.SEQDB);
64 addDbSourceProperty(DBRefSource.PROTSEQDB);
65 // addDbSourceProperty(DBRefSource.MULTIACC, new Integer(50));
71 * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
73 public String getAccessionSeparator()
81 * @see jalview.ws.DbSourceProxy#getAccessionValidator()
83 public Regex getAccessionValidator()
85 return new Regex("([A-Z]+[0-9]+[A-Z0-9]+|[A-Z0-9]+_[A-Z0-9]+)");
91 * @see jalview.ws.DbSourceProxy#getDbSource()
93 public String getDbSource()
95 return DBRefSource.UNIPROT;
101 * @see jalview.ws.DbSourceProxy#getDbVersion()
103 public String getDbVersion()
105 return "0"; // we really don't know what version we're on.
109 * Reads a file containing the reply to the EBI Fetch Uniprot data query,
110 * unmarshals it to a UniprotFile object, and returns the list of UniprotEntry
111 * data models (mapped from <entry> elements)
116 public Vector<UniprotEntry> getUniprotEntries(Reader fileReader)
118 UniprotFile uni = new UniprotFile();
123 // 1. Load the mapping information from the file
124 map = new org.exolab.castor.mapping.Mapping(uni.getClass()
126 java.net.URL url = getClass().getResource("/uniprot_mapping.xml");
127 map.loadMapping(url);
130 // 2. Unmarshal the data
131 Unmarshaller unmar = new Unmarshaller(uni);
132 unmar.setIgnoreExtraElements(true);
133 unmar.setMapping(map);
134 if (fileReader != null)
136 uni = (UniprotFile) unmar.unmarshal(fileReader);
138 } catch (Exception e)
140 System.out.println("Error getUniprotEntries() " + e);
143 return uni.getUniprotEntries();
149 * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
151 public AlignmentI getSequenceRecords(String queries) throws Exception
156 queries = queries.toUpperCase().replaceAll(
157 "(UNIPROT\\|?|UNIPROT_|UNIREF\\d+_|UNIREF\\d+\\|?)", "");
158 AlignmentI al = null;
159 EBIFetchClient ebi = new EBIFetchClient();
160 // uniprotxml parameter required since december 2007
161 // uniprotkb dbname changed introduced december 2008
162 File file = ebi.fetchDataAsFile("uniprotkb:" + queries, "uniprotxml",
164 Vector<UniprotEntry> entries = getUniprotEntries(new FileReader(file));
169 * If Castor binding included sequence@length, we could guesstimate the
170 * size of buffer to hold the alignment
172 StringBuffer result = new StringBuffer(128);
173 // First, make the new sequences
174 for (UniprotEntry entry : entries)
176 StringBuilder name = constructSequenceFastaHeader(entry);
178 result.append(name).append(NEWLINE)
179 .append(entry.getUniprotSequence().getContent())
183 // Then read in the features and apply them to the dataset
184 al = parseResult(result.toString());
187 // Decorate the alignment with database entries.
188 addUniprotXrefs(al, entries);
197 } catch (Exception e)
205 * Construct a Fasta-format sequence header by concatenating the source,
206 * accession id(s) and name(s), delimited by '|', plus any protein names, now
207 * with space rather than bar delimiter
212 public static StringBuilder constructSequenceFastaHeader(
215 StringBuilder name = new StringBuilder(32);
216 name.append(">UniProt/Swiss-Prot");
217 for (String accessionId : entry.getAccession())
219 name.append(BAR_DELIMITER);
220 name.append(accessionId);
222 for (String n : entry.getName())
224 name.append(BAR_DELIMITER);
228 if (entry.getProtein() != null && entry.getProtein().getName() != null)
230 for (String nm : entry.getProtein().getName())
232 name.append(" ").append(nm);
239 * add an ordered set of UniprotEntry objects to an ordered set of seuqences.
242 * - a sequence of n sequences
244 * a list of n uniprot entries to be analysed.
246 public void addUniprotXrefs(AlignmentI al, Vector<UniprotEntry> entries)
248 final String dbVersion = getDbVersion();
250 for (int i = 0; i < entries.size(); i++)
252 UniprotEntry entry = entries.elementAt(i);
253 Vector<PDBEntry> onlyPdbEntries = new Vector<PDBEntry>();
254 Vector<DBRefEntry> dbxrefs = new Vector<DBRefEntry>();
256 for (PDBEntry pdb : entry.getDbReference())
258 DBRefEntry dbr = new DBRefEntry();
259 dbr.setSource(pdb.getType());
260 dbr.setAccessionId(pdb.getId());
261 dbr.setVersion(DBRefSource.UNIPROT + ":" + dbVersion);
262 dbxrefs.addElement(dbr);
263 if ("PDB".equals(pdb.getType()))
265 onlyPdbEntries.addElement(pdb);
269 SequenceI sq = al.getSequenceAt(i);
270 while (sq.getDatasetSequence() != null)
272 sq = sq.getDatasetSequence();
275 for (String accessionId : entry.getAccession())
278 * add as uniprot whether retrieved from uniprot or uniprot_name
280 sq.addDBRef(new DBRefEntry(DBRefSource.UNIPROT, dbVersion,
284 for (DBRefEntry dbRef : dbxrefs)
288 sq.setPDBId(onlyPdbEntries);
289 if (entry.getFeature() != null)
291 for (SequenceFeature sf : entry.getFeature())
293 sf.setFeatureGroup("Uniprot");
294 sq.addSequenceFeature(sf);
303 * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
305 public boolean isValidReference(String accession)
307 // TODO: make the following a standard validator
308 return (accession == null || accession.length() < 2) ? false
309 : getAccessionValidator().search(accession);
313 * return LDHA_CHICK uniprot entry
315 public String getTestQuery()
320 public String getDbName()
322 return "Uniprot"; // getDbSource();