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