applied copyright 2008
[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     super();
56     addDbSourceProperty(DBRefSource.SEQDB, DBRefSource.SEQDB);
57     addDbSourceProperty(DBRefSource.PROTSEQDB);
58 //    addDbSourceProperty(DBRefSource.MULTIACC, new Integer(50));
59   }
60
61   /*
62    * (non-Javadoc)
63    * 
64    * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
65    */
66   public String getAccessionSeparator()
67   {
68     return null; // ";";
69   }
70
71   /*
72    * (non-Javadoc)
73    * 
74    * @see jalview.ws.DbSourceProxy#getAccessionValidator()
75    */
76   public Regex getAccessionValidator()
77   {
78     return null;
79   }
80
81   /*
82    * (non-Javadoc)
83    * 
84    * @see jalview.ws.DbSourceProxy#getDbSource()
85    */
86   public String getDbSource()
87   {
88     return DBRefSource.UNIPROT;
89   }
90
91   /*
92    * (non-Javadoc)
93    * 
94    * @see jalview.ws.DbSourceProxy#getDbVersion()
95    */
96   public String getDbVersion()
97   {
98     return "0"; // we really don't know what version we're on.
99   }
100
101   private EBIFetchClient ebi = null;
102
103   public Vector getUniprotEntries(File file)
104   {
105     UniprotFile uni = new UniprotFile();
106     try
107     {
108       // 1. Load the mapping information from the file
109       org.exolab.castor.mapping.Mapping map = new org.exolab.castor.mapping.Mapping(uni.getClass().getClassLoader());
110       java.net.URL url = getClass().getResource("/uniprot_mapping.xml");
111       map.loadMapping(url);
112
113       // 2. Unmarshal the data
114       Unmarshaller unmar = new Unmarshaller(uni);
115       unmar.setIgnoreExtraElements(true);
116       unmar.setMapping(map);
117
118       uni = (UniprotFile) unmar.unmarshal(new FileReader(file));
119     }
120     catch (Exception e)
121     {
122       System.out.println("Error getUniprotEntries() " + e);
123     }
124
125     return uni.getUniprotEntries();
126   }
127
128   /*
129    * (non-Javadoc)
130    * 
131    * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
132    */
133   public AlignmentI getSequenceRecords(String queries) throws Exception
134   {
135     startQuery();
136     try
137     {
138       Alignment al=null;
139       ebi = new EBIFetchClient();
140       StringBuffer result=new StringBuffer();
141       // uniprotxml parameter required since december 2007
142       File file = ebi.fetchDataAsFile("uniprot:" + queries, "uniprotxml", null);
143       Vector entries = getUniprotEntries(file);
144
145       if (entries != null)
146       {
147         // First, make the new sequences
148         Enumeration en = entries.elements();
149         while (en.hasMoreElements())
150         {
151           UniprotEntry entry = (UniprotEntry) en.nextElement();
152
153           StringBuffer name = new StringBuffer(">UniProt/Swiss-Prot");
154           Enumeration en2 = entry.getAccession().elements();
155           while (en2.hasMoreElements())
156           {
157             name.append("|");
158             name.append(en2.nextElement());
159           }
160           en2 = entry.getName().elements();
161           while (en2.hasMoreElements())
162           {
163             name.append("|");
164             name.append(en2.nextElement());
165           }
166
167           if (entry.getProtein()!=null && entry.getProtein().getName()!=null)
168           {
169               for (int nm=0,nmSize=entry.getProtein().getName().size(); nm<nmSize;nm++)
170               {
171                 name.append(" " + entry.getProtein().getName().elementAt(nm));
172               }
173           }
174
175           result.append(name + "\n"
176                   + entry.getUniprotSequence().getContent() + "\n");
177
178         }
179
180         // Then read in the features and apply them to the dataset
181         al = parseResult(result.toString());
182         if (al!=null)
183         {
184           // Decorate the alignment with database entries.
185           addUniprotXrefs(al, entries);
186         } else {
187           results = result;
188         }
189       }
190       stopQuery();
191       return al;
192     } catch (Exception e)
193     {
194       stopQuery();
195       throw(e);
196     }
197   }
198
199   /**
200    * add an ordered set of UniprotEntry objects to an ordered set of seuqences.
201    * 
202    * @param al -
203    *          a sequence of n sequences
204    * @param entries
205    *          a seuqence of n uniprot entries to be analysed.
206    */
207   public void addUniprotXrefs(Alignment al, Vector entries)
208   {
209     for (int i = 0; i < entries.size(); i++)
210     {
211       UniprotEntry entry = (UniprotEntry) entries.elementAt(i);
212       Enumeration e = entry.getDbReference().elements();
213       Vector onlyPdbEntries = new Vector();
214       Vector dbxrefs = new Vector();
215       while (e.hasMoreElements())
216       {
217         PDBEntry pdb = (PDBEntry) e.nextElement();
218         DBRefEntry dbr = new DBRefEntry();
219         dbr.setSource(pdb.getType());
220         dbr.setAccessionId(pdb.getId());
221         dbr.setVersion(DBRefSource.UNIPROT+":"+getDbVersion());
222         dbxrefs.addElement(dbr);
223         if (!pdb.getType().equals("PDB"))
224         {
225           continue;
226         }
227         
228         onlyPdbEntries.addElement(pdb);
229       }
230       SequenceI sq = al.getSequenceAt(i);
231       while (sq.getDatasetSequence()!=null)
232       {
233         sq = sq.getDatasetSequence();
234       }
235
236       Enumeration en2 = entry.getAccession().elements();
237       while (en2.hasMoreElements())
238       {
239         // we always add as uniprot if we retrieved from uniprot or uniprot name
240         sq.addDBRef(
241                 new DBRefEntry(DBRefSource.UNIPROT, getDbVersion(), en2.nextElement()
242                         .toString()));
243       }
244       en2 = dbxrefs.elements();
245       while (en2.hasMoreElements())
246       {
247      // we always add as uniprot if we retrieved from uniprot or uniprot name
248         sq.addDBRef((DBRefEntry) en2.nextElement());
249                     
250       }
251       sq.setPDBId(onlyPdbEntries);
252       if (entry.getFeature() != null)
253       {
254         e = entry.getFeature().elements();
255         while (e.hasMoreElements())
256         {
257           SequenceFeature sf = (SequenceFeature) e.nextElement();
258           sf.setFeatureGroup("Uniprot");
259           sq.addSequenceFeature(sf);
260         }
261       }
262     }
263   }
264
265   /*
266    * (non-Javadoc)
267    * 
268    * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
269    */
270   public boolean isValidReference(String accession)
271   {
272     return true;
273   }
274   /**
275    * return LDHA_CHICK uniprot entry
276    */
277   public String getTestQuery()
278   {
279     return "P00340";
280   }
281   public String getDbName()
282   {
283     return "Uniprot"; // getDbSource();
284   }
285 }