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