1 /*******************************************************************************
2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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/>.
17 *******************************************************************************/
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;
27 import jalview.ws.rest.InputType.molType;
28 import jalview.ws.rest.RestServiceDescription;
30 import java.io.BufferedOutputStream;
31 import java.io.ByteArrayOutputStream;
33 import java.io.FileOutputStream;
34 import java.io.OutputStreamWriter;
35 import java.io.PrintWriter;
36 import java.io.StringWriter;
37 import java.io.UnsupportedEncodingException;
38 import java.net.URLEncoder;
39 import java.nio.charset.Charset;
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
59 { AlignmentI.class });
62 String format = "FASTA";
66 boolean jvsuffix = false;
69 * input data as a file upload rather than inline content
71 public boolean writeAsFile=false;
74 public ContentBody formatForInput(RestJob rj)
75 throws UnsupportedEncodingException, NoValidInputDataException
77 AlignmentI alignment = rj.getAlignmentForInput(token, type);
82 File fa = File.createTempFile("jvmime", ".fa");
83 PrintWriter pw = new PrintWriter(
84 new OutputStreamWriter(new BufferedOutputStream(
85 new FileOutputStream(fa)), "UTF-8"));
86 pw.append(new jalview.io.FormatAdapter().formatSequences(format,
87 alignment, jvsuffix));
89 return new FileBody(fa, "text/plain");
90 } catch (Exception ex)
92 throw new NoValidInputDataException(
93 "Couldn't write out alignment to file.", ex);
98 jalview.io.FormatAdapter fa = new jalview.io.FormatAdapter();
99 fa.setNewlineString("\r\n");
100 return new StringBody(
101 (fa.formatSequences(format, alignment, jvsuffix)));
103 // "text/plain",Charset.forName("UTF-8"));
104 // , "text/plain", Charset.forName("UTF-8"));
105 // sb.getContentTypeParameters().put("filename", "alignment.fa");
110 public List<String> getURLEncodedParameter()
112 ArrayList<String> prms = new ArrayList<String>();
113 prms.add("format='" + format + "'");
116 prms.add("type='" + type.toString() + "'");
120 prms.add("jvsuffix");
125 prms.add("writeasfile");
132 public String getURLtokenPrefix()
138 public boolean configureProperty(String tok, String val,
139 StringBuffer warnings)
141 if (tok.startsWith("jvsuffix"))
146 if (tok.startsWith("writeasfile"))
152 if (tok.startsWith("format"))
154 for (String fmt : jalview.io.FormatAdapter.WRITEABLE_FORMATS)
156 if (val.equalsIgnoreCase(fmt))
162 warnings.append("Invalid alignment format '" + val
163 + "'. Must be one of (");
164 for (String fmt : jalview.io.FormatAdapter.WRITEABLE_FORMATS)
166 warnings.append(" " + fmt);
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");
190 public List<OptionI> getOptions()
192 List<OptionI> lst = getBaseOptions();
193 lst.add(new BooleanOption("jvsuffix","Append jalview style /start-end suffix to ID", false, false, jvsuffix, null));
194 lst.add(new BooleanOption("writeasfile","Append jalview style /start-end suffix to ID", false, false, writeAsFile, null));
196 lst.add(new Option("format",
197 "Alignment upload format", true, "FASTA",
198 format, Arrays.asList(jalview.io.FormatAdapter.WRITEABLE_FORMATS), null));
199 lst.add(createMolTypeOption("type", "Sequence type", false, type,