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