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