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.FormatAdapter;
25 import jalview.ws.params.OptionI;
26 import jalview.ws.params.simple.BooleanOption;
27 import jalview.ws.params.simple.Option;
28 import jalview.ws.rest.InputType;
29 import jalview.ws.rest.NoValidInputDataException;
30 import jalview.ws.rest.RestJob;
32 import java.io.BufferedOutputStream;
34 import java.io.FileOutputStream;
35 import java.io.OutputStreamWriter;
36 import java.io.PrintWriter;
37 import java.io.UnsupportedEncodingException;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
42 import org.apache.http.entity.mime.content.ContentBody;
43 import org.apache.http.entity.mime.content.FileBody;
44 import org.apache.http.entity.mime.content.StringBody;
47 * format an alignment for input to rest service.
52 public class Alignment extends InputType
57 { AlignmentI.class });
60 String format = "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");
123 prms.add("writeasfile");
130 public String getURLtokenPrefix()
136 public boolean configureProperty(String tok, String val,
137 StringBuffer warnings)
139 if (tok.startsWith("jvsuffix"))
144 if (tok.startsWith("writeasfile"))
150 if (tok.startsWith("format"))
152 for (String fmt : FormatAdapter.WRITEABLE_FORMATS)
154 if (val.equalsIgnoreCase(fmt))
160 warnings.append("Invalid alignment format '" + val
161 + "'. Must be one of (");
162 for (String fmt : FormatAdapter.WRITEABLE_FORMATS)
164 warnings.append(" " + fmt);
166 warnings.append(")\n");
168 if (tok.startsWith("type"))
172 type = molType.valueOf(val);
174 } catch (Exception x)
176 warnings.append("Invalid molecule type '" + val
177 + "'. Must be one of (");
178 for (molType v : molType.values())
180 warnings.append(" " + v);
182 warnings.append(")\n");
189 public List<OptionI> getOptions()
191 List<OptionI> lst = getBaseOptions();
192 lst.add(new BooleanOption("jvsuffix",
193 "Append jalview style /start-end suffix to ID", false, false,
195 lst.add(new BooleanOption("writeasfile",
196 "Append jalview style /start-end suffix to ID", false, false,
199 lst.add(new Option("format", "Alignment upload format", true, "FASTA",
201 .asList(FormatAdapter.WRITEABLE_FORMATS),
203 lst.add(createMolTypeOption("type", "Sequence type", false, type, null));