JAL-976 clean out associated annotation
[jalview.git] / src / jalview / ws / jws2 / AAConsClient.java
1 package jalview.ws.jws2;
2
3 import jalview.datamodel.AlignmentAnnotation;
4 import jalview.datamodel.AlignmentI;
5 import jalview.datamodel.Annotation;
6 import jalview.gui.AlignFrame;
7 import jalview.gui.AlignmentPanel;
8 import jalview.ws.jws2.dm.AAConsSettings;
9 import jalview.ws.jws2.jabaws2.Jws2Instance;
10 import jalview.ws.params.WsParamSetI;
11
12 import java.util.ArrayList;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.TreeSet;
17
18 import compbio.data.sequence.Score;
19 import compbio.metadata.Argument;
20
21 public class AAConsClient extends JabawsAlignCalcWorker
22 {
23
24   public AAConsClient(Jws2Instance service, AlignFrame alignFrame,
25           WsParamSetI preset, List<Argument> paramset)
26   {
27     super(service, alignFrame, preset, paramset);
28     submitGaps = true;
29     alignedSeqs = true;
30     nucleotidesAllowed = false;
31     proteinAllowed = true;
32     gapMap=new boolean[0];
33     initViewportParams();
34   }
35
36   protected void initViewportParams()
37   {
38     ((jalview.gui.AlignViewport) alignViewport).setCalcIdSettingsFor(getCalcId(), new AAConsSettings(true, service, this.preset, (arguments!=null) ?  JabaParamStore.getJwsArgsfromJaba(arguments) : null), true);
39   }
40   @Override
41   public void updateParameters(WsParamSetI newpreset, java.util.List<Argument> newarguments) {
42     super.updateParameters(newpreset, newarguments);
43     initViewportParams();
44   };
45   public String getServiceActionText()
46   {
47     return "calculating Amino acid consensus using AACons service";
48   }
49
50   /**
51    * update the consensus annotation from the sequence profile data using
52    * current visualization settings.
53    */
54
55   public void updateResultAnnotation(boolean immediate)
56   {
57     if (immediate || !calcMan.isWorking(this) && scoremanager != null)
58     {
59       AlignmentAnnotation annotation;
60       Map<String, TreeSet<Score>> scoremap = scoremanager.asMap();
61       int alWidth = alignViewport.getAlignment().getWidth();
62       AlignmentI alignment;
63       int ann = (alignment = alignViewport.getAlignment())
64               .getAlignmentAnnotation().length;
65       ArrayList<AlignmentAnnotation> ourAnnot = new ArrayList<AlignmentAnnotation>();
66       for (String score : scoremap.keySet())
67       {
68         TreeSet<Score> scores = scoremap.get(score);
69         for (Score scr : scores)
70         {
71           if (scr.getRanges() != null && scr.getRanges().size() > 0)
72           {
73             /**
74              * annotation in range annotation = findOrCreate(scr.getMethod(),
75              * true, null, null); Annotation[] elm = new Annotation[alWidth];
76              * Iterator<Float> vals = scr.getScores().iterator(); for (Range rng
77              * : scr.getRanges()) { float val = vals.next().floatValue(); for
78              * (int i = rng.from; i <= rng.to; i++) { elm[i] = new
79              * Annotation("", "", ' ', val); } } annotation.annotations = elm;
80              * annotation.validateRangeAndDisplay();
81              */
82           }
83           else
84           {
85             // simple annotation row
86             annotation = alignViewport.getAlignment()
87                     .findOrCreateAnnotation(scr.getMethod(), getCalcId(), true,
88                             null, null);
89
90             Annotation[] elm = new Annotation[alWidth];
91             if (alWidth == gapMap.length) // scr.getScores().size())
92             {
93               Iterator<Float> vals = scr.getScores().iterator();
94               float m = 0f, x = 0f;
95               for (int i = 0; vals.hasNext(); i++)
96               {
97                 float val = vals.next().floatValue();
98                 if (i == 0)
99                 {
100                   m = val;
101                   x = val;
102                 }
103                 else
104                 {
105                   if (m > val)
106                   {
107                     m = val;
108                   }
109                   ;
110                   if (x < val)
111                   {
112                     x = val;
113                   }
114                 }
115                 // if we're at a gapped column then skip to next ungapped position
116                 if (gapMap != null && gapMap.length > 0)
117                 {
118                   while (!gapMap[i])
119                   {
120                     elm[i++]=new Annotation("", "" , ' ', Float.NaN);
121                   }
122                 }
123                 elm[i] = new Annotation("", "" + val, ' ', val);
124               }
125
126               annotation.annotations = elm;
127               annotation.belowAlignment = true;
128               if (x < 0)
129               {
130                 x = 0;
131               }
132               x += (x - m) * 0.1;
133               annotation.graphMax = x;
134               annotation.graphMin = m;
135               annotation.validateRangeAndDisplay();
136               ourAnnot.add(annotation);
137             }
138           }
139         }
140       }
141       if (ourAnnot.size() > 0)
142       {
143         List<AlignmentAnnotation> our = ourAnnots;
144         ourAnnots = ourAnnot;
145         if (our != null)
146         {
147           if (our.size() > 0)
148           {
149             for (AlignmentAnnotation an : our)
150             {
151               if (!ourAnnots.contains(an))
152               {
153                 // remove the old annotation
154                 alignment.deleteAnnotation(an);
155               }
156             }
157           }
158           our.clear();
159         }
160       }
161       // if (ann !=
162       // alignViewport.getAlignment().getAlignmentAnnotation().length)
163       {
164         ap.adjustAnnotationHeight();
165       }
166       /*
167        * else { ap.paintAlignment(true); }
168        */
169     }
170   }
171
172   public String getCalcId()
173   {
174     return SequenceAnnotationWSClient.AAConsCalcId;
175   }
176
177   public static void removeAAConsAnnotation(AlignmentPanel alignPanel)
178   {
179     for (AlignmentAnnotation aa: alignPanel.getAlignment().findAnnotation(SequenceAnnotationWSClient.AAConsCalcId))
180     {
181       alignPanel.getAlignment().deleteAnnotation(aa);
182     }
183     // List<AlignmentAnnotation>
184     // ourannots=alignPanel.getAlignment().findAnnotation(Screen3D.)
185     // TODO Auto-generated method stub
186
187   }
188 }