2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
3 * Copyright (C) 2014 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 of the License, or (at your option) any later version.
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.
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.
19 package jalview.ws.rest.params;
21 import jalview.datamodel.AlignmentI;
22 import jalview.ws.params.OptionI;
23 import jalview.ws.params.simple.BooleanOption;
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;
29 import java.io.BufferedOutputStream;
31 import java.io.FileOutputStream;
32 import java.io.OutputStreamWriter;
33 import java.io.PrintWriter;
34 import java.io.UnsupportedEncodingException;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.List;
39 import org.apache.http.entity.mime.content.ContentBody;
40 import org.apache.http.entity.mime.content.FileBody;
41 import org.apache.http.entity.mime.content.StringBody;
44 * format an alignment for input to rest service.
49 public class Alignment extends InputType
54 { AlignmentI.class });
57 String format = "FASTA";
61 boolean jvsuffix = false;
64 * input data as a file upload rather than inline content
66 public boolean writeAsFile = false;
69 public ContentBody formatForInput(RestJob rj)
70 throws UnsupportedEncodingException, NoValidInputDataException
72 AlignmentI alignment = rj.getAlignmentForInput(token, type);
77 File fa = File.createTempFile("jvmime", ".fa");
78 PrintWriter pw = new PrintWriter(
79 new OutputStreamWriter(new BufferedOutputStream(
80 new FileOutputStream(fa)), "UTF-8"));
81 pw.append(new jalview.io.FormatAdapter().formatSequences(format,
82 alignment, jvsuffix));
84 return new FileBody(fa, "text/plain");
85 } catch (Exception ex)
87 throw new NoValidInputDataException(
88 "Couldn't write out alignment to file.", ex);
93 jalview.io.FormatAdapter fa = new jalview.io.FormatAdapter();
94 fa.setNewlineString("\r\n");
95 return new StringBody(
96 (fa.formatSequences(format, alignment, jvsuffix)));
98 // "text/plain",Charset.forName("UTF-8"));
99 // , "text/plain", Charset.forName("UTF-8"));
100 // sb.getContentTypeParameters().put("filename", "alignment.fa");
105 public List<String> getURLEncodedParameter()
107 ArrayList<String> prms = new ArrayList<String>();
108 prms.add("format='" + format + "'");
111 prms.add("type='" + type.toString() + "'");
115 prms.add("jvsuffix");
120 prms.add("writeasfile");
127 public String getURLtokenPrefix()
133 public boolean configureProperty(String tok, String val,
134 StringBuffer warnings)
136 if (tok.startsWith("jvsuffix"))
141 if (tok.startsWith("writeasfile"))
147 if (tok.startsWith("format"))
149 for (String fmt : jalview.io.FormatAdapter.WRITEABLE_FORMATS)
151 if (val.equalsIgnoreCase(fmt))
157 warnings.append("Invalid alignment format '" + val
158 + "'. Must be one of (");
159 for (String fmt : jalview.io.FormatAdapter.WRITEABLE_FORMATS)
161 warnings.append(" " + fmt);
163 warnings.append(")\n");
165 if (tok.startsWith("type"))
169 type = molType.valueOf(val);
171 } catch (Exception x)
173 warnings.append("Invalid molecule type '" + val
174 + "'. Must be one of (");
175 for (molType v : molType.values())
177 warnings.append(" " + v);
179 warnings.append(")\n");
186 public List<OptionI> getOptions()
188 List<OptionI> lst = getBaseOptions();
189 lst.add(new BooleanOption("jvsuffix",
190 "Append jalview style /start-end suffix to ID", false, false,
192 lst.add(new BooleanOption("writeasfile",
193 "Append jalview style /start-end suffix to ID", false, false,
196 lst.add(new Option("format", "Alignment upload format", true, "FASTA",
198 .asList(jalview.io.FormatAdapter.WRITEABLE_FORMATS),
200 lst.add(createMolTypeOption("type", "Sequence type", false, type, null));