1 package jalview.ws.rest.params;
3 import jalview.datamodel.AlignmentI;
4 import jalview.datamodel.SequenceGroup;
5 import jalview.datamodel.SequenceI;
6 import jalview.ws.params.OptionI;
7 import jalview.ws.params.simple.IntegerParameter;
8 import jalview.ws.params.simple.Option;
9 import jalview.ws.rest.AlignmentProcessor;
10 import jalview.ws.rest.InputType;
11 import jalview.ws.rest.NoValidInputDataException;
12 import jalview.ws.rest.RestJob;
13 import jalview.ws.rest.RestServiceDescription;
14 import jalview.ws.rest.InputType.molType;
16 import java.io.UnsupportedEncodingException;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
20 import java.util.Vector;
22 import org.apache.http.entity.mime.content.ContentBody;
23 import org.apache.http.entity.mime.content.StringBody;
26 * Represents the partitions defined on the alignment as indices e.g. for a
27 * partition (A,B,C),(D,E),(F) The indices would be 3,2,1. Note, the alignment
28 * must be ordered so groups are contiguous before this input type can be used.
33 public class SeqGroupIndexVector extends InputType implements
36 public SeqGroupIndexVector()
39 { AlignmentI.class });
43 * separator for list of sequence Indices - default is ','
45 public String sep = ",";
48 * min size of each partition
50 public int minsize = 1;
55 * prepare the context alignment for this input
58 * - alignment to be processed
59 * @return al or a new alignment with appropriate attributes/order for input
61 public AlignmentI prepareAlignment(AlignmentI al)
63 jalview.analysis.AlignmentSorter.sortByGroup(al);
68 public ContentBody formatForInput(RestJob rj)
69 throws UnsupportedEncodingException, NoValidInputDataException
71 StringBuffer idvector = new StringBuffer();
73 AlignmentI al = rj.getAlignmentForInput(token, type);
74 // assume that alignment is properly ordered so groups form consecutive
76 ArrayList<int[]> gl = new ArrayList<int[]>();
78 for (SequenceGroup sg : (Vector<SequenceGroup>) al.getGroups())
80 if (sg.getSize() < minsize)
82 throw new NoValidInputDataException("Group contains less than "
83 + minsize + " sequences.");
85 // TODO: refactor to sequenceGroup for efficiency -
86 // getAlignmentRowInterval(AlignmentI al)
88 for (SequenceI sq : sg.getSequencesInOrder(al))
109 // are there any more sequences ungrouped that should be added as a single
110 // remaining group ? - these might be at the start or the end
113 int[] tail = gl.get(0);
116 if (1 + tail[0] > minsize)
123 // lets be intelligent here - if the remaining sequences aren't enough
124 // to make a final group, then don't make one.
126 // NoValidInputDataException("Group from remaining ungrouped sequences in input contains less than "+minsize+" sequences.");
131 tail = gl.get(gl.size() - 1);
132 if (1 + tail[1] < al.getHeight())
134 if (al.getHeight() - (1 + tail[1]) > minsize)
137 { tail[1] + 1, al.getHeight() - 1 });
141 // lets be intelligent here - if the remaining sequences aren't
142 // enough to make a final group, then don't make one.
144 // NoValidInputDataException("Group from remaining ungrouped sequences in input contains less than "+minsize+" sequences.");
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", "Minimum size of partition allowed by service", true, 1, minsize, 1,0));
281 lst.add(createMolTypeOption("type", "Sequence type", false, type, molType.MIX));