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