JAL-3851 some changes. HighlightSequenceEndpoint and SelectSequenceEndpoint
[jalview.git] / src / jalview / rest / HighlightSequenceEndpoint.java
1 package jalview.rest;
2
3 import java.util.HashMap;
4 import java.util.List;
5 import java.util.Map;
6
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9
10 import jalview.api.AlignmentViewPanel;
11 import jalview.bin.Cache;
12 import jalview.datamodel.AlignmentI;
13 import jalview.datamodel.SequenceI;
14 import jalview.gui.AlignFrame;
15 import jalview.gui.Desktop;
16 import jalview.structure.StructureSelectionManager;
17
18 public class HighlightSequenceEndpoint extends AbstractEndpoint
19 {
20   public HighlightSequenceEndpoint(API api)
21   {
22     super(api, path, name, parameters, description);
23   }
24
25   protected static final String path = "highlightsequence";
26
27   private static final String name = "Highlight sequence positions";
28
29   private static final String parameters = "<sequence names>,<ranges>";
30
31   private static final String description = "Highlight the specified sequences at the specified position";
32
33   public void processEndpoint(HttpServletRequest request,
34           HttpServletResponse response)
35   {
36     if (!checkParameters(request, response, 2))
37     {
38       return;
39     }
40     String[] parameters = getEndpointPathParameters(request);
41
42     String posString = parameters[1];
43     int pos = -1;
44     try
45     {
46       pos = Integer.parseInt(posString);
47     } catch (NumberFormatException e)
48     {
49       returnError(request, response,
50               "Could not parse postition integer " + posString);
51     }
52
53     String sequenceNames = parameters[0];
54     String fromIdString = request.getParameter("fromId");
55
56     Map<SequenceI, StructureSelectionManager> ssmMap = new HashMap<>();
57     AlignFrame[] alignFrames;
58     if (fromIdString != null)
59     {
60       AlignFrame af = AlignFrame.getAlignFrameFromRestId(fromIdString);
61       if (af == null)
62       {
63         returnError(request, response,
64                 "fromId value '" + fromIdString + "' results not found");
65         return;
66       }
67       alignFrames = new AlignFrame[] { af };
68     }
69     else
70     {
71       alignFrames = Desktop.getAlignFrames();
72     }
73     if (alignFrames == null)
74       return;
75     for (int i = 0; i < alignFrames.length; i++)
76     {
77       AlignFrame af = alignFrames[i];
78       List<AlignmentViewPanel> aps = (List<AlignmentViewPanel>) af
79               .getAlignPanels();
80       for (AlignmentViewPanel ap : aps)
81       {
82         StructureSelectionManager ssm = ap.getStructureSelectionManager();
83         // ap.getAlignViewport().getSequenceSetId()
84         AlignmentI al = ap.getAlignment();
85         List<SequenceI> seqs = (List<SequenceI>) al.getSequences();
86         for (SequenceI seq : seqs)
87         {
88           Cache.info("REMOVEME sequence name=" + seq.getName());
89           if (sequenceNames.equals(seq.getName()))
90           {
91             Cache.info("REMOVEME MATCHED " + seq.getName());
92             ssmMap.put(seq, ssm);
93           }
94         }
95       }
96     }
97     // highlight
98     for (SequenceI seq : ssmMap.keySet())
99     {
100       StructureSelectionManager ssm = ssmMap.get(seq);
101       if (ssm == null)
102       {
103         Cache.info("REMOVEME skipping sequence " + seq.getName());
104         continue;
105       }
106       Cache.info("REMOVEME Attempting to highlight sequence "
107               + seq.getName() + " at postition " + pos);
108       ssm.mouseOverSequence(seq, pos, -1, null);
109     }
110
111   }
112 }