2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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/>.
18 package jalview.ws.rest.params;
20 import jalview.datamodel.AlignmentI;
21 import jalview.ws.params.OptionI;
22 import jalview.ws.params.simple.BooleanOption;
23 import jalview.ws.params.simple.Option;
24 import jalview.ws.rest.InputType;
25 import jalview.ws.rest.NoValidInputDataException;
26 import jalview.ws.rest.RestJob;
28 import java.io.BufferedOutputStream;
30 import java.io.FileOutputStream;
31 import java.io.OutputStreamWriter;
32 import java.io.PrintWriter;
33 import java.io.UnsupportedEncodingException;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.List;
38 import org.apache.http.entity.mime.content.ContentBody;
39 import org.apache.http.entity.mime.content.FileBody;
40 import org.apache.http.entity.mime.content.StringBody;
43 * format an alignment for input to rest service.
48 public class Alignment extends InputType
53 { AlignmentI.class });
56 String format = "FASTA";
60 boolean jvsuffix = false;
63 * input data as a file upload rather than inline content
65 public boolean writeAsFile = false;
68 public ContentBody formatForInput(RestJob rj)
69 throws UnsupportedEncodingException, NoValidInputDataException
71 AlignmentI alignment = rj.getAlignmentForInput(token, type);
76 File fa = File.createTempFile("jvmime", ".fa");
77 PrintWriter pw = new PrintWriter(
78 new OutputStreamWriter(new BufferedOutputStream(
79 new FileOutputStream(fa)), "UTF-8"));
80 pw.append(new jalview.io.FormatAdapter().formatSequences(format,
81 alignment, jvsuffix));
83 return new FileBody(fa, "text/plain");
84 } catch (Exception ex)
86 throw new NoValidInputDataException(
87 "Couldn't write out alignment to file.", ex);
92 jalview.io.FormatAdapter fa = new jalview.io.FormatAdapter();
93 fa.setNewlineString("\r\n");
94 return new StringBody(
95 (fa.formatSequences(format, alignment, jvsuffix)));
97 // "text/plain",Charset.forName("UTF-8"));
98 // , "text/plain", Charset.forName("UTF-8"));
99 // sb.getContentTypeParameters().put("filename", "alignment.fa");
104 public List<String> getURLEncodedParameter()
106 ArrayList<String> prms = new ArrayList<String>();
107 prms.add("format='" + format + "'");
110 prms.add("type='" + type.toString() + "'");
114 prms.add("jvsuffix");
119 prms.add("writeasfile");
126 public String getURLtokenPrefix()
132 public boolean configureProperty(String tok, String val,
133 StringBuffer warnings)
135 if (tok.startsWith("jvsuffix"))
140 if (tok.startsWith("writeasfile"))
146 if (tok.startsWith("format"))
148 for (String fmt : jalview.io.FormatAdapter.WRITEABLE_FORMATS)
150 if (val.equalsIgnoreCase(fmt))
156 warnings.append("Invalid alignment format '" + val
157 + "'. Must be one of (");
158 for (String fmt : jalview.io.FormatAdapter.WRITEABLE_FORMATS)
160 warnings.append(" " + fmt);
162 warnings.append(")\n");
164 if (tok.startsWith("type"))
168 type = molType.valueOf(val);
170 } catch (Exception x)
172 warnings.append("Invalid molecule type '" + val
173 + "'. Must be one of (");
174 for (molType v : molType.values())
176 warnings.append(" " + v);
178 warnings.append(")\n");
185 public List<OptionI> getOptions()
187 List<OptionI> lst = getBaseOptions();
188 lst.add(new BooleanOption("jvsuffix",
189 "Append jalview style /start-end suffix to ID", false, false,
191 lst.add(new BooleanOption("writeasfile",
192 "Append jalview style /start-end suffix to ID", false, false,
195 lst.add(new Option("format", "Alignment upload format", true, "FASTA",
197 .asList(jalview.io.FormatAdapter.WRITEABLE_FORMATS),
199 lst.add(createMolTypeOption("type", "Sequence type", false, type, null));