JAL-967 JAL-1482 new feature renderer api - needs more design work
[jalview.git] / src / jalview / ws / jws2 / AADisorderClient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.ws.jws2;
20
21 import jalview.api.AlignCalcWorkerI;
22 import jalview.datamodel.AlignmentAnnotation;
23
24 import jalview.datamodel.GraphLine;
25 import jalview.datamodel.SequenceFeature;
26 import jalview.datamodel.SequenceI;
27 import jalview.gui.AlignFrame;
28 import jalview.schemes.GraduatedColor;
29 import jalview.schemes.UserColourScheme;
30 import jalview.ws.jws2.jabaws2.Jws2Instance;
31 import jalview.ws.params.WsParamSetI;
32
33 import java.awt.Color;
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.Hashtable;
37 import java.util.Iterator;
38 import java.util.List;
39 import java.util.Map;
40 import compbio.data.sequence.Range;
41 import compbio.data.sequence.Score;
42 import compbio.data.sequence.ScoreManager.ScoreHolder;
43 import compbio.metadata.Argument;
44
45 public class AADisorderClient extends JabawsCalcWorker implements
46         AlignCalcWorkerI
47 {
48
49   private static final String THRESHOLD = "THRESHOLD";
50   private static final String RANGE = "RANGE";
51
52   String typeName;
53
54   String methodName;
55
56   String groupName;
57
58   AlignFrame af;
59
60   public AADisorderClient(Jws2Instance sh, AlignFrame alignFrame,
61           WsParamSetI preset, List<Argument> paramset)
62   {
63     super(sh, alignFrame, preset, paramset);
64     af = alignFrame;
65     typeName = sh.action;
66     methodName = sh.serviceType;
67
68     submitGaps = false;
69     alignedSeqs = false;
70     nucleotidesAllowed = false;
71     proteinAllowed = true;
72     bySequence = true;
73   }
74
75   @Override
76   public String getServiceActionText()
77   {
78     return "Submitting amino acid sequences for disorder prediction.";
79   }
80
81   private static Map<String, Map<String, String[]>> featureMap;
82
83   private static Map<String, Map<String, Map<String, Object>>> annotMap;
84
85   private static String DONTCOMBINE = "DONTCOMBINE";
86
87   private static String INVISIBLE = "INVISIBLE";
88   static
89   {
90     // TODO: turn this into some kind of configuration file that's a bit easier
91     // to edit
92     featureMap = new HashMap<String, Map<String, String[]>>();
93     Map<String, String[]> fmap;
94     featureMap.put(compbio.ws.client.Services.IUPredWS.toString(),
95             fmap = new HashMap<String, String[]>());
96     fmap.put("Glob", new String[]
97     { "Globular Domain", "Predicted globular domain" });
98     featureMap.put(compbio.ws.client.Services.JronnWS.toString(),
99             fmap = new HashMap<String, String[]>());
100     featureMap.put(compbio.ws.client.Services.DisemblWS.toString(),
101             fmap = new HashMap<String, String[]>());
102     fmap.put("REM465", new String[]
103     { "REM465", "Missing density" });
104     fmap.put("HOTLOOPS", new String[]
105     { "HOTLOOPS", "Flexible loops" });
106     fmap.put("COILS", new String[]
107     { "COILS", "Random coil" });
108     featureMap.put(compbio.ws.client.Services.GlobPlotWS.toString(),
109             fmap = new HashMap<String, String[]>());
110     fmap.put("GlobDoms", new String[]
111     { "Globular Domain", "Predicted globular domain" });
112     fmap.put("Disorder", new String[]
113     { "Protein Disorder", "Probable unstructured peptide region" });
114     Map<String, Map<String, Object>> amap;
115     annotMap = new HashMap<String, Map<String, Map<String, Object>>>();
116     annotMap.put(compbio.ws.client.Services.GlobPlotWS.toString(),
117             amap = new HashMap<String, Map<String, Object>>());
118     amap.put("Dydx", new HashMap<String, Object>());
119     amap.get("Dydx").put(DONTCOMBINE, DONTCOMBINE);
120     amap.get("Dydx").put(THRESHOLD, new double[]
121     { 1, 0 });
122     amap.get("Dydx").put(RANGE, new float[]
123             { -1, +1 });
124
125     amap.put("SmoothedScore", new HashMap<String, Object>());
126     amap.get("SmoothedScore").put(INVISIBLE, INVISIBLE);
127     amap.put("RawScore", new HashMap<String, Object>());
128     amap.get("RawScore").put(INVISIBLE, INVISIBLE);
129     annotMap.put(compbio.ws.client.Services.DisemblWS.toString(),
130             amap = new HashMap<String, Map<String, Object>>());
131     amap.put("COILS", new HashMap<String, Object>());
132     amap.put("HOTLOOPS", new HashMap<String, Object>());
133     amap.put("REM465", new HashMap<String, Object>());
134     amap.get("COILS").put(THRESHOLD, new double[]
135     { 1, 0.516 });
136     amap.get("COILS").put(RANGE, new float[]
137             { 0, 1 });
138
139     amap.get("HOTLOOPS").put(THRESHOLD, new double[]
140     { 1, 0.6 });
141     amap.get("HOTLOOPS").put(RANGE, new float[]
142             { 0, 1 });
143     amap.get("REM465").put(THRESHOLD, new double[]
144     { 1, 0.1204 });
145     amap.get("REM465").put(RANGE, new float[]
146             { 0, 1 });
147
148
149     annotMap.put(compbio.ws.client.Services.IUPredWS.toString(),
150             amap = new HashMap<String, Map<String, Object>>());
151     amap.put("Long", new HashMap<String, Object>());
152     amap.put("Short", new HashMap<String, Object>());
153     amap.get("Long").put(THRESHOLD, new double[]
154     { 1, 0.5 });
155     amap.get("Long").put(RANGE, new float[]
156             { 0, 1 });
157     amap.get("Short").put(THRESHOLD, new double[]
158     { 1, 0.5 });
159     amap.get("Short").put(RANGE, new float[]
160             { 0, 1 });
161     annotMap.put(compbio.ws.client.Services.JronnWS.toString(),
162             amap = new HashMap<String, Map<String, Object>>());
163     amap.put("JRonn", new HashMap<String, Object>());
164     amap.get("JRonn").put(THRESHOLD, new double[]
165     { 1, 0.5 });
166     amap.get("JRonn").put(RANGE, new float[]
167             { 0, 1 });
168   }
169
170   @Override
171   public void updateResultAnnotation(boolean immediate)
172   {
173
174     if (immediate || !calcMan.isWorking(this) && scoremanager != null)
175     {
176       Map<String, String[]> featureTypeMap = featureMap
177               .get(service.serviceType);
178       Map<String, Map<String, Object>> annotTypeMap = annotMap
179               .get(service.serviceType);
180       boolean dispFeatures = false;
181       Map<String, Object> fc = new Hashtable<String, Object>();
182       List<AlignmentAnnotation> ourAnnot = new ArrayList<AlignmentAnnotation>();
183       /**
184        * grouping for any annotation rows created
185        */
186       int graphGroup = 1;
187       if (alignViewport.getAlignment().getAlignmentAnnotation() != null)
188       {
189         for (AlignmentAnnotation ala : alignViewport.getAlignment()
190                 .getAlignmentAnnotation())
191         {
192           if (ala.graphGroup > graphGroup)
193           {
194             graphGroup = ala.graphGroup;
195           }
196         }
197       }
198
199       for (String seqId : seqNames.keySet())
200       {
201         boolean sameGroup = false;
202         SequenceI dseq, aseq, seq = seqNames.get(seqId);
203         int base = seq.findPosition(start)-1;
204         aseq = seq;
205         while ((dseq = seq).getDatasetSequence() != null)
206         {
207           seq = seq.getDatasetSequence();
208         }
209         ;
210         ScoreHolder scores = scoremanager.getAnnotationForSequence(seqId);
211         float last = Float.NaN, val = Float.NaN;
212         int lastAnnot = ourAnnot.size();
213         if (scores!=null && scores.scores!=null) {
214           for (Score scr : scores.scores)
215           {
216
217             if (scr.getRanges() != null && scr.getRanges().size() > 0)
218             {
219               Iterator<Float> vals = scr.getScores().iterator();
220               // make features on sequence
221               for (Range rn : scr.getRanges())
222               {
223
224                 SequenceFeature sf;
225                 String[] type = featureTypeMap.get(scr.getMethod());
226                 if (type == null)
227                 {
228                   // create a default type for this feature
229                   type = new String[]
230                   { typeName + " (" + scr.getMethod() + ")",
231                       service.getActionText() };
232                 }
233                 if (vals.hasNext())
234                 {
235                   sf = new SequenceFeature(type[0], type[1],
236                           base + rn.from, base + rn.to, val = vals.next()
237                                   .floatValue(), methodName);
238                 }
239                 else
240                 {
241                   sf = new SequenceFeature(type[0], type[1], null, base
242                           + rn.from, base + rn.to, methodName);
243                 }
244                 dseq.addSequenceFeature(sf);
245                 if (last != val && last != Float.NaN)
246                 {
247                   fc.put(sf.getType(), sf);
248                 }
249                 last = val;
250                 dispFeatures = true;
251               }
252             }
253             else
254             {
255               if (scr.getScores().size() == 0)
256               {
257                 continue;
258               }
259               AlignmentAnnotation annot = createAnnotationRowsForScores(
260                       ourAnnot,
261                       service.serviceType + " (" + scr.getMethod() + ")",
262                       service.getServiceTypeURI() + "/" + scr.getMethod(),
263                       aseq, base + 1, scr);
264               annot.graph = AlignmentAnnotation.LINE_GRAPH;
265               
266               Map<String,Object> styleMap = (annotTypeMap == null) ? null : annotTypeMap.get(scr.getMethod());
267               
268               annot.visible = (styleMap == null || styleMap.get(INVISIBLE) == null);
269               double[] thrsh = (styleMap==null) ? null
270                       : (double[]) styleMap.get(
271                               THRESHOLD);
272               float[] range = (styleMap==null) ? null : (float[]) styleMap.get(
273                       RANGE);
274               if (range!=null)
275               {
276                 annot.graphMin = range[0];
277                 annot.graphMax = range[1];
278               }
279               if (styleMap==null || styleMap.get(DONTCOMBINE) == null)
280               {
281                 {
282                   if (!sameGroup)
283                   {
284                     graphGroup++;
285                     sameGroup = true;
286                   }
287
288                   annot.graphGroup = graphGroup;
289                 }
290               }
291
292               annot.description = "<html>" + service.getActionText()
293                       + " - raw scores";
294               if (thrsh != null)
295               {
296                 String threshNote = (thrsh[0] > 0 ? "Above " : "Below ")
297                         + thrsh[1] + " indicates disorder";
298                 annot.threshold = new GraphLine((float) thrsh[1],
299                         threshNote, Color.red);
300                 annot.description += "<br/>" + threshNote;
301               }
302               annot.description += "</html>";
303               Color col = new UserColourScheme(typeName)
304                       .createColourFromName(typeName + scr.getMethod());
305               for (int p = 0, ps = annot.annotations.length; p < ps; p++)
306               {
307                 if (annot.annotations[p] != null)
308                 {
309                   annot.annotations[p].colour = col;
310                 }
311               }
312               annot._linecolour = col;
313             }
314           }
315         }
316         if (lastAnnot + 1 == ourAnnot.size())
317         {
318           // remove singleton alignment annotation row
319           ourAnnot.get(lastAnnot).graphGroup = -1;
320         }
321       }
322       {
323         if (dispFeatures)
324         {
325           jalview.api.FeatureRenderer fr = ((jalview.gui.AlignmentPanel) ap)
326                   .cloneFeatureRenderer();
327           for (String ft : fc.keySet())
328           {
329             Object gc = fr.getFeatureStyle(ft);
330             if (gc instanceof Color)
331             {
332               // set graduated color as fading to white for minimum, and
333               // autoscaling to values on alignment
334               GraduatedColor ggc = new GraduatedColor(Color.white,
335                       (Color) gc, Float.MIN_VALUE, Float.MAX_VALUE);
336               ggc.setAutoScaled(true);
337               fr.setColour(ft, ggc);
338             }
339           }
340           // TODO: JAL-1150 - create sequence feature settings API for defining
341           // styles and enabling/disabling feature overlay on alignment panel
342           ((jalview.gui.AlignmentPanel) ap).updateFeatureRendererFrom(fr);
343           if (af.alignPanel == ap)
344           {
345             // only do this if the alignFrame is currently showing this view.
346             af.setShowSeqFeatures(true);
347           }
348           ap.paintAlignment(true);
349         }
350         if (ourAnnot.size() > 0)
351         {
352           // Modify the visible annotation on the alignment viewport with the
353           // new alignment annotation rows created.
354           updateOurAnnots(ourAnnot);
355           ap.adjustAnnotationHeight();
356         }
357       }
358     }
359   }
360
361 }