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