040e000d88397b7a40809969a693ac2a8f297cf5
[jalview.git] / src / jalview / ws / rest / params / SeqVector.java
1 package jalview.ws.rest.params;
2
3 import jalview.datamodel.AlignmentI;
4 import jalview.datamodel.SequenceI;
5 import jalview.ws.params.OptionI;
6 import jalview.ws.params.simple.Option;
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;
12
13 import java.io.UnsupportedEncodingException;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.List;
17
18 import org.apache.http.entity.mime.content.ContentBody;
19 import org.apache.http.entity.mime.content.StringBody;
20 import org.jmol.util.ArrayUtil;
21
22 /**
23  * input a list of sequences separated by some separator 
24  * @author JimP
25  *
26  */
27 public class SeqVector extends InputType {
28   String sep;
29   molType type;
30   public SeqVector()
31   {
32     super(new Class[] { AlignmentI.class} );
33   }
34
35   @Override
36   public ContentBody formatForInput(RestJob rj) throws UnsupportedEncodingException, NoValidInputDataException
37   {
38     StringBuffer idvector = new StringBuffer();
39     boolean list=false;
40     for (SequenceI seq:rj.getSequencesForInput(token, type))
41     {
42       if (list)
43       {
44         idvector.append(sep);
45       }
46       idvector.append(seq.getSequence());
47     }
48     return new StringBody(idvector.toString());
49   }
50   @Override
51   public List<String> getURLEncodedParameter()
52   {
53     ArrayList<String> prms = new ArrayList<String>();
54     super.addBaseParams(prms);
55     prms.add("sep='"+ sep+"'");
56     if (type!=null)
57     {
58       prms.add("type='"+type+"'");
59     }
60     return prms;
61   }
62
63   @Override
64   public String getURLtokenPrefix()
65   {
66     return "SEQS";
67   }
68
69   @Override
70   public boolean configureProperty(String tok, String val,
71           StringBuffer warnings)
72   {
73
74     if (tok.startsWith("sep"))
75     {
76       sep=val;
77       return true;
78     }
79     if (tok.startsWith("type"))
80     {
81       try {
82         type=molType.valueOf(val);
83         return true;
84       } catch (Exception x)
85       {
86         warnings.append("Invalid molecule type '"+val+"'. Must be one of (");
87         for (molType v:molType.values())
88         {
89           warnings.append(" "+v);
90         }
91         warnings.append(")\n");
92       }
93     }
94     return false;
95   }
96
97   @Override
98   public List<OptionI> getOptions()
99   {
100     List<OptionI> lst = getBaseOptions();
101     lst.add(new Option("sep",
102             "Separator character between elements of vector", true, ",",
103             sep, Arrays.asList(new String[]
104             { " ", ",", ";", "\t", "|" }), null));
105     lst.add(createMolTypeOption("type", "Sequence type", false, type,
106             molType.MIX));
107     
108     return lst;
109   }
110
111 }