avoid importing the mapping every time we parse a uniprot record.
[jalview.git] / src / jalview / ws / dbsources / Uniprot.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
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 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.ws.dbsources;
19
20 import java.io.File;
21 import java.io.FileReader;
22 import java.io.IOException;
23 import java.util.Enumeration;
24 import java.util.Hashtable;
25 import java.util.Vector;
26
27 import org.exolab.castor.xml.Unmarshaller;
28
29 import com.stevesoft.pat.Regex;
30
31 import jalview.datamodel.Alignment;
32 import jalview.datamodel.AlignmentI;
33 import jalview.datamodel.DBRefEntry;
34 import jalview.datamodel.DBRefSource;
35 import jalview.datamodel.PDBEntry;
36 import jalview.datamodel.SequenceFeature;
37 import jalview.datamodel.SequenceI;
38 import jalview.datamodel.UniprotEntry;
39 import jalview.datamodel.UniprotFile;
40 import jalview.io.FormatAdapter;
41 import jalview.io.IdentifyFile;
42 import jalview.ws.DBRefFetcher;
43 import jalview.ws.ebi.EBIFetchClient;
44 import jalview.ws.seqfetcher.DbSourceProxy;
45 import jalview.ws.seqfetcher.DbSourceProxyImpl;
46
47 /**
48  * @author JimP
49  * 
50  */
51 public class Uniprot extends DbSourceProxyImpl implements DbSourceProxy
52 {
53   public Uniprot()
54   {
55     super();
56     addDbSourceProperty(DBRefSource.SEQDB, DBRefSource.SEQDB);
57     addDbSourceProperty(DBRefSource.PROTSEQDB);
58     // addDbSourceProperty(DBRefSource.MULTIACC, new Integer(50));
59   }
60
61   /*
62    * (non-Javadoc)
63    * 
64    * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
65    */
66   public String getAccessionSeparator()
67   {
68     return null; // ";";
69   }
70
71   /*
72    * (non-Javadoc)
73    * 
74    * @see jalview.ws.DbSourceProxy#getAccessionValidator()
75    */
76   public Regex getAccessionValidator()
77   {
78     return new Regex("([A-Z]+[0-9]+[A-Z0-9]+|[A-Z0-9]+_[A-Z0-9]+)");
79   }
80
81   /*
82    * (non-Javadoc)
83    * 
84    * @see jalview.ws.DbSourceProxy#getDbSource()
85    */
86   public String getDbSource()
87   {
88     return DBRefSource.UNIPROT;
89   }
90
91   /*
92    * (non-Javadoc)
93    * 
94    * @see jalview.ws.DbSourceProxy#getDbVersion()
95    */
96   public String getDbVersion()
97   {
98     return "0"; // we really don't know what version we're on.
99   }
100
101   private EBIFetchClient ebi = null;
102
103   private static org.exolab.castor.mapping.Mapping map;
104
105   public Vector getUniprotEntries(File file)
106   {
107     UniprotFile uni = new UniprotFile();
108     try
109     {
110       if (map == null)
111       {
112         // 1. Load the mapping information from the file
113         map = new org.exolab.castor.mapping.Mapping(uni.getClass()
114                 .getClassLoader());
115         java.net.URL url = getClass().getResource("/uniprot_mapping.xml");
116         map.loadMapping(url);
117       }
118
119       // 2. Unmarshal the data
120       Unmarshaller unmar = new Unmarshaller(uni);
121       unmar.setIgnoreExtraElements(true);
122       unmar.setMapping(map);
123       if (file != null)
124       {
125         uni = (UniprotFile) unmar.unmarshal(new FileReader(file));
126       }
127     } catch (Exception e)
128     {
129       System.out.println("Error getUniprotEntries() " + e);
130     }
131
132     return uni.getUniprotEntries();
133   }
134
135   /*
136    * (non-Javadoc)
137    * 
138    * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
139    */
140   public AlignmentI getSequenceRecords(String queries) throws Exception
141   {
142     startQuery();
143     try
144     {
145       queries = queries.toUpperCase().replaceAll(
146               "(UNIPROT\\|?|UNIPROT_|UNIREF\\d+_|UNIREF\\d+\\|?)", "");
147       Alignment al = null;
148       ebi = new EBIFetchClient();
149       StringBuffer result = new StringBuffer();
150       // uniprotxml parameter required since december 2007
151       // uniprotkb dbname changed introduced december 2008
152       File file = ebi.fetchDataAsFile("uniprotkb:" + queries, "uniprotxml",
153               null);
154       Vector entries = getUniprotEntries(file);
155
156       if (entries != null)
157       {
158         // First, make the new sequences
159         Enumeration en = entries.elements();
160         while (en.hasMoreElements())
161         {
162           UniprotEntry entry = (UniprotEntry) en.nextElement();
163
164           StringBuffer name = new StringBuffer(">UniProt/Swiss-Prot");
165           Enumeration en2 = entry.getAccession().elements();
166           while (en2.hasMoreElements())
167           {
168             name.append("|");
169             name.append(en2.nextElement());
170           }
171           en2 = entry.getName().elements();
172           while (en2.hasMoreElements())
173           {
174             name.append("|");
175             name.append(en2.nextElement());
176           }
177
178           if (entry.getProtein() != null
179                   && entry.getProtein().getName() != null)
180           {
181             for (int nm = 0, nmSize = entry.getProtein().getName().size(); nm < nmSize; nm++)
182             {
183               name.append(" " + entry.getProtein().getName().elementAt(nm));
184             }
185           }
186
187           result.append(name + "\n"
188                   + entry.getUniprotSequence().getContent() + "\n");
189
190         }
191
192         // Then read in the features and apply them to the dataset
193         al = parseResult(result.toString());
194         if (al != null)
195         {
196           // Decorate the alignment with database entries.
197           addUniprotXrefs(al, entries);
198         }
199         else
200         {
201           results = result;
202         }
203       }
204       stopQuery();
205       return al;
206     } catch (Exception e)
207     {
208       stopQuery();
209       throw (e);
210     }
211   }
212
213   /**
214    * add an ordered set of UniprotEntry objects to an ordered set of seuqences.
215    * 
216    * @param al
217    *          - a sequence of n sequences
218    * @param entries
219    *          a seuqence of n uniprot entries to be analysed.
220    */
221   public void addUniprotXrefs(Alignment al, Vector entries)
222   {
223     for (int i = 0; i < entries.size(); i++)
224     {
225       UniprotEntry entry = (UniprotEntry) entries.elementAt(i);
226       Enumeration e = entry.getDbReference().elements();
227       Vector onlyPdbEntries = new Vector();
228       Vector dbxrefs = new Vector();
229       while (e.hasMoreElements())
230       {
231         PDBEntry pdb = (PDBEntry) e.nextElement();
232         DBRefEntry dbr = new DBRefEntry();
233         dbr.setSource(pdb.getType());
234         dbr.setAccessionId(pdb.getId());
235         dbr.setVersion(DBRefSource.UNIPROT + ":" + getDbVersion());
236         dbxrefs.addElement(dbr);
237         if (!pdb.getType().equals("PDB"))
238         {
239           continue;
240         }
241
242         onlyPdbEntries.addElement(pdb);
243       }
244       SequenceI sq = al.getSequenceAt(i);
245       while (sq.getDatasetSequence() != null)
246       {
247         sq = sq.getDatasetSequence();
248       }
249
250       Enumeration en2 = entry.getAccession().elements();
251       while (en2.hasMoreElements())
252       {
253         // we always add as uniprot if we retrieved from uniprot or uniprot name
254         sq.addDBRef(new DBRefEntry(DBRefSource.UNIPROT, getDbVersion(), en2
255                 .nextElement().toString()));
256       }
257       en2 = dbxrefs.elements();
258       while (en2.hasMoreElements())
259       {
260         // we always add as uniprot if we retrieved from uniprot or uniprot name
261         sq.addDBRef((DBRefEntry) en2.nextElement());
262
263       }
264       sq.setPDBId(onlyPdbEntries);
265       if (entry.getFeature() != null)
266       {
267         e = entry.getFeature().elements();
268         while (e.hasMoreElements())
269         {
270           SequenceFeature sf = (SequenceFeature) e.nextElement();
271           sf.setFeatureGroup("Uniprot");
272           sq.addSequenceFeature(sf);
273         }
274       }
275     }
276   }
277
278   /*
279    * (non-Javadoc)
280    * 
281    * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
282    */
283   public boolean isValidReference(String accession)
284   {
285     // TODO: make the following a standard validator
286     return (accession == null || accession.length() < 2) ? false
287             : getAccessionValidator().search(accession);
288   }
289
290   /**
291    * return LDHA_CHICK uniprot entry
292    */
293   public String getTestQuery()
294   {
295     return "P00340";
296   }
297
298   public String getDbName()
299   {
300     return "Uniprot"; // getDbSource();
301   }
302 }