JAL-3035 more das bits removed
[jalview.git] / src / jalview / ws / DBRefFetcher.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.ws;
22
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.Desktop;
32 import jalview.gui.FeatureSettings;
33 import jalview.gui.IProgressIndicator;
34 import jalview.gui.OOMWarning;
35 import jalview.util.DBRefUtils;
36 import jalview.util.MessageManager;
37 import jalview.ws.seqfetcher.DbSourceProxy;
38
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.Enumeration;
42 import java.util.Hashtable;
43 import java.util.List;
44 import java.util.StringTokenizer;
45 import java.util.Vector;
46
47 import uk.ac.ebi.picr.model.UPEntry;
48 import uk.ac.ebi.www.picr.AccessionMappingService.AccessionMapperServiceLocator;
49
50 /**
51  * Implements a runnable for validating a sequence against external databases
52  * and then propagating references and features onto the sequence(s)
53  * 
54  * @author $author$
55  * @version $Revision$
56  */
57 public class DBRefFetcher implements Runnable
58 {
59   private static final String NEWLINE = System.lineSeparator();
60
61   public static final String TRIM_RETRIEVED_SEQUENCES = "TRIM_FETCHED_DATASET_SEQS";
62
63   public interface FetchFinishedListenerI
64   {
65     void finished();
66   }
67
68   SequenceI[] dataset;
69
70   IProgressIndicator progressWindow;
71
72   CutAndPasteTransfer output = new CutAndPasteTransfer();
73
74   boolean running = false;
75
76   /**
77    * picr client instance
78    */
79   uk.ac.ebi.www.picr.AccessionMappingService.AccessionMapperInterface picrClient = null;
80
81   // This will be a collection of Vectors of sequenceI refs.
82   // The key will be the seq name or accession id of the seq
83   Hashtable<String, Vector<SequenceI>> seqRefs;
84
85   DbSourceProxy[] dbSources;
86
87   SequenceFetcher sfetcher;
88
89   private List<FetchFinishedListenerI> listeners;
90
91   private SequenceI[] alseqs;
92
93   /*
94    * when true - retrieved sequences will be trimmed to cover longest derived
95    * alignment sequence
96    */
97   private boolean trimDsSeqs = true;
98
99   /**
100    * Creates a new DBRefFetcher object and fetches from the currently selected
101    * set of databases, if this is null then it fetches based on feature settings
102    * 
103    * @param seqs
104    *          fetch references for these SequenceI array
105    * @param progressIndicatorFrame
106    *          the frame for progress bar monitoring
107    * @param sources
108    *          array of DbSourceProxy to query references form
109    * @param featureSettings
110    *          FeatureSettings to get alternative DbSourceProxy from
111    * @param isNucleotide
112    *          indicates if the array of SequenceI are Nucleotides or not
113    */
114   public DBRefFetcher(SequenceI[] seqs,
115           IProgressIndicator progressIndicatorFrame,
116           DbSourceProxy[] sources, FeatureSettings featureSettings,
117           boolean isNucleotide)
118   {
119     listeners = new ArrayList<>();
120     this.progressWindow = progressIndicatorFrame;
121     alseqs = new SequenceI[seqs.length];
122     SequenceI[] ds = new SequenceI[seqs.length];
123     for (int i = 0; i < seqs.length; i++)
124     {
125       alseqs[i] = seqs[i];
126       if (seqs[i].getDatasetSequence() != null)
127       {
128         ds[i] = seqs[i].getDatasetSequence();
129       }
130       else
131       {
132         ds[i] = seqs[i];
133       }
134     }
135     this.dataset = ds;
136     // TODO Jalview 2.5 lots of this code should be in the gui package!
137     sfetcher = jalview.gui.SequenceFetcher
138             .getSequenceFetcherSingleton(progressIndicatorFrame);
139     // set default behaviour for transferring excess sequence data to the
140     // dataset
141     trimDsSeqs = Cache.getDefault(TRIM_RETRIEVED_SEQUENCES, true);
142     if (sources == null)
143     {
144       setDatabaseSources(featureSettings, isNucleotide);
145     }
146     else
147     {
148       // we assume the caller knows what they're doing and ensured that all the
149       // db source names are valid
150       dbSources = sources;
151     }
152   }
153
154   /**
155    * Helper method to configure the list of database sources to query
156    * 
157    * @param featureSettings
158    * @param forNucleotide
159    */
160   void setDatabaseSources(FeatureSettings featureSettings,
161           boolean forNucleotide)
162   {
163     // af.featureSettings_actionPerformed(null);
164     String[] defdb = null;
165     List<DbSourceProxy> selsources = new ArrayList<>();
166     // select appropriate databases based on alignFrame context.
167     if (forNucleotide)
168     {
169       defdb = DBRefSource.DNACODINGDBS;
170     }
171     else
172     {
173       defdb = DBRefSource.PROTEINDBS;
174     }
175     List<DbSourceProxy> srces = new ArrayList<>();
176     for (String ddb : defdb)
177     {
178       List<DbSourceProxy> srcesfordb = sfetcher.getSourceProxy(ddb);
179       if (srcesfordb != null)
180       {
181         for (DbSourceProxy src : srcesfordb)
182         {
183           if (!srces.contains(src))
184           {
185             srces.addAll(srcesfordb);
186           }
187         }
188       }
189     }
190     // append the PDB data source, since it is 'special', catering for both
191     // nucleotide and protein
192     // srces.addAll(sfetcher.getSourceProxy(DBRefSource.PDB));
193
194     srces.addAll(selsources);
195     dbSources = srces.toArray(new DbSourceProxy[srces.size()]);
196   }
197
198   /**
199    * Constructor with only sequences provided
200    * 
201    * @param sequences
202    */
203   public DBRefFetcher(SequenceI[] sequences)
204   {
205     this(sequences, null, null, null, false);
206   }
207
208   /**
209    * Add a listener to be notified when sequence fetching is complete
210    * 
211    * @param l
212    */
213   public void addListener(FetchFinishedListenerI l)
214   {
215     listeners.add(l);
216   }
217
218   /**
219    * start the fetcher thread
220    * 
221    * @param waitTillFinished
222    *          true to block until the fetcher has finished
223    */
224   public void fetchDBRefs(boolean waitTillFinished)
225   {
226     // TODO can we not simply write
227     // if (waitTillFinished) { run(); } else { new Thread(this).start(); }
228
229     Thread thread = new Thread(this);
230     thread.start();
231     running = true;
232
233     if (waitTillFinished)
234     {
235       while (running)
236       {
237         try
238         {
239           Thread.sleep(500);
240         } catch (Exception ex)
241         {
242         }
243       }
244     }
245   }
246
247   /**
248    * The sequence will be added to a vector of sequences belonging to key which
249    * could be either seq name or dbref id
250    * 
251    * @param seq
252    *          SequenceI
253    * @param key
254    *          String
255    */
256   void addSeqId(SequenceI seq, String key)
257   {
258     key = key.toUpperCase();
259
260     Vector<SequenceI> seqs;
261     if (seqRefs.containsKey(key))
262     {
263       seqs = seqRefs.get(key);
264
265       if (seqs != null && !seqs.contains(seq))
266       {
267         seqs.addElement(seq);
268       }
269       else if (seqs == null)
270       {
271         seqs = new Vector<>();
272         seqs.addElement(seq);
273       }
274
275     }
276     else
277     {
278       seqs = new Vector<>();
279       seqs.addElement(seq);
280     }
281
282     seqRefs.put(key, seqs);
283   }
284
285   /**
286    * DOCUMENT ME!
287    */
288   @Override
289   public void run()
290   {
291     if (dbSources == null)
292     {
293       throw new Error(MessageManager
294               .getString("error.implementation_error_must_init_dbsources"));
295     }
296     running = true;
297     long startTime = System.currentTimeMillis();
298     if (progressWindow != null)
299     {
300       progressWindow.setProgressBar(
301               MessageManager.getString("status.fetching_db_refs"),
302               startTime);
303     }
304     try
305     {
306       if (Cache.getDefault("DBREFFETCH_USEPICR", false))
307       {
308         picrClient = new AccessionMapperServiceLocator()
309                 .getAccessionMapperPort();
310       }
311     } catch (Exception e)
312     {
313       System.err.println("Couldn't locate PICR service instance.\n");
314       e.printStackTrace();
315     }
316
317     Vector<SequenceI> sdataset = new Vector<>(
318             Arrays.asList(dataset));
319     List<String> warningMessages = new ArrayList<>();
320
321     int db = 0;
322     while (sdataset.size() > 0 && db < dbSources.length)
323     {
324       int maxqlen = 1; // default number of queries made at one time
325       System.out.println("Verifying against " + dbSources[db].getDbName());
326
327       // iterate through db for each remaining un-verified sequence
328       SequenceI[] currSeqs = new SequenceI[sdataset.size()];
329       sdataset.copyInto(currSeqs);// seqs that are to be validated against
330       // dbSources[db]
331       Vector<String> queries = new Vector<>(); // generated queries curSeq
332       seqRefs = new Hashtable<>();
333
334       int seqIndex = 0;
335
336       DbSourceProxy dbsource = dbSources[db];
337       // for moment, we dumbly iterate over all retrieval sources for a
338       // particular database
339       // TODO: introduce multithread multisource queries and logic to remove a
340       // query from other sources if any source for a database returns a
341       // record
342       maxqlen = dbsource.getMaximumQueryCount();
343
344       while (queries.size() > 0 || seqIndex < currSeqs.length)
345       {
346         if (queries.size() > 0)
347         {
348           // Still queries to make for current seqIndex
349           StringBuffer queryString = new StringBuffer("");
350           int numq = 0;
351           int nqSize = (maxqlen > queries.size()) ? queries.size()
352                   : maxqlen;
353
354           while (queries.size() > 0 && numq < nqSize)
355           {
356             String query = queries.elementAt(0);
357             if (dbsource.isValidReference(query))
358             {
359               queryString.append(
360                       (numq == 0) ? "" : dbsource.getAccessionSeparator());
361               queryString.append(query);
362               numq++;
363             }
364             // remove the extracted query string
365             queries.removeElementAt(0);
366           }
367           // make the queries and process the response
368           AlignmentI retrieved = null;
369           try
370           {
371             if (Cache.log.isDebugEnabled())
372             {
373               Cache.log.debug("Querying " + dbsource.getDbName()
374                       + " with : '" + queryString.toString() + "'");
375             }
376             retrieved = dbsource.getSequenceRecords(queryString.toString());
377           } catch (Exception ex)
378           {
379             ex.printStackTrace();
380           } catch (OutOfMemoryError err)
381           {
382             new OOMWarning("retrieving database references ("
383                     + queryString.toString() + ")", err);
384           }
385           if (retrieved != null)
386           {
387             transferReferences(sdataset, dbsource.getDbSource(), retrieved,
388                     trimDsSeqs, warningMessages);
389           }
390         }
391         else
392         {
393           // make some more strings for use as queries
394           for (int i = 0; (seqIndex < dataset.length)
395                   && (i < 50); seqIndex++, i++)
396           {
397             SequenceI sequence = dataset[seqIndex];
398             DBRefEntry[] uprefs = DBRefUtils
399                     .selectRefs(sequence.getDBRefs(), new String[]
400                     { dbsource.getDbSource() }); // jalview.datamodel.DBRefSource.UNIPROT
401             // });
402             // check for existing dbrefs to use
403             if (uprefs != null && uprefs.length > 0)
404             {
405               for (int j = 0; j < uprefs.length; j++)
406               {
407                 addSeqId(sequence, uprefs[j].getAccessionId());
408                 queries.addElement(
409                         uprefs[j].getAccessionId().toUpperCase());
410               }
411             }
412             else
413             {
414               // generate queries from sequence ID string
415               StringTokenizer st = new StringTokenizer(sequence.getName(),
416                       "|");
417               while (st.hasMoreTokens())
418               {
419                 String token = st.nextToken();
420                 UPEntry[] presp = null;
421                 if (picrClient != null)
422                 {
423                   // resolve the string against PICR to recover valid IDs
424                   try
425                   {
426                     presp = picrClient.getUPIForAccession(token, null,
427                             picrClient.getMappedDatabaseNames(), null,
428                             true);
429                   } catch (Exception e)
430                   {
431                     System.err.println(
432                             "Exception with Picr for '" + token + "'\n");
433                     e.printStackTrace();
434                   }
435                 }
436                 if (presp != null && presp.length > 0)
437                 {
438                   for (int id = 0; id < presp.length; id++)
439                   {
440                     // construct sequences from response if sequences are
441                     // present, and do a transferReferences
442                     // otherwise transfer non sequence x-references directly.
443                   }
444                   System.out.println(
445                           "Validated ID against PICR... (for what its worth):"
446                                   + token);
447                   addSeqId(sequence, token);
448                   queries.addElement(token.toUpperCase());
449                 }
450                 else
451                 {
452                   // if ()
453                   // System.out.println("Not querying source with
454                   // token="+token+"\n");
455                   addSeqId(sequence, token);
456                   queries.addElement(token.toUpperCase());
457                 }
458               }
459             }
460           }
461         }
462       }
463       // advance to next database
464       db++;
465     } // all databases have been queried
466     if (!warningMessages.isEmpty())
467     {
468       StringBuilder sb = new StringBuilder(warningMessages.size() * 30);
469       sb.append(MessageManager
470               .getString("label.your_sequences_have_been_verified"));
471       for (String msg : warningMessages)
472       {
473         sb.append(msg).append(NEWLINE);
474       }
475       output.setText(sb.toString());
476
477       Desktop.addInternalFrame(output,
478               MessageManager.getString("label.sequences_updated"), 600,
479               300);
480       // The above is the dataset, we must now find out the index
481       // of the viewed sequence
482
483     }
484     if (progressWindow != null)
485     {
486       progressWindow.setProgressBar(
487               MessageManager.getString("label.dbref_search_completed"),
488               startTime);
489     }
490
491     for (FetchFinishedListenerI listener : listeners)
492     {
493       listener.finished();
494     }
495     running = false;
496   }
497
498   /**
499    * Verify local sequences in seqRefs against the retrieved sequence database
500    * records. Returns true if any sequence was modified as a result (start/end
501    * changed and/or sequence enlarged), else false.
502    * 
503    * @param sdataset
504    *          dataset sequences we are retrieving for
505    * @param dbSource
506    *          database source we are retrieving from
507    * @param retrievedAl
508    *          retrieved sequences as alignment
509    * @param trimDatasetSeqs
510    *          if true, sequences will not be enlarged to match longer retrieved
511    *          sequences, only their start/end adjusted
512    * @param warningMessages
513    *          a list of messages to add to
514    */
515   boolean transferReferences(Vector<SequenceI> sdataset, String dbSource,
516           AlignmentI retrievedAl, boolean trimDatasetSeqs,
517           List<String> warningMessages)
518   {
519     // System.out.println("trimming ? " + trimDatasetSeqs);
520     if (retrievedAl == null || retrievedAl.getHeight() == 0)
521     {
522       return false;
523     }
524
525     boolean modified = false;
526     SequenceI[] retrieved = recoverDbSequences(
527             retrievedAl.getSequencesArray());
528     SequenceI sequence = null;
529
530     for (SequenceI retrievedSeq : retrieved)
531     {
532       // Work out which sequences this sequence matches,
533       // taking into account all accessionIds and names in the file
534       Vector<SequenceI> sequenceMatches = new Vector<>();
535       // look for corresponding accession ids
536       DBRefEntry[] entryRefs = DBRefUtils
537               .selectRefs(retrievedSeq.getDBRefs(), new String[]
538               { dbSource });
539       if (entryRefs == null)
540       {
541         System.err
542                 .println("Dud dbSource string ? no entryrefs selected for "
543                         + dbSource + " on " + retrievedSeq.getName());
544         continue;
545       }
546       for (int j = 0; j < entryRefs.length; j++)
547       {
548         String accessionId = entryRefs[j].getAccessionId();
549         // match up on accessionId
550         if (seqRefs.containsKey(accessionId.toUpperCase()))
551         {
552           Vector<SequenceI> seqs = seqRefs.get(accessionId);
553           for (int jj = 0; jj < seqs.size(); jj++)
554           {
555             sequence = seqs.elementAt(jj);
556             if (!sequenceMatches.contains(sequence))
557             {
558               sequenceMatches.addElement(sequence);
559             }
560           }
561         }
562       }
563       if (sequenceMatches.isEmpty())
564       {
565         // failed to match directly on accessionId==query so just compare all
566         // sequences to entry
567         Enumeration<String> e = seqRefs.keys();
568         while (e.hasMoreElements())
569         {
570           Vector<SequenceI> sqs = seqRefs.get(e.nextElement());
571           if (sqs != null && sqs.size() > 0)
572           {
573             Enumeration<SequenceI> sqe = sqs.elements();
574             while (sqe.hasMoreElements())
575             {
576               sequenceMatches.addElement(sqe.nextElement());
577             }
578           }
579         }
580       }
581       // look for corresponding names
582       // this is uniprot specific ?
583       // could be useful to extend this so we try to find any 'significant'
584       // information in common between two sequence objects.
585       /*
586        * DBRefEntry[] entryRefs =
587        * jalview.util.DBRefUtils.selectRefs(entry.getDBRef(), new String[] {
588        * dbSource }); for (int j = 0; j < entry.getName().size(); j++) { String
589        * name = entry.getName().elementAt(j).toString(); if
590        * (seqRefs.containsKey(name)) { Vector seqs = (Vector) seqRefs.get(name);
591        * for (int jj = 0; jj < seqs.size(); jj++) { sequence = (SequenceI)
592        * seqs.elementAt(jj); if (!sequenceMatches.contains(sequence)) {
593        * sequenceMatches.addElement(sequence); } } } }
594        */
595       // sequenceMatches now contains the set of all sequences associated with
596       // the returned db record
597       final String retrievedSeqString = retrievedSeq.getSequenceAsString();
598       String entrySeq = retrievedSeqString.toUpperCase();
599       for (int m = 0; m < sequenceMatches.size(); m++)
600       {
601         sequence = sequenceMatches.elementAt(m);
602         // only update start and end positions and shift features if there are
603         // no existing references
604         // TODO: test for legacy where uniprot or EMBL refs exist but no
605         // mappings are made (but content matches retrieved set)
606         boolean updateRefFrame = sequence.getDBRefs() == null
607                 || sequence.getDBRefs().length == 0;
608         // TODO:
609         // verify sequence against the entry sequence
610
611         Mapping mp;
612         final int sequenceStart = sequence.getStart();
613
614         boolean remoteEnclosesLocal = false;
615         String nonGapped = AlignSeq
616                 .extractGaps("-. ", sequence.getSequenceAsString())
617                 .toUpperCase();
618         int absStart = entrySeq.indexOf(nonGapped);
619         if (absStart == -1)
620         {
621           // couldn't find local sequence in sequence from database, so check if
622           // the database sequence is a subsequence of local sequence
623           absStart = nonGapped.indexOf(entrySeq);
624           if (absStart == -1)
625           {
626             // verification failed. couldn't find any relationship between
627             // entrySeq and local sequence
628             // messages suppressed as many-to-many matches are confusing
629             // String msg = sequence.getName()
630             // + " Sequence not 100% match with "
631             // + retrievedSeq.getName();
632             // addWarningMessage(warningMessages, msg);
633             continue;
634           }
635           /*
636            * retrieved sequence is a proper subsequence of local sequence
637            */
638           String msg = sequence.getName() + " has " + absStart
639                   + " prefixed residues compared to "
640                   + retrievedSeq.getName();
641           addWarningMessage(warningMessages, msg);
642
643           /*
644            * So create a mapping to the external entry from the matching region of 
645            * the local sequence, and leave local start/end untouched. 
646            */
647           mp = new Mapping(null,
648                   new int[]
649                   { sequenceStart + absStart,
650                       sequenceStart + absStart + entrySeq.length() - 1 },
651                   new int[]
652                   { retrievedSeq.getStart(),
653                       retrievedSeq.getStart() + entrySeq.length() - 1 },
654                   1, 1);
655           updateRefFrame = false;
656         }
657         else
658         {
659           /*
660            * local sequence is a subsequence of (or matches) retrieved sequence
661            */
662           remoteEnclosesLocal = true;
663           mp = null;
664
665           if (updateRefFrame)
666           {
667             /*
668              * relocate existing sequence features by offset
669              */
670             int startShift = absStart - sequenceStart + 1;
671             if (startShift != 0)
672             {
673               modified |= sequence.getFeatures().shiftFeatures(startShift);
674             }
675           }
676         }
677
678         System.out.println("Adding dbrefs to " + sequence.getName()
679                 + " from " + dbSource + " sequence : "
680                 + retrievedSeq.getName());
681         sequence.transferAnnotation(retrievedSeq, mp);
682
683         absStart += retrievedSeq.getStart();
684         int absEnd = absStart + nonGapped.length() - 1;
685         if (!trimDatasetSeqs)
686         {
687           /*
688            * update start position and/or expand to longer retrieved sequence
689            */
690           if (!retrievedSeqString.equals(sequence.getSequenceAsString())
691                   && remoteEnclosesLocal)
692           {
693             sequence.setSequence(retrievedSeqString);
694             modified = true;
695             addWarningMessage(warningMessages,
696                     "Sequence for " + sequence.getName() + " expanded from "
697                             + retrievedSeq.getName());
698           }
699           if (sequence.getStart() != retrievedSeq.getStart())
700           {
701             sequence.setStart(retrievedSeq.getStart());
702             modified = true;
703             if (absStart != sequenceStart)
704             {
705               addWarningMessage(warningMessages,
706                       "Start/end position for " + sequence.getName()
707                               + " updated from " + retrievedSeq.getName());
708             }
709           }
710         }
711         if (updateRefFrame)
712         {
713           // finally, update local sequence reference frame if we're allowed
714           if (trimDatasetSeqs)
715           {
716             // just fix start/end
717             if (sequence.getStart() != absStart
718                     || sequence.getEnd() != absEnd)
719             {
720               sequence.setStart(absStart);
721               sequence.setEnd(absEnd);
722               modified = true;
723               addWarningMessage(warningMessages,
724                       "Start/end for " + sequence.getName()
725                               + " updated from " + retrievedSeq.getName());
726             }
727           }
728           // search for alignment sequences to update coordinate frame for
729           for (int alsq = 0; alsq < alseqs.length; alsq++)
730           {
731             if (alseqs[alsq].getDatasetSequence() == sequence)
732             {
733               String ngAlsq = AlignSeq
734                       .extractGaps("-. ",
735                               alseqs[alsq].getSequenceAsString())
736                       .toUpperCase();
737               int oldstrt = alseqs[alsq].getStart();
738               alseqs[alsq].setStart(sequence.getSequenceAsString()
739                       .toUpperCase().indexOf(ngAlsq) + sequence.getStart());
740               if (oldstrt != alseqs[alsq].getStart())
741               {
742                 alseqs[alsq].setEnd(
743                         ngAlsq.length() + alseqs[alsq].getStart() - 1);
744                 modified = true;
745               }
746             }
747           }
748           // TODO: search for all other references to this dataset sequence, and
749           // update start/end
750           // TODO: update all AlCodonMappings which involve this alignment
751           // sequence (e.g. Q30167 cdna translation from exon2 product (vamsas
752           // demo)
753         }
754         // and remove it from the rest
755         // TODO: decide if we should remove annotated sequence from set
756         sdataset.remove(sequence);
757       }
758     }
759     return modified;
760   }
761
762   /**
763    * Adds the message to the list unless it already contains it
764    * 
765    * @param messageList
766    * @param msg
767    */
768   void addWarningMessage(List<String> messageList, String msg)
769   {
770     if (!messageList.contains(msg))
771     {
772       messageList.add(msg);
773     }
774   }
775
776   /**
777    * loop thru and collect additional sequences in Map.
778    * 
779    * @param sequencesArray
780    * @return
781    */
782   private SequenceI[] recoverDbSequences(SequenceI[] sequencesArray)
783   {
784     Vector<SequenceI> nseq = new Vector<>();
785     for (int i = 0; sequencesArray != null
786             && i < sequencesArray.length; i++)
787     {
788       nseq.addElement(sequencesArray[i]);
789       DBRefEntry[] dbr = sequencesArray[i].getDBRefs();
790       Mapping map = null;
791       for (int r = 0; (dbr != null) && r < dbr.length; r++)
792       {
793         if ((map = dbr[r].getMap()) != null)
794         {
795           if (map.getTo() != null && !nseq.contains(map.getTo()))
796           {
797             nseq.addElement(map.getTo());
798           }
799         }
800       }
801     }
802     if (nseq.size() > 0)
803     {
804       sequencesArray = new SequenceI[nseq.size()];
805       nseq.toArray(sequencesArray);
806     }
807     return sequencesArray;
808   }
809 }