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.rest.AlignmentProcessor;
7 import jalview.ws.rest.InputType;
8 import jalview.ws.rest.NoValidInputDataException;
9 import jalview.ws.rest.RestJob;
10 import jalview.ws.rest.RestServiceDescription;
11 import jalview.ws.rest.InputType.molType;
13 import java.io.UnsupportedEncodingException;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Vector;
18 import org.apache.http.entity.mime.content.ContentBody;
19 import org.apache.http.entity.mime.content.StringBody;
22 * Represents the partitions defined on the alignment as indices e.g. for a
23 * partition (A,B,C),(D,E),(F) The indices would be 3,2,1. Note, the alignment
24 * must be ordered so groups are contiguous before this input type can be used.
29 public class SeqGroupIndexVector extends InputType implements
32 public SeqGroupIndexVector()
35 { AlignmentI.class });
39 * separator for list of sequence Indices - default is ','
41 public String sep = ",";
44 * min size of each partition
46 public int minsize = 1;
51 * prepare the context alignment for this input
54 * - alignment to be processed
55 * @return al or a new alignment with appropriate attributes/order for input
57 public AlignmentI prepareAlignment(AlignmentI al)
59 jalview.analysis.AlignmentSorter.sortByGroup(al);
64 public ContentBody formatForInput(RestJob rj)
65 throws UnsupportedEncodingException, NoValidInputDataException
67 StringBuffer idvector = new StringBuffer();
69 AlignmentI al = rj.getAlignmentForInput(token, type);
70 // assume that alignment is properly ordered so groups form consecutive
72 ArrayList<int[]> gl = new ArrayList<int[]>();
74 for (SequenceGroup sg : (Vector<SequenceGroup>) al.getGroups())
76 if (sg.getSize()<minsize)
78 throw new NoValidInputDataException("Group contains less than "+minsize+" sequences.");
80 // TODO: refactor to sequenceGroup for efficiency -
81 // getAlignmentRowInterval(AlignmentI al)
83 for (SequenceI sq : sg.getSequencesInOrder(al))
104 // are there any more sequences ungrouped that should be added as a single remaining group ? - these might be at the start or the end
107 int[] tail=gl.get(0);
109 if (1+tail[0]>minsize)
111 gl.add(0,new int[] { 0,tail[0]-1});
113 // lets be intelligent here - if the remaining sequences aren't enough to make a final group, then don't make one.
114 // throw new NoValidInputDataException("Group from remaining ungrouped sequences in input contains less than "+minsize+" sequences.");
117 tail=gl.get(gl.size()-1);
118 if (1+tail[1]<al.getHeight())
120 if (al.getHeight()-(1+tail[1])>minsize) {
121 gl.add(new int[] { tail[1]+1, al.getHeight()-1});
123 // lets be intelligent here - if the remaining sequences aren't enough to make a final group, then don't make one.
124 // throw new NoValidInputDataException("Group from remaining ungrouped sequences in input contains less than "+minsize+" sequences.");
129 gl.add(new int[] { 0, al.getHeight()-1});
131 if (min>=0 && gl.size()<min)
133 throw new NoValidInputDataException("Not enough sequence groups for input. Need at least "+min+" groups (including ungrouped regions).");
135 if (max>0 && gl.size()>max)
137 throw new NoValidInputDataException("Too many sequence groups for input. Need at most "+max+" groups (including ungrouped regions).");
139 int[][] vals = gl.toArray(new int[gl.size()][]);
140 int[] srt = new int[gl.size()];
141 for (int i = 0; i < vals.length; i++)
143 jalview.util.QuickSort.sort(srt, vals);
145 int last = vals[0][0] - 1;
146 for (int[] range : vals)
152 idvector.append(sep);
154 idvector.append(range[1] - last);
159 return new StringBody(idvector.toString());
163 * set minimum number of sequences allowed in a partition. Default is 1 sequence.
164 * @param i (number greater than 1)
166 public void setMinsize(int i)
176 public List<String> getURLEncodedParameter()
178 ArrayList<String> prms = new ArrayList<String>();
179 super.addBaseParams(prms);
180 prms.add("minsize='"+ minsize+"'");
181 prms.add("sep='"+ sep+"'");
184 prms.add("type='"+type+"'");
190 public String getURLtokenPrefix()
196 public boolean configureProperty(String tok, String val,
197 StringBuffer warnings)
200 if (tok.startsWith("sep"))
205 if (tok.startsWith("minsize"))
208 minsize=Integer.valueOf(val);
211 } catch (Exception x)
215 warnings.append("Invalid minsize value '"+val+"'. Must be a positive integer.\n");
217 if (tok.startsWith("type"))
220 type=molType.valueOf(val);
222 } catch (Exception x)
224 warnings.append("Invalid molecule type '"+val+"'. Must be one of (");
225 for (molType v:molType.values())
227 warnings.append(" "+v);
229 warnings.append(")\n");