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.ws.params.OptionI;
25 import jalview.ws.params.simple.BooleanOption;
26 import jalview.ws.params.simple.Option;
27 import jalview.ws.rest.InputType;
28 import jalview.ws.rest.NoValidInputDataException;
29 import jalview.ws.rest.RestJob;
31 import java.io.BufferedOutputStream;
33 import java.io.FileOutputStream;
34 import java.io.OutputStreamWriter;
35 import java.io.PrintWriter;
36 import java.io.UnsupportedEncodingException;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.List;
41 import org.apache.http.entity.mime.content.ContentBody;
42 import org.apache.http.entity.mime.content.FileBody;
43 import org.apache.http.entity.mime.content.StringBody;
46 * format an alignment for input to rest service.
51 public class Alignment extends InputType
56 { AlignmentI.class });
59 String format = "FASTA";
61 public void setAlignmentFormat(String formatString)
63 if (jalview.io.FormatAdapter.isValidIOFormat(formatString, true))
65 format = formatString;
69 throw new Error("Implemementation error: '" + formatString
70 + "' is not a valid format for writing alignment data.");
76 boolean jvsuffix = false;
79 * input data as a file upload rather than inline content
81 public boolean writeAsFile = false;
84 public ContentBody formatForInput(RestJob rj)
85 throws UnsupportedEncodingException, NoValidInputDataException
87 AlignmentI alignment = rj.getAlignmentForInput(token, type);
92 File fa = File.createTempFile("jvmime", ".fa");
93 PrintWriter pw = new PrintWriter(
94 new OutputStreamWriter(new BufferedOutputStream(
95 new FileOutputStream(fa)), "UTF-8"));
96 pw.append(new jalview.io.FormatAdapter().formatSequences(format,
97 alignment, jvsuffix));
99 return new FileBody(fa, "text/plain");
100 } catch (Exception ex)
102 throw new NoValidInputDataException(
103 "Couldn't write out alignment to file.", ex);
108 jalview.io.FormatAdapter fa = new jalview.io.FormatAdapter();
109 fa.setNewlineString("\r\n");
110 return new StringBody(
111 (fa.formatSequences(format, alignment, jvsuffix)));
113 // "text/plain",Charset.forName("UTF-8"));
114 // , "text/plain", Charset.forName("UTF-8"));
115 // sb.getContentTypeParameters().put("filename", "alignment.fa");
120 public List<String> getURLEncodedParameter()
122 ArrayList<String> prms = new ArrayList<String>();
123 prms.add("format='" + format + "'");
126 prms.add("type='" + type.toString() + "'");
130 prms.add("jvsuffix");
135 prms.add("writeasfile");
142 public String getURLtokenPrefix()
148 public boolean configureProperty(String tok, String val,
149 StringBuffer warnings)
151 if (tok.startsWith("jvsuffix"))
156 if (tok.startsWith("writeasfile"))
162 if (tok.startsWith("format"))
164 for (String fmt : jalview.io.FormatAdapter.WRITEABLE_FORMATS)
166 if (val.equalsIgnoreCase(fmt))
172 warnings.append("Invalid alignment format '" + val
173 + "'. Must be one of (");
174 for (String fmt : jalview.io.FormatAdapter.WRITEABLE_FORMATS)
176 warnings.append(" " + fmt);
178 warnings.append(")\n");
180 if (tok.startsWith("type"))
184 type = molType.valueOf(val);
186 } catch (Exception x)
188 warnings.append("Invalid molecule type '" + val
189 + "'. Must be one of (");
190 for (molType v : molType.values())
192 warnings.append(" " + v);
194 warnings.append(")\n");
201 public List<OptionI> getOptions()
203 List<OptionI> lst = getBaseOptions();
204 lst.add(new BooleanOption("jvsuffix",
205 "Append jalview style /start-end suffix to ID", false, false,
207 lst.add(new BooleanOption("writeasfile",
208 "Append jalview style /start-end suffix to ID", false, false,
211 lst.add(new Option("format", "Alignment upload format", true, "FASTA",
213 .asList(jalview.io.FormatAdapter.WRITEABLE_FORMATS),
215 lst.add(createMolTypeOption("type", "Sequence type", false, type, null));