2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.ws.rest.params;
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.SequenceGroup;
25 import jalview.datamodel.SequenceI;
26 import jalview.util.MessageManager;
27 import jalview.ws.params.OptionI;
28 import jalview.ws.params.simple.IntegerParameter;
29 import jalview.ws.params.simple.Option;
30 import jalview.ws.rest.AlignmentProcessor;
31 import jalview.ws.rest.InputType;
32 import jalview.ws.rest.NoValidInputDataException;
33 import jalview.ws.rest.RestJob;
35 import java.io.UnsupportedEncodingException;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.List;
40 import org.apache.http.entity.mime.content.ContentBody;
41 import org.apache.http.entity.mime.content.StringBody;
44 * Represents the partitions defined on the alignment as indices e.g. for a
45 * partition (A,B,C),(D,E),(F) The indices would be 3,2,1. Note, the alignment
46 * must be ordered so groups are contiguous before this input type can be used.
51 public class SeqGroupIndexVector extends InputType
52 implements AlignmentProcessor
54 public SeqGroupIndexVector()
56 super(new Class[] { AlignmentI.class });
60 * separator for list of sequence Indices - default is ','
62 public String sep = ",";
65 * min size of each partition
67 public int minsize = 1;
72 * prepare the context alignment for this input
75 * - alignment to be processed
76 * @return al or a new alignment with appropriate attributes/order for input
79 public AlignmentI prepareAlignment(AlignmentI al)
81 jalview.analysis.AlignmentSorter.sortByGroup(al);
86 public ContentBody formatForInput(RestJob rj)
87 throws UnsupportedEncodingException, NoValidInputDataException
89 StringBuffer idvector = new StringBuffer();
91 AlignmentI al = rj.getAlignmentForInput(token, type);
92 // assume that alignment is properly ordered so groups form consecutive
94 ArrayList<int[]> gl = new ArrayList<>();
95 int p = 0, lowest = al.getHeight(), highest = 0;
96 List<SequenceGroup> sgs = al.getGroups();
99 for (SequenceGroup sg : sgs)
101 if (sg.getSize() < minsize)
103 throw new NoValidInputDataException(MessageManager.formatMessage(
104 "exception.notvaliddata_group_contains_less_than_min_seqs",
106 { Integer.valueOf(minsize).toString() }));
108 // TODO: refactor to sequenceGroup for efficiency -
109 // getAlignmentRowInterval(AlignmentI al)
111 for (SequenceI sq : sg.getSequencesInOrder(al))
113 p = al.findIndex(sq);
124 se = new int[] { p, p };
144 // are there any more sequences ungrouped that should be added as a single
145 // remaining group ? - these might be at the start or the end
148 if (lowest - 1 > minsize)
150 gl.add(0, new int[] { 0, lowest - 2 });
152 if ((al.getHeight() - 1 - highest) > minsize)
154 gl.add(new int[] { highest + 1, al.getHeight() - 1 });
159 gl.add(new int[] { 0, al.getHeight() - 1 });
161 if (min >= 0 && gl.size() < min)
163 throw new NoValidInputDataException(
164 "Not enough sequence groups for input. Need at least " + min
165 + " groups (including ungrouped regions).");
167 if (max > 0 && gl.size() > max)
169 throw new NoValidInputDataException(
170 "Too many sequence groups for input. Need at most " + max
171 + " groups (including ungrouped regions).");
173 int[][] vals = gl.toArray(new int[gl.size()][]);
174 int[] srt = new int[gl.size()];
175 for (int i = 0; i < vals.length; i++)
179 jalview.util.QuickSort.sort(srt, vals);
181 int last = vals[0][0] - 1;
182 for (int[] range : vals)
188 idvector.append(sep);
190 idvector.append(range[1] - last);
195 return new StringBody(idvector.toString());
199 * set minimum number of sequences allowed in a partition. Default is 1
203 * (number greater than 1)
205 public void setMinsize(int i)
218 public List<String> getURLEncodedParameter()
220 ArrayList<String> prms = new ArrayList<>();
221 super.addBaseParams(prms);
222 prms.add("minsize='" + minsize + "'");
223 prms.add("sep='" + sep + "'");
226 prms.add("type='" + type + "'");
232 public String getURLtokenPrefix()
238 public boolean configureProperty(String tok, String val,
239 StringBuffer warnings)
242 if (tok.startsWith("sep"))
247 if (tok.startsWith("minsize"))
251 minsize = Integer.valueOf(val);
256 } catch (Exception x)
260 warnings.append("Invalid minsize value '" + val
261 + "'. Must be a positive integer.\n");
263 if (tok.startsWith("type"))
267 type = molType.valueOf(val);
269 } catch (Exception x)
272 "Invalid molecule type '" + val + "'. Must be one of (");
273 for (molType v : molType.values())
275 warnings.append(" " + v);
277 warnings.append(")\n");
284 public List<OptionI> getOptions()
286 List<OptionI> lst = getBaseOptions();
287 lst.add(new Option("sep",
288 "Separator character between elements of vector", true, ",",
289 sep, Arrays.asList(new String[]
290 { " ", ",", ";", "\t", "|" }), null));
291 lst.add(new IntegerParameter("minsize",
292 "Minimum size of partition allowed by service", true, 1,
294 lst.add(createMolTypeOption("type", "Sequence type", false, type,