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