FileFormatI further tweaks, clean compile
[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.io.FileFormat;
25 import jalview.io.FileFormatI;
26 import jalview.io.FormatAdapter;
27 import jalview.ws.params.OptionI;
28 import jalview.ws.params.simple.BooleanOption;
29 import jalview.ws.params.simple.Option;
30 import jalview.ws.rest.InputType;
31 import jalview.ws.rest.NoValidInputDataException;
32 import jalview.ws.rest.RestJob;
33
34 import java.io.BufferedOutputStream;
35 import java.io.File;
36 import java.io.FileOutputStream;
37 import java.io.OutputStreamWriter;
38 import java.io.PrintWriter;
39 import java.io.UnsupportedEncodingException;
40 import java.util.ArrayList;
41 import java.util.List;
42
43 import org.apache.http.entity.mime.content.ContentBody;
44 import org.apache.http.entity.mime.content.FileBody;
45 import org.apache.http.entity.mime.content.StringBody;
46
47 /**
48  * format an alignment for input to rest service.
49  * 
50  * @author JimP
51  * 
52  */
53 public class Alignment extends InputType
54 {
55   public Alignment()
56   {
57     super(new Class[] { AlignmentI.class });
58   }
59
60   FileFormatI format = FileFormat.Fasta;
61
62   molType type;
63
64   boolean jvsuffix = false;
65
66   /**
67    * input data as a file upload rather than inline content
68    */
69   public boolean writeAsFile = false;
70
71   @Override
72   public ContentBody formatForInput(RestJob rj)
73           throws UnsupportedEncodingException, NoValidInputDataException
74   {
75     AlignmentI alignment = rj.getAlignmentForInput(token, type);
76     if (writeAsFile)
77     {
78       try
79       {
80         File fa = File.createTempFile("jvmime", ".fa");
81         PrintWriter pw = new PrintWriter(
82                 new OutputStreamWriter(new BufferedOutputStream(
83                         new FileOutputStream(fa)), "UTF-8"));
84         pw.append(new FormatAdapter().formatSequences(format,
85                 alignment, jvsuffix));
86         pw.close();
87         return new FileBody(fa, "text/plain");
88       } catch (Exception ex)
89       {
90         throw new NoValidInputDataException(
91                 "Couldn't write out alignment to file.", ex);
92       }
93     }
94     else
95     {
96       FormatAdapter fa = new FormatAdapter();
97       fa.setNewlineString("\r\n");
98       return new StringBody(
99               (fa.formatSequences(format, alignment, jvsuffix)));
100       // ,
101       // "text/plain",Charset.forName("UTF-8"));
102       // , "text/plain", Charset.forName("UTF-8"));
103       // sb.getContentTypeParameters().put("filename", "alignment.fa");
104     }
105   }
106
107   @Override
108   public List<String> getURLEncodedParameter()
109   {
110     ArrayList<String> prms = new ArrayList<String>();
111     prms.add("format='" + format + "'");
112     if (type != null)
113     {
114       prms.add("type='" + type.toString() + "'");
115     }
116     if (jvsuffix)
117     {
118       prms.add("jvsuffix");
119     }
120     if (writeAsFile)
121     {
122       prms.add("writeasfile");
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 (FileFormatI fmt : FileFormat.values())
151       {
152         if (fmt.isWritable() && val.equalsIgnoreCase(fmt.toString()))
153         {
154           format = fmt;
155           return true;
156         }
157       }
158       warnings.append("Invalid alignment format '" + val
159               + "'. Must be one of (");
160       for (FileFormatI fmt : FileFormat.values())
161       {
162         if (fmt.isWritable())
163         {
164           warnings.append(" " + fmt).toString();
165         }
166       }
167       warnings.append(")\n");
168     }
169     if (tok.startsWith("type"))
170     {
171       try
172       {
173         type = molType.valueOf(val);
174         return true;
175       } catch (Exception x)
176       {
177         warnings.append("Invalid molecule type '" + val
178                 + "'. Must be one of (");
179         for (molType v : molType.values())
180         {
181           warnings.append(" " + v);
182         }
183         warnings.append(")\n");
184       }
185     }
186     return false;
187   }
188
189   @Override
190   public List<OptionI> getOptions()
191   {
192     List<OptionI> lst = getBaseOptions();
193     lst.add(new BooleanOption("jvsuffix",
194             "Append jalview style /start-end suffix to ID", false, false,
195             jvsuffix, null));
196     lst.add(new BooleanOption("writeasfile",
197             "Append jalview style /start-end suffix to ID", false, false,
198             writeAsFile, null));
199
200     lst.add(new Option("format", "Alignment upload format", true,
201             FileFormat.Fasta.toString(), format.toString(), getWritableFormats(),
202             null));
203     lst.add(createMolTypeOption("type", "Sequence type", false, type, null));
204
205     return lst;
206   }
207
208   /**
209    * @return
210    */
211   protected List<String> getWritableFormats()
212   {
213     List<String> formats = new ArrayList<String>();
214     for (FileFormatI ff : FileFormat.values())
215     {
216       if (ff.isWritable())
217       {
218         formats.add(ff.toString());
219       }
220     }
221     return formats;
222   }
223
224 }