2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.analysis.AlignSeq;
24 import jalview.bin.Cache;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.DBRefEntry;
27 import jalview.datamodel.DBRefSource;
28 import jalview.datamodel.Mapping;
29 import jalview.datamodel.SequenceI;
30 import jalview.gui.CutAndPasteTransfer;
31 import jalview.gui.DasSourceBrowser;
32 import jalview.gui.Desktop;
33 import jalview.gui.FeatureSettings;
34 import jalview.gui.IProgressIndicator;
35 import jalview.gui.OOMWarning;
36 import jalview.util.DBRefUtils;
37 import jalview.util.MessageManager;
38 import jalview.ws.dbsources.das.api.jalviewSourceI;
39 import jalview.ws.dbsources.das.datamodel.DasSequenceSource;
40 import jalview.ws.seqfetcher.DbSourceProxy;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.Enumeration;
45 import java.util.Hashtable;
46 import java.util.List;
47 import java.util.StringTokenizer;
48 import java.util.Vector;
50 import uk.ac.ebi.picr.model.UPEntry;
51 import uk.ac.ebi.www.picr.AccessionMappingService.AccessionMapperServiceLocator;
54 * Implements a runnable for validating a sequence against external databases
55 * and then propagating references and features onto the sequence(s)
60 public class DBRefFetcher implements Runnable
62 private static final String NEWLINE = System.lineSeparator();
64 public interface FetchFinishedListenerI
71 IProgressIndicator progressWindow;
73 CutAndPasteTransfer output = new CutAndPasteTransfer();
75 boolean running = false;
78 * picr client instance
80 uk.ac.ebi.www.picr.AccessionMappingService.AccessionMapperInterface picrClient = null;
82 // This will be a collection of Vectors of sequenceI refs.
83 // The key will be the seq name or accession id of the seq
84 Hashtable<String, Vector<SequenceI>> seqRefs;
86 DbSourceProxy[] dbSources;
88 SequenceFetcher sfetcher;
90 private List<FetchFinishedListenerI> listeners;
92 private SequenceI[] alseqs;
95 * when true - retrieved sequences will be trimmed to cover longest derived
98 private boolean trimDsSeqs = true;
101 * Creates a new DBRefFetcher object and fetches from the currently selected
102 * set of databases, if this is null then it fetches based on feature settings
105 * fetch references for these SequenceI array
106 * @param progressIndicatorFrame
107 * the frame for progress bar monitoring
109 * array of DbSourceProxy to query references form
110 * @param featureSettings
111 * FeatureSettings to get alternative DbSourceProxy from
112 * @param isNucleotide
113 * indicates if the array of SequenceI are Nucleotides or not
115 public DBRefFetcher(SequenceI[] seqs,
116 IProgressIndicator progressIndicatorFrame,
117 DbSourceProxy[] sources, FeatureSettings featureSettings,
118 boolean isNucleotide)
120 listeners = new ArrayList<FetchFinishedListenerI>();
121 this.progressWindow = progressIndicatorFrame;
122 alseqs = new SequenceI[seqs.length];
123 SequenceI[] ds = new SequenceI[seqs.length];
124 for (int i = 0; i < seqs.length; i++)
127 if (seqs[i].getDatasetSequence() != null)
129 ds[i] = seqs[i].getDatasetSequence();
137 // TODO Jalview 2.5 lots of this code should be in the gui package!
138 sfetcher = jalview.gui.SequenceFetcher
139 .getSequenceFetcherSingleton(progressIndicatorFrame);
140 // set default behaviour for transferring excess sequence data to the
142 trimDsSeqs = Cache.getDefault("TRIM_FETCHED_DATASET_SEQS", true);
145 setDatabaseSources(featureSettings, isNucleotide);
149 // we assume the caller knows what they're doing and ensured that all the
150 // db source names are valid
156 * Helper method to configure the list of database sources to query
158 * @param featureSettings
159 * @param forNucleotide
161 void setDatabaseSources(FeatureSettings featureSettings,
162 boolean forNucleotide)
164 // af.featureSettings_actionPerformed(null);
165 String[] defdb = null;
166 List<DbSourceProxy> selsources = new ArrayList<DbSourceProxy>();
167 Vector<jalviewSourceI> dasselsrc = (featureSettings != null) ? featureSettings
168 .getSelectedSources() : new DasSourceBrowser()
169 .getSelectedSources();
171 for (jalviewSourceI src : dasselsrc)
173 List<DbSourceProxy> sp = src.getSequenceSourceProxies();
176 selsources.addAll(sp);
179 Cache.log.debug("Added many Db Sources for :" + src.getTitle());
183 // select appropriate databases based on alignFrame context.
186 defdb = DBRefSource.DNACODINGDBS;
190 defdb = DBRefSource.PROTEINDBS;
192 List<DbSourceProxy> srces = new ArrayList<DbSourceProxy>();
193 for (String ddb : defdb)
195 List<DbSourceProxy> srcesfordb = sfetcher.getSourceProxy(ddb);
196 if (srcesfordb != null)
198 for (DbSourceProxy src : srcesfordb)
200 if (!srces.contains(src))
202 srces.addAll(srcesfordb);
207 // append the PDB data source, since it is 'special', catering for both
208 // nucleotide and protein
209 // srces.addAll(sfetcher.getSourceProxy(DBRefSource.PDB));
211 srces.addAll(selsources);
212 dbSources = srces.toArray(new DbSourceProxy[srces.size()]);
216 * Constructor with only sequences provided
220 public DBRefFetcher(SequenceI[] sequences)
222 this(sequences, null, null, null, false);
226 * Add a listener to be notified when sequence fetching is complete
230 public void addListener(FetchFinishedListenerI l)
236 * retrieve all the das sequence sources and add them to the list of db
237 * sources to retrieve from
239 public void appendAllDasSources()
241 if (dbSources == null)
243 dbSources = new DbSourceProxy[0];
245 // append additional sources
246 DbSourceProxy[] otherdb = sfetcher
247 .getDbSourceProxyInstances(DasSequenceSource.class);
248 if (otherdb != null && otherdb.length > 0)
250 DbSourceProxy[] newsrc = new DbSourceProxy[dbSources.length
252 System.arraycopy(dbSources, 0, newsrc, 0, dbSources.length);
253 System.arraycopy(otherdb, 0, newsrc, dbSources.length, otherdb.length);
259 * start the fetcher thread
261 * @param waitTillFinished
262 * true to block until the fetcher has finished
264 public void fetchDBRefs(boolean waitTillFinished)
266 // TODO can we not simply write
267 // if (waitTillFinished) { run(); } else { new Thread(this).start(); }
269 Thread thread = new Thread(this);
273 if (waitTillFinished)
280 } catch (Exception ex)
288 * The sequence will be added to a vector of sequences belonging to key which
289 * could be either seq name or dbref id
296 void addSeqId(SequenceI seq, String key)
298 key = key.toUpperCase();
300 Vector<SequenceI> seqs;
301 if (seqRefs.containsKey(key))
303 seqs = seqRefs.get(key);
305 if (seqs != null && !seqs.contains(seq))
307 seqs.addElement(seq);
309 else if (seqs == null)
311 seqs = new Vector<SequenceI>();
312 seqs.addElement(seq);
318 seqs = new Vector<SequenceI>();
319 seqs.addElement(seq);
322 seqRefs.put(key, seqs);
331 if (dbSources == null)
335 .getString("error.implementation_error_must_init_dbsources"));
338 long startTime = System.currentTimeMillis();
339 if (progressWindow != null)
341 progressWindow.setProgressBar(
342 MessageManager.getString("status.fetching_db_refs"),
347 if (Cache.getDefault("DBREFFETCH_USEPICR", false))
349 picrClient = new AccessionMapperServiceLocator()
350 .getAccessionMapperPort();
352 } catch (Exception e)
354 System.err.println("Couldn't locate PICR service instance.\n");
358 Vector<SequenceI> sdataset = new Vector<SequenceI>(
359 Arrays.asList(dataset));
360 List<String> warningMessages = new ArrayList<String>();
363 while (sdataset.size() > 0 && db < dbSources.length)
365 int maxqlen = 1; // default number of queries made at one time
366 System.out.println("Verifying against " + dbSources[db].getDbName());
368 // iterate through db for each remaining un-verified sequence
369 SequenceI[] currSeqs = new SequenceI[sdataset.size()];
370 sdataset.copyInto(currSeqs);// seqs that are to be validated against
372 Vector<String> queries = new Vector<String>(); // generated queries curSeq
373 seqRefs = new Hashtable<String, Vector<SequenceI>>();
377 DbSourceProxy dbsource = dbSources[db];
378 // for moment, we dumbly iterate over all retrieval sources for a
379 // particular database
380 // TODO: introduce multithread multisource queries and logic to remove a
381 // query from other sources if any source for a database returns a
383 maxqlen = dbsource.getMaximumQueryCount();
385 while (queries.size() > 0 || seqIndex < currSeqs.length)
387 if (queries.size() > 0)
389 // Still queries to make for current seqIndex
390 StringBuffer queryString = new StringBuffer("");
392 int nqSize = (maxqlen > queries.size()) ? queries.size()
395 while (queries.size() > 0 && numq < nqSize)
397 String query = queries.elementAt(0);
398 if (dbsource.isValidReference(query))
400 queryString.append((numq == 0) ? "" : dbsource
401 .getAccessionSeparator());
402 queryString.append(query);
405 // remove the extracted query string
406 queries.removeElementAt(0);
408 // make the queries and process the response
409 AlignmentI retrieved = null;
412 if (Cache.log.isDebugEnabled())
414 Cache.log.debug("Querying " + dbsource.getDbName()
415 + " with : '" + queryString.toString() + "'");
417 retrieved = dbsource.getSequenceRecords(queryString.toString());
418 } catch (Exception ex)
420 ex.printStackTrace();
421 } catch (OutOfMemoryError err)
423 new OOMWarning("retrieving database references ("
424 + queryString.toString() + ")", err);
426 if (retrieved != null)
428 transferReferences(sdataset, dbsource.getDbSource(), retrieved,
429 trimDsSeqs, warningMessages);
434 // make some more strings for use as queries
435 for (int i = 0; (seqIndex < dataset.length) && (i < 50); seqIndex++, i++)
437 SequenceI sequence = dataset[seqIndex];
438 DBRefEntry[] uprefs = DBRefUtils.selectRefs(
439 sequence.getDBRefs(),
440 new String[] { dbsource.getDbSource() }); // jalview.datamodel.DBRefSource.UNIPROT
442 // check for existing dbrefs to use
443 if (uprefs != null && uprefs.length > 0)
445 for (int j = 0; j < uprefs.length; j++)
447 addSeqId(sequence, uprefs[j].getAccessionId());
448 queries.addElement(uprefs[j].getAccessionId().toUpperCase());
453 // generate queries from sequence ID string
454 StringTokenizer st = new StringTokenizer(sequence.getName(),
456 while (st.hasMoreTokens())
458 String token = st.nextToken();
459 UPEntry[] presp = null;
460 if (picrClient != null)
462 // resolve the string against PICR to recover valid IDs
466 .getUPIForAccession(token, null,
467 picrClient.getMappedDatabaseNames(),
469 } catch (Exception e)
471 System.err.println("Exception with Picr for '" + token
476 if (presp != null && presp.length > 0)
478 for (int id = 0; id < presp.length; id++)
480 // construct sequences from response if sequences are
481 // present, and do a transferReferences
482 // otherwise transfer non sequence x-references directly.
485 .println("Validated ID against PICR... (for what its worth):"
487 addSeqId(sequence, token);
488 queries.addElement(token.toUpperCase());
493 // System.out.println("Not querying source with token="+token+"\n");
494 addSeqId(sequence, token);
495 queries.addElement(token.toUpperCase());
502 // advance to next database
504 } // all databases have been queried
505 if (!warningMessages.isEmpty())
507 StringBuilder sb = new StringBuilder(warningMessages.size() * 30);
508 sb.append(MessageManager
509 .getString("label.your_sequences_have_been_verified"));
510 for (String msg : warningMessages)
512 sb.append(msg).append(NEWLINE);
514 output.setText(sb.toString());
516 Desktop.addInternalFrame(output,
517 MessageManager.getString("label.sequences_updated"), 600, 300);
518 // The above is the dataset, we must now find out the index
519 // of the viewed sequence
522 if (progressWindow != null)
524 progressWindow.setProgressBar(
525 MessageManager.getString("label.dbref_search_completed"),
529 for (FetchFinishedListenerI listener : listeners)
537 * Verify local sequences in seqRefs against the retrieved sequence database
538 * records. Returns true if any sequence was modified as a result (start/end
539 * changed and/or sequence enlarged), else false.
542 * dataset sequences we are retrieving for
544 * database source we are retrieving from
546 * retrieved sequences as alignment
547 * @param trimDatasetSeqs
548 * if true, sequences will not be enlarged to match longer retrieved
549 * sequences, only their start/end adjusted
550 * @param warningMessages
551 * a list of messages to add to
553 boolean transferReferences(Vector<SequenceI> sdataset, String dbSource,
554 AlignmentI retrievedAl, boolean trimDatasetSeqs,
555 List<String> warningMessages)
557 // System.out.println("trimming ? " + trimDatasetSeqs);
558 if (retrievedAl == null || retrievedAl.getHeight() == 0)
563 boolean modified = false;
564 SequenceI[] retrieved = recoverDbSequences(retrievedAl
565 .getSequencesArray());
566 SequenceI sequence = null;
568 for (SequenceI retrievedSeq : retrieved)
570 // Work out which sequences this sequence matches,
571 // taking into account all accessionIds and names in the file
572 Vector<SequenceI> sequenceMatches = new Vector<SequenceI>();
573 // look for corresponding accession ids
574 DBRefEntry[] entryRefs = DBRefUtils.selectRefs(
575 retrievedSeq.getDBRefs(), new String[] { dbSource });
576 if (entryRefs == null)
579 .println("Dud dbSource string ? no entryrefs selected for "
580 + dbSource + " on " + retrievedSeq.getName());
583 for (int j = 0; j < entryRefs.length; j++)
585 String accessionId = entryRefs[j].getAccessionId();
586 // match up on accessionId
587 if (seqRefs.containsKey(accessionId.toUpperCase()))
589 Vector<SequenceI> seqs = seqRefs.get(accessionId);
590 for (int jj = 0; jj < seqs.size(); jj++)
592 sequence = seqs.elementAt(jj);
593 if (!sequenceMatches.contains(sequence))
595 sequenceMatches.addElement(sequence);
600 if (sequenceMatches.isEmpty())
602 // failed to match directly on accessionId==query so just compare all
603 // sequences to entry
604 Enumeration<String> e = seqRefs.keys();
605 while (e.hasMoreElements())
607 Vector<SequenceI> sqs = seqRefs.get(e.nextElement());
608 if (sqs != null && sqs.size() > 0)
610 Enumeration<SequenceI> sqe = sqs.elements();
611 while (sqe.hasMoreElements())
613 sequenceMatches.addElement(sqe.nextElement());
618 // look for corresponding names
619 // this is uniprot specific ?
620 // could be useful to extend this so we try to find any 'significant'
621 // information in common between two sequence objects.
623 * DBRefEntry[] entryRefs =
624 * jalview.util.DBRefUtils.selectRefs(entry.getDBRef(), new String[] {
625 * dbSource }); for (int j = 0; j < entry.getName().size(); j++) { String
626 * name = entry.getName().elementAt(j).toString(); if
627 * (seqRefs.containsKey(name)) { Vector seqs = (Vector) seqRefs.get(name);
628 * for (int jj = 0; jj < seqs.size(); jj++) { sequence = (SequenceI)
629 * seqs.elementAt(jj); if (!sequenceMatches.contains(sequence)) {
630 * sequenceMatches.addElement(sequence); } } } }
632 // sequenceMatches now contains the set of all sequences associated with
633 // the returned db record
634 final String retrievedSeqString = retrievedSeq.getSequenceAsString();
635 String entrySeq = retrievedSeqString.toUpperCase();
636 for (int m = 0; m < sequenceMatches.size(); m++)
638 sequence = sequenceMatches.elementAt(m);
639 // only update start and end positions and shift features if there are
640 // no existing references
641 // TODO: test for legacy where uniprot or EMBL refs exist but no
642 // mappings are made (but content matches retrieved set)
643 boolean updateRefFrame = sequence.getDBRefs() == null
644 || sequence.getDBRefs().length == 0;
646 // verify sequence against the entry sequence
649 final int sequenceStart = sequence.getStart();
651 boolean remoteEnclosesLocal = false;
652 String nonGapped = AlignSeq.extractGaps("-. ",
653 sequence.getSequenceAsString()).toUpperCase();
654 int absStart = entrySeq.indexOf(nonGapped);
657 // couldn't find local sequence in sequence from database, so check if
658 // the database sequence is a subsequence of local sequence
659 absStart = nonGapped.indexOf(entrySeq);
662 // verification failed. couldn't find any relationship between
663 // entrySeq and local sequence
664 // messages suppressed as many-to-many matches are confusing
665 // String msg = sequence.getName()
666 // + " Sequence not 100% match with "
667 // + retrievedSeq.getName();
668 // addWarningMessage(warningMessages, msg);
672 * retrieved sequence is a proper subsequence of local sequence
674 String msg = sequence.getName() + " has " + absStart
675 + " prefixed residues compared to "
676 + retrievedSeq.getName();
677 addWarningMessage(warningMessages, msg);
680 * So create a mapping to the external entry from the matching region of
681 * the local sequence, and leave local start/end untouched.
683 mp = new Mapping(null, new int[] { sequenceStart + absStart,
684 sequenceStart + absStart + entrySeq.length() - 1 }, new int[]
685 { retrievedSeq.getStart(),
686 retrievedSeq.getStart() + entrySeq.length() - 1 }, 1, 1);
687 updateRefFrame = false;
692 * local sequence is a subsequence of (or matches) retrieved sequence
694 remoteEnclosesLocal = true;
700 * relocate existing sequence features by offset
702 int startShift = absStart - sequenceStart + 1;
705 modified |= sequence.getFeatures().shiftFeatures(startShift);
710 System.out.println("Adding dbrefs to " + sequence.getName()
711 + " from " + dbSource + " sequence : "
712 + retrievedSeq.getName());
713 sequence.transferAnnotation(retrievedSeq, mp);
715 absStart += retrievedSeq.getStart();
716 int absEnd = absStart + nonGapped.length() - 1;
717 if (!trimDatasetSeqs)
720 * update start position and/or expand to longer retrieved sequence
722 if (!retrievedSeqString.equals(sequence.getSequenceAsString())
723 && remoteEnclosesLocal)
725 sequence.setSequence(retrievedSeqString);
727 addWarningMessage(warningMessages,
728 "Sequence for " + sequence.getName()
729 + " expanded from " + retrievedSeq.getName());
731 if (sequence.getStart() != retrievedSeq.getStart())
733 sequence.setStart(retrievedSeq.getStart());
735 if (absStart != sequenceStart)
737 addWarningMessage(warningMessages, "Start/end position for "
738 + sequence.getName() + " updated from "
739 + retrievedSeq.getName());
745 // finally, update local sequence reference frame if we're allowed
748 // just fix start/end
749 if (sequence.getStart() != absStart
750 || sequence.getEnd() != absEnd)
752 sequence.setStart(absStart);
753 sequence.setEnd(absEnd);
755 addWarningMessage(warningMessages, "Start/end for "
756 + sequence.getName() + " updated from "
757 + retrievedSeq.getName());
760 // search for alignment sequences to update coordinate frame for
761 for (int alsq = 0; alsq < alseqs.length; alsq++)
763 if (alseqs[alsq].getDatasetSequence() == sequence)
765 String ngAlsq = AlignSeq.extractGaps("-. ",
766 alseqs[alsq].getSequenceAsString()).toUpperCase();
767 int oldstrt = alseqs[alsq].getStart();
768 alseqs[alsq].setStart(sequence.getSequenceAsString()
769 .toUpperCase().indexOf(ngAlsq)
770 + sequence.getStart());
771 if (oldstrt != alseqs[alsq].getStart())
773 alseqs[alsq].setEnd(ngAlsq.length()
774 + alseqs[alsq].getStart() - 1);
779 // TODO: search for all other references to this dataset sequence, and
781 // TODO: update all AlCodonMappings which involve this alignment
782 // sequence (e.g. Q30167 cdna translation from exon2 product (vamsas
785 // and remove it from the rest
786 // TODO: decide if we should remove annotated sequence from set
787 sdataset.remove(sequence);
788 // TODO: should we make a note of sequences that have received new DB
789 // ids, so we can query all enabled DAS servers for them ?
796 * Adds the message to the list unless it already contains it
801 void addWarningMessage(List<String> messageList, String msg)
803 if (!messageList.contains(msg))
805 messageList.add(msg);
810 * loop thru and collect additional sequences in Map.
812 * @param sequencesArray
815 private SequenceI[] recoverDbSequences(SequenceI[] sequencesArray)
817 Vector<SequenceI> nseq = new Vector<SequenceI>();
818 for (int i = 0; sequencesArray != null && i < sequencesArray.length; i++)
820 nseq.addElement(sequencesArray[i]);
821 DBRefEntry[] dbr = sequencesArray[i].getDBRefs();
823 for (int r = 0; (dbr != null) && r < dbr.length; r++)
825 if ((map = dbr[r].getMap()) != null)
827 if (map.getTo() != null && !nseq.contains(map.getTo()))
829 nseq.addElement(map.getTo());
836 sequencesArray = new SequenceI[nseq.size()];
837 nseq.toArray(sequencesArray);
839 return sequencesArray;