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