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