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