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