Jalview 2.8 Source Header
[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.bin.Cache;
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 import compbio.ws.client.Services;
45
46 public class AADisorderClient extends JabawsAlignCalcWorker implements
47         AlignCalcWorkerI
48 {
49
50   private static final String THRESHOLD = "THRESHOLD";
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.put("SmoothedScore", new HashMap<String, Object>());
123     amap.get("SmoothedScore").put(INVISIBLE, INVISIBLE);
124     amap.put("RawScore", new HashMap<String, Object>());
125     amap.get("RawScore").put(INVISIBLE, INVISIBLE);
126     annotMap.put(compbio.ws.client.Services.DisemblWS.toString(),
127             amap = new HashMap<String, Map<String, Object>>());
128     amap.put("COILS", new HashMap<String, Object>());
129     amap.put("HOTLOOPS", new HashMap<String, Object>());
130     amap.put("REM465", new HashMap<String, Object>());
131     amap.get("COILS").put(THRESHOLD, new double[]
132     { 1, 0.516 });
133     amap.get("HOTLOOPS").put(THRESHOLD, new double[]
134     { 1, 0.6 });
135     amap.get("REM465").put(THRESHOLD, new double[]
136     { 1, 0.1204 });
137
138     annotMap.put(compbio.ws.client.Services.IUPredWS.toString(),
139             amap = new HashMap<String, Map<String, Object>>());
140     amap.put("Long", new HashMap<String, Object>());
141     amap.put("Short", new HashMap<String, Object>());
142     amap.get("Long").put(THRESHOLD, new double[]
143     { 1, 0.5 });
144     amap.get("Short").put(THRESHOLD, new double[]
145     { 1, 0.5 });
146     annotMap.put(compbio.ws.client.Services.JronnWS.toString(),
147             amap = new HashMap<String, Map<String, Object>>());
148     amap.put("JRonn", new HashMap<String, Object>());
149     amap.get("JRonn").put(THRESHOLD, new double[]
150     { 1, 0.5 });
151   }
152
153   @Override
154   public void updateResultAnnotation(boolean immediate)
155   {
156
157     if (immediate || !calcMan.isWorking(this) && scoremanager != null)
158     {
159       Map<String, String[]> featureTypeMap = featureMap
160               .get(service.serviceType);
161       Map<String, Map<String, Object>> annotTypeMap = annotMap
162               .get(service.serviceType);
163       boolean dispFeatures = false;
164       Map<String, Object> fc = new Hashtable<String, Object>();
165       List<AlignmentAnnotation> ourAnnot = new ArrayList<AlignmentAnnotation>();
166       /**
167        * grouping for any annotation rows created
168        */
169       int graphGroup = 1;
170       if (alignViewport.getAlignment().getAlignmentAnnotation() != null)
171       {
172         for (AlignmentAnnotation ala : alignViewport.getAlignment()
173                 .getAlignmentAnnotation())
174         {
175           if (ala.graphGroup > graphGroup)
176           {
177             graphGroup = ala.graphGroup;
178           }
179         }
180       }
181
182       for (String seqId : seqNames.keySet())
183       {
184         boolean sameGroup = false;
185         SequenceI dseq, aseq, seq = seqNames.get(seqId);
186         int base = seq.getStart() - 1;
187         aseq = seq;
188         while ((dseq = seq).getDatasetSequence() != null)
189         {
190           seq = seq.getDatasetSequence();
191         }
192         ;
193         ScoreHolder scores = scoremanager.getAnnotationForSequence(seqId);
194         float last = Float.NaN, val = Float.NaN;
195         int lastAnnot = ourAnnot.size();
196         for (Score scr : scores.scores)
197         {
198
199           if (scr.getRanges() != null && scr.getRanges().size() > 0)
200           {
201             Iterator<Float> vals = scr.getScores().iterator();
202             // make features on sequence
203             for (Range rn : scr.getRanges())
204             {
205
206               SequenceFeature sf;
207               String[] type = featureTypeMap.get(scr.getMethod());
208               if (type == null)
209               {
210                 // create a default type for this feature
211                 type = new String[]
212                 { typeName + " (" + scr.getMethod() + ")",
213                     service.getActionText() };
214               }
215               if (vals.hasNext())
216               {
217                 sf = new SequenceFeature(type[0], type[1], base + rn.from,
218                         base + rn.to, val = vals.next().floatValue(),
219                         methodName);
220               }
221               else
222               {
223                 sf = new SequenceFeature(type[0], type[1], null, base
224                         + rn.from, base + rn.to, methodName);
225               }
226               dseq.addSequenceFeature(sf);
227               if (last != val && last != Float.NaN)
228               {
229                 fc.put(sf.getType(), sf);
230               }
231               last = val;
232               dispFeatures = true;
233             }
234           }
235           else
236           {
237             if (scr.getScores().size() == 0)
238             {
239               continue;
240             }
241             AlignmentAnnotation annot = createAnnotationRowsForScores(
242                     ourAnnot, service.serviceType + " (" + scr.getMethod()
243                             + ")",
244                     service.getServiceTypeURI() + "/" + scr.getMethod(),
245                     aseq, base + 1, scr);
246             annot.graph = AlignmentAnnotation.LINE_GRAPH;
247             annot.visible = (annotTypeMap == null
248                     || annotTypeMap.get(scr.getMethod()) == null || annotTypeMap
249                     .get(scr.getMethod()).get(INVISIBLE) == null);
250             double[] thrsh = (annotTypeMap == null || annotTypeMap.get(scr
251                     .getMethod()) == null) ? null : (double[]) annotTypeMap
252                     .get(scr.getMethod()).get(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], threshNote,
275                       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         if (lastAnnot + 1 == ourAnnot.size())
292         {
293           // remove singleton alignment annotation row
294           ourAnnot.get(lastAnnot).graphGroup = -1;
295         }
296       }
297       {
298         if (dispFeatures)
299         {
300           jalview.gui.FeatureRenderer fr = ((jalview.gui.AlignmentPanel) ap)
301                   .cloneFeatureRenderer();
302           for (String ft : fc.keySet())
303           {
304             Object gc = fr.getFeatureStyle(ft);
305             if (gc instanceof Color)
306             {
307               // set graduated color as fading to white for minimum, and
308               // autoscaling to values on alignment
309               GraduatedColor ggc = new GraduatedColor(Color.white,
310                       (Color) gc, Float.MIN_VALUE, Float.MAX_VALUE);
311               ggc.setAutoScaled(true);
312               fr.setColour(ft, ggc);
313             }
314           }
315           // TODO: JAL-1150 - create sequence feature settings API for defining
316           // styles and enabling/disabling feature overlay on alignment panel
317           ((jalview.gui.AlignmentPanel) ap).updateFeatureRendererFrom(fr);
318           if (af.alignPanel == ap)
319           {
320             // only do this if the alignFrame is currently showing this view.
321             af.setShowSeqFeatures(true);
322           }
323           ap.paintAlignment(true);
324         }
325         if (ourAnnot.size() > 0)
326         {
327           // Modify the visible annotation on the alignment viewport with the
328           // new alignment annotation rows created.
329           updateOurAnnots(ourAnnot);
330           ap.adjustAnnotationHeight();
331         }
332       }
333     }
334   }
335
336 }