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