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