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