JAL-3851 merged to develop 2022-03-22
[jalview.git] / src / jalview / rest / SelectSequencesEndpoint.java
1 package jalview.rest;
2
3 import java.util.Arrays;
4 import java.util.List;
5
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8
9 import jalview.api.AlignViewportI;
10 import jalview.api.AlignmentViewPanel;
11 import jalview.datamodel.AlignmentI;
12 import jalview.datamodel.SequenceGroup;
13 import jalview.datamodel.SequenceI;
14 import jalview.gui.AlignFrame;
15
16 public class SelectSequencesEndpoint extends AbstractEndpoint
17 {
18   public SelectSequencesEndpoint(API api)
19   {
20     super(api, path, name, parameters, description);
21   }
22
23   protected static final String path = "selectsequences";
24
25   private static final String name = "Select sequence(s) positions";
26
27   private static final String parameters = "<sequence names>/<range>";
28
29   private static final String description = "Select the specified sequence(s) with the specified range";
30
31   public void processEndpoint(HttpServletRequest request,
32           HttpServletResponse response)
33   {
34     if (!checkParameters(request, response, 2))
35     {
36       return;
37     }
38     String[] parameters = getEndpointPathParameters(request);
39
40     String rangesString = parameters[1];
41     int[][] ranges = parseIntRanges(rangesString);
42     if (ranges == null || ranges.length < 2 || ranges[0].length < 1)
43     {
44       returnError(request, response,
45               "couldn't parse range '" + rangesString + "'");
46       return;
47     }
48     if (ranges[0].length > 1)
49     {
50       returnError(request, response,
51               "only provide 1 range '" + rangesString + "'");
52       return;
53     }
54     int start = ranges[0][0];
55     int end = ranges[1][0];
56
57     String sequenceNamesString = parameters[0];
58     List<String> sequenceNames = Arrays
59             .asList(sequenceNamesString.split(","));
60
61     AlignFrame[] alignFrames = getAlignFrames(request, true);
62     if (alignFrames == null)
63     {
64       returnError(request, response, "could not find results");
65       return;
66     }
67     for (int i = 0; i < alignFrames.length; i++)
68     {
69       AlignFrame af = alignFrames[i];
70       List<AlignmentViewPanel> aps = (List<AlignmentViewPanel>) af
71               .getAlignPanels();
72       for (AlignmentViewPanel ap : aps)
73       {
74         AlignViewportI avp = ap.getAlignViewport();
75         AlignmentI al = ap.getAlignment();
76         List<SequenceI> seqs = (List<SequenceI>) al.getSequences();
77         SequenceGroup stretchGroup = new SequenceGroup();
78         for (SequenceI seq : seqs)
79         {
80           if (sequenceNames.contains(seq.getName())
81                   || (sequenceNamesString.equals("*")
82                           && alignFrames.length == 1))
83           {
84             stretchGroup.addSequence(seq, false);
85           }
86         }
87         if (start == -1 && end == -1) // "*" as range
88         {
89           stretchGroup.setStartRes(al.getStartRes());
90           stretchGroup.setEndRes(al.getEndRes());
91         }
92         else
93         {
94           stretchGroup.setStartRes(start);
95           stretchGroup.setEndRes(end);
96         }
97         avp.setSelectionGroup(stretchGroup);
98         ap.paintAlignment(false, false);
99         avp.sendSelection();
100       }
101     }
102   }
103 }