JAL-1807 explicit imports (jalview.ws.*)
[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.FormatAdapter;
25 import jalview.ws.params.OptionI;
26 import jalview.ws.params.simple.BooleanOption;
27 import jalview.ws.params.simple.Option;
28 import jalview.ws.rest.InputType;
29 import jalview.ws.rest.NoValidInputDataException;
30 import jalview.ws.rest.RestJob;
31
32 import java.io.BufferedOutputStream;
33 import java.io.File;
34 import java.io.FileOutputStream;
35 import java.io.OutputStreamWriter;
36 import java.io.PrintWriter;
37 import java.io.UnsupportedEncodingException;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
41
42 import org.apache.http.entity.mime.content.ContentBody;
43 import org.apache.http.entity.mime.content.FileBody;
44 import org.apache.http.entity.mime.content.StringBody;
45
46 /**
47  * format an alignment for input to rest service.
48  * 
49  * @author JimP
50  * 
51  */
52 public class Alignment extends InputType
53 {
54   public Alignment()
55   {
56     super(new Class[]
57     { AlignmentI.class });
58   }
59
60   String format = "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     ;
121     if (writeAsFile)
122     {
123       prms.add("writeasfile");
124     }
125     ;
126     return prms;
127   }
128
129   @Override
130   public String getURLtokenPrefix()
131   {
132     return "ALIGNMENT";
133   }
134
135   @Override
136   public boolean configureProperty(String tok, String val,
137           StringBuffer warnings)
138   {
139     if (tok.startsWith("jvsuffix"))
140     {
141       jvsuffix = true;
142       return true;
143     }
144     if (tok.startsWith("writeasfile"))
145     {
146       writeAsFile = true;
147       return true;
148     }
149
150     if (tok.startsWith("format"))
151     {
152       for (String fmt : FormatAdapter.WRITEABLE_FORMATS)
153       {
154         if (val.equalsIgnoreCase(fmt))
155         {
156           format = fmt;
157           return true;
158         }
159       }
160       warnings.append("Invalid alignment format '" + val
161               + "'. Must be one of (");
162       for (String fmt : FormatAdapter.WRITEABLE_FORMATS)
163       {
164         warnings.append(" " + fmt);
165       }
166       warnings.append(")\n");
167     }
168     if (tok.startsWith("type"))
169     {
170       try
171       {
172         type = molType.valueOf(val);
173         return true;
174       } catch (Exception x)
175       {
176         warnings.append("Invalid molecule type '" + val
177                 + "'. Must be one of (");
178         for (molType v : molType.values())
179         {
180           warnings.append(" " + v);
181         }
182         warnings.append(")\n");
183       }
184     }
185     return false;
186   }
187
188   @Override
189   public List<OptionI> getOptions()
190   {
191     List<OptionI> lst = getBaseOptions();
192     lst.add(new BooleanOption("jvsuffix",
193             "Append jalview style /start-end suffix to ID", false, false,
194             jvsuffix, null));
195     lst.add(new BooleanOption("writeasfile",
196             "Append jalview style /start-end suffix to ID", false, false,
197             writeAsFile, null));
198
199     lst.add(new Option("format", "Alignment upload format", true, "FASTA",
200             format, Arrays
201                     .asList(FormatAdapter.WRITEABLE_FORMATS),
202             null));
203     lst.add(createMolTypeOption("type", "Sequence type", false, type, null));
204
205     return lst;
206   }
207
208 }