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