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