843828b16f9da51bee051434dcce48ccfb4c66ef
[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.ebi.EBIFetchClient;
34 import jalview.ws.seqfetcher.DbSourceProxy;
35 import jalview.ws.seqfetcher.DbSourceProxyImpl;
36
37 import java.io.File;
38 import java.io.FileReader;
39 import java.io.Reader;
40 import java.util.ArrayList;
41 import java.util.Vector;
42
43 import org.exolab.castor.xml.Unmarshaller;
44
45 import com.stevesoft.pat.Regex;
46
47 /**
48  * @author JimP
49  * 
50  */
51 public class Uniprot extends DbSourceProxyImpl implements DbSourceProxy
52 {
53
54   private static final String BAR_DELIMITER = "|";
55
56   private static org.exolab.castor.mapping.Mapping map;
57
58   /**
59    * Constructor
60    */
61   public Uniprot()
62   {
63     super();
64     addDbSourceProperty(DBRefSource.SEQDB, DBRefSource.SEQDB);
65     addDbSourceProperty(DBRefSource.PROTSEQDB);
66   }
67
68   /*
69    * (non-Javadoc)
70    * 
71    * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
72    */
73   @Override
74   public String getAccessionSeparator()
75   {
76     return null;
77   }
78
79   /*
80    * (non-Javadoc)
81    * 
82    * @see jalview.ws.DbSourceProxy#getAccessionValidator()
83    */
84   @Override
85   public Regex getAccessionValidator()
86   {
87     return new Regex("([A-Z]+[0-9]+[A-Z0-9]+|[A-Z0-9]+_[A-Z0-9]+)");
88   }
89
90   /*
91    * (non-Javadoc)
92    * 
93    * @see jalview.ws.DbSourceProxy#getDbSource()
94    */
95   @Override
96   public String getDbSource()
97   {
98     return DBRefSource.UNIPROT;
99   }
100
101   /*
102    * (non-Javadoc)
103    * 
104    * @see jalview.ws.DbSourceProxy#getDbVersion()
105    */
106   @Override
107   public String getDbVersion()
108   {
109     return "0"; // we really don't know what version we're on.
110   }
111
112   /**
113    * Reads a file containing the reply to the EBI Fetch Uniprot data query,
114    * unmarshals it to a UniprotFile object, and returns the list of UniprotEntry
115    * data models (mapped from &lt;entry&gt; elements)
116    * 
117    * @param fileReader
118    * @return
119    */
120   public Vector<UniprotEntry> getUniprotEntries(Reader fileReader)
121   {
122     UniprotFile uni = new UniprotFile();
123     try
124     {
125       if (map == null)
126       {
127         // 1. Load the mapping information from the file
128         map = new org.exolab.castor.mapping.Mapping(uni.getClass()
129                 .getClassLoader());
130         java.net.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       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",
168               null);
169       Vector<UniprotEntry> entries = getUniprotEntries(new FileReader(file));
170
171       if (entries != null)
172       {
173         ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
174         for (UniprotEntry entry : entries)
175         {
176           seqs.add(uniprotEntryToSequenceI(entry));
177         }
178         al = new Alignment(seqs.toArray(new SequenceI[0]));
179
180       }
181       stopQuery();
182       return al;
183     } catch (Exception e)
184     {
185       stopQuery();
186       throw (e);
187     }
188   }
189
190   /**
191    * 
192    * @param entry
193    *          UniprotEntry
194    * @return SequenceI instance created from the UniprotEntry instance
195    */
196   public SequenceI uniprotEntryToSequenceI(UniprotEntry entry){
197     String id = getUniprotEntryId(entry);
198     SequenceI sequence = new Sequence(id, entry.getUniprotSequence()
199             .getContent());
200     sequence.setDescription(getUniprotEntryDescription(entry));
201
202     final String dbVersion = getDbVersion();
203     ArrayList<DBRefEntry> dbRefs = new ArrayList<DBRefEntry>();
204     for (String accessionId : entry.getAccession())
205     {
206       DBRefEntry dbRef = new DBRefEntry(DBRefSource.UNIPROT, dbVersion,
207               accessionId);
208       dbRefs.add(dbRef);
209     }
210     sequence.setSourceDBRef((dbRefs != null && dbRefs.size() > 0) ? dbRefs
211             .get(0) : null);
212
213     Vector<PDBEntry> onlyPdbEntries = new Vector<PDBEntry>();
214     for (PDBEntry pdb : entry.getDbReference())
215     {
216       DBRefEntry dbr = new DBRefEntry();
217       dbr.setSource(pdb.getType());
218       dbr.setAccessionId(pdb.getId());
219       dbr.setVersion(DBRefSource.UNIPROT + ":" + dbVersion);
220       dbRefs.add(dbr);
221       if ("PDB".equals(pdb.getType()))
222       {
223         onlyPdbEntries.addElement(pdb);
224       }
225     }
226
227     sequence.setPDBId(onlyPdbEntries);
228     if (entry.getFeature() != null)
229     {
230       for (SequenceFeature sf : entry.getFeature())
231       {
232         sf.setFeatureGroup("Uniprot");
233         sequence.addSequenceFeature(sf);
234       }
235     }
236     sequence.setDBRefs(dbRefs.toArray(new DBRefEntry[0]));
237     return sequence;
238   }
239
240   /**
241    * 
242    * @param entry
243    *          UniportEntry
244    * @return protein name(s) delimited by a white space character
245    */
246   public static String getUniprotEntryDescription(UniprotEntry entry)
247   {
248     StringBuilder desc = new StringBuilder(32);
249     if (entry.getProtein() != null && entry.getProtein().getName() != null)
250     {
251       for (String nm : entry.getProtein().getName())
252       {
253         desc.append(nm).append(" ");
254       }
255     }
256     return desc.toString();
257   }
258
259   /**
260    *
261    * @param entry
262    *          UniportEntry
263    * @return The accession id(s) and name(s) delimited by '|'.
264    */
265   public static String getUniprotEntryId(UniprotEntry entry)
266   {
267     StringBuilder name = new StringBuilder(32);
268     name.append("UniProt/Swiss-Prot");
269     for (String accessionId : entry.getAccession())
270     {
271       name.append(BAR_DELIMITER);
272       name.append(accessionId);
273     }
274     for (String n : entry.getName())
275     {
276       name.append(BAR_DELIMITER);
277       name.append(n);
278     }
279     return name.toString();
280   }
281
282   /*
283    * (non-Javadoc)
284    * 
285    * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
286    */
287   @Override
288   public boolean isValidReference(String accession)
289   {
290     // TODO: make the following a standard validator
291     return (accession == null || accession.length() < 2) ? false
292             : getAccessionValidator().search(accession);
293   }
294
295   /**
296    * return LDHA_CHICK uniprot entry
297    */
298   @Override
299   public String getTestQuery()
300   {
301     return "P00340";
302   }
303
304   @Override
305   public String getDbName()
306   {
307     return "Uniprot"; // getDbSource();
308   }
309
310   @Override
311   public int getTier()
312   {
313     return 0;
314   }
315 }