JAL-1620 version bump and release notes
[jalview.git] / src / jalview / ws / rest / params / Alignment.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.ws.rest.params;
22
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;
30
31 import java.io.BufferedOutputStream;
32 import java.io.File;
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;
40
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;
44
45 /**
46  * format an alignment for input to rest service.
47  * 
48  * @author JimP
49  * 
50  */
51 public class Alignment extends InputType
52 {
53   public Alignment()
54   {
55     super(new Class[]
56     { AlignmentI.class });
57   }
58
59   String format = "FASTA";
60
61   molType type;
62
63   boolean jvsuffix = false;
64
65   /**
66    * input data as a file upload rather than inline content
67    */
68   public boolean writeAsFile = false;
69
70   @Override
71   public ContentBody formatForInput(RestJob rj)
72           throws UnsupportedEncodingException, NoValidInputDataException
73   {
74     AlignmentI alignment = rj.getAlignmentForInput(token, type);
75     if (writeAsFile)
76     {
77       try
78       {
79         File fa = File.createTempFile("jvmime", ".fa");
80         PrintWriter pw = new PrintWriter(
81                 new OutputStreamWriter(new BufferedOutputStream(
82                         new FileOutputStream(fa)), "UTF-8"));
83         pw.append(new jalview.io.FormatAdapter().formatSequences(format,
84                 alignment, jvsuffix));
85         pw.close();
86         return new FileBody(fa, "text/plain");
87       } catch (Exception ex)
88       {
89         throw new NoValidInputDataException(
90                 "Couldn't write out alignment to file.", ex);
91       }
92     }
93     else
94     {
95       jalview.io.FormatAdapter fa = new jalview.io.FormatAdapter();
96       fa.setNewlineString("\r\n");
97       return new StringBody(
98               (fa.formatSequences(format, alignment, jvsuffix)));
99       // ,
100       // "text/plain",Charset.forName("UTF-8"));
101       // , "text/plain", Charset.forName("UTF-8"));
102       // sb.getContentTypeParameters().put("filename", "alignment.fa");
103     }
104   }
105
106   @Override
107   public List<String> getURLEncodedParameter()
108   {
109     ArrayList<String> prms = new ArrayList<String>();
110     prms.add("format='" + format + "'");
111     if (type != null)
112     {
113       prms.add("type='" + type.toString() + "'");
114     }
115     if (jvsuffix)
116     {
117       prms.add("jvsuffix");
118     }
119     ;
120     if (writeAsFile)
121     {
122       prms.add("writeasfile");
123     }
124     ;
125     return prms;
126   }
127
128   @Override
129   public String getURLtokenPrefix()
130   {
131     return "ALIGNMENT";
132   }
133
134   @Override
135   public boolean configureProperty(String tok, String val,
136           StringBuffer warnings)
137   {
138     if (tok.startsWith("jvsuffix"))
139     {
140       jvsuffix = true;
141       return true;
142     }
143     if (tok.startsWith("writeasfile"))
144     {
145       writeAsFile = true;
146       return true;
147     }
148
149     if (tok.startsWith("format"))
150     {
151       for (String fmt : jalview.io.FormatAdapter.WRITEABLE_FORMATS)
152       {
153         if (val.equalsIgnoreCase(fmt))
154         {
155           format = fmt;
156           return true;
157         }
158       }
159       warnings.append("Invalid alignment format '" + val
160               + "'. Must be one of (");
161       for (String fmt : jalview.io.FormatAdapter.WRITEABLE_FORMATS)
162       {
163         warnings.append(" " + fmt);
164       }
165       warnings.append(")\n");
166     }
167     if (tok.startsWith("type"))
168     {
169       try
170       {
171         type = molType.valueOf(val);
172         return true;
173       } catch (Exception x)
174       {
175         warnings.append("Invalid molecule type '" + val
176                 + "'. Must be one of (");
177         for (molType v : molType.values())
178         {
179           warnings.append(" " + v);
180         }
181         warnings.append(")\n");
182       }
183     }
184     return false;
185   }
186
187   @Override
188   public List<OptionI> getOptions()
189   {
190     List<OptionI> lst = getBaseOptions();
191     lst.add(new BooleanOption("jvsuffix",
192             "Append jalview style /start-end suffix to ID", false, false,
193             jvsuffix, null));
194     lst.add(new BooleanOption("writeasfile",
195             "Append jalview style /start-end suffix to ID", false, false,
196             writeAsFile, null));
197
198     lst.add(new Option("format", "Alignment upload format", true, "FASTA",
199             format, Arrays
200                     .asList(jalview.io.FormatAdapter.WRITEABLE_FORMATS),
201             null));
202     lst.add(createMolTypeOption("type", "Sequence type", false, type, null));
203
204     return lst;
205   }
206
207 }