2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.ws.rest.params;
23 import jalview.datamodel.AlignmentI;
24 import jalview.io.FileFormat;
25 import jalview.io.FileFormatI;
26 import jalview.io.FormatAdapter;
27 import jalview.ws.params.OptionI;
28 import jalview.ws.params.simple.BooleanOption;
29 import jalview.ws.params.simple.Option;
30 import jalview.ws.rest.InputType;
31 import jalview.ws.rest.NoValidInputDataException;
32 import jalview.ws.rest.RestJob;
34 import java.io.BufferedOutputStream;
36 import java.io.FileOutputStream;
37 import java.io.OutputStreamWriter;
38 import java.io.PrintWriter;
39 import java.io.UnsupportedEncodingException;
40 import java.util.ArrayList;
41 import java.util.List;
43 import org.apache.http.entity.mime.content.ContentBody;
44 import org.apache.http.entity.mime.content.FileBody;
45 import org.apache.http.entity.mime.content.StringBody;
48 * format an alignment for input to rest service.
53 public class Alignment extends InputType
57 super(new Class[] { AlignmentI.class });
60 FileFormatI format = FileFormat.Fasta;
64 boolean jvsuffix = false;
67 * input data as a file upload rather than inline content
69 public boolean writeAsFile = false;
72 public ContentBody formatForInput(RestJob rj)
73 throws UnsupportedEncodingException, NoValidInputDataException
75 AlignmentI alignment = rj.getAlignmentForInput(token, type);
80 File fa = File.createTempFile("jvmime", ".fa");
81 PrintWriter pw = new PrintWriter(
82 new OutputStreamWriter(new BufferedOutputStream(
83 new FileOutputStream(fa)), "UTF-8"));
84 pw.append(new FormatAdapter().formatSequences(format,
85 alignment, jvsuffix));
87 return new FileBody(fa, "text/plain");
88 } catch (Exception ex)
90 throw new NoValidInputDataException(
91 "Couldn't write out alignment to file.", ex);
96 FormatAdapter fa = new FormatAdapter();
97 fa.setNewlineString("\r\n");
98 return new StringBody(
99 (fa.formatSequences(format, alignment, jvsuffix)));
101 // "text/plain",Charset.forName("UTF-8"));
102 // , "text/plain", Charset.forName("UTF-8"));
103 // sb.getContentTypeParameters().put("filename", "alignment.fa");
108 public List<String> getURLEncodedParameter()
110 ArrayList<String> prms = new ArrayList<String>();
111 prms.add("format='" + format + "'");
114 prms.add("type='" + type.toString() + "'");
118 prms.add("jvsuffix");
122 prms.add("writeasfile");
128 public String getURLtokenPrefix()
134 public boolean configureProperty(String tok, String val,
135 StringBuffer warnings)
137 if (tok.startsWith("jvsuffix"))
142 if (tok.startsWith("writeasfile"))
148 if (tok.startsWith("format"))
150 for (FileFormatI fmt : FileFormat.values())
152 if (fmt.isWritable() && val.equalsIgnoreCase(fmt.toString()))
158 warnings.append("Invalid alignment format '" + val
159 + "'. Must be one of (");
160 for (FileFormatI fmt : FileFormat.values())
162 if (fmt.isWritable())
164 warnings.append(" " + fmt).toString();
167 warnings.append(")\n");
169 if (tok.startsWith("type"))
173 type = molType.valueOf(val);
175 } catch (Exception x)
177 warnings.append("Invalid molecule type '" + val
178 + "'. Must be one of (");
179 for (molType v : molType.values())
181 warnings.append(" " + v);
183 warnings.append(")\n");
190 public List<OptionI> getOptions()
192 List<OptionI> lst = getBaseOptions();
193 lst.add(new BooleanOption("jvsuffix",
194 "Append jalview style /start-end suffix to ID", false, false,
196 lst.add(new BooleanOption("writeasfile",
197 "Append jalview style /start-end suffix to ID", false, false,
200 lst.add(new Option("format", "Alignment upload format", true,
201 FileFormat.Fasta.toString(), format.toString(), getWritableFormats(),
203 lst.add(createMolTypeOption("type", "Sequence type", false, type, null));
211 protected List<String> getWritableFormats()
213 List<String> formats = new ArrayList<String>();
214 for (FileFormatI ff : FileFormat.values())
218 formats.add(ff.toString());