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.InputType.molType;
12 import java.io.UnsupportedEncodingException;
13 import java.util.ArrayList;
14 import java.util.Vector;
16 import org.apache.http.entity.mime.content.ContentBody;
17 import org.apache.http.entity.mime.content.StringBody;
20 * Represents the partitions defined on the alignment as indices e.g. for a
21 * partition (A,B,C),(D,E),(F) The indices would be 3,2,1. Note, the alignment
22 * must be ordered so groups are contiguous before this input type can be used.
27 public class SeqGroupIndexVector extends InputType implements
30 public SeqGroupIndexVector()
33 { AlignmentI.class });
37 * separator for list of sequence Indices - default is ','
39 public String sep = ",";
42 * min size of each partition
44 public int minsize = 1;
49 * prepare the context alignment for this input
52 * - alignment to be processed
53 * @return al or a new alignment with appropriate attributes/order for input
55 public AlignmentI prepareAlignment(AlignmentI al)
57 jalview.analysis.AlignmentSorter.sortByGroup(al);
62 public ContentBody formatForInput(RestJob rj)
63 throws UnsupportedEncodingException, NoValidInputDataException
65 StringBuffer idvector = new StringBuffer();
67 AlignmentI al = rj.getAlignmentForInput(token, type);
68 // assume that alignment is properly ordered so groups form consecutive
70 ArrayList<int[]> gl = new ArrayList<int[]>();
72 for (SequenceGroup sg : (Vector<SequenceGroup>) al.getGroups())
74 if (sg.getSize()<minsize)
76 throw new NoValidInputDataException("Group contains less than "+minsize+" sequences.");
78 // TODO: refactor to sequenceGroup for efficiency -
79 // getAlignmentRowInterval(AlignmentI al)
81 for (SequenceI sq : sg.getSequencesInOrder(al))
102 // are there any more sequences ungrouped that should be added as a single remaining group ? - these might be at the start or the end
105 int[] tail=gl.get(0);
107 if (1+tail[0]>minsize)
109 gl.add(0,new int[] { 0,tail[0]-1});
111 // lets be intelligent here - if the remaining sequences aren't enough to make a final group, then don't make one.
112 // throw new NoValidInputDataException("Group from remaining ungrouped sequences in input contains less than "+minsize+" sequences.");
115 tail=gl.get(gl.size()-1);
116 if (1+tail[1]<al.getHeight())
118 if (al.getHeight()-(1+tail[1])>minsize) {
119 gl.add(new int[] { tail[1]+1, al.getHeight()-1});
121 // lets be intelligent here - if the remaining sequences aren't enough to make a final group, then don't make one.
122 // throw new NoValidInputDataException("Group from remaining ungrouped sequences in input contains less than "+minsize+" sequences.");
127 gl.add(new int[] { 0, al.getHeight()-1});
129 if (min>=0 && gl.size()<min)
131 throw new NoValidInputDataException("Not enough sequence groups for input. Need at least "+min+" groups (including ungrouped regions).");
133 if (max>0 && gl.size()>max)
135 throw new NoValidInputDataException("Too many sequence groups for input. Need at most "+max+" groups (including ungrouped regions).");
137 int[][] vals = gl.toArray(new int[gl.size()][]);
138 int[] srt = new int[gl.size()];
139 for (int i = 0; i < vals.length; i++)
141 jalview.util.QuickSort.sort(srt, vals);
143 int last = vals[0][0] - 1;
144 for (int[] range : vals)
150 idvector.append(sep);
152 idvector.append(range[1] - last);
157 return new StringBody(idvector.toString());
161 * set minimum number of sequences allowed in a partition. Default is 1 sequence.
162 * @param i (number greater than 1)
164 public void setMinsize(int i)