2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
3 * Copyright (C) 2014 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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.ws.rest.params;
21 import jalview.datamodel.AlignmentI;
22 import jalview.datamodel.SequenceGroup;
23 import jalview.datamodel.SequenceI;
24 import jalview.ws.params.OptionI;
25 import jalview.ws.params.simple.IntegerParameter;
26 import jalview.ws.params.simple.Option;
27 import jalview.ws.rest.AlignmentProcessor;
28 import jalview.ws.rest.InputType;
29 import jalview.ws.rest.NoValidInputDataException;
30 import jalview.ws.rest.RestJob;
32 import java.io.UnsupportedEncodingException;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.List;
37 import org.apache.http.entity.mime.content.ContentBody;
38 import org.apache.http.entity.mime.content.StringBody;
41 * Represents the partitions defined on the alignment as indices e.g. for a
42 * partition (A,B,C),(D,E),(F) The indices would be 3,2,1. Note, the alignment
43 * must be ordered so groups are contiguous before this input type can be used.
48 public class SeqGroupIndexVector extends InputType implements
51 public SeqGroupIndexVector()
54 { AlignmentI.class });
58 * separator for list of sequence Indices - default is ','
60 public String sep = ",";
63 * min size of each partition
65 public int minsize = 1;
70 * prepare the context alignment for this input
73 * - alignment to be processed
74 * @return al or a new alignment with appropriate attributes/order for input
76 public AlignmentI prepareAlignment(AlignmentI al)
78 jalview.analysis.AlignmentSorter.sortByGroup(al);
83 public ContentBody formatForInput(RestJob rj)
84 throws UnsupportedEncodingException, NoValidInputDataException
86 StringBuffer idvector = new StringBuffer();
88 AlignmentI al = rj.getAlignmentForInput(token, type);
89 // assume that alignment is properly ordered so groups form consecutive
91 ArrayList<int[]> gl = new ArrayList<int[]>();
92 int p = 0,lowest=al.getHeight(), highest=0;
93 List<SequenceGroup> sgs;
94 synchronized (sgs = al.getGroups())
96 for (SequenceGroup sg : sgs)
98 if (sg.getSize() < minsize)
100 throw new NoValidInputDataException("Group contains less than "
101 + minsize + " sequences.");
103 // TODO: refactor to sequenceGroup for efficiency -
104 // getAlignmentRowInterval(AlignmentI al)
106 for (SequenceI sq : sg.getSequencesInOrder(al))
108 p = al.findIndex(sq);
136 // are there any more sequences ungrouped that should be added as a single
137 // remaining group ? - these might be at the start or the end
140 if (lowest-1>minsize)
145 if ((al.getHeight()-1-highest)>minsize)
147 gl.add(new int[] { highest+1, al.getHeight()-1});
153 { 0, al.getHeight() - 1 });
155 if (min >= 0 && gl.size() < min)
157 throw new NoValidInputDataException(
158 "Not enough sequence groups for input. Need at least " + min
159 + " groups (including ungrouped regions).");
161 if (max > 0 && gl.size() > max)
163 throw new NoValidInputDataException(
164 "Too many sequence groups for input. Need at most " + max
165 + " groups (including ungrouped regions).");
167 int[][] vals = gl.toArray(new int[gl.size()][]);
168 int[] srt = new int[gl.size()];
169 for (int i = 0; i < vals.length; i++)
171 jalview.util.QuickSort.sort(srt, vals);
173 int last = vals[0][0] - 1;
174 for (int[] range : vals)
180 idvector.append(sep);
182 idvector.append(range[1] - last);
187 return new StringBody(idvector.toString());
191 * set minimum number of sequences allowed in a partition. Default is 1
195 * (number greater than 1)
197 public void setMinsize(int i)
210 public List<String> getURLEncodedParameter()
212 ArrayList<String> prms = new ArrayList<String>();
213 super.addBaseParams(prms);
214 prms.add("minsize='" + minsize + "'");
215 prms.add("sep='" + sep + "'");
218 prms.add("type='" + type + "'");
224 public String getURLtokenPrefix()
230 public boolean configureProperty(String tok, String val,
231 StringBuffer warnings)
234 if (tok.startsWith("sep"))
239 if (tok.startsWith("minsize"))
243 minsize = Integer.valueOf(val);
246 } catch (Exception x)
250 warnings.append("Invalid minsize value '" + val
251 + "'. Must be a positive integer.\n");
253 if (tok.startsWith("type"))
257 type = molType.valueOf(val);
259 } catch (Exception x)
261 warnings.append("Invalid molecule type '" + val
262 + "'. Must be one of (");
263 for (molType v : molType.values())
265 warnings.append(" " + v);
267 warnings.append(")\n");
274 public List<OptionI> getOptions()
276 List<OptionI> lst = getBaseOptions();
277 lst.add(new Option("sep",
278 "Separator character between elements of vector", true, ",",
279 sep, Arrays.asList(new String[]
280 { " ", ",", ";", "\t", "|" }), null));
281 lst.add(new IntegerParameter("minsize",
282 "Minimum size of partition allowed by service", true, 1,
284 lst.add(createMolTypeOption("type", "Sequence type", false, type,