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.DbSourceProxyImpl;
35 import java.io.FileReader;
36 import java.io.Reader;
38 import java.util.Vector;
40 import org.exolab.castor.mapping.Mapping;
41 import org.exolab.castor.xml.Unmarshaller;
43 import com.stevesoft.pat.Regex;
49 public class Uniprot extends DbSourceProxyImpl
52 private static final String BAR_DELIMITER = "|";
54 private static final String NEWLINE = "\n";
56 private static Mapping map;
69 * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
72 public String getAccessionSeparator()
80 * @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()
94 public String getDbSource()
96 return DBRefSource.UNIPROT;
102 * @see jalview.ws.DbSourceProxy#getDbVersion()
105 public String getDbVersion()
107 return "0"; // we really don't know what version we're on.
111 * Reads a file containing the reply to the EBI Fetch Uniprot data query,
112 * unmarshals it to a UniprotFile object, and returns the list of UniprotEntry
113 * data models (mapped from <entry> elements)
118 public Vector<UniprotEntry> getUniprotEntries(Reader fileReader)
120 UniprotFile uni = new UniprotFile();
125 // 1. Load the mapping information from the file
126 map = new Mapping(uni.getClass().getClassLoader());
127 URL url = getClass().getResource("/uniprot_mapping.xml");
128 map.loadMapping(url);
131 // 2. Unmarshal the data
132 Unmarshaller unmar = new Unmarshaller(uni);
133 unmar.setIgnoreExtraElements(true);
134 unmar.setMapping(map);
135 if (fileReader != null)
137 uni = (UniprotFile) unmar.unmarshal(fileReader);
139 } catch (Exception e)
141 System.out.println("Error getUniprotEntries() " + e);
144 return uni.getUniprotEntries();
150 * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
153 public AlignmentI getSequenceRecords(String queries) throws Exception
158 queries = queries.toUpperCase().replaceAll(
159 "(UNIPROT\\|?|UNIPROT_|UNIREF\\d+_|UNIREF\\d+\\|?)", "");
160 AlignmentI al = null;
161 EBIFetchClient ebi = new EBIFetchClient();
162 // uniprotxml parameter required since december 2007
163 // uniprotkb dbname changed introduced december 2008
164 File file = ebi.fetchDataAsFile("uniprotkb:" + queries, "uniprotxml",
166 Vector<UniprotEntry> entries = getUniprotEntries(new FileReader(file));
171 * If Castor binding included sequence@length, we could guesstimate the
172 * size of buffer to hold the alignment
174 StringBuffer result = new StringBuffer(128);
175 // First, make the new sequences
176 for (UniprotEntry entry : entries)
178 StringBuilder name = constructSequenceFastaHeader(entry);
180 result.append(name).append(NEWLINE)
181 .append(entry.getUniprotSequence().getContent())
185 // Then read in the features and apply them to the dataset
186 al = parseResult(result.toString());
189 // Decorate the alignment with database entries.
190 addUniprotXrefs(al, entries);
199 } catch (Exception e)
207 * Construct a Fasta-format sequence header by concatenating the source,
208 * accession id(s) and name(s), delimited by '|', plus any protein names, now
209 * with space rather than bar delimiter
214 public static StringBuilder constructSequenceFastaHeader(
217 StringBuilder name = new StringBuilder(32);
218 name.append(">UniProt/Swiss-Prot");
219 for (String accessionId : entry.getAccession())
221 name.append(BAR_DELIMITER);
222 name.append(accessionId);
224 for (String n : entry.getName())
226 name.append(BAR_DELIMITER);
230 if (entry.getProtein() != null && entry.getProtein().getName() != null)
232 for (String nm : entry.getProtein().getName())
234 name.append(" ").append(nm);
241 * add an ordered set of UniprotEntry objects to an ordered set of seuqences.
244 * - a sequence of n sequences
246 * a list of n uniprot entries to be analysed.
248 public void addUniprotXrefs(AlignmentI al, Vector<UniprotEntry> entries)
250 final String dbVersion = getDbVersion();
252 for (int i = 0; i < entries.size(); i++)
254 UniprotEntry entry = entries.elementAt(i);
255 Vector<PDBEntry> onlyPdbEntries = new Vector<PDBEntry>();
256 Vector<DBRefEntry> dbxrefs = new Vector<DBRefEntry>();
258 for (PDBEntry pdb : entry.getDbReference())
260 DBRefEntry dbr = new DBRefEntry();
261 dbr.setSource(pdb.getType());
262 dbr.setAccessionId(pdb.getId());
263 dbr.setVersion(DBRefSource.UNIPROT + ":" + dbVersion);
264 dbxrefs.addElement(dbr);
265 if ("PDB".equals(pdb.getType()))
267 onlyPdbEntries.addElement(pdb);
271 SequenceI sq = al.getSequenceAt(i);
272 while (sq.getDatasetSequence() != null)
274 sq = sq.getDatasetSequence();
277 for (String accessionId : entry.getAccession())
280 * add as uniprot whether retrieved from uniprot or uniprot_name
282 sq.addDBRef(new DBRefEntry(DBRefSource.UNIPROT, dbVersion,
286 for (DBRefEntry dbRef : dbxrefs)
290 sq.setPDBId(onlyPdbEntries);
291 if (entry.getFeature() != null)
293 for (SequenceFeature sf : entry.getFeature())
295 sf.setFeatureGroup("Uniprot");
296 sq.addSequenceFeature(sf);
305 * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
308 public boolean isValidReference(String accession)
310 // TODO: make the following a standard validator
311 return (accession == null || accession.length() < 2) ? false
312 : getAccessionValidator().search(accession);
316 * return LDHA_CHICK uniprot entry
319 public String getTestQuery()
325 public String getDbName()
327 return "Uniprot"; // getDbSource();
337 public int getMaximumQueryCount()
339 // relocated this commented out code...
340 // addDbSourceProperty(DBRefSource.MULTIACC, new Integer(50));
342 return super.getMaximumQueryCount();