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