JAL-3810 - Replace log.trace() calls with log.debug()
[jalview.git] / src / jalview / ws / jws2 / SeqAnnotationServiceCalcWorker.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.jws2;
22
23 import jalview.analysis.AlignSeq;
24 import jalview.analysis.AlignmentAnnotationUtils;
25 import jalview.analysis.SeqsetUtils;
26 import jalview.api.AlignViewportI;
27 import jalview.api.AlignmentViewPanel;
28 import jalview.api.FeatureColourI;
29 import jalview.api.PollableAlignCalcWorkerI;
30 import jalview.bin.Cache;
31 import jalview.datamodel.AlignmentAnnotation;
32 import jalview.datamodel.AlignmentI;
33 import jalview.datamodel.AnnotatedCollectionI;
34 import jalview.datamodel.Annotation;
35 import jalview.datamodel.ContiguousI;
36 import jalview.datamodel.Mapping;
37 import jalview.datamodel.SequenceI;
38 import jalview.datamodel.features.FeatureMatcherSetI;
39 import jalview.gui.AlignFrame;
40 import jalview.gui.Desktop;
41 import jalview.gui.IProgressIndicator;
42 import jalview.gui.IProgressIndicatorHandler;
43 import jalview.gui.JvOptionPane;
44 import jalview.gui.WebserviceInfo;
45 import jalview.schemes.FeatureSettingsAdapter;
46 import jalview.schemes.ResidueProperties;
47 import jalview.util.MapList;
48 import jalview.util.MessageManager;
49 import jalview.workers.AlignCalcWorker;
50 import jalview.ws.JobStateSummary;
51 import jalview.ws.api.CancellableI;
52 import jalview.ws.api.JalviewServiceEndpointProviderI;
53 import jalview.ws.api.JobId;
54 import jalview.ws.api.SequenceAnnotationServiceI;
55 import jalview.ws.api.ServiceWithParameters;
56 import jalview.ws.api.WSAnnotationCalcManagerI;
57 import jalview.ws.gui.AnnotationWsJob;
58 import jalview.ws.jws2.dm.AAConSettings;
59 import jalview.ws.params.ArgumentI;
60 import jalview.ws.params.WsParamSetI;
61
62 import java.util.ArrayList;
63 import java.util.HashMap;
64 import java.util.List;
65 import java.util.Map;
66
67 public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
68         implements WSAnnotationCalcManagerI, PollableAlignCalcWorkerI
69 {
70
71   protected ServiceWithParameters service;
72
73   protected WsParamSetI preset;
74
75   protected List<ArgumentI> arguments;
76
77   protected IProgressIndicator guiProgress;
78
79   protected boolean submitGaps = true;
80
81   /**
82    * by default, we filter out non-standard residues before submission
83    */
84   protected boolean filterNonStandardResidues = true;
85
86   /**
87    * Recover any existing parameters for this service
88    */
89   protected void initViewportParams()
90   {
91     if (getCalcId() != null)
92     {
93       ((jalview.gui.AlignViewport) alignViewport).setCalcIdSettingsFor(
94               getCalcId(),
95               new AAConSettings(true, service, this.preset, arguments),
96               true);
97     }
98   }
99
100   /**
101    * 
102    * @return null or a string used to recover all annotation generated by this
103    *         worker
104    */
105   public String getCalcId()
106   {
107     return service.getAlignAnalysisUI() == null ? null
108             : service.getAlignAnalysisUI().getCalcId();
109   }
110
111   public WsParamSetI getPreset()
112   {
113     return preset;
114   }
115
116   public List<ArgumentI> getArguments()
117   {
118     return arguments;
119   }
120
121   /**
122    * reconfigure and restart the AAConClient. This method will spawn a new
123    * thread that will wait until any current jobs are finished, modify the
124    * parameters and restart the conservation calculation with the new values.
125    * 
126    * @param newpreset
127    * @param newarguments
128    */
129   public void updateParameters(final WsParamSetI newpreset,
130           final List<ArgumentI> newarguments)
131   {
132     preset = newpreset;
133     arguments = newarguments;
134     calcMan.startWorker(this);
135     initViewportParams();
136   }
137   protected boolean alignedSeqs = true;
138
139   protected boolean nucleotidesAllowed = false;
140
141   protected boolean proteinAllowed = false;
142
143   /**
144    * record sequences for mapping result back to afterwards
145    */
146   protected boolean bySequence = false;
147
148   protected Map<String, SequenceI> seqNames;
149
150   // TODO: convert to bitset
151   protected boolean[] gapMap;
152
153   int realw;
154
155   protected int start;
156
157   int end;
158
159   private AlignFrame alignFrame;
160
161   public boolean[] getGapMap()
162   {
163     return gapMap;
164   }
165
166   public SeqAnnotationServiceCalcWorker(ServiceWithParameters service,
167           AlignFrame alignFrame,
168           WsParamSetI preset, List<ArgumentI> paramset)
169   {
170     super(alignFrame.getCurrentView(), alignFrame.alignPanel);
171     // TODO: both these fields needed ?
172     this.alignFrame = alignFrame;
173     this.guiProgress = alignFrame;
174     this.preset = preset;
175     this.arguments = paramset;
176     this.service = service;
177     try
178     {
179       annotService = (jalview.ws.api.SequenceAnnotationServiceI) ((JalviewServiceEndpointProviderI) service)
180               .getEndpoint();
181     } catch (ClassCastException cce)
182     {
183       annotService = null;
184       JvOptionPane.showMessageDialog(Desktop.getInstance(),
185               MessageManager.formatMessage(
186                       "label.service_called_is_not_an_annotation_service",
187                       new String[]
188                       { service.getName() }),
189               MessageManager.getString("label.internal_jalview_error"),
190               JvOptionPane.WARNING_MESSAGE);
191
192     }
193     cancellable = CancellableI.class.isInstance(annotService);
194     // configure submission flags
195     proteinAllowed = service.isProteinService();
196     nucleotidesAllowed = service.isNucleotideService();
197     alignedSeqs = service.isNeedsAlignedSequences();
198     bySequence = !service.isAlignmentAnalysis();
199     filterNonStandardResidues = service.isFilterSymbols();
200     min_valid_seqs = service.getMinimumInputSequences();
201     submitGaps = service.isAlignmentAnalysis();
202
203     if (service.isInteractiveUpdate())
204     {
205       initViewportParams();
206     }
207   }
208
209   /**
210    * 
211    * @return true if the submission thread should attempt to submit data
212    */
213   public boolean hasService()
214   {
215     return annotService != null;
216   }
217
218   protected SequenceAnnotationServiceI annotService;
219   protected final boolean cancellable;
220
221   volatile JobId rslt = null;
222
223   AnnotationWsJob running = null;
224
225   private int min_valid_seqs;
226
227
228   private long progressId = -1;
229   JobStateSummary job = null;
230   WebserviceInfo info = null;
231   List<SequenceI> seqs = null;
232   
233   @Override public void startUp() throws Throwable
234   {
235     if (alignViewport.isClosed())
236     {
237       abortAndDestroy();
238       return;
239     }
240     if (!hasService())
241     {
242       return;
243     }
244
245     StringBuffer msg = new StringBuffer();
246     job = new JobStateSummary();
247     info = new WebserviceInfo("foo", "bar", false);
248
249     seqs = getInputSequences(
250             alignViewport.getAlignment(),
251             bySequence ? alignViewport.getSelectionGroup() : null);
252
253     if (seqs == null || !checkValidInputSeqs(seqs))
254     {
255       jalview.bin.Cache.log.debug(
256               "Sequences for analysis service were null or not valid");
257       return;
258     }
259
260     if (guiProgress != null)
261     {
262       guiProgress.setProgressBar(service.getActionText(),
263               progressId = System.currentTimeMillis());
264     }
265     jalview.bin.Cache.log.debug("submitted " + seqs.size()
266             + " sequences to " + service.getActionText());
267
268     rslt = annotService.submitToService(seqs, getPreset(),
269             getArguments());
270     if (rslt == null)
271     {
272       return;
273     }
274     // TODO: handle job submission error reporting here.
275     Cache.log.debug("Service " + service.getUri() + "\nSubmitted job ID: "
276             + rslt);
277     // ///
278     // otherwise, construct WsJob and any UI handlers
279     running = new AnnotationWsJob();
280     running.setJobHandle(rslt);
281     running.setSeqNames(seqNames);
282     running.setStartPos(start);
283     running.setSeqs(seqs);
284     job.updateJobPanelState(info, "", running);
285     if (guiProgress != null)
286     {
287       guiProgress.registerHandler(progressId,
288               new IProgressIndicatorHandler()
289               {
290
291                 @Override
292                 public boolean cancelActivity(long id)
293                 {
294                   calcMan.cancelWorker(SeqAnnotationServiceCalcWorker.this);
295                   return true;
296                 }
297
298                 @Override
299                 public boolean canCancel()
300                 {
301                   return cancellable;
302                 }
303               });
304     }
305   }
306   
307   @Override public boolean poll() throws Throwable
308   {
309     boolean finished = false;
310     
311     Cache.log.debug("Updating status for annotation service.");
312     annotService.updateStatus(running);
313     job.updateJobPanelState(info, "", running);
314     if (running.isSubjobComplete())
315     {
316       Cache.log.debug(
317               "Finished polling analysis service job: status reported is "
318                       + running.getState());
319       finished = true;
320     }
321     else
322     {
323       Cache.log.debug("Status now " + running.getState());
324     }
325
326     // pull any stats - some services need to flush log output before
327     // results are available
328     Cache.log.debug("Updating progress log for annotation service.");
329
330     try
331     {
332       annotService.updateJobProgress(running);
333     } catch (Throwable thr)
334     {
335       Cache.log.debug("Ignoring exception during progress update.",
336               thr);
337     }
338     Cache.log.debug("Result of poll: " + running.getStatus());
339     
340     
341     if (finished)
342     {
343       Cache.log.debug("Job poll loop exited. Job is " + running.getState());
344       if (running.isFinished())
345       {
346         // expect there to be results to collect
347         // configure job with the associated view's feature renderer, if one
348         // exists.
349         // TODO: here one would also grab the 'master feature renderer' in order
350         // to enable/disable
351         // features automatically according to user preferences
352         running.setFeatureRenderer(
353                 ((jalview.gui.AlignmentPanel) ap).cloneFeatureRenderer());
354         Cache.log.debug("retrieving job results.");
355         final Map<String, FeatureColourI> featureColours = new HashMap<>();
356         final Map<String, FeatureMatcherSetI> featureFilters = new HashMap<>();
357         List<AlignmentAnnotation> returnedAnnot = annotService
358                 .getAnnotationResult(running.getJobHandle(), seqs,
359                         featureColours, featureFilters);
360
361         Cache.log.debug("Obtained " + (returnedAnnot == null ? "no rows"
362                 : ("" + returnedAnnot.size())));
363         Cache.log.debug("There were " + featureColours.size()
364                 + " feature colours and " + featureFilters.size()
365                 + " filters defined.");
366
367         // TODO
368         // copy over each annotation row reurned and also defined on each
369         // sequence, excluding regions not annotated due to gapMap/column
370         // visibility
371
372         // update calcId if it is not already set on returned annotation
373         if (returnedAnnot != null)
374         {
375           for (AlignmentAnnotation aa : returnedAnnot)
376           {
377             // assume that any CalcIds already set
378             if (getCalcId() != null && aa.getCalcId() == null
379                     || "".equals(aa.getCalcId()))
380             {
381               aa.setCalcId(getCalcId());
382             }
383             // autocalculated annotation are created by interactive alignment
384             // analysis services
385             aa.autoCalculated = service.isAlignmentAnalysis()
386                     && service.isInteractiveUpdate();
387           }
388         }
389
390         running.setAnnotation(returnedAnnot);
391
392         if (running.hasResults())
393         {
394           jalview.bin.Cache.log.debug("Updating result annotation from Job "
395                   + rslt + " at " + service.getUri());
396           updateResultAnnotation(true);
397           if (running.isTransferSequenceFeatures())
398           {
399             // TODO
400             // look at each sequence and lift over any features, excluding
401             // regions
402             // not annotated due to gapMap/column visibility
403
404             jalview.bin.Cache.log.debug(
405                     "Updating feature display settings and transferring features from Job "
406                             + rslt + " at " + service.getUri());
407             // TODO: consider merge rather than apply here
408             alignViewport.applyFeaturesStyle(new FeatureSettingsAdapter()
409             {
410               @Override
411               public FeatureColourI getFeatureColour(String type)
412               {
413                 return featureColours.get(type);
414               }
415
416               @Override
417               public FeatureMatcherSetI getFeatureFilters(String type)
418               {
419                 return featureFilters.get(type);
420               }
421
422               @Override
423               public boolean isFeatureDisplayed(String type)
424               {
425                 return featureColours.containsKey(type);
426               }
427
428             });
429             // TODO: JAL-1150 - create sequence feature settings API for
430             // defining
431             // styles and enabling/disabling feature overlay on alignment panel
432
433             if (alignFrame.alignPanel == ap)
434             {
435               alignViewport.setShowSequenceFeatures(true);
436               alignFrame.setMenusForViewport();
437             }
438           }
439           ap.adjustAnnotationHeight();
440         }
441       }
442       Cache.log.debug("Annotation Service Worker thread finished.");
443
444     }
445     
446     return finished;
447   }
448   
449   @Override public void cancel()
450   {
451     cancelCurrentJob();
452   }
453   
454   @Override public void done()
455   {
456     if (ap != null)
457     {
458       if (guiProgress != null && progressId != -1)
459       {
460         guiProgress.removeProgressBar(progressId);
461       }
462       // TODO: may not need to paintAlignment again !
463       ap.paintAlignment(false, false);
464     }
465   }
466
467   /**
468    * validate input for dynamic/non-dynamic update context TODO: move to
469    * analysis interface ?
470    * @param seqs
471    * 
472    * @return true if input is valid
473    */
474   boolean checkValidInputSeqs(List<SequenceI> seqs)
475   {
476     int nvalid = 0;
477     for (SequenceI sq : seqs)
478     {
479       if (sq.getStart() <= sq.getEnd()
480               && (sq.isProtein() ? proteinAllowed : nucleotidesAllowed))
481       {
482         if (submitGaps
483                 || sq.getLength() == (sq.getEnd() - sq.getStart() + 1))
484         {
485           nvalid++;
486         }
487       }
488     }
489     return nvalid >= min_valid_seqs;
490   }
491
492   public void cancelCurrentJob()
493   {
494     try
495     {
496       String id = running.getJobId();
497       if (cancellable && ((CancellableI) annotService).cancel(running))
498       {
499         System.err.println("Cancelled job " + id);
500       }
501       else
502       {
503         System.err.println("Job " + id + " couldn't be cancelled.");
504       }
505     } catch (Exception q)
506     {
507       q.printStackTrace();
508     }
509   }
510
511   /**
512    * Interactive updating. Analysis calculations that work on the currently
513    * displayed alignment data should cancel existing jobs when the input data
514    * has changed.
515    * 
516    * @return true if a running job should be cancelled because new input data is
517    *         available for analysis
518    */
519   boolean isInteractiveUpdate()
520   {
521     return service.isInteractiveUpdate();
522   }
523
524   /**
525    * decide what sequences will be analysed TODO: refactor to generate
526    * List<SequenceI> for submission to service interface
527    * 
528    * @param alignment
529    * @param inputSeqs
530    * @return
531    */
532   public List<SequenceI> getInputSequences(AlignmentI alignment,
533           AnnotatedCollectionI inputSeqs)
534   {
535     if (alignment == null || alignment.getWidth() <= 0
536             || alignment.getSequences() == null || alignment.isNucleotide()
537                     ? !nucleotidesAllowed
538                     : !proteinAllowed)
539     {
540       return null;
541     }
542     if (inputSeqs == null || inputSeqs.getWidth() <= 0
543             || inputSeqs.getSequences() == null
544             || inputSeqs.getSequences().size() < 1)
545     {
546       inputSeqs = alignment;
547     }
548
549     List<SequenceI> seqs = new ArrayList<>();
550
551     int minlen = 10;
552     int ln = -1;
553     if (bySequence)
554     {
555       seqNames = new HashMap<>();
556     }
557     gapMap = new boolean[0];
558     start = inputSeqs.getStartRes();
559     end = inputSeqs.getEndRes();
560     // TODO: URGENT! unify with JPred / MSA code to handle hidden regions
561     // correctly
562     // TODO: push attributes into WsJob instance (so they can be safely
563     // persisted/restored
564     for (SequenceI sq : (inputSeqs.getSequences()))
565     {
566       if (bySequence
567               ? sq.findPosition(end + 1)
568                       - sq.findPosition(start + 1) > minlen - 1
569               : sq.getEnd() - sq.getStart() > minlen - 1)
570       {
571         String newname = SeqsetUtils.unique_name(seqs.size() + 1);
572         // make new input sequence with or without gaps
573         if (seqNames != null)
574         {
575           seqNames.put(newname, sq);
576         }
577         SequenceI seq;
578         if (submitGaps)
579         {
580           seqs.add(seq = new jalview.datamodel.Sequence(newname,
581                   sq.getSequenceAsString()));
582           if (gapMap == null || gapMap.length < seq.getLength())
583           {
584             boolean[] tg = gapMap;
585             gapMap = new boolean[seq.getLength()];
586             System.arraycopy(tg, 0, gapMap, 0, tg.length);
587             for (int p = tg.length; p < gapMap.length; p++)
588             {
589               gapMap[p] = false; // init as a gap
590             }
591           }
592           for (int apos : sq.gapMap())
593           {
594             char sqc = sq.getCharAt(apos);
595             if (!filterNonStandardResidues
596                     || (sq.isProtein() ? ResidueProperties.aaIndex[sqc] < 20
597                             : ResidueProperties.nucleotideIndex[sqc] < 5))
598             {
599               gapMap[apos] = true; // aligned and real amino acid residue
600             }
601             ;
602           }
603         }
604         else
605         {
606           // TODO: add ability to exclude hidden regions
607           seqs.add(seq = new jalview.datamodel.Sequence(newname,
608                   AlignSeq.extractGaps(jalview.util.Comparison.GapChars,
609                           sq.getSequenceAsString(start, end + 1))));
610           // for annotation need to also record map to sequence start/end
611           // position in range
612           // then transfer back to original sequence on return.
613         }
614         if (seq.getLength() > ln)
615         {
616           ln = seq.getLength();
617         }
618       }
619     }
620     if (alignedSeqs && submitGaps)
621     {
622       realw = 0;
623       for (int i = 0; i < gapMap.length; i++)
624       {
625         if (gapMap[i])
626         {
627           realw++;
628         }
629       }
630       // try real hard to return something submittable
631       // TODO: some of AAcon measures need a minimum of two or three amino
632       // acids at each position, and AAcon doesn't gracefully degrade.
633       for (int p = 0; p < seqs.size(); p++)
634       {
635         SequenceI sq = seqs.get(p);
636         // strip gapped columns
637         char[] padded = new char[realw],
638                 orig = sq.getSequence();
639         for (int i = 0, pp = 0; i < realw; pp++)
640         {
641           if (gapMap[pp])
642           {
643             if (orig.length > pp)
644             {
645               padded[i++] = orig[pp];
646             }
647             else
648             {
649               padded[i++] = '-';
650             }
651           }
652         }
653         seqs.set(p, new jalview.datamodel.Sequence(sq.getName(),
654                 new String(padded)));
655       }
656     }
657     return seqs;
658   }
659
660   @Override
661   public void updateAnnotation()
662   {
663     updateResultAnnotation(false);
664   }
665
666   public void updateResultAnnotation(boolean immediate)
667   {
668     if ((immediate || !calcMan.isWorking(this)) && running != null
669             && running.hasResults())
670     {
671       List<AlignmentAnnotation> ourAnnot = running.getAnnotation(),
672               newAnnots = new ArrayList<>();
673       //
674       // update graphGroup for all annotation
675       //
676       /**
677        * find a graphGroup greater than any existing ones this could be a method
678        * provided by alignment Alignment.getNewGraphGroup() - returns next
679        * unused graph group
680        */
681       int graphGroup = 1;
682       if (alignViewport.getAlignment().getAlignmentAnnotation() != null)
683       {
684         for (AlignmentAnnotation ala : alignViewport.getAlignment()
685                 .getAlignmentAnnotation())
686         {
687           if (ala.graphGroup > graphGroup)
688           {
689             graphGroup = ala.graphGroup;
690           }
691         }
692       }
693       /**
694        * update graphGroup in the annotation rows returned from service
695        */
696       // TODO: look at sequence annotation rows and update graph groups in the
697       // case of reference annotation.
698       for (AlignmentAnnotation ala : ourAnnot)
699       {
700         if (ala.graphGroup > 0)
701         {
702           ala.graphGroup += graphGroup;
703         }
704         SequenceI aseq = null;
705
706         /**
707          * transfer sequence refs and adjust gapmap
708          */
709         if (ala.sequenceRef != null)
710         {
711           SequenceI seq = running.getSeqNames()
712                   .get(ala.sequenceRef.getName());
713           aseq = seq;
714           while (seq.getDatasetSequence() != null)
715           {
716             seq = seq.getDatasetSequence();
717           }
718         }
719         Annotation[] resAnnot = ala.annotations,
720                 gappedAnnot = new Annotation[Math.max(
721                         alignViewport.getAlignment().getWidth(),
722                         gapMap.length)];
723         for (int p = 0, ap = start; ap < gappedAnnot.length; ap++)
724         {
725           if (gapMap != null && gapMap.length > ap && !gapMap[ap])
726           {
727             gappedAnnot[ap] = new Annotation("", "", ' ', Float.NaN);
728           }
729           else if (p < resAnnot.length)
730           {
731             gappedAnnot[ap] = resAnnot[p++];
732           }
733         }
734         ala.sequenceRef = aseq;
735         ala.annotations = gappedAnnot;
736         AlignmentAnnotation newAnnot = getAlignViewport().getAlignment()
737                 .updateFromOrCopyAnnotation(ala);
738         if (aseq != null)
739         {
740
741           aseq.addAlignmentAnnotation(newAnnot);
742           newAnnot.adjustForAlignment();
743
744           AlignmentAnnotationUtils.replaceAnnotationOnAlignmentWith(
745                   newAnnot, newAnnot.label, newAnnot.getCalcId());
746         }
747         newAnnots.add(newAnnot);
748
749       }
750       for (SequenceI sq : running.getSeqs())
751       {
752         if (!sq.getFeatures().hasFeatures()
753                 && (sq.getDBRefs() == null || sq.getDBRefs().size() == 0))
754         {
755           continue;
756         }
757         running.setTransferSequenceFeatures(true);
758         SequenceI seq = running.getSeqNames().get(sq.getName());
759         SequenceI dseq;
760         ContiguousI seqRange = seq.findPositions(start, end);
761
762         while ((dseq = seq).getDatasetSequence() != null)
763         {
764           seq = seq.getDatasetSequence();
765         }
766         List<ContiguousI> sourceRange = new ArrayList();
767         if (gapMap != null && gapMap.length >= end)
768         {
769           int lastcol = start, col = start;
770           do
771           {
772             if (col == end || !gapMap[col])
773             {
774               if (lastcol <= (col - 1))
775               {
776                 seqRange = seq.findPositions(lastcol, col);
777                 sourceRange.add(seqRange);
778               }
779               lastcol = col + 1;
780             }
781           } while (++col <= end);
782         }
783         else
784         {
785           sourceRange.add(seq.findPositions(start, end));
786         }
787         int i = 0;
788         int source_startend[] = new int[sourceRange.size() * 2];
789
790         for (ContiguousI range : sourceRange)
791         {
792           source_startend[i++] = range.getBegin();
793           source_startend[i++] = range.getEnd();
794         }
795         Mapping mp = new Mapping(
796                 new MapList(source_startend, new int[]
797                 { seq.getStart(), seq.getEnd() }, 1, 1));
798         dseq.transferAnnotation(sq, mp);
799
800       }
801       updateOurAnnots(newAnnots);
802     }
803   }
804
805   protected void updateOurAnnots(List<AlignmentAnnotation> ourAnnot)
806   {
807     List<AlignmentAnnotation> our = ourAnnots;
808     ourAnnots = ourAnnot;
809     AlignmentI alignment = alignViewport.getAlignment();
810     if (our != null)
811     {
812       if (our.size() > 0)
813       {
814         for (AlignmentAnnotation an : our)
815         {
816           if (!ourAnnots.contains(an))
817           {
818             // remove the old annotation
819             alignment.deleteAnnotation(an);
820           }
821         }
822       }
823       our.clear();
824     }
825
826     // validate rows and update Alignmment state
827     for (AlignmentAnnotation an : ourAnnots)
828     {
829       alignViewport.getAlignment().validateAnnotation(an);
830     }
831     // TODO: may need a menu refresh after this
832     // af.setMenusForViewport();
833     ap.adjustAnnotationHeight();
834
835   }
836
837   public SequenceAnnotationServiceI getService()
838   {
839     return annotService;
840   }
841
842 }