(JAL-974,JAL-975) refactor common code for a JABAWS sequence annotation client to...
[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.ws.jws2.jabaws2.Jws2Instance;
8 import jalview.ws.params.WsParamSetI;
9
10 import java.util.ArrayList;
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.TreeSet;
15
16 import compbio.data.sequence.Score;
17 import compbio.metadata.Argument;
18
19 public class AAConsClient extends JabawsAlignCalcWorker
20 {
21
22   public AAConsClient(Jws2Instance service, AlignFrame alignFrame,
23           WsParamSetI preset, List<Argument> paramset)
24   {
25     super(service, alignFrame, preset, paramset);
26     submitGaps=true;
27     alignedSeqs=true;
28     nucleotidesAllowed=false;
29     proteinAllowed=true;
30   }
31
32   public String getServiceActionText()
33   {
34     return "calculating Amino acid consensus using AACons service";
35   }
36
37   /**
38    * update the consensus annotation from the sequence profile data using
39    * current visualization settings.
40    */
41
42   public void updateResultAnnotation(boolean immediate)
43   {
44
45     if (immediate || !calcMan.isWorking(this) && scoremanager != null)
46     {
47       AlignmentAnnotation annotation;
48       ;
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 = findOrCreate(scr.getMethod(), true, null, null);
76             Annotation[] elm = new Annotation[alWidth];
77             if (alWidth == scr.getScores().size())
78             {
79               Iterator<Float> vals = scr.getScores().iterator();
80               float m = 0f, x = 0f;
81               for (int i = 0; vals.hasNext(); i++)
82               {
83                 float val = vals.next().floatValue();
84                 if (i == 0)
85                 {
86                   m = val;
87                   x = val;
88                 }
89                 else
90                 {
91                   if (m > val)
92                   {
93                     m = val;
94                   }
95                   ;
96                   if (x < val)
97                   {
98                     x = val;
99                   }
100                 }
101                 elm[i] = new Annotation("", "" + val, ' ', val);
102               }
103
104               annotation.annotations = elm;
105               annotation.belowAlignment = true;
106               if (x < 0)
107               {
108                 x = 0;
109               }
110               x += (x - m) * 0.1;
111               annotation.graphMax = x;
112               annotation.graphMin = m;
113               annotation.validateRangeAndDisplay();
114               ourAnnot.add(annotation);
115             }
116           }
117         }
118       }
119       if (ourAnnot.size() > 0)
120       {
121         List<AlignmentAnnotation> our = ourAnnots;
122         ourAnnots = ourAnnot;
123         if (our != null)
124         {
125           if (our.size() > 0)
126           {
127             for (AlignmentAnnotation an : our)
128             {
129               if (!ourAnnots.contains(an))
130               {
131                 // remove the old annotation
132                 alignment.deleteAnnotation(an);
133               }
134             }
135           }
136           our.clear();
137         }
138       }
139       // if (ann !=
140       // alignViewport.getAlignment().getAlignmentAnnotation().length)
141       {
142         ap.adjustAnnotationHeight();
143       }
144       /*
145        * else { ap.paintAlignment(true); }
146        */
147     }
148   }
149
150 }