JAL-974 set GlobPlot raw/smooth score annotation rows to be hidden by default
[jalview.git] / src / jalview / ws / jws2 / AADisorderClient.java
1 package jalview.ws.jws2;
2
3 import jalview.api.AlignCalcWorkerI;
4 import jalview.bin.Cache;
5 import jalview.datamodel.AlignmentAnnotation;
6
7 import jalview.datamodel.SequenceFeature;
8 import jalview.datamodel.SequenceI;
9 import jalview.gui.AlignFrame;
10 import jalview.schemes.GraduatedColor;
11 import jalview.schemes.UserColourScheme;
12 import jalview.ws.jws2.jabaws2.Jws2Instance;
13 import jalview.ws.params.WsParamSetI;
14
15 import java.awt.Color;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.Hashtable;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Map;
22 import compbio.data.sequence.Range;
23 import compbio.data.sequence.Score;
24 import compbio.data.sequence.ScoreManager.ScoreHolder;
25 import compbio.metadata.Argument;
26 import compbio.ws.client.Services;
27
28 public class AADisorderClient extends JabawsAlignCalcWorker implements
29         AlignCalcWorkerI
30 {
31
32   String typeName;
33
34   String methodName;
35
36   String groupName;
37
38   AlignFrame af;
39
40   public AADisorderClient(Jws2Instance sh, AlignFrame alignFrame,
41           WsParamSetI preset, List<Argument> paramset)
42   {
43     super(sh, alignFrame, preset, paramset);
44     af = alignFrame;
45     typeName = sh.action;
46     methodName = sh.serviceType;
47
48     submitGaps = false;
49     alignedSeqs = false;
50     nucleotidesAllowed = false;
51     proteinAllowed = true;
52     bySequence = true;
53   }
54
55   @Override
56   public String getServiceActionText()
57   {
58     return "Submitting amino acid sequences for disorder prediction.";
59   }
60
61   private static Map<String, Map<String, String[]>> featureMap;
62
63   private static Map<String, Map<String, Map<String, Object>>> annotMap;
64
65   private static String DONTCOMBINE = "DONTCOMBINE";
66   private static String INVISIBLE = "INVISIBLE";
67   static
68   {
69     featureMap = new HashMap<String, Map<String, String[]>>();
70     Map<String, String[]> fmap;
71     featureMap.put(compbio.ws.client.Services.IUPredWS.toString(),
72             fmap = new HashMap<String, String[]>());
73     fmap.put("Glob", new String[]
74     { "Globular Domain", "Predicted globular domain" });
75     featureMap.put(compbio.ws.client.Services.JronnWS.toString(),
76             fmap = new HashMap<String, String[]>());
77     featureMap.put(compbio.ws.client.Services.DisemblWS.toString(),
78             fmap = new HashMap<String, String[]>());
79     fmap.put("REM465", new String[]
80     { "REM465", "Missing density" });
81     fmap.put("HOTLOOPS", new String[]
82     { "HOTLOOPS", "Flexible loops" });
83     fmap.put("COILS", new String[]
84     { "COILS", "Random coil" });
85     featureMap.put(compbio.ws.client.Services.GlobPlotWS.toString(),
86             fmap = new HashMap<String, String[]>());
87     fmap.put("GlobDoms", new String[]
88     { "Globular Domain", "Predicted globular domain" });
89     fmap.put("Disorder", new String[]
90     { "Protein Disorder", "Probable unstructured peptide region" });
91     Map<String, Map<String, Object>> amap;
92     annotMap = new HashMap<String, Map<String, Map<String, Object>>>();
93     annotMap.put(compbio.ws.client.Services.GlobPlotWS.toString(),
94             amap = new HashMap<String, Map<String, Object>>());
95     amap.put("Dydx", new HashMap<String, Object>());
96     amap.get("Dydx").put(DONTCOMBINE, DONTCOMBINE);
97     amap.put("SmoothedScore", new HashMap<String, Object>());
98     amap.get("SmoothedScore").put(INVISIBLE,INVISIBLE);
99     amap.put("RawScore", new HashMap<String, Object>());
100     amap.get("RawScore").put(INVISIBLE,INVISIBLE);
101     annotMap.put(compbio.ws.client.Services.DisemblWS.toString(),
102             amap = new HashMap<String, Map<String, Object>>());
103     
104   }
105
106   @Override
107   public void updateResultAnnotation(boolean immediate)
108   {
109
110     if (immediate || !calcMan.isWorking(this) && scoremanager != null)
111     {
112       Map<String, String[]> featureTypeMap = featureMap
113               .get(service.serviceType);
114       Map<String, Map<String, Object>> annotTypeMap = annotMap
115               .get(service.serviceType);
116       boolean dispFeatures = false;
117       Map<String, Object> fc = new Hashtable<String, Object>();
118       List<AlignmentAnnotation> ourAnnot = new ArrayList<AlignmentAnnotation>();
119       /**
120        * grouping for any annotation rows created
121        */
122       int graphGroup = 1, iGraphGroup = 1;
123       if (alignViewport.getAlignment().getAlignmentAnnotation() != null)
124       {
125         for (AlignmentAnnotation ala : alignViewport.getAlignment()
126                 .getAlignmentAnnotation())
127         {
128           if (ala.graphGroup > graphGroup)
129           {
130             graphGroup = ala.graphGroup;
131           }
132         }
133       }
134
135       for (String seqId : seqNames.keySet())
136       {
137         boolean sameGroup = false;
138         SequenceI dseq, aseq, seq = seqNames.get(seqId);
139         int base = seq.getStart() - 1;
140         aseq = seq;
141         while ((dseq = seq).getDatasetSequence() != null)
142         {
143           seq = seq.getDatasetSequence();
144         }
145         ;
146         ScoreHolder scores = scoremanager.getAnnotationForSequence(seqId);
147         float last = Float.NaN, val = Float.NaN;
148         int lastAnnot = ourAnnot.size();
149         for (Score scr : scores.scores)
150         {
151
152           if (scr.getRanges() != null && scr.getRanges().size() > 0)
153           {
154             Iterator<Float> vals = scr.getScores().iterator();
155             // make features on sequence
156             for (Range rn : scr.getRanges())
157             {
158
159               SequenceFeature sf;
160               String[] type = featureTypeMap.get(scr.getMethod());
161               if (type == null)
162               {
163                 // create a default type for this feature
164                 type = new String[]
165                 { typeName + " (" + scr.getMethod() + ")",
166                     service.getActionText() };
167               }
168               if (vals.hasNext())
169               {
170                 sf = new SequenceFeature(type[0], type[1], base + rn.from,
171                         base + rn.to, val = vals.next().floatValue(),
172                         methodName);
173               }
174               else
175               {
176                 sf = new SequenceFeature(type[0], type[1], null, base
177                         + rn.from, base + rn.to, methodName);
178               }
179               dseq.addSequenceFeature(sf);
180               if (last != val && last != Float.NaN)
181               {
182                 fc.put(sf.getType(), sf);
183               }
184               last = val;
185               dispFeatures = true;
186             }
187           }
188           else
189           {
190
191             AlignmentAnnotation annot = createAnnotationRowsForScores(
192                     ourAnnot, service.serviceType + " (" + scr.getMethod()
193                             + ")", service.getServiceTypeURI()+"/"+ scr.getMethod(), aseq,
194                     base, scr);
195             annot.graph = AlignmentAnnotation.LINE_GRAPH;
196             annot.visible = (annotTypeMap==null || annotTypeMap.get(scr.getMethod())==null || annotTypeMap.get(scr.getMethod()).get(INVISIBLE) == null);
197
198             if (annotTypeMap==null || annotTypeMap.get(scr.getMethod())==null || annotTypeMap.get(scr.getMethod()).get(DONTCOMBINE) == null)
199             {
200               {
201                 if (!sameGroup)
202                 {
203                   graphGroup++;
204                   sameGroup = true;
205                 }
206
207                 annot.graphGroup = graphGroup;
208               }
209             }
210             
211             annot.description = service.getActionText() + " - raw scores";
212             Color col = new UserColourScheme(typeName)
213                     .createColourFromName(typeName + scr.getMethod());
214             for (int p = 0, ps = annot.annotations.length; p < ps; p++)
215             {
216               if (annot.annotations[p] != null)
217               {
218                 annot.annotations[p].colour = col;
219               }
220             }
221             annot._linecolour=col;
222           }
223         }
224         if (lastAnnot + 1 == ourAnnot.size())
225         {
226           // remove singleton alignment annotation row
227           ourAnnot.get(lastAnnot).graphGroup = -1;
228         }
229       }
230       {
231         if (dispFeatures)
232         {
233           jalview.gui.FeatureRenderer fr = ((jalview.gui.AlignmentPanel) ap)
234                   .cloneFeatureRenderer();
235           for (String ft : fc.keySet())
236           {
237             Object gc = fr.getFeatureStyle(ft);
238             if (gc instanceof Color)
239             {
240               // set graduated color as fading to white for minimum, and
241               // autoscaling to values on alignment
242               GraduatedColor ggc = new GraduatedColor(Color.white,
243                       (Color) gc, Float.MIN_VALUE, Float.MAX_VALUE);
244               ggc.setAutoScaled(true);
245               fr.setColour(ft, ggc);
246             }
247           }
248           // TODO: JAL-1150 - create sequence feature settings API for defining styles and enabling/disabling feature overlay on alignment panel
249           ((jalview.gui.AlignmentPanel) ap).updateFeatureRendererFrom(fr);
250           if (af.alignPanel==ap)
251           {
252             // only do this if the alignFrame is currently showing this view.
253             af.setShowSeqFeatures(true);
254           } 
255           ap.paintAlignment(true);
256         }
257         if (ourAnnot.size() > 0)
258         {
259           // Modify the visible annotation on the alignment viewport with the
260           // new alignment annotation rows created.
261           updateOurAnnots(ourAnnot);
262           ap.adjustAnnotationHeight();
263         }
264       }
265     }
266   }
267
268 }