2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 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
47 ArrayList unknownSequences;
\r
48 CutAndPasteTransfer output = new CutAndPasteTransfer();
\r
49 StringBuffer sbuffer = new StringBuffer();
\r
50 boolean uniprotFlag = false;
\r
51 boolean running = false;
\r
53 public DBRefFetcher()
\r
56 public Vector getUniprotEntries(File file)
\r
58 UniprotFile uni = new UniprotFile();
\r
61 // 1. Load the mapping information from the file
\r
62 Mapping map = new Mapping(uni.getClass().getClassLoader());
\r
63 java.net.URL url = getClass().getResource("/uniprot_mapping.xml");
\r
64 map.loadMapping(url);
\r
66 // 2. Unmarshal the data
\r
67 Unmarshaller unmar = new Unmarshaller(uni);
\r
68 unmar.setIgnoreExtraElements(true);
\r
69 unmar.setMapping(map);
\r
70 // unmar.setDebug(true);
\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 unknownSequences = new ArrayList();
\r
94 this.dataset = align.getDataset();
\r
97 public boolean fetchDBRefs(boolean waitTillFinished)
\r
99 Thread thread = new Thread(this);
\r
103 if(waitTillFinished)
\r
109 }catch(Exception ex){}
\r
121 long startTime = System.currentTimeMillis();
\r
122 af.setProgressBar("Fetching db refs", startTime);
\r
128 Vector sequences = dataset.getSequences();
\r
130 while (seqIndex < sequences.size())
\r
132 Vector ids = new Vector();
\r
134 for (int i = 0; (seqIndex < sequences.size()) && (i < 50);
\r
137 Sequence sequence = (Sequence) sequences.get(seqIndex);
\r
138 DBRefEntry [] uprefs = jalview.util.DBRefUtils.selectRefs(sequence.getDBRef(), new String[] {
\r
139 jalview.datamodel.DBRefSource.UNIPROT});
\r
142 // we know the id for this entry, so don't note its ID in the unknownSequences list
\r
143 for (int j=0,k=uprefs.length; j<k; j++)
\r
144 ids.add(uprefs[j].getAccessionId());
\r
145 unknownSequences.add(sequence);
\r
147 if (!ids.contains(sequence.getName()))
\r
149 ids.add(sequence.getName());
\r
150 unknownSequences.add(sequence);
\r
155 ///////////////////////////////////
\r
157 if (ids.size() > 0)
\r
159 StringBuffer remainingIds = new StringBuffer("uniprot:");
\r
160 for (int i = 0; i < ids.size(); i++)
\r
162 if(ids.get(i).toString().indexOf("|")>-1)
\r
164 remainingIds.append(ids.get(i).toString().substring(
\r
165 ids.get(i).toString().lastIndexOf("|") + 1));
\r
166 uniprotFlag = true;
\r
169 remainingIds.append(ids.get(i));
\r
171 remainingIds.append(";");
\r
174 EBIFetchClient ebi = new EBIFetchClient();
\r
175 File file = ebi.fetchDataAsFile(remainingIds.toString(),
\r
182 ReadUniprotFile(file, ids);
\r
187 catch (Exception ex)
\r
189 ex.printStackTrace();
\r
192 if (sbuffer.length() > 0)
\r
195 "Your sequences have been matched to Uniprot. Some of the ids have been\n" +
\r
196 "altered, most likely the start/end residue will have been updated.\n" +
\r
197 "Save your alignment to maintain the updated id.\n\n" +
\r
198 sbuffer.toString());
\r
199 Desktop.addInternalFrame(output, "Sequence names updated ", 600, 300);
\r
200 // The above is the dataset, we must now find out the index
\r
201 // of the viewed sequence
\r
205 af.setProgressBar("DBRef search completed", startTime);
\r
206 // promptBeforeBlast();
\r
213 void promptBeforeBlast()
\r
215 // This must be outside the run() body as java 1.5
\r
216 // will not return any value from the OptionPane to the expired thread.
\r
217 if (unknownSequences.size() > 0)
\r
219 // int reply = javax.swing.JOptionPane.showConfirmDialog(
\r
220 // Desktop.desktop, "Couldn't find a match for "+unknownSequences.size()+" sequences."
\r
221 // +"\nPerform blast for unknown sequences?",
\r
222 // "Blast for Unidentified Sequences",
\r
223 // javax.swing.JOptionPane.YES_NO_OPTION, javax.swing.JOptionPane.QUESTION_MESSAGE);
\r
224 javax.swing.JOptionPane.showMessageDialog(
\r
225 Desktop.desktop, "Couldn't find a match for "+unknownSequences.size()+" sequences.",
\r
226 "Unidentified Sequences",
\r
227 javax.swing.JOptionPane.WARNING_MESSAGE);
\r
230 // if(reply == javax.swing.JOptionPane.YES_OPTION)
\r
231 // new WSWUBlastClient(ap, align, unknownSequences);
\r
238 * @param result DOCUMENT ME!
\r
239 * @param out DOCUMENT ME!
\r
240 * @param align DOCUMENT ME!
\r
242 void ReadUniprotFile(File file, Vector ids)
\r
247 SequenceI sequence = null;
\r
249 Vector entries = getUniprotEntries(file);
\r
251 int i, iSize = entries==null?0:entries.size();
\r
252 UniprotEntry entry;
\r
253 for (i = 0; i < iSize; i++)
\r
255 entry = (UniprotEntry) entries.elementAt(i);
\r
256 String idmatch = entry.getAccession().elementAt(0).toString();
\r
257 sequence = dataset.findName(idmatch);
\r
259 if (sequence == null)
\r
261 //Sequence maybe Name, not Accession
\r
262 idmatch = entry.getName().elementAt(0).toString();
\r
263 sequence = dataset.findName(idmatch);
\r
267 ids.remove(sequence.getName());
\r
269 else if (sequence == null && uniprotFlag)
\r
271 StringBuffer upid = new StringBuffer("UniProt/Swiss-Prot|");
\r
272 for(int u=0; u<entry.getAccession().size(); u++)
\r
273 upid.append(entry.getAccession().elementAt(u)+"|");
\r
275 sequence = dataset.findName(upid+idmatch);
\r
276 ids.remove(idmatch);
\r
279 if(sequence ==null)
\r
281 System.out.println(idmatch+" not found");
\r
286 sequence.addDBRef(new DBRefEntry(DBRefSource.UNIPROT,
\r
288 entry.getAccession().elementAt(0).toString()));
\r
290 System.out.println("Adding dbref to "+sequence.getName()+" : "+
\r
291 entry.getAccession().elementAt(0).toString());
\r
293 String nonGapped = AlignSeq.extractGaps("-. ", sequence.getSequence());
\r
295 int absStart = entry.getUniprotSequence().getContent().indexOf(
\r
296 nonGapped.toString());
\r
298 if (absStart == -1)
\r
300 // Is UniprotSequence contained in dataset sequence?
\r
301 absStart = nonGapped.toString().indexOf(entry.getUniprotSequence().getContent());
\r
304 sbuffer.append(sequence.getName() +
\r
305 " SEQUENCE NOT %100 MATCH \n");
\r
310 if (entry.getFeature() != null)
\r
312 Enumeration e = entry.getFeature().elements();
\r
313 while (e.hasMoreElements())
\r
315 SequenceFeature sf = (SequenceFeature) e.nextElement();
\r
316 sf.setBegin(sf.getBegin() + absStart + 1);
\r
317 sf.setEnd(sf.getEnd() + absStart + 1);
\r
320 sbuffer.append(sequence.getName() +
\r
321 " HAS " + absStart +
\r
322 " PREFIXED RESIDUES COMPARED TO UNIPROT - ANY SEQUENCE FEATURES"
\r
323 + " HAVE BEEN ADJUSTED ACCORDINGLY \n");
\r
329 unknownSequences.remove(sequence);
\r
331 int absEnd = absStart + nonGapped.toString().length();
\r
334 Enumeration e = entry.getDbReference().elements();
\r
335 Vector onlyPdbEntries = new Vector();
\r
336 while(e.hasMoreElements())
\r
338 PDBEntry pdb = (PDBEntry)e.nextElement();
\r
339 if(!pdb.getType().equals(DBRefSource.PDB))
\r
342 sequence.addDBRef(new DBRefEntry(DBRefSource.PDB,
\r
346 onlyPdbEntries.addElement(pdb);
\r
349 sequence.setPDBId(onlyPdbEntries);
\r
351 if (entry.getFeature()!=null) {
\r
352 //e = entry.getFeature().elements();
\r
353 // while (e.hasMoreElements())
\r
355 // SequenceFeature sf = (SequenceFeature) e.nextElement();
\r
356 // sf.setFeatureGroup("Uniprot");
\r
357 // sequence.addSequenceFeature( sf );
\r
360 sequence.setStart(absStart);
\r
361 sequence.setEnd(absEnd);
\r
366 while (n < align.getHeight())
\r
368 //This loop enables multiple sequences with the same
\r
369 //id to have features added and seq limits updated
\r
370 seq2 = align.getSequenceAt(n);
\r
371 if (seq2.getName().equals(idmatch))
\r
373 nonGapped = AlignSeq.extractGaps("-. ", seq2.getSequence());
\r
375 absStart = sequence.getSequence().indexOf(nonGapped);
\r
376 absEnd = absStart + nonGapped.toString().length() - 1;
\r
378 // This is the Viewd alignment sequences
\r
379 // No need to tell the user of the dataset updates
\r
380 if ( (seq2.getStart() != absStart+sequence.getStart())
\r
381 || (seq2.getEnd() != absEnd+sequence.getStart()))
\r
383 sbuffer.append("Updated: " + seq2.getName() + " " +
\r
384 seq2.getStart() + "/" + seq2.getEnd() +
\r
385 " to " + (absStart + sequence.getStart()) + "/" +
\r
386 (absEnd + sequence.getStart()) + "\n");
\r
388 seq2.setStart(absStart + sequence.getStart());
\r
389 seq2.setEnd(absEnd + sequence.getStart());
\r