update author list in license for (JAL-826)
[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   public Vector getUniprotEntries(File file)
104   {
105     UniprotFile uni = new UniprotFile();
106     try
107     {
108       // 1. Load the mapping information from the file
109       org.exolab.castor.mapping.Mapping map = new org.exolab.castor.mapping.Mapping(
110               uni.getClass().getClassLoader());
111       java.net.URL url = getClass().getResource("/uniprot_mapping.xml");
112       map.loadMapping(url);
113
114       // 2. Unmarshal the data
115       Unmarshaller unmar = new Unmarshaller(uni);
116       unmar.setIgnoreExtraElements(true);
117       unmar.setMapping(map);
118       if (file != null)
119       {
120         uni = (UniprotFile) unmar.unmarshal(new FileReader(file));
121       }
122     } catch (Exception e)
123     {
124       System.out.println("Error getUniprotEntries() " + e);
125     }
126
127     return uni.getUniprotEntries();
128   }
129
130   /*
131    * (non-Javadoc)
132    * 
133    * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
134    */
135   public AlignmentI getSequenceRecords(String queries) throws Exception
136   {
137     startQuery();
138     try
139     {
140       queries = queries.toUpperCase().replaceAll(
141               "(UNIPROT\\|?|UNIPROT_|UNIREF\\d+_|UNIREF\\d+\\|?)", "");
142       Alignment al = null;
143       ebi = new EBIFetchClient();
144       StringBuffer result = new StringBuffer();
145       // uniprotxml parameter required since december 2007
146       // uniprotkb dbname changed introduced december 2008
147       File file = ebi.fetchDataAsFile("uniprotkb:" + queries, "uniprotxml",
148               null);
149       Vector entries = getUniprotEntries(file);
150
151       if (entries != null)
152       {
153         // First, make the new sequences
154         Enumeration en = entries.elements();
155         while (en.hasMoreElements())
156         {
157           UniprotEntry entry = (UniprotEntry) en.nextElement();
158
159           StringBuffer name = new StringBuffer(">UniProt/Swiss-Prot");
160           Enumeration en2 = entry.getAccession().elements();
161           while (en2.hasMoreElements())
162           {
163             name.append("|");
164             name.append(en2.nextElement());
165           }
166           en2 = entry.getName().elements();
167           while (en2.hasMoreElements())
168           {
169             name.append("|");
170             name.append(en2.nextElement());
171           }
172
173           if (entry.getProtein() != null
174                   && entry.getProtein().getName() != null)
175           {
176             for (int nm = 0, nmSize = entry.getProtein().getName().size(); nm < nmSize; nm++)
177             {
178               name.append(" " + entry.getProtein().getName().elementAt(nm));
179             }
180           }
181
182           result.append(name + "\n"
183                   + entry.getUniprotSequence().getContent() + "\n");
184
185         }
186
187         // Then read in the features and apply them to the dataset
188         al = parseResult(result.toString());
189         if (al != null)
190         {
191           // Decorate the alignment with database entries.
192           addUniprotXrefs(al, entries);
193         }
194         else
195         {
196           results = result;
197         }
198       }
199       stopQuery();
200       return al;
201     } catch (Exception e)
202     {
203       stopQuery();
204       throw (e);
205     }
206   }
207
208   /**
209    * add an ordered set of UniprotEntry objects to an ordered set of seuqences.
210    * 
211    * @param al
212    *          - a sequence of n sequences
213    * @param entries
214    *          a seuqence of n uniprot entries to be analysed.
215    */
216   public void addUniprotXrefs(Alignment al, Vector entries)
217   {
218     for (int i = 0; i < entries.size(); i++)
219     {
220       UniprotEntry entry = (UniprotEntry) entries.elementAt(i);
221       Enumeration e = entry.getDbReference().elements();
222       Vector onlyPdbEntries = new Vector();
223       Vector dbxrefs = new Vector();
224       while (e.hasMoreElements())
225       {
226         PDBEntry pdb = (PDBEntry) e.nextElement();
227         DBRefEntry dbr = new DBRefEntry();
228         dbr.setSource(pdb.getType());
229         dbr.setAccessionId(pdb.getId());
230         dbr.setVersion(DBRefSource.UNIPROT + ":" + getDbVersion());
231         dbxrefs.addElement(dbr);
232         if (!pdb.getType().equals("PDB"))
233         {
234           continue;
235         }
236
237         onlyPdbEntries.addElement(pdb);
238       }
239       SequenceI sq = al.getSequenceAt(i);
240       while (sq.getDatasetSequence() != null)
241       {
242         sq = sq.getDatasetSequence();
243       }
244
245       Enumeration en2 = entry.getAccession().elements();
246       while (en2.hasMoreElements())
247       {
248         // we always add as uniprot if we retrieved from uniprot or uniprot name
249         sq.addDBRef(new DBRefEntry(DBRefSource.UNIPROT, getDbVersion(), en2
250                 .nextElement().toString()));
251       }
252       en2 = dbxrefs.elements();
253       while (en2.hasMoreElements())
254       {
255         // we always add as uniprot if we retrieved from uniprot or uniprot name
256         sq.addDBRef((DBRefEntry) en2.nextElement());
257
258       }
259       sq.setPDBId(onlyPdbEntries);
260       if (entry.getFeature() != null)
261       {
262         e = entry.getFeature().elements();
263         while (e.hasMoreElements())
264         {
265           SequenceFeature sf = (SequenceFeature) e.nextElement();
266           sf.setFeatureGroup("Uniprot");
267           sq.addSequenceFeature(sf);
268         }
269       }
270     }
271   }
272
273   /*
274    * (non-Javadoc)
275    * 
276    * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
277    */
278   public boolean isValidReference(String accession)
279   {
280     // TODO: make the following a standard validator
281     return (accession == null || accession.length() < 2) ? false
282             : getAccessionValidator().search(accession);
283   }
284
285   /**
286    * return LDHA_CHICK uniprot entry
287    */
288   public String getTestQuery()
289   {
290     return "P00340";
291   }
292
293   public String getDbName()
294   {
295     return "Uniprot"; // getDbSource();
296   }
297 }