f7773d883e3207c08b3cfda1e0eace6a13756943
[jalview.git] / src / jalview / ws / dbsources / Uniprot.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 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 null;
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       Alignment al = null;
142       ebi = new EBIFetchClient();
143       StringBuffer result = new StringBuffer();
144       // uniprotxml parameter required since december 2007
145       File file = ebi.fetchDataAsFile("uniprot:" + queries, "uniprotxml",
146               null);
147       Vector entries = getUniprotEntries(file);
148
149       if (entries != null)
150       {
151         // First, make the new sequences
152         Enumeration en = entries.elements();
153         while (en.hasMoreElements())
154         {
155           UniprotEntry entry = (UniprotEntry) en.nextElement();
156
157           StringBuffer name = new StringBuffer(">UniProt/Swiss-Prot");
158           Enumeration en2 = entry.getAccession().elements();
159           while (en2.hasMoreElements())
160           {
161             name.append("|");
162             name.append(en2.nextElement());
163           }
164           en2 = entry.getName().elements();
165           while (en2.hasMoreElements())
166           {
167             name.append("|");
168             name.append(en2.nextElement());
169           }
170
171           if (entry.getProtein() != null
172                   && entry.getProtein().getName() != null)
173           {
174             for (int nm = 0, nmSize = entry.getProtein().getName().size(); nm < nmSize; nm++)
175             {
176               name.append(" " + entry.getProtein().getName().elementAt(nm));
177             }
178           }
179
180           result.append(name + "\n"
181                   + entry.getUniprotSequence().getContent() + "\n");
182
183         }
184
185         // Then read in the features and apply them to the dataset
186         al = parseResult(result.toString());
187         if (al != null)
188         {
189           // Decorate the alignment with database entries.
190           addUniprotXrefs(al, entries);
191         }
192         else
193         {
194           results = result;
195         }
196       }
197       stopQuery();
198       return al;
199     } catch (Exception e)
200     {
201       stopQuery();
202       throw (e);
203     }
204   }
205
206   /**
207    * add an ordered set of UniprotEntry objects to an ordered set of seuqences.
208    * 
209    * @param al -
210    *                a sequence of n sequences
211    * @param entries
212    *                a seuqence of n uniprot entries to be analysed.
213    */
214   public void addUniprotXrefs(Alignment al, Vector entries)
215   {
216     for (int i = 0; i < entries.size(); i++)
217     {
218       UniprotEntry entry = (UniprotEntry) entries.elementAt(i);
219       Enumeration e = entry.getDbReference().elements();
220       Vector onlyPdbEntries = new Vector();
221       Vector dbxrefs = new Vector();
222       while (e.hasMoreElements())
223       {
224         PDBEntry pdb = (PDBEntry) e.nextElement();
225         DBRefEntry dbr = new DBRefEntry();
226         dbr.setSource(pdb.getType());
227         dbr.setAccessionId(pdb.getId());
228         dbr.setVersion(DBRefSource.UNIPROT + ":" + getDbVersion());
229         dbxrefs.addElement(dbr);
230         if (!pdb.getType().equals("PDB"))
231         {
232           continue;
233         }
234
235         onlyPdbEntries.addElement(pdb);
236       }
237       SequenceI sq = al.getSequenceAt(i);
238       while (sq.getDatasetSequence() != null)
239       {
240         sq = sq.getDatasetSequence();
241       }
242
243       Enumeration en2 = entry.getAccession().elements();
244       while (en2.hasMoreElements())
245       {
246         // we always add as uniprot if we retrieved from uniprot or uniprot name
247         sq.addDBRef(new DBRefEntry(DBRefSource.UNIPROT, getDbVersion(), en2
248                 .nextElement().toString()));
249       }
250       en2 = dbxrefs.elements();
251       while (en2.hasMoreElements())
252       {
253         // we always add as uniprot if we retrieved from uniprot or uniprot name
254         sq.addDBRef((DBRefEntry) en2.nextElement());
255
256       }
257       sq.setPDBId(onlyPdbEntries);
258       if (entry.getFeature() != null)
259       {
260         e = entry.getFeature().elements();
261         while (e.hasMoreElements())
262         {
263           SequenceFeature sf = (SequenceFeature) e.nextElement();
264           sf.setFeatureGroup("Uniprot");
265           sq.addSequenceFeature(sf);
266         }
267       }
268     }
269   }
270
271   /*
272    * (non-Javadoc)
273    * 
274    * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
275    */
276   public boolean isValidReference(String accession)
277   {
278     return true;
279   }
280
281   /**
282    * return LDHA_CHICK uniprot entry
283    */
284   public String getTestQuery()
285   {
286     return "P00340";
287   }
288
289   public String getDbName()
290   {
291     return "Uniprot"; // getDbSource();
292   }
293 }