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