2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
21 import jalview.datamodel.*;
\r
23 import jalview.gui.*;
\r
29 import org.exolab.castor.mapping.Mapping;
\r
31 import org.exolab.castor.xml.*;
\r
32 import jalview.analysis.AlignSeq;
\r
40 * @version $Revision$
\r
42 public class DBRefFetcher implements Runnable
\r
46 CutAndPasteTransfer output = new CutAndPasteTransfer();
\r
47 StringBuffer sbuffer = new StringBuffer();
\r
48 boolean running = false;
\r
50 ///This will be a collection of Vectors of sequenceI refs.
\r
51 //The key will be the seq name or accession id of the seq
\r
54 public DBRefFetcher()
\r
57 public Vector getUniprotEntries(File file)
\r
59 UniprotFile uni = new UniprotFile();
\r
62 // 1. Load the mapping information from the file
\r
63 Mapping map = new Mapping(uni.getClass().getClassLoader());
\r
64 java.net.URL url = getClass().getResource("/uniprot_mapping.xml");
\r
65 map.loadMapping(url);
\r
67 // 2. Unmarshal the data
\r
68 Unmarshaller unmar = new Unmarshaller(uni);
\r
69 unmar.setIgnoreExtraElements(true);
\r
70 unmar.setMapping(map);
\r
72 uni = (UniprotFile) unmar.unmarshal(new FileReader(file));
\r
76 System.out.println("Error getUniprotEntries() "+e);
\r
80 return uni.getUniprotEntries();
\r
84 * Creates a new SequenceFeatureFetcher object.
\r
86 * @param align DOCUMENT ME!
\r
87 * @param ap DOCUMENT ME!
\r
89 public DBRefFetcher(AlignmentI align, AlignFrame af)
\r
92 this.dataset = align.getDataset();
\r
95 public boolean fetchDBRefs(boolean waitTillFinished)
\r
97 Thread thread = new Thread(this);
\r
101 if(waitTillFinished)
\r
107 }catch(Exception ex){}
\r
115 * The sequence will be added to a vector of sequences
\r
116 * belonging to key which could be either seq name or dbref id
\r
117 * @param seq SequenceI
\r
118 * @param key String
\r
120 void addSeqId(SequenceI seq, String key)
\r
122 key = key.toUpperCase();
\r
125 if(seqRefs.containsKey(key))
\r
127 seqs = (Vector)seqRefs.get(key);
\r
129 if(seqs!=null && !seqs.contains(seq))
\r
131 seqs.addElement(seq);
\r
133 else if(seqs==null)
\r
135 seqs = new Vector();
\r
136 seqs.addElement(seq);
\r
142 seqs = new Vector();
\r
143 seqs.addElement(seq);
\r
146 seqRefs.put(key, seqs);
\r
155 long startTime = System.currentTimeMillis();
\r
156 af.setProgressBar("Fetching db refs", startTime);
\r
159 seqRefs = new Hashtable();
\r
164 Vector sequences = dataset.getSequences();
\r
166 while (seqIndex < sequences.size())
\r
168 StringBuffer queryString = new StringBuffer("uniprot:");
\r
170 for (int i = 0; (seqIndex < sequences.size()) && (i < 50);
\r
173 Sequence sequence = (Sequence) sequences.get(seqIndex);
\r
174 DBRefEntry [] uprefs = jalview.util.DBRefUtils.selectRefs(sequence.getDBRef(), new String[] {
\r
175 jalview.datamodel.DBRefSource.UNIPROT});
\r
178 if(uprefs.length+i>50)
\r
181 for(int j=0; j<uprefs.length; j++)
\r
183 addSeqId(sequence, uprefs[j].getAccessionId());
\r
184 queryString.append(uprefs[j].getAccessionId()+";");
\r
189 StringTokenizer st = new StringTokenizer(sequence.getName(), "|");
\r
190 if(st.countTokens()+i>50)
\r
192 //Dont send more than 50 id strings to dbFetch!!
\r
197 while(st.hasMoreTokens())
\r
199 String token = st.nextToken();
\r
200 addSeqId(sequence, token);
\r
201 queryString.append(token+";");
\r
207 ///////////////////////////////////
\r
209 EBIFetchClient ebi = new EBIFetchClient();
\r
210 File file = ebi.fetchDataAsFile(queryString.toString(), "xml", "raw");
\r
213 ReadUniprotFile(file);
\r
217 catch (Exception ex)
\r
219 ex.printStackTrace();
\r
222 if (sbuffer.length() > 0)
\r
225 "Your sequences have been matched to Uniprot. Some of the ids have been\n" +
\r
226 "altered, most likely the start/end residue will have been updated.\n" +
\r
227 "Save your alignment to maintain the updated id.\n\n" +
\r
228 sbuffer.toString());
\r
229 Desktop.addInternalFrame(output, "Sequence names updated ", 600, 300);
\r
230 // The above is the dataset, we must now find out the index
\r
231 // of the viewed sequence
\r
235 af.setProgressBar("DBRef search completed", startTime);
\r
236 // promptBeforeBlast();
\r
246 * @param result DOCUMENT ME!
\r
247 * @param out DOCUMENT ME!
\r
248 * @param align DOCUMENT ME!
\r
250 void ReadUniprotFile(File file)
\r
252 if (!file.exists())
\r
255 SequenceI sequence = null;
\r
257 Vector entries = getUniprotEntries(file);
\r
259 int i, iSize = entries == null ? 0 : entries.size();
\r
260 UniprotEntry entry;
\r
261 for (i = 0; i < iSize; i++)
\r
263 entry = (UniprotEntry) entries.elementAt(i);
\r
265 //Work out which sequences this Uniprot file has matches to,
\r
266 //taking into account all accessionIds and names in the file
\r
267 Vector sequenceMatches = new Vector();
\r
268 for (int j = 0; j < entry.getAccession().size(); j++)
\r
270 String accessionId = entry.getAccession().elementAt(j).toString();
\r
271 if (seqRefs.containsKey(accessionId))
\r
273 Vector seqs = (Vector) seqRefs.get(accessionId);
\r
274 for (int jj = 0; jj < seqs.size(); jj++)
\r
276 sequence = (SequenceI) seqs.elementAt(jj);
\r
277 if (!sequenceMatches.contains(sequence))
\r
278 sequenceMatches.addElement(sequence);
\r
282 for (int j = 0; j < entry.getName().size(); j++)
\r
284 String name = entry.getName().elementAt(j).toString();
\r
285 if (seqRefs.containsKey(name))
\r
287 Vector seqs = (Vector) seqRefs.get(name);
\r
288 for (int jj = 0; jj < seqs.size(); jj++)
\r
290 sequence = (SequenceI) seqs.elementAt(jj);
\r
291 if (!sequenceMatches.contains(sequence))
\r
292 sequenceMatches.addElement(sequence);
\r
297 for (int m = 0; m < sequenceMatches.size(); m++)
\r
299 sequence = (SequenceI) sequenceMatches.elementAt(m);
\r
300 sequence.addDBRef(new DBRefEntry(DBRefSource.UNIPROT,
\r
302 entry.getAccession().elementAt(0).
\r
305 System.out.println("Adding dbref to " + sequence.getName() + " : " +
\r
306 entry.getAccession().elementAt(0).toString());
\r
308 String nonGapped = AlignSeq.extractGaps("-. ",
\r
309 sequence.getSequenceAsString()).
\r
312 int absStart = entry.getUniprotSequence().getContent().indexOf(
\r
313 nonGapped.toString());
\r
315 if (absStart == -1)
\r
317 // Is UniprotSequence contained in dataset sequence?
\r
318 absStart = nonGapped.toString().indexOf(entry.getUniprotSequence().
\r
320 if (absStart == -1)
\r
322 sbuffer.append(sequence.getName() + " SEQUENCE NOT %100 MATCH \n");
\r
326 if (entry.getFeature() != null)
\r
328 Enumeration e = entry.getFeature().elements();
\r
329 while (e.hasMoreElements())
\r
331 SequenceFeature sf = (SequenceFeature) e.nextElement();
\r
332 sf.setBegin(sf.getBegin() + absStart + 1);
\r
333 sf.setEnd(sf.getEnd() + absStart + 1);
\r
336 sbuffer.append(sequence.getName() +
\r
337 " HAS " + absStart +
\r
338 " PREFIXED RESIDUES COMPARED TO UNIPROT - ANY SEQUENCE FEATURES"
\r
339 + " HAVE BEEN ADJUSTED ACCORDINGLY \n");
\r
345 //unknownSequences.remove(sequence);
\r
347 int absEnd = absStart + nonGapped.toString().length();
\r
350 Enumeration e = entry.getDbReference().elements();
\r
351 Vector onlyPdbEntries = new Vector();
\r
352 while (e.hasMoreElements())
\r
354 PDBEntry pdb = (PDBEntry) e.nextElement();
\r
355 if (!pdb.getType().equals(DBRefSource.PDB))
\r
358 sequence.addDBRef(new DBRefEntry(DBRefSource.PDB,
\r
362 onlyPdbEntries.addElement(pdb);
\r
365 sequence.setPDBId(onlyPdbEntries);
\r
367 sequence.setStart(absStart);
\r
368 sequence.setEnd(absEnd);
\r