1 package jalview.ws.rest.params;
3 import jalview.datamodel.AlignmentI;
4 import jalview.ws.params.OptionI;
5 import jalview.ws.params.simple.BooleanOption;
6 import jalview.ws.params.simple.Option;
7 import jalview.ws.rest.InputType;
8 import jalview.ws.rest.NoValidInputDataException;
9 import jalview.ws.rest.RestJob;
10 import jalview.ws.rest.InputType.molType;
11 import jalview.ws.rest.RestServiceDescription;
13 import java.io.BufferedOutputStream;
14 import java.io.ByteArrayOutputStream;
16 import java.io.FileOutputStream;
17 import java.io.OutputStreamWriter;
18 import java.io.PrintWriter;
19 import java.io.StringWriter;
20 import java.io.UnsupportedEncodingException;
21 import java.net.URLEncoder;
22 import java.nio.charset.Charset;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.List;
27 import org.apache.http.entity.mime.content.ContentBody;
28 import org.apache.http.entity.mime.content.FileBody;
29 import org.apache.http.entity.mime.content.StringBody;
32 * format an alignment for input to rest service.
37 public class Alignment extends InputType
42 { AlignmentI.class });
45 String format = "FASTA";
49 boolean jvsuffix = false;
52 * input data as a file upload rather than inline content
54 public boolean writeAsFile=false;
57 public ContentBody formatForInput(RestJob rj)
58 throws UnsupportedEncodingException, NoValidInputDataException
60 AlignmentI alignment = rj.getAlignmentForInput(token, type);
65 File fa = File.createTempFile("jvmime", ".fa");
66 PrintWriter pw = new PrintWriter(
67 new OutputStreamWriter(new BufferedOutputStream(
68 new FileOutputStream(fa)), "UTF-8"));
69 pw.append(new jalview.io.FormatAdapter().formatSequences(format,
70 alignment, jvsuffix));
72 return new FileBody(fa, "text/plain");
73 } catch (Exception ex)
75 throw new NoValidInputDataException(
76 "Couldn't write out alignment to file.", ex);
81 jalview.io.FormatAdapter fa = new jalview.io.FormatAdapter();
82 fa.setNewlineString("\r\n");
83 return new StringBody(
84 (fa.formatSequences(format, alignment, jvsuffix)));
86 // "text/plain",Charset.forName("UTF-8"));
87 // , "text/plain", Charset.forName("UTF-8"));
88 // sb.getContentTypeParameters().put("filename", "alignment.fa");
93 public List<String> getURLEncodedParameter()
95 ArrayList<String> prms = new ArrayList<String>();
96 prms.add("format='" + format + "'");
99 prms.add("type='" + type.toString() + "'");
103 prms.add("jvsuffix");
108 prms.add("writeasfile");
115 public String getURLtokenPrefix()
121 public boolean configureProperty(String tok, String val,
122 StringBuffer warnings)
124 if (tok.startsWith("jvsuffix"))
129 if (tok.startsWith("writeasfile"))
135 if (tok.startsWith("format"))
137 for (String fmt : jalview.io.FormatAdapter.WRITEABLE_FORMATS)
139 if (val.equalsIgnoreCase(fmt))
145 warnings.append("Invalid alignment format '" + val
146 + "'. Must be one of (");
147 for (String fmt : jalview.io.FormatAdapter.WRITEABLE_FORMATS)
149 warnings.append(" " + fmt);
151 warnings.append(")\n");
153 if (tok.startsWith("type"))
157 type = molType.valueOf(val);
159 } catch (Exception x)
161 warnings.append("Invalid molecule type '" + val
162 + "'. Must be one of (");
163 for (molType v : molType.values())
165 warnings.append(" " + v);
167 warnings.append(")\n");
173 public List<OptionI> getOptions()
175 List<OptionI> lst = getBaseOptions();
176 lst.add(new BooleanOption("jvsuffix","Append jalview style /start-end suffix to ID", false, false, jvsuffix, null));
177 lst.add(new BooleanOption("writeasfile","Append jalview style /start-end suffix to ID", false, false, writeAsFile, null));
179 lst.add(new Option("format",
180 "Alignment upload format", true, "FASTA",
181 format, Arrays.asList(jalview.io.FormatAdapter.WRITEABLE_FORMATS), null));
182 lst.add(createMolTypeOption("type", "Sequence type", false, type,