JAL-1432 updated copyright notices
[jalview.git] / src / jalview / ws / rest / params / SeqIdVector.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 sequence IDs separated by some separator
39  * 
40  * @author JimP
41  * 
42  */
43 public class SeqIdVector extends InputType
44 {
45   public SeqIdVector()
46   {
47     super(new Class[]
48     { AlignmentI.class });
49   }
50
51   /**
52    * separator for list of sequence IDs - default is ','
53    */
54   String sep = ",";
55
56   molType type;
57
58   @Override
59   public ContentBody formatForInput(RestJob rj)
60           throws UnsupportedEncodingException, NoValidInputDataException
61   {
62     StringBuffer idvector = new StringBuffer();
63     boolean list = false;
64     for (SequenceI seq : rj.getSequencesForInput(token, type))
65     {
66       if (list)
67       {
68         idvector.append(sep);
69       }
70       idvector.append(seq.getName());
71     }
72     return new StringBody(idvector.toString());
73   }
74
75   @Override
76   public List<String> getURLEncodedParameter()
77   {
78     ArrayList<String> prms = new ArrayList<String>();
79     super.addBaseParams(prms);
80     prms.add("sep='" + sep + "'");
81     if (type != null)
82     {
83       prms.add("type='" + type + "'");
84     }
85     return prms;
86   }
87
88   @Override
89   public String getURLtokenPrefix()
90   {
91     return "SEQIDS";
92   }
93
94   @Override
95   public boolean configureProperty(String tok, String val,
96           StringBuffer warnings)
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, null));
132     return lst;
133   }
134 }