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