JAL-2745 JAL-2748 direct download of XML from uniprot.org
[jalview.git] / src / jalview / ws / dbsources / Uniprot.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10  * of the License, or (at your option) any later version.
11  *  
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.
16  * 
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.
20  */
21 package jalview.ws.dbsources;
22
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.seqfetcher.DbSourceProxyImpl;
34
35 import java.io.InputStream;
36 import java.io.InputStreamReader;
37 import java.io.Reader;
38 import java.net.URL;
39 import java.net.URLConnection;
40 import java.util.ArrayList;
41 import java.util.Vector;
42
43 import org.exolab.castor.mapping.Mapping;
44 import org.exolab.castor.xml.Unmarshaller;
45
46 import com.stevesoft.pat.Regex;
47
48 /**
49  * @author JimP
50  * 
51  */
52 public class Uniprot extends DbSourceProxyImpl
53 {
54   private static final String BAR_DELIMITER = "|";
55
56   /*
57    * Castor mapping loaded from uniprot_mapping.xml
58    */
59   private static Mapping map;
60
61   /**
62    * Constructor
63    */
64   public Uniprot()
65   {
66     super();
67   }
68
69   /*
70    * (non-Javadoc)
71    * 
72    * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
73    */
74   @Override
75   public String getAccessionSeparator()
76   {
77     return null;
78   }
79
80   /*
81    * (non-Javadoc)
82    * 
83    * @see jalview.ws.DbSourceProxy#getAccessionValidator()
84    */
85   @Override
86   public Regex getAccessionValidator()
87   {
88     return new Regex("([A-Z]+[0-9]+[A-Z0-9]+|[A-Z0-9]+_[A-Z0-9]+)");
89   }
90
91   /*
92    * (non-Javadoc)
93    * 
94    * @see jalview.ws.DbSourceProxy#getDbSource()
95    */
96   @Override
97   public String getDbSource()
98   {
99     return DBRefSource.UNIPROT;
100   }
101
102   /*
103    * (non-Javadoc)
104    * 
105    * @see jalview.ws.DbSourceProxy#getDbVersion()
106    */
107   @Override
108   public String getDbVersion()
109   {
110     return "0"; // we really don't know what version we're on.
111   }
112
113   /**
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 &lt;entry&gt; elements)
117    * 
118    * @param fileReader
119    * @return
120    */
121   public Vector<UniprotEntry> getUniprotEntries(Reader fileReader)
122   {
123     UniprotFile uni = new UniprotFile();
124     try
125     {
126       if (map == null)
127       {
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);
132       }
133
134       // 2. Unmarshal the data
135       Unmarshaller unmar = new Unmarshaller(uni);
136       unmar.setIgnoreExtraElements(true);
137       unmar.setMapping(map);
138       if (fileReader != null)
139       {
140         uni = (UniprotFile) unmar.unmarshal(fileReader);
141       }
142     } catch (Exception e)
143     {
144       System.out.println("Error getUniprotEntries() " + e);
145     }
146
147     return uni.getUniprotEntries();
148   }
149
150   /*
151    * (non-Javadoc)
152    * 
153    * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
154    */
155   @Override
156   public AlignmentI getSequenceRecords(String queries) throws Exception
157   {
158     startQuery();
159     try
160     {
161       queries = queries.toUpperCase().replaceAll(
162               "(UNIPROT\\|?|UNIPROT_|UNIREF\\d+_|UNIREF\\d+\\|?)", "");
163       AlignmentI al = null;
164
165       String downloadstring = "http://www.uniprot.org/uniprot/" + queries
166               + ".xml";
167       URL url = null;
168       URLConnection urlconn = null;
169
170       url = new URL(downloadstring);
171       urlconn = url.openConnection();
172       InputStream istr = urlconn.getInputStream();
173       Vector<UniprotEntry> entries = getUniprotEntries(
174               new InputStreamReader(istr, "UTF-8"));
175
176       if (entries != null)
177       {
178         ArrayList<SequenceI> seqs = new ArrayList<>();
179         for (UniprotEntry entry : entries)
180         {
181           seqs.add(uniprotEntryToSequenceI(entry));
182         }
183         al = new Alignment(seqs.toArray(new SequenceI[0]));
184
185       }
186       stopQuery();
187       return al;
188     } catch (Exception e)
189     {
190       throw (e);
191     } finally
192     {
193       stopQuery();
194     }
195   }
196
197   /**
198    * 
199    * @param entry
200    *          UniprotEntry
201    * @return SequenceI instance created from the UniprotEntry instance
202    */
203   public SequenceI uniprotEntryToSequenceI(UniprotEntry entry)
204   {
205     String id = getUniprotEntryId(entry);
206     SequenceI sequence = new Sequence(id,
207             entry.getUniprotSequence().getContent());
208     sequence.setDescription(getUniprotEntryDescription(entry));
209
210     final String dbVersion = getDbVersion();
211     ArrayList<DBRefEntry> dbRefs = new ArrayList<DBRefEntry>();
212     for (String accessionId : entry.getAccession())
213     {
214       DBRefEntry dbRef = new DBRefEntry(DBRefSource.UNIPROT, dbVersion,
215               accessionId);
216
217       // mark dbRef as a primary reference for this sequence
218       dbRefs.add(dbRef);
219     }
220
221     Vector<PDBEntry> onlyPdbEntries = new Vector<PDBEntry>();
222     for (PDBEntry pdb : entry.getDbReference())
223     {
224       DBRefEntry dbr = new DBRefEntry();
225       dbr.setSource(pdb.getType());
226       dbr.setAccessionId(pdb.getId());
227       dbr.setVersion(DBRefSource.UNIPROT + ":" + dbVersion);
228       dbRefs.add(dbr);
229       if ("PDB".equals(pdb.getType()))
230       {
231         onlyPdbEntries.addElement(pdb);
232       }
233       if ("EMBL".equals(pdb.getType()))
234       {
235         // look for a CDS reference and add it, too.
236         String cdsId = (String) pdb.getProperty("protein sequence ID");
237         if (cdsId != null && cdsId.trim().length() > 0)
238         {
239           // remove version
240           String[] vrs = cdsId.split("\\.");
241           dbr = new DBRefEntry(DBRefSource.EMBLCDS, vrs.length > 1 ? vrs[1]
242                   : DBRefSource.UNIPROT + ":" + dbVersion, vrs[0]);
243           dbRefs.add(dbr);
244         }
245       }
246       if ("Ensembl".equals(pdb.getType()))
247       {
248         /*UniprotXML
249          * <dbReference type="Ensembl" id="ENST00000321556">
250         * <molecule id="Q9BXM7-1"/>
251         * <property type="protein sequence ID" value="ENSP00000364204"/>
252         * <property type="gene ID" value="ENSG00000158828"/>
253         * </dbReference> 
254          */
255         String cdsId = (String) pdb.getProperty("protein sequence ID");
256         if (cdsId != null && cdsId.trim().length() > 0)
257         {
258           dbr = new DBRefEntry(DBRefSource.ENSEMBL,
259                   DBRefSource.UNIPROT + ":" + dbVersion, cdsId.trim());
260           dbRefs.add(dbr);
261
262         }
263       }
264
265     }
266
267     sequence.setPDBId(onlyPdbEntries);
268     if (entry.getFeature() != null)
269     {
270       for (SequenceFeature sf : entry.getFeature())
271       {
272         sf.setFeatureGroup("Uniprot");
273         sequence.addSequenceFeature(sf);
274       }
275     }
276     for (DBRefEntry dbr : dbRefs)
277     {
278       sequence.addDBRef(dbr);
279     }
280     return sequence;
281   }
282
283   /**
284    * 
285    * @param entry
286    *          UniportEntry
287    * @return protein name(s) delimited by a white space character
288    */
289   public static String getUniprotEntryDescription(UniprotEntry entry)
290   {
291     StringBuilder desc = new StringBuilder(32);
292     if (entry.getProtein() != null && entry.getProtein().getName() != null)
293     {
294       boolean first = true;
295       for (String nm : entry.getProtein().getName())
296       {
297         if (!first)
298         {
299           desc.append(" ");
300         }
301         first = false;
302         desc.append(nm);
303       }
304     }
305     return desc.toString();
306   }
307
308   /**
309    *
310    * @param entry
311    *          UniportEntry
312    * @return The accession id(s) and name(s) delimited by '|'.
313    */
314   public static String getUniprotEntryId(UniprotEntry entry)
315   {
316     StringBuilder name = new StringBuilder(32);
317     // name.append("UniProt/Swiss-Prot");
318     // use 'canonicalised' name for optimal id matching
319     name.append(DBRefSource.UNIPROT);
320     for (String accessionId : entry.getAccession())
321     {
322       name.append(BAR_DELIMITER);
323       name.append(accessionId);
324     }
325     for (String n : entry.getName())
326     {
327       name.append(BAR_DELIMITER);
328       name.append(n);
329     }
330     return name.toString();
331   }
332
333   /*
334    * (non-Javadoc)
335    * 
336    * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
337    */
338   @Override
339   public boolean isValidReference(String accession)
340   {
341     // TODO: make the following a standard validator
342     return (accession == null || accession.length() < 2) ? false
343             : getAccessionValidator().search(accession);
344   }
345
346   /**
347    * return LDHA_CHICK uniprot entry
348    */
349   @Override
350   public String getTestQuery()
351   {
352     return "P00340";
353   }
354
355   @Override
356   public String getDbName()
357   {
358     return "Uniprot"; // getDbSource();
359   }
360
361   @Override
362   public int getTier()
363   {
364     return 0;
365   }
366 }