JAL-724
[jalview.git] / src / jalview / ws / rest / params / SeqGroupIndexVector.java
1 package jalview.ws.rest.params;
2
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;
11
12 import java.io.UnsupportedEncodingException;
13 import java.util.ArrayList;
14 import java.util.Vector;
15
16 import org.apache.http.entity.mime.content.ContentBody;
17 import org.apache.http.entity.mime.content.StringBody;
18
19 /**
20  * Represents the partitions defined on the alignment as indices
21  * e.g. for a partition (A,B,C),(D,E),(F)
22  * The indices would be 3,2,1. Note, the alignment must be ordered so groups are contiguous before this input type can be used.
23  * @author JimP
24  *
25  */
26 public class SeqGroupIndexVector extends InputType implements AlignmentProcessor{
27   public SeqGroupIndexVector()
28   {
29     super(new Class[] { AlignmentI.class} );
30   }
31   /**
32    * separator for list of sequence Indices - default is ','
33    */
34   public String sep=",";
35   molType type;
36   /**
37    * prepare the context alignment for this input
38    * @param al - alignment to be processed
39    * @return al or a new alignment with appropriate attributes/order for input
40    */
41   public AlignmentI prepareAlignment(AlignmentI al)
42   {
43     jalview.analysis.AlignmentSorter.sortByGroup(al);
44     return al;
45   }
46   @Override
47   public ContentBody formatForInput(RestJob rj) throws UnsupportedEncodingException, NoValidInputDataException
48   {
49     StringBuffer idvector = new StringBuffer();
50     boolean list=false;
51     AlignmentI al = rj.getAlignmentForInput(token, type);
52     // assume that alignment is properly ordered so groups form consecutive blocks
53     ArrayList<int[]> gl = new ArrayList<int[]>();
54     for (SequenceGroup sg : (Vector<SequenceGroup>)al.getGroups())
55     {
56       // TODO: refactor to sequenceGroup for efficiency - getAlignmentRowInterval(AlignmentI al)
57       int[] se=null;
58       for (SequenceI sq: sg.getSequencesInOrder(al))
59       {
60         int p = al.findIndex(sq);
61         if (se==null)
62         {
63           se=new int[] { p, p};
64         }
65         else {
66           if (p<se[0])
67             se[0]=p;
68           if (p>se[1])
69             se[1]=p;
70         }
71       }
72       if (se!=null)
73       {
74         gl.add(se);
75       }
76     }
77     int[][] vals = gl.toArray(new int[gl.size()][]);
78     int[] srt = new int[gl.size()];
79     for (int i=0;i<vals.length;i++)
80       srt[i]=vals[i][0];
81     jalview.util.QuickSort.sort(srt, vals);
82     list=false;
83     int last=vals[0][0]-1;
84     for (int[] range:vals)
85     {
86       if (range[1]>last) {
87         if (list)
88       {
89         idvector.append(sep);
90       }
91       idvector.append(range[1]-last);
92       last=range[1];
93       list=true;
94       }
95     }
96     return new StringBody(idvector.toString());
97   }
98   
99 }