tidy imports
[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.getStart() - 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         for (Score scr : scores.scores)
195         {
196
197           if (scr.getRanges() != null && scr.getRanges().size() > 0)
198           {
199             Iterator<Float> vals = scr.getScores().iterator();
200             // make features on sequence
201             for (Range rn : scr.getRanges())
202             {
203
204               SequenceFeature sf;
205               String[] type = featureTypeMap.get(scr.getMethod());
206               if (type == null)
207               {
208                 // create a default type for this feature
209                 type = new String[]
210                 { typeName + " (" + scr.getMethod() + ")",
211                     service.getActionText() };
212               }
213               if (vals.hasNext())
214               {
215                 sf = new SequenceFeature(type[0], type[1], base + rn.from,
216                         base + rn.to, val = vals.next().floatValue(),
217                         methodName);
218               }
219               else
220               {
221                 sf = new SequenceFeature(type[0], type[1], null, base
222                         + rn.from, base + rn.to, methodName);
223               }
224               dseq.addSequenceFeature(sf);
225               if (last != val && last != Float.NaN)
226               {
227                 fc.put(sf.getType(), sf);
228               }
229               last = val;
230               dispFeatures = true;
231             }
232           }
233           else
234           {
235             if (scr.getScores().size() == 0)
236             {
237               continue;
238             }
239             AlignmentAnnotation annot = createAnnotationRowsForScores(
240                     ourAnnot, service.serviceType + " (" + scr.getMethod()
241                             + ")",
242                     service.getServiceTypeURI() + "/" + scr.getMethod(),
243                     aseq, base + 1, scr);
244             annot.graph = AlignmentAnnotation.LINE_GRAPH;
245             annot.visible = (annotTypeMap == null
246                     || annotTypeMap.get(scr.getMethod()) == null || annotTypeMap
247                     .get(scr.getMethod()).get(INVISIBLE) == null);
248             double[] thrsh = (annotTypeMap == null || annotTypeMap.get(scr
249                     .getMethod()) == null) ? null : (double[]) annotTypeMap
250                     .get(scr.getMethod()).get(THRESHOLD);
251             if (annotTypeMap == null
252                     || annotTypeMap.get(scr.getMethod()) == null
253                     || annotTypeMap.get(scr.getMethod()).get(DONTCOMBINE) == null)
254             {
255               {
256                 if (!sameGroup)
257                 {
258                   graphGroup++;
259                   sameGroup = true;
260                 }
261
262                 annot.graphGroup = graphGroup;
263               }
264             }
265
266             annot.description = "<html>" + service.getActionText()
267                     + " - raw scores";
268             if (thrsh != null)
269             {
270               String threshNote = (thrsh[0] > 0 ? "Above " : "Below ")
271                       + thrsh[1] + " indicates disorder";
272               annot.threshold = new GraphLine((float) thrsh[1], threshNote,
273                       Color.red);
274               annot.description += "<br/>" + threshNote;
275             }
276             annot.description += "</html>";
277             Color col = new UserColourScheme(typeName)
278                     .createColourFromName(typeName + scr.getMethod());
279             for (int p = 0, ps = annot.annotations.length; p < ps; p++)
280             {
281               if (annot.annotations[p] != null)
282               {
283                 annot.annotations[p].colour = col;
284               }
285             }
286             annot._linecolour = col;
287           }
288         }
289         if (lastAnnot + 1 == ourAnnot.size())
290         {
291           // remove singleton alignment annotation row
292           ourAnnot.get(lastAnnot).graphGroup = -1;
293         }
294       }
295       {
296         if (dispFeatures)
297         {
298           jalview.gui.FeatureRenderer fr = ((jalview.gui.AlignmentPanel) ap)
299                   .cloneFeatureRenderer();
300           for (String ft : fc.keySet())
301           {
302             Object gc = fr.getFeatureStyle(ft);
303             if (gc instanceof Color)
304             {
305               // set graduated color as fading to white for minimum, and
306               // autoscaling to values on alignment
307               GraduatedColor ggc = new GraduatedColor(Color.white,
308                       (Color) gc, Float.MIN_VALUE, Float.MAX_VALUE);
309               ggc.setAutoScaled(true);
310               fr.setColour(ft, ggc);
311             }
312           }
313           // TODO: JAL-1150 - create sequence feature settings API for defining
314           // styles and enabling/disabling feature overlay on alignment panel
315           ((jalview.gui.AlignmentPanel) ap).updateFeatureRendererFrom(fr);
316           if (af.alignPanel == ap)
317           {
318             // only do this if the alignFrame is currently showing this view.
319             af.setShowSeqFeatures(true);
320           }
321           ap.paintAlignment(true);
322         }
323         if (ourAnnot.size() > 0)
324         {
325           // Modify the visible annotation on the alignment viewport with the
326           // new alignment annotation rows created.
327           updateOurAnnots(ourAnnot);
328           ap.adjustAnnotationHeight();
329         }
330       }
331     }
332   }
333
334 }