JAL-3878 Add getCalcName to AlignCalcWorkerI.
[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
234   public String getCalcName()
235   {
236     return service.getName();
237   }
238
239   @Override public void startUp() throws Throwable
240   {
241     if (alignViewport.isClosed())
242     {
243       abortAndDestroy();
244       return;
245     }
246     if (!hasService())
247     {
248       return;
249     }
250
251     StringBuffer msg = new StringBuffer();
252     job = new JobStateSummary();
253     info = new WebserviceInfo("foo", "bar", false);
254
255     seqs = getInputSequences(
256             alignViewport.getAlignment(),
257             bySequence ? alignViewport.getSelectionGroup() : null);
258
259     if (seqs == null || !checkValidInputSeqs(seqs))
260     {
261       jalview.bin.Cache.log.debug(
262               "Sequences for analysis service were null or not valid");
263       return;
264     }
265
266     if (guiProgress != null)
267     {
268       guiProgress.setProgressBar(service.getActionText(),
269               progressId = System.currentTimeMillis());
270     }
271     jalview.bin.Cache.log.debug("submitted " + seqs.size()
272             + " sequences to " + service.getActionText());
273
274     rslt = annotService.submitToService(seqs, getPreset(),
275             getArguments());
276     if (rslt == null)
277     {
278       return;
279     }
280     // TODO: handle job submission error reporting here.
281     Cache.log.debug("Service " + service.getUri() + "\nSubmitted job ID: "
282             + rslt);
283     // ///
284     // otherwise, construct WsJob and any UI handlers
285     running = new AnnotationWsJob();
286     running.setJobHandle(rslt);
287     running.setSeqNames(seqNames);
288     running.setStartPos(start);
289     running.setSeqs(seqs);
290     job.updateJobPanelState(info, "", running);
291     if (guiProgress != null)
292     {
293       guiProgress.registerHandler(progressId,
294               new IProgressIndicatorHandler()
295               {
296
297                 @Override
298                 public boolean cancelActivity(long id)
299                 {
300                   calcMan.cancelWorker(SeqAnnotationServiceCalcWorker.this);
301                   return true;
302                 }
303
304                 @Override
305                 public boolean canCancel()
306                 {
307                   return cancellable;
308                 }
309               });
310     }
311   }
312   
313   @Override public boolean poll() throws Throwable
314   {
315     boolean finished = false;
316     
317     Cache.log.debug("Updating status for annotation service.");
318     annotService.updateStatus(running);
319     job.updateJobPanelState(info, "", running);
320     if (running.isSubjobComplete())
321     {
322       Cache.log.debug(
323               "Finished polling analysis service job: status reported is "
324                       + running.getState());
325       finished = true;
326     }
327     else
328     {
329       Cache.log.debug("Status now " + running.getState());
330     }
331
332     // pull any stats - some services need to flush log output before
333     // results are available
334     Cache.log.debug("Updating progress log for annotation service.");
335
336     try
337     {
338       annotService.updateJobProgress(running);
339     } catch (Throwable thr)
340     {
341       Cache.log.debug("Ignoring exception during progress update.",
342               thr);
343     }
344     Cache.log.debug("Result of poll: " + running.getStatus());
345     
346     
347     if (finished)
348     {
349       Cache.log.debug("Job poll loop exited. Job is " + running.getState());
350       if (running.isFinished())
351       {
352         // expect there to be results to collect
353         // configure job with the associated view's feature renderer, if one
354         // exists.
355         // TODO: here one would also grab the 'master feature renderer' in order
356         // to enable/disable
357         // features automatically according to user preferences
358         running.setFeatureRenderer(
359                 ((jalview.gui.AlignmentPanel) ap).cloneFeatureRenderer());
360         Cache.log.debug("retrieving job results.");
361         final Map<String, FeatureColourI> featureColours = new HashMap<>();
362         final Map<String, FeatureMatcherSetI> featureFilters = new HashMap<>();
363         List<AlignmentAnnotation> returnedAnnot = annotService
364                 .getAnnotationResult(running.getJobHandle(), seqs,
365                         featureColours, featureFilters);
366
367         Cache.log.debug("Obtained " + (returnedAnnot == null ? "no rows"
368                 : ("" + returnedAnnot.size())));
369         Cache.log.debug("There were " + featureColours.size()
370                 + " feature colours and " + featureFilters.size()
371                 + " filters defined.");
372
373         // TODO
374         // copy over each annotation row reurned and also defined on each
375         // sequence, excluding regions not annotated due to gapMap/column
376         // visibility
377
378         // update calcId if it is not already set on returned annotation
379         if (returnedAnnot != null)
380         {
381           for (AlignmentAnnotation aa : returnedAnnot)
382           {
383             // assume that any CalcIds already set
384             if (getCalcId() != null && aa.getCalcId() == null
385                     || "".equals(aa.getCalcId()))
386             {
387               aa.setCalcId(getCalcId());
388             }
389             // autocalculated annotation are created by interactive alignment
390             // analysis services
391             aa.autoCalculated = service.isAlignmentAnalysis()
392                     && service.isInteractiveUpdate();
393           }
394         }
395
396         running.setAnnotation(returnedAnnot);
397
398         if (running.hasResults())
399         {
400           jalview.bin.Cache.log.debug("Updating result annotation from Job "
401                   + rslt + " at " + service.getUri());
402           updateResultAnnotation(true);
403           if (running.isTransferSequenceFeatures())
404           {
405             // TODO
406             // look at each sequence and lift over any features, excluding
407             // regions
408             // not annotated due to gapMap/column visibility
409
410             jalview.bin.Cache.log.debug(
411                     "Updating feature display settings and transferring features from Job "
412                             + rslt + " at " + service.getUri());
413             // TODO: consider merge rather than apply here
414             alignViewport.applyFeaturesStyle(new FeatureSettingsAdapter()
415             {
416               @Override
417               public FeatureColourI getFeatureColour(String type)
418               {
419                 return featureColours.get(type);
420               }
421
422               @Override
423               public FeatureMatcherSetI getFeatureFilters(String type)
424               {
425                 return featureFilters.get(type);
426               }
427
428               @Override
429               public boolean isFeatureDisplayed(String type)
430               {
431                 return featureColours.containsKey(type);
432               }
433
434             });
435             // TODO: JAL-1150 - create sequence feature settings API for
436             // defining
437             // styles and enabling/disabling feature overlay on alignment panel
438
439             if (alignFrame.alignPanel == ap)
440             {
441               alignViewport.setShowSequenceFeatures(true);
442               alignFrame.setMenusForViewport();
443             }
444           }
445           ap.adjustAnnotationHeight();
446         }
447       }
448       Cache.log.debug("Annotation Service Worker thread finished.");
449
450     }
451     
452     return finished;
453   }
454   
455   @Override public void cancel()
456   {
457     cancelCurrentJob();
458   }
459   
460   @Override public void done()
461   {
462     if (ap != null)
463     {
464       if (guiProgress != null && progressId != -1)
465       {
466         guiProgress.removeProgressBar(progressId);
467       }
468       // TODO: may not need to paintAlignment again !
469       ap.paintAlignment(false, false);
470     }
471   }
472
473   /**
474    * validate input for dynamic/non-dynamic update context TODO: move to
475    * analysis interface ?
476    * @param seqs
477    * 
478    * @return true if input is valid
479    */
480   boolean checkValidInputSeqs(List<SequenceI> seqs)
481   {
482     int nvalid = 0;
483     for (SequenceI sq : seqs)
484     {
485       if (sq.getStart() <= sq.getEnd()
486               && (sq.isProtein() ? proteinAllowed : nucleotidesAllowed))
487       {
488         if (submitGaps
489                 || sq.getLength() == (sq.getEnd() - sq.getStart() + 1))
490         {
491           nvalid++;
492         }
493       }
494     }
495     return nvalid >= min_valid_seqs;
496   }
497
498   public void cancelCurrentJob()
499   {
500     try
501     {
502       String id = running.getJobId();
503       if (cancellable && ((CancellableI) annotService).cancel(running))
504       {
505         System.err.println("Cancelled job " + id);
506       }
507       else
508       {
509         System.err.println("Job " + id + " couldn't be cancelled.");
510       }
511     } catch (Exception q)
512     {
513       q.printStackTrace();
514     }
515   }
516
517   /**
518    * Interactive updating. Analysis calculations that work on the currently
519    * displayed alignment data should cancel existing jobs when the input data
520    * has changed.
521    * 
522    * @return true if a running job should be cancelled because new input data is
523    *         available for analysis
524    */
525   boolean isInteractiveUpdate()
526   {
527     return service.isInteractiveUpdate();
528   }
529
530   /**
531    * decide what sequences will be analysed TODO: refactor to generate
532    * List<SequenceI> for submission to service interface
533    * 
534    * @param alignment
535    * @param inputSeqs
536    * @return
537    */
538   public List<SequenceI> getInputSequences(AlignmentI alignment,
539           AnnotatedCollectionI inputSeqs)
540   {
541     if (alignment == null || alignment.getWidth() <= 0
542             || alignment.getSequences() == null || alignment.isNucleotide()
543                     ? !nucleotidesAllowed
544                     : !proteinAllowed)
545     {
546       return null;
547     }
548     if (inputSeqs == null || inputSeqs.getWidth() <= 0
549             || inputSeqs.getSequences() == null
550             || inputSeqs.getSequences().size() < 1)
551     {
552       inputSeqs = alignment;
553     }
554
555     List<SequenceI> seqs = new ArrayList<>();
556
557     int minlen = 10;
558     int ln = -1;
559     if (bySequence)
560     {
561       seqNames = new HashMap<>();
562     }
563     gapMap = new boolean[0];
564     start = inputSeqs.getStartRes();
565     end = inputSeqs.getEndRes();
566     // TODO: URGENT! unify with JPred / MSA code to handle hidden regions
567     // correctly
568     // TODO: push attributes into WsJob instance (so they can be safely
569     // persisted/restored
570     for (SequenceI sq : (inputSeqs.getSequences()))
571     {
572       if (bySequence
573               ? sq.findPosition(end + 1)
574                       - sq.findPosition(start + 1) > minlen - 1
575               : sq.getEnd() - sq.getStart() > minlen - 1)
576       {
577         String newname = SeqsetUtils.unique_name(seqs.size() + 1);
578         // make new input sequence with or without gaps
579         if (seqNames != null)
580         {
581           seqNames.put(newname, sq);
582         }
583         SequenceI seq;
584         if (submitGaps)
585         {
586           seqs.add(seq = new jalview.datamodel.Sequence(newname,
587                   sq.getSequenceAsString()));
588           if (gapMap == null || gapMap.length < seq.getLength())
589           {
590             boolean[] tg = gapMap;
591             gapMap = new boolean[seq.getLength()];
592             System.arraycopy(tg, 0, gapMap, 0, tg.length);
593             for (int p = tg.length; p < gapMap.length; p++)
594             {
595               gapMap[p] = false; // init as a gap
596             }
597           }
598           for (int apos : sq.gapMap())
599           {
600             char sqc = sq.getCharAt(apos);
601             if (!filterNonStandardResidues
602                     || (sq.isProtein() ? ResidueProperties.aaIndex[sqc] < 20
603                             : ResidueProperties.nucleotideIndex[sqc] < 5))
604             {
605               gapMap[apos] = true; // aligned and real amino acid residue
606             }
607             ;
608           }
609         }
610         else
611         {
612           // TODO: add ability to exclude hidden regions
613           seqs.add(seq = new jalview.datamodel.Sequence(newname,
614                   AlignSeq.extractGaps(jalview.util.Comparison.GapChars,
615                           sq.getSequenceAsString(start, end + 1))));
616           // for annotation need to also record map to sequence start/end
617           // position in range
618           // then transfer back to original sequence on return.
619         }
620         if (seq.getLength() > ln)
621         {
622           ln = seq.getLength();
623         }
624       }
625     }
626     if (alignedSeqs && submitGaps)
627     {
628       realw = 0;
629       for (int i = 0; i < gapMap.length; i++)
630       {
631         if (gapMap[i])
632         {
633           realw++;
634         }
635       }
636       // try real hard to return something submittable
637       // TODO: some of AAcon measures need a minimum of two or three amino
638       // acids at each position, and AAcon doesn't gracefully degrade.
639       for (int p = 0; p < seqs.size(); p++)
640       {
641         SequenceI sq = seqs.get(p);
642         // strip gapped columns
643         char[] padded = new char[realw],
644                 orig = sq.getSequence();
645         for (int i = 0, pp = 0; i < realw; pp++)
646         {
647           if (gapMap[pp])
648           {
649             if (orig.length > pp)
650             {
651               padded[i++] = orig[pp];
652             }
653             else
654             {
655               padded[i++] = '-';
656             }
657           }
658         }
659         seqs.set(p, new jalview.datamodel.Sequence(sq.getName(),
660                 new String(padded)));
661       }
662     }
663     return seqs;
664   }
665
666   @Override
667   public void updateAnnotation()
668   {
669     updateResultAnnotation(false);
670   }
671
672   public void updateResultAnnotation(boolean immediate)
673   {
674     if ((immediate || !calcMan.isWorking(this)) && running != null
675             && running.hasResults())
676     {
677       List<AlignmentAnnotation> ourAnnot = running.getAnnotation(),
678               newAnnots = new ArrayList<>();
679       //
680       // update graphGroup for all annotation
681       //
682       /**
683        * find a graphGroup greater than any existing ones this could be a method
684        * provided by alignment Alignment.getNewGraphGroup() - returns next
685        * unused graph group
686        */
687       int graphGroup = 1;
688       if (alignViewport.getAlignment().getAlignmentAnnotation() != null)
689       {
690         for (AlignmentAnnotation ala : alignViewport.getAlignment()
691                 .getAlignmentAnnotation())
692         {
693           if (ala.graphGroup > graphGroup)
694           {
695             graphGroup = ala.graphGroup;
696           }
697         }
698       }
699       /**
700        * update graphGroup in the annotation rows returned from service
701        */
702       // TODO: look at sequence annotation rows and update graph groups in the
703       // case of reference annotation.
704       for (AlignmentAnnotation ala : ourAnnot)
705       {
706         if (ala.graphGroup > 0)
707         {
708           ala.graphGroup += graphGroup;
709         }
710         SequenceI aseq = null;
711
712         /**
713          * transfer sequence refs and adjust gapmap
714          */
715         if (ala.sequenceRef != null)
716         {
717           SequenceI seq = running.getSeqNames()
718                   .get(ala.sequenceRef.getName());
719           aseq = seq;
720           while (seq.getDatasetSequence() != null)
721           {
722             seq = seq.getDatasetSequence();
723           }
724         }
725         Annotation[] resAnnot = ala.annotations,
726                 gappedAnnot = new Annotation[Math.max(
727                         alignViewport.getAlignment().getWidth(),
728                         gapMap.length)];
729         for (int p = 0, ap = start; ap < gappedAnnot.length; ap++)
730         {
731           if (gapMap != null && gapMap.length > ap && !gapMap[ap])
732           {
733             gappedAnnot[ap] = new Annotation("", "", ' ', Float.NaN);
734           }
735           else if (p < resAnnot.length)
736           {
737             gappedAnnot[ap] = resAnnot[p++];
738           }
739         }
740         ala.sequenceRef = aseq;
741         ala.annotations = gappedAnnot;
742         AlignmentAnnotation newAnnot = getAlignViewport().getAlignment()
743                 .updateFromOrCopyAnnotation(ala);
744         if (aseq != null)
745         {
746
747           aseq.addAlignmentAnnotation(newAnnot);
748           newAnnot.adjustForAlignment();
749
750           AlignmentAnnotationUtils.replaceAnnotationOnAlignmentWith(
751                   newAnnot, newAnnot.label, newAnnot.getCalcId());
752         }
753         newAnnots.add(newAnnot);
754
755       }
756       for (SequenceI sq : running.getSeqs())
757       {
758         if (!sq.getFeatures().hasFeatures()
759                 && (sq.getDBRefs() == null || sq.getDBRefs().size() == 0))
760         {
761           continue;
762         }
763         running.setTransferSequenceFeatures(true);
764         SequenceI seq = running.getSeqNames().get(sq.getName());
765         SequenceI dseq;
766         ContiguousI seqRange = seq.findPositions(start, end);
767
768         while ((dseq = seq).getDatasetSequence() != null)
769         {
770           seq = seq.getDatasetSequence();
771         }
772         List<ContiguousI> sourceRange = new ArrayList();
773         if (gapMap != null && gapMap.length >= end)
774         {
775           int lastcol = start, col = start;
776           do
777           {
778             if (col == end || !gapMap[col])
779             {
780               if (lastcol <= (col - 1))
781               {
782                 seqRange = seq.findPositions(lastcol, col);
783                 sourceRange.add(seqRange);
784               }
785               lastcol = col + 1;
786             }
787           } while (++col <= end);
788         }
789         else
790         {
791           sourceRange.add(seq.findPositions(start, end));
792         }
793         int i = 0;
794         int source_startend[] = new int[sourceRange.size() * 2];
795
796         for (ContiguousI range : sourceRange)
797         {
798           source_startend[i++] = range.getBegin();
799           source_startend[i++] = range.getEnd();
800         }
801         Mapping mp = new Mapping(
802                 new MapList(source_startend, new int[]
803                 { seq.getStart(), seq.getEnd() }, 1, 1));
804         dseq.transferAnnotation(sq, mp);
805
806       }
807       updateOurAnnots(newAnnots);
808     }
809   }
810
811   protected void updateOurAnnots(List<AlignmentAnnotation> ourAnnot)
812   {
813     List<AlignmentAnnotation> our = ourAnnots;
814     ourAnnots = ourAnnot;
815     AlignmentI alignment = alignViewport.getAlignment();
816     if (our != null)
817     {
818       if (our.size() > 0)
819       {
820         for (AlignmentAnnotation an : our)
821         {
822           if (!ourAnnots.contains(an))
823           {
824             // remove the old annotation
825             alignment.deleteAnnotation(an);
826           }
827         }
828       }
829       our.clear();
830     }
831
832     // validate rows and update Alignmment state
833     for (AlignmentAnnotation an : ourAnnots)
834     {
835       alignViewport.getAlignment().validateAnnotation(an);
836     }
837     // TODO: may need a menu refresh after this
838     // af.setMenusForViewport();
839     ap.adjustAnnotationHeight();
840
841   }
842
843   public SequenceAnnotationServiceI getService()
844   {
845     return annotService;
846   }
847
848 }