a633e0f2f63379dea558d5fb74aaf7a58970a4f8
[jalview.git] / src / jalview / ws / rest / params / SeqVector.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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  */
18 package jalview.ws.rest.params;
19
20 import jalview.datamodel.AlignmentI;
21 import jalview.datamodel.SequenceI;
22 import jalview.ws.params.OptionI;
23 import jalview.ws.params.simple.Option;
24 import jalview.ws.rest.InputType;
25 import jalview.ws.rest.NoValidInputDataException;
26 import jalview.ws.rest.RestJob;
27
28 import java.io.UnsupportedEncodingException;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.List;
32
33 import org.apache.http.entity.mime.content.ContentBody;
34 import org.apache.http.entity.mime.content.StringBody;
35
36 /**
37  * input a list of sequences separated by some separator
38  * 
39  * @author JimP
40  * 
41  */
42 public class SeqVector extends InputType
43 {
44   String sep;
45
46   molType type;
47
48   public SeqVector()
49   {
50     super(new Class[]
51     { AlignmentI.class });
52   }
53
54   @Override
55   public ContentBody formatForInput(RestJob rj)
56           throws UnsupportedEncodingException, NoValidInputDataException
57   {
58     StringBuffer idvector = new StringBuffer();
59     boolean list = false;
60     for (SequenceI seq : rj.getSequencesForInput(token, type))
61     {
62       if (list)
63       {
64         idvector.append(sep);
65       }
66       idvector.append(seq.getSequence());
67     }
68     return new StringBody(idvector.toString());
69   }
70
71   @Override
72   public List<String> getURLEncodedParameter()
73   {
74     ArrayList<String> prms = new ArrayList<String>();
75     super.addBaseParams(prms);
76     prms.add("sep='" + sep + "'");
77     if (type != null)
78     {
79       prms.add("type='" + type + "'");
80     }
81     return prms;
82   }
83
84   @Override
85   public String getURLtokenPrefix()
86   {
87     return "SEQS";
88   }
89
90   @Override
91   public boolean configureProperty(String tok, String val,
92           StringBuffer warnings)
93   {
94
95     if (tok.startsWith("sep"))
96     {
97       sep = val;
98       return true;
99     }
100     if (tok.startsWith("type"))
101     {
102       try
103       {
104         type = molType.valueOf(val);
105         return true;
106       } catch (Exception x)
107       {
108         warnings.append("Invalid molecule type '" + val
109                 + "'. Must be one of (");
110         for (molType v : molType.values())
111         {
112           warnings.append(" " + v);
113         }
114         warnings.append(")\n");
115       }
116     }
117     return false;
118   }
119
120   @Override
121   public List<OptionI> getOptions()
122   {
123     List<OptionI> lst = getBaseOptions();
124     lst.add(new Option("sep",
125             "Separator character between elements of vector", true, ",",
126             sep, Arrays.asList(new String[]
127             { " ", ",", ";", "\t", "|" }), null));
128     lst.add(createMolTypeOption("type", "Sequence type", false, type,
129             molType.MIX));
130
131     return lst;
132   }
133
134 }