2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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/>.
18 package jalview.ws.rest.params;
20 import jalview.datamodel.AlignmentI;
21 import jalview.datamodel.SequenceGroup;
22 import jalview.datamodel.SequenceI;
23 import jalview.ws.params.OptionI;
24 import jalview.ws.params.simple.IntegerParameter;
25 import jalview.ws.params.simple.Option;
26 import jalview.ws.rest.AlignmentProcessor;
27 import jalview.ws.rest.InputType;
28 import jalview.ws.rest.NoValidInputDataException;
29 import jalview.ws.rest.RestJob;
31 import java.io.UnsupportedEncodingException;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.List;
36 import org.apache.http.entity.mime.content.ContentBody;
37 import org.apache.http.entity.mime.content.StringBody;
40 * Represents the partitions defined on the alignment as indices e.g. for a
41 * partition (A,B,C),(D,E),(F) The indices would be 3,2,1. Note, the alignment
42 * must be ordered so groups are contiguous before this input type can be used.
47 public class SeqGroupIndexVector extends InputType implements
50 public SeqGroupIndexVector()
53 { AlignmentI.class });
57 * separator for list of sequence Indices - default is ','
59 public String sep = ",";
62 * min size of each partition
64 public int minsize = 1;
69 * prepare the context alignment for this input
72 * - alignment to be processed
73 * @return al or a new alignment with appropriate attributes/order for input
75 public AlignmentI prepareAlignment(AlignmentI al)
77 jalview.analysis.AlignmentSorter.sortByGroup(al);
82 public ContentBody formatForInput(RestJob rj)
83 throws UnsupportedEncodingException, NoValidInputDataException
85 StringBuffer idvector = new StringBuffer();
87 AlignmentI al = rj.getAlignmentForInput(token, type);
88 // assume that alignment is properly ordered so groups form consecutive
90 ArrayList<int[]> gl = new ArrayList<int[]>();
91 int p = 0,lowest=al.getHeight(), highest=0;
92 List<SequenceGroup> sgs;
93 synchronized (sgs = al.getGroups())
95 for (SequenceGroup sg : sgs)
97 if (sg.getSize() < minsize)
99 throw new NoValidInputDataException("Group contains less than "
100 + minsize + " sequences.");
102 // TODO: refactor to sequenceGroup for efficiency -
103 // getAlignmentRowInterval(AlignmentI al)
105 for (SequenceI sq : sg.getSequencesInOrder(al))
107 p = al.findIndex(sq);
135 // are there any more sequences ungrouped that should be added as a single
136 // remaining group ? - these might be at the start or the end
139 if (lowest-1>minsize)
144 if ((al.getHeight()-1-highest)>minsize)
146 gl.add(new int[] { highest+1, al.getHeight()-1});
152 { 0, al.getHeight() - 1 });
154 if (min >= 0 && gl.size() < min)
156 throw new NoValidInputDataException(
157 "Not enough sequence groups for input. Need at least " + min
158 + " groups (including ungrouped regions).");
160 if (max > 0 && gl.size() > max)
162 throw new NoValidInputDataException(
163 "Too many sequence groups for input. Need at most " + max
164 + " groups (including ungrouped regions).");
166 int[][] vals = gl.toArray(new int[gl.size()][]);
167 int[] srt = new int[gl.size()];
168 for (int i = 0; i < vals.length; i++)
170 jalview.util.QuickSort.sort(srt, vals);
172 int last = vals[0][0] - 1;
173 for (int[] range : vals)
179 idvector.append(sep);
181 idvector.append(range[1] - last);
186 return new StringBody(idvector.toString());
190 * set minimum number of sequences allowed in a partition. Default is 1
194 * (number greater than 1)
196 public void setMinsize(int i)
209 public List<String> getURLEncodedParameter()
211 ArrayList<String> prms = new ArrayList<String>();
212 super.addBaseParams(prms);
213 prms.add("minsize='" + minsize + "'");
214 prms.add("sep='" + sep + "'");
217 prms.add("type='" + type + "'");
223 public String getURLtokenPrefix()
229 public boolean configureProperty(String tok, String val,
230 StringBuffer warnings)
233 if (tok.startsWith("sep"))
238 if (tok.startsWith("minsize"))
242 minsize = Integer.valueOf(val);
245 } catch (Exception x)
249 warnings.append("Invalid minsize value '" + val
250 + "'. Must be a positive integer.\n");
252 if (tok.startsWith("type"))
256 type = molType.valueOf(val);
258 } catch (Exception x)
260 warnings.append("Invalid molecule type '" + val
261 + "'. Must be one of (");
262 for (molType v : molType.values())
264 warnings.append(" " + v);
266 warnings.append(")\n");
273 public List<OptionI> getOptions()
275 List<OptionI> lst = getBaseOptions();
276 lst.add(new Option("sep",
277 "Separator character between elements of vector", true, ",",
278 sep, Arrays.asList(new String[]
279 { " ", ",", ";", "\t", "|" }), null));
280 lst.add(new IntegerParameter("minsize",
281 "Minimum size of partition allowed by service", true, 1,
283 lst.add(createMolTypeOption("type", "Sequence type", false, type,