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