JAL-1620 version bump and release notes
[jalview.git] / src / jalview / ws / rest / params / SeqVector.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 sequences separated by some separator
41  * 
42  * @author JimP
43  * 
44  */
45 public class SeqVector extends InputType
46 {
47   String sep;
48
49   molType type;
50
51   public SeqVector()
52   {
53     super(new Class[]
54     { AlignmentI.class });
55   }
56
57   @Override
58   public ContentBody formatForInput(RestJob rj)
59           throws UnsupportedEncodingException, NoValidInputDataException
60   {
61     StringBuffer idvector = new StringBuffer();
62     boolean list = false;
63     for (SequenceI seq : rj.getSequencesForInput(token, type))
64     {
65       if (list)
66       {
67         idvector.append(sep);
68       }
69       idvector.append(seq.getSequence());
70     }
71     return new StringBody(idvector.toString());
72   }
73
74   @Override
75   public List<String> getURLEncodedParameter()
76   {
77     ArrayList<String> prms = new ArrayList<String>();
78     super.addBaseParams(prms);
79     prms.add("sep='" + sep + "'");
80     if (type != null)
81     {
82       prms.add("type='" + type + "'");
83     }
84     return prms;
85   }
86
87   @Override
88   public String getURLtokenPrefix()
89   {
90     return "SEQS";
91   }
92
93   @Override
94   public boolean configureProperty(String tok, String val,
95           StringBuffer warnings)
96   {
97
98     if (tok.startsWith("sep"))
99     {
100       sep = val;
101       return true;
102     }
103     if (tok.startsWith("type"))
104     {
105       try
106       {
107         type = molType.valueOf(val);
108         return true;
109       } catch (Exception x)
110       {
111         warnings.append("Invalid molecule type '" + val
112                 + "'. Must be one of (");
113         for (molType v : molType.values())
114         {
115           warnings.append(" " + v);
116         }
117         warnings.append(")\n");
118       }
119     }
120     return false;
121   }
122
123   @Override
124   public List<OptionI> getOptions()
125   {
126     List<OptionI> lst = getBaseOptions();
127     lst.add(new Option("sep",
128             "Separator character between elements of vector", true, ",",
129             sep, Arrays.asList(new String[]
130             { " ", ",", ";", "\t", "|" }), null));
131     lst.add(createMolTypeOption("type", "Sequence type", false, type,
132             molType.MIX));
133
134     return lst;
135   }
136
137 }