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.ws.ebi.EBIFetchClient;
34 import jalview.ws.seqfetcher.DbSourceProxyImpl;
37 import java.io.FileReader;
38 import java.io.Reader;
40 import java.util.ArrayList;
41 import java.util.Vector;
43 import org.exolab.castor.mapping.Mapping;
44 import org.exolab.castor.xml.Unmarshaller;
46 import com.stevesoft.pat.Regex;
52 public class Uniprot extends DbSourceProxyImpl
54 private static final String BAR_DELIMITER = "|";
57 * Castor mapping loaded from uniprot_mapping.xml
59 private static Mapping map;
72 * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
75 public String getAccessionSeparator()
83 * @see jalview.ws.DbSourceProxy#getAccessionValidator()
86 public Regex getAccessionValidator()
88 return new Regex("([A-Z]+[0-9]+[A-Z0-9]+|[A-Z0-9]+_[A-Z0-9]+)");
94 * @see jalview.ws.DbSourceProxy#getDbSource()
97 public String getDbSource()
99 return DBRefSource.UNIPROT;
105 * @see jalview.ws.DbSourceProxy#getDbVersion()
108 public String getDbVersion()
110 return "0"; // we really don't know what version we're on.
114 * Reads a file containing the reply to the EBI Fetch Uniprot data query,
115 * unmarshals it to a UniprotFile object, and returns the list of UniprotEntry
116 * data models (mapped from <entry> elements)
121 public Vector<UniprotEntry> getUniprotEntries(Reader fileReader)
123 UniprotFile uni = new UniprotFile();
128 // 1. Load the mapping information from the file
129 map = new Mapping(uni.getClass().getClassLoader());
130 URL url = getClass().getResource("/uniprot_mapping.xml");
131 map.loadMapping(url);
134 // 2. Unmarshal the data
135 Unmarshaller unmar = new Unmarshaller(uni);
136 unmar.setIgnoreExtraElements(true);
137 unmar.setMapping(map);
138 if (fileReader != null)
140 uni = (UniprotFile) unmar.unmarshal(fileReader);
142 } catch (Exception e)
144 System.out.println("Error getUniprotEntries() " + e);
147 return uni.getUniprotEntries();
153 * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
156 public AlignmentI getSequenceRecords(String queries) throws Exception
161 queries = queries.toUpperCase().replaceAll(
162 "(UNIPROT\\|?|UNIPROT_|UNIREF\\d+_|UNIREF\\d+\\|?)", "");
163 AlignmentI al = null;
164 EBIFetchClient ebi = new EBIFetchClient();
165 // uniprotxml parameter required since december 2007
166 // uniprotkb dbname changed introduced december 2008
167 File file = ebi.fetchDataAsFile("uniprotkb:" + queries, "uniprotxml",
169 Vector<UniprotEntry> entries = getUniprotEntries(new FileReader(file));
173 ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
174 for (UniprotEntry entry : entries)
176 seqs.add(uniprotEntryToSequenceI(entry));
178 al = new Alignment(seqs.toArray(new SequenceI[0]));
183 } catch (Exception e)
194 * @return SequenceI instance created from the UniprotEntry instance
196 public SequenceI uniprotEntryToSequenceI(UniprotEntry entry){
197 String id = getUniprotEntryId(entry);
198 SequenceI sequence = new Sequence(id, entry.getUniprotSequence()
200 sequence.setDescription(getUniprotEntryDescription(entry));
202 final String dbVersion = getDbVersion();
203 ArrayList<DBRefEntry> dbRefs = new ArrayList<DBRefEntry>();
204 for (String accessionId : entry.getAccession())
206 DBRefEntry dbRef = new DBRefEntry(DBRefSource.UNIPROT, dbVersion,
210 sequence.setSourceDBRef((dbRefs != null && dbRefs.size() > 0) ? dbRefs
213 Vector<PDBEntry> onlyPdbEntries = new Vector<PDBEntry>();
214 for (PDBEntry pdb : entry.getDbReference())
216 DBRefEntry dbr = new DBRefEntry();
217 dbr.setSource(pdb.getType());
218 dbr.setAccessionId(pdb.getId());
219 dbr.setVersion(DBRefSource.UNIPROT + ":" + dbVersion);
221 if ("PDB".equals(pdb.getType()))
223 onlyPdbEntries.addElement(pdb);
227 sequence.setPDBId(onlyPdbEntries);
228 if (entry.getFeature() != null)
230 for (SequenceFeature sf : entry.getFeature())
232 sf.setFeatureGroup("Uniprot");
233 sequence.addSequenceFeature(sf);
236 sequence.setDBRefs(dbRefs.toArray(new DBRefEntry[0]));
244 * @return protein name(s) delimited by a white space character
246 public static String getUniprotEntryDescription(UniprotEntry entry)
248 StringBuilder desc = new StringBuilder(32);
249 if (entry.getProtein() != null && entry.getProtein().getName() != null)
251 boolean first = true;
252 for (String nm : entry.getProtein().getName())
262 return desc.toString();
269 * @return The accession id(s) and name(s) delimited by '|'.
271 public static String getUniprotEntryId(UniprotEntry entry)
273 StringBuilder name = new StringBuilder(32);
274 // name.append("UniProt/Swiss-Prot");
275 // use 'canonicalised' name for optimal id matching
276 name.append(DBRefSource.UNIPROT);
277 for (String accessionId : entry.getAccession())
279 name.append(BAR_DELIMITER);
280 name.append(accessionId);
282 for (String n : entry.getName())
284 name.append(BAR_DELIMITER);
287 return name.toString();
293 * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
296 public boolean isValidReference(String accession)
298 // TODO: make the following a standard validator
299 return (accession == null || accession.length() < 2) ? false
300 : getAccessionValidator().search(accession);
304 * return LDHA_CHICK uniprot entry
307 public String getTestQuery()
313 public String getDbName()
315 return "Uniprot"; // getDbSource();