JAL-974 show thresholds for disorder predictor annotation rows
[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   private static String INVISIBLE = "INVISIBLE";
70   static
71   {
72     // TODO: turn this into some kind of configuration file that's a bit easier to edit
73     featureMap = new HashMap<String, Map<String, String[]>>();
74     Map<String, String[]> fmap;
75     featureMap.put(compbio.ws.client.Services.IUPredWS.toString(),
76             fmap = new HashMap<String, String[]>());
77     fmap.put("Glob", new String[]
78     { "Globular Domain", "Predicted globular domain" });
79     featureMap.put(compbio.ws.client.Services.JronnWS.toString(),
80             fmap = new HashMap<String, String[]>());
81     featureMap.put(compbio.ws.client.Services.DisemblWS.toString(),
82             fmap = new HashMap<String, String[]>());
83     fmap.put("REM465", new String[]
84     { "REM465", "Missing density" });
85     fmap.put("HOTLOOPS", new String[]
86     { "HOTLOOPS", "Flexible loops" });
87     fmap.put("COILS", new String[]
88     { "COILS", "Random coil" });
89     featureMap.put(compbio.ws.client.Services.GlobPlotWS.toString(),
90             fmap = new HashMap<String, String[]>());
91     fmap.put("GlobDoms", new String[]
92     { "Globular Domain", "Predicted globular domain" });
93     fmap.put("Disorder", new String[]
94     { "Protein Disorder", "Probable unstructured peptide region" });
95     Map<String, Map<String, Object>> amap;
96     annotMap = new HashMap<String, Map<String, Map<String, Object>>>();
97     annotMap.put(compbio.ws.client.Services.GlobPlotWS.toString(),
98             amap = new HashMap<String, Map<String, Object>>());
99     amap.put("Dydx", new HashMap<String, Object>());
100     amap.get("Dydx").put(DONTCOMBINE, DONTCOMBINE);
101     amap.get("Dydx").put(THRESHOLD, new double[] {1, 0});
102     amap.put("SmoothedScore", new HashMap<String, Object>());
103     amap.get("SmoothedScore").put(INVISIBLE,INVISIBLE);
104     amap.put("RawScore", new HashMap<String, Object>());
105     amap.get("RawScore").put(INVISIBLE,INVISIBLE);
106     annotMap.put(compbio.ws.client.Services.DisemblWS.toString(),
107             amap = new HashMap<String, Map<String, Object>>());
108     amap.put("COILS", new HashMap<String, Object>());
109     amap.put("HOTLOOPS", new HashMap<String, Object>());
110     amap.put("REM465", new HashMap<String, Object>());
111     amap.get("COILS").put(THRESHOLD, new double[] { 1, 0.516});
112     amap.get("HOTLOOPS").put(THRESHOLD, new double[] { 1, 0.6});
113     amap.get("REM465").put(THRESHOLD, new double[] { 1, 0.1204});
114     
115     annotMap.put(compbio.ws.client.Services.IUPredWS.toString(),
116             amap = new HashMap<String, Map<String, Object>>());
117     amap.put("Long", new HashMap<String, Object>());
118     amap.put("Short", new HashMap<String, Object>());
119     amap.get("Long").put(THRESHOLD, new double[] { 1, 0.5});
120     amap.get("Short").put(THRESHOLD, new double[] { 1, 0.5});
121
122   }
123
124   @Override
125   public void updateResultAnnotation(boolean immediate)
126   {
127
128     if (immediate || !calcMan.isWorking(this) && scoremanager != null)
129     {
130       Map<String, String[]> featureTypeMap = featureMap
131               .get(service.serviceType);
132       Map<String, Map<String, Object>> annotTypeMap = annotMap
133               .get(service.serviceType);
134       boolean dispFeatures = false;
135       Map<String, Object> fc = new Hashtable<String, Object>();
136       List<AlignmentAnnotation> ourAnnot = new ArrayList<AlignmentAnnotation>();
137       /**
138        * grouping for any annotation rows created
139        */
140       int graphGroup = 1, iGraphGroup = 1;
141       if (alignViewport.getAlignment().getAlignmentAnnotation() != null)
142       {
143         for (AlignmentAnnotation ala : alignViewport.getAlignment()
144                 .getAlignmentAnnotation())
145         {
146           if (ala.graphGroup > graphGroup)
147           {
148             graphGroup = ala.graphGroup;
149           }
150         }
151       }
152
153       for (String seqId : seqNames.keySet())
154       {
155         boolean sameGroup = false;
156         SequenceI dseq, aseq, seq = seqNames.get(seqId);
157         int base = seq.getStart() - 1;
158         aseq = seq;
159         while ((dseq = seq).getDatasetSequence() != null)
160         {
161           seq = seq.getDatasetSequence();
162         }
163         ;
164         ScoreHolder scores = scoremanager.getAnnotationForSequence(seqId);
165         float last = Float.NaN, val = Float.NaN;
166         int lastAnnot = ourAnnot.size();
167         for (Score scr : scores.scores)
168         {
169
170           if (scr.getRanges() != null && scr.getRanges().size() > 0)
171           {
172             Iterator<Float> vals = scr.getScores().iterator();
173             // make features on sequence
174             for (Range rn : scr.getRanges())
175             {
176
177               SequenceFeature sf;
178               String[] type = featureTypeMap.get(scr.getMethod());
179               if (type == null)
180               {
181                 // create a default type for this feature
182                 type = new String[]
183                 { typeName + " (" + scr.getMethod() + ")",
184                     service.getActionText() };
185               }
186               if (vals.hasNext())
187               {
188                 sf = new SequenceFeature(type[0], type[1], base + rn.from,
189                         base + rn.to, val = vals.next().floatValue(),
190                         methodName);
191               }
192               else
193               {
194                 sf = new SequenceFeature(type[0], type[1], null, base
195                         + rn.from, base + rn.to, methodName);
196               }
197               dseq.addSequenceFeature(sf);
198               if (last != val && last != Float.NaN)
199               {
200                 fc.put(sf.getType(), sf);
201               }
202               last = val;
203               dispFeatures = true;
204             }
205           }
206           else
207           {
208             if (scr.getScores().size()==0)
209             {
210               continue;
211             }
212             AlignmentAnnotation annot = createAnnotationRowsForScores(
213                     ourAnnot, service.serviceType + " (" + scr.getMethod()
214                             + ")", service.getServiceTypeURI()+"/"+ scr.getMethod(), aseq,
215                     base, scr);
216             annot.graph = AlignmentAnnotation.LINE_GRAPH;
217             annot.visible = (annotTypeMap==null || annotTypeMap.get(scr.getMethod())==null || annotTypeMap.get(scr.getMethod()).get(INVISIBLE) == null);
218             double[] thrsh=(annotTypeMap==null || annotTypeMap.get(scr.getMethod())==null) ? null : (double[]) annotTypeMap.get(scr.getMethod()).get(THRESHOLD);
219             if (annotTypeMap==null || annotTypeMap.get(scr.getMethod())==null || annotTypeMap.get(scr.getMethod()).get(DONTCOMBINE) == null)
220             {
221               {
222                 if (!sameGroup)
223                 {
224                   graphGroup++;
225                   sameGroup = true;
226                 }
227
228                 annot.graphGroup = graphGroup;
229               }
230             }
231             
232             annot.description = "<html>"+service.getActionText() + " - raw scores";
233             if (thrsh!=null)
234             {
235               String threshNote=(thrsh[0]>0 ? "Above " : "Below ")+thrsh[1]+" indicates disorder";
236               annot.threshold=new GraphLine((float) thrsh[1], threshNote, Color.red);
237               annot.description+="<br/>"+threshNote;
238             }
239             annot.description+="</html>";
240             Color col = new UserColourScheme(typeName)
241                     .createColourFromName(typeName + scr.getMethod());
242             for (int p = 0, ps = annot.annotations.length; p < ps; p++)
243             {
244               if (annot.annotations[p] != null)
245               {
246                 annot.annotations[p].colour = col;
247               }
248             }
249             annot._linecolour=col;
250           }
251         }
252         if (lastAnnot + 1 == ourAnnot.size())
253         {
254           // remove singleton alignment annotation row
255           ourAnnot.get(lastAnnot).graphGroup = -1;
256         }
257       }
258       {
259         if (dispFeatures)
260         {
261           jalview.gui.FeatureRenderer fr = ((jalview.gui.AlignmentPanel) ap)
262                   .cloneFeatureRenderer();
263           for (String ft : fc.keySet())
264           {
265             Object gc = fr.getFeatureStyle(ft);
266             if (gc instanceof Color)
267             {
268               // set graduated color as fading to white for minimum, and
269               // autoscaling to values on alignment
270               GraduatedColor ggc = new GraduatedColor(Color.white,
271                       (Color) gc, Float.MIN_VALUE, Float.MAX_VALUE);
272               ggc.setAutoScaled(true);
273               fr.setColour(ft, ggc);
274             }
275           }
276           // TODO: JAL-1150 - create sequence feature settings API for defining styles and enabling/disabling feature overlay on alignment panel
277           ((jalview.gui.AlignmentPanel) ap).updateFeatureRendererFrom(fr);
278           if (af.alignPanel==ap)
279           {
280             // only do this if the alignFrame is currently showing this view.
281             af.setShowSeqFeatures(true);
282           } 
283           ap.paintAlignment(true);
284         }
285         if (ourAnnot.size() > 0)
286         {
287           // Modify the visible annotation on the alignment viewport with the
288           // new alignment annotation rows created.
289           updateOurAnnots(ourAnnot);
290           ap.adjustAnnotationHeight();
291         }
292       }
293     }
294   }
295
296 }