regex validator for uniprot
[jalview.git] / src / jalview / ws / dbsources / Uniprot.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3  * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.ws.dbsources;
20
21 import java.io.File;
22 import java.io.FileReader;
23 import java.io.IOException;
24 import java.util.Enumeration;
25 import java.util.Hashtable;
26 import java.util.Vector;
27
28 import org.exolab.castor.xml.Unmarshaller;
29
30 import com.stevesoft.pat.Regex;
31
32 import jalview.datamodel.Alignment;
33 import jalview.datamodel.AlignmentI;
34 import jalview.datamodel.DBRefEntry;
35 import jalview.datamodel.DBRefSource;
36 import jalview.datamodel.PDBEntry;
37 import jalview.datamodel.SequenceFeature;
38 import jalview.datamodel.SequenceI;
39 import jalview.datamodel.UniprotEntry;
40 import jalview.datamodel.UniprotFile;
41 import jalview.io.FormatAdapter;
42 import jalview.io.IdentifyFile;
43 import jalview.ws.DBRefFetcher;
44 import jalview.ws.ebi.EBIFetchClient;
45 import jalview.ws.seqfetcher.DbSourceProxy;
46 import jalview.ws.seqfetcher.DbSourceProxyImpl;
47
48 /**
49  * @author JimP
50  * 
51  */
52 public class Uniprot extends DbSourceProxyImpl implements DbSourceProxy
53 {
54   public Uniprot()
55   {
56     super();
57     addDbSourceProperty(DBRefSource.SEQDB, DBRefSource.SEQDB);
58     addDbSourceProperty(DBRefSource.PROTSEQDB);
59     // addDbSourceProperty(DBRefSource.MULTIACC, new Integer(50));
60   }
61
62   /*
63    * (non-Javadoc)
64    * 
65    * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
66    */
67   public String getAccessionSeparator()
68   {
69     return null; // ";";
70   }
71
72   /*
73    * (non-Javadoc)
74    * 
75    * @see jalview.ws.DbSourceProxy#getAccessionValidator()
76    */
77   public Regex getAccessionValidator()
78   {
79     return new Regex("([A-Z]+[0-9]+[A-Z0-9]+|[A-Z0-9]+_[A-Z0-9]+)");
80   }
81
82   /*
83    * (non-Javadoc)
84    * 
85    * @see jalview.ws.DbSourceProxy#getDbSource()
86    */
87   public String getDbSource()
88   {
89     return DBRefSource.UNIPROT;
90   }
91
92   /*
93    * (non-Javadoc)
94    * 
95    * @see jalview.ws.DbSourceProxy#getDbVersion()
96    */
97   public String getDbVersion()
98   {
99     return "0"; // we really don't know what version we're on.
100   }
101
102   private EBIFetchClient ebi = null;
103
104   public Vector getUniprotEntries(File file)
105   {
106     UniprotFile uni = new UniprotFile();
107     try
108     {
109       // 1. Load the mapping information from the file
110       org.exolab.castor.mapping.Mapping map = new org.exolab.castor.mapping.Mapping(
111               uni.getClass().getClassLoader());
112       java.net.URL url = getClass().getResource("/uniprot_mapping.xml");
113       map.loadMapping(url);
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("(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 : getAccessionValidator().search(accession);
282   }
283
284   /**
285    * return LDHA_CHICK uniprot entry
286    */
287   public String getTestQuery()
288   {
289     return "P00340";
290   }
291
292   public String getDbName()
293   {
294     return "Uniprot"; // getDbSource();
295   }
296 }