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.Arrays;
42 import java.util.List;
44 import org.apache.http.entity.mime.content.ContentBody;
45 import org.apache.http.entity.mime.content.FileBody;
46 import org.apache.http.entity.mime.content.StringBody;
49 * format an alignment for input to rest service.
54 public class Alignment extends InputType
58 super(new Class[] { AlignmentI.class });
61 FileFormatI format = FileFormat.Fasta;
65 boolean jvsuffix = false;
68 * input data as a file upload rather than inline content
70 public boolean writeAsFile = false;
73 public ContentBody formatForInput(RestJob rj)
74 throws UnsupportedEncodingException, NoValidInputDataException
76 AlignmentI alignment = rj.getAlignmentForInput(token, type);
81 File fa = File.createTempFile("jvmime", ".fa");
82 PrintWriter pw = new PrintWriter(
83 new OutputStreamWriter(new BufferedOutputStream(
84 new FileOutputStream(fa)), "UTF-8"));
85 pw.append(new FormatAdapter().formatSequences(format,
86 alignment, jvsuffix));
88 return new FileBody(fa, "text/plain");
89 } catch (Exception ex)
91 throw new NoValidInputDataException(
92 "Couldn't write out alignment to file.", ex);
97 FormatAdapter fa = new FormatAdapter();
98 fa.setNewlineString("\r\n");
99 return new StringBody(
100 (fa.formatSequences(format, alignment, jvsuffix)));
102 // "text/plain",Charset.forName("UTF-8"));
103 // , "text/plain", Charset.forName("UTF-8"));
104 // sb.getContentTypeParameters().put("filename", "alignment.fa");
109 public List<String> getURLEncodedParameter()
111 ArrayList<String> prms = new ArrayList<String>();
112 prms.add("format='" + format + "'");
115 prms.add("type='" + type.toString() + "'");
119 prms.add("jvsuffix");
123 prms.add("writeasfile");
129 public String getURLtokenPrefix()
135 public boolean configureProperty(String tok, String val,
136 StringBuffer warnings)
138 if (tok.startsWith("jvsuffix"))
143 if (tok.startsWith("writeasfile"))
149 if (tok.startsWith("format"))
151 for (FileFormatI fmt : FileFormat.values())
153 if (fmt.isWritable() && val.equalsIgnoreCase(fmt.toString()))
159 warnings.append("Invalid alignment format '" + val
160 + "'. Must be one of (");
161 for (FileFormatI fmt : FileFormat.values())
163 if (fmt.isWritable())
165 warnings.append(" " + fmt).toString();
168 warnings.append(")\n");
170 if (tok.startsWith("type"))
174 type = molType.valueOf(val);
176 } catch (Exception x)
178 warnings.append("Invalid molecule type '" + val
179 + "'. Must be one of (");
180 for (molType v : molType.values())
182 warnings.append(" " + v);
184 warnings.append(")\n");
191 public List<OptionI> getOptions()
193 List<OptionI> lst = getBaseOptions();
194 lst.add(new BooleanOption("jvsuffix",
195 "Append jalview style /start-end suffix to ID", false, false,
197 lst.add(new BooleanOption("writeasfile",
198 "Append jalview style /start-end suffix to ID", false, false,
201 lst.add(new Option("format", "Alignment upload format", true,
202 FileFormat.Fasta.toString(),
203 format.toString(), Arrays
204 .asList(jalview.io.FormatAdapter.WRITEABLE_FORMATS),
206 lst.add(createMolTypeOption("type", "Sequence type", false, type, null));