JAL-1620 version bump and release notes
[jalview.git] / src / jalview / ws / rest / params / SeqIdVector.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ws.rest.params;
22
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.SequenceI;
25 import jalview.ws.params.OptionI;
26 import jalview.ws.params.simple.Option;
27 import jalview.ws.rest.InputType;
28 import jalview.ws.rest.NoValidInputDataException;
29 import jalview.ws.rest.RestJob;
30
31 import java.io.UnsupportedEncodingException;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.List;
35
36 import org.apache.http.entity.mime.content.ContentBody;
37 import org.apache.http.entity.mime.content.StringBody;
38
39 /**
40  * input a list of sequence IDs separated by some separator
41  * 
42  * @author JimP
43  * 
44  */
45 public class SeqIdVector extends InputType
46 {
47   public SeqIdVector()
48   {
49     super(new Class[]
50     { AlignmentI.class });
51   }
52
53   /**
54    * separator for list of sequence IDs - default is ','
55    */
56   String sep = ",";
57
58   molType type;
59
60   @Override
61   public ContentBody formatForInput(RestJob rj)
62           throws UnsupportedEncodingException, NoValidInputDataException
63   {
64     StringBuffer idvector = new StringBuffer();
65     boolean list = false;
66     for (SequenceI seq : rj.getSequencesForInput(token, type))
67     {
68       if (list)
69       {
70         idvector.append(sep);
71       }
72       idvector.append(seq.getName());
73     }
74     return new StringBody(idvector.toString());
75   }
76
77   @Override
78   public List<String> getURLEncodedParameter()
79   {
80     ArrayList<String> prms = new ArrayList<String>();
81     super.addBaseParams(prms);
82     prms.add("sep='" + sep + "'");
83     if (type != null)
84     {
85       prms.add("type='" + type + "'");
86     }
87     return prms;
88   }
89
90   @Override
91   public String getURLtokenPrefix()
92   {
93     return "SEQIDS";
94   }
95
96   @Override
97   public boolean configureProperty(String tok, String val,
98           StringBuffer warnings)
99   {
100     if (tok.startsWith("sep"))
101     {
102       sep = val;
103       return true;
104     }
105     if (tok.startsWith("type"))
106     {
107       try
108       {
109         type = molType.valueOf(val);
110         return true;
111       } catch (Exception x)
112       {
113         warnings.append("Invalid molecule type '" + val
114                 + "'. Must be one of (");
115         for (molType v : molType.values())
116         {
117           warnings.append(" " + v);
118         }
119         warnings.append(")\n");
120       }
121     }
122     return false;
123   }
124
125   @Override
126   public List<OptionI> getOptions()
127   {
128     List<OptionI> lst = getBaseOptions();
129     lst.add(new Option("sep",
130             "Separator character between elements of vector", true, ",",
131             sep, Arrays.asList(new String[]
132             { " ", ",", ";", "\t", "|" }), null));
133     lst.add(createMolTypeOption("type", "Sequence type", false, type, null));
134     return lst;
135   }
136 }