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