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