JAL-1620 version bump and release notes
[jalview.git] / src / jalview / ws / rest / params / AnnotationFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
3  * Copyright (C) 2014 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.Option;
26 import jalview.ws.rest.InputType;
27 import jalview.ws.rest.NoValidInputDataException;
28 import jalview.ws.rest.RestJob;
29
30 import java.io.UnsupportedEncodingException;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
34
35 import org.apache.http.entity.mime.content.ContentBody;
36 import org.apache.http.entity.mime.content.StringBody;
37
38 /**
39  * format a jalview annotation file for input to a rest service.
40  * 
41  * @author JimP
42  * 
43  */
44 public class AnnotationFile extends InputType
45 {
46   public AnnotationFile()
47   {
48     super(new Class[]
49     { AlignmentI.class });
50   }
51
52   /**
53    * standard jalview annotation file
54    */
55   final String JVANNOT = "JalviewAnnotation";
56
57   /**
58    * export annotation row as simple csv
59    */
60   final String CSVANNOT = "CsvAnnotationRow";
61
62   /**
63    * format of annotation file
64    */
65   String format = JVANNOT;
66
67   // TODO verify annotation file format enumeration
68   @Override
69   public ContentBody formatForInput(RestJob rj)
70           throws UnsupportedEncodingException, NoValidInputDataException
71   {
72     AlignmentI al = rj.getAlignmentForInput(token, molType.MIX);
73     if (format.equals(JVANNOT))
74     {
75       return new StringBody(
76               new jalview.io.AnnotationFile().printAnnotations(
77                       al.getAlignmentAnnotation(), al.getGroups(),
78                       al.getProperties()));
79     }
80     else
81     {
82       if (!format.equals(CSVANNOT))
83       {
84         throw new UnsupportedEncodingException(
85                 "Unrecognised format for exporting Annotation (" + format
86                         + ")");
87       }
88       return new StringBody(
89               new jalview.io.AnnotationFile().printCSVAnnotations(al
90                       .getAlignmentAnnotation()));
91     }
92   }
93
94   @Override
95   public List<String> getURLEncodedParameter()
96   {
97     ArrayList<String> prms = new ArrayList<String>();
98     super.addBaseParams(prms);
99     prms.add("format='" + format + "'");
100     return prms;
101   }
102
103   @Override
104   public String getURLtokenPrefix()
105   {
106     return "ALANNOTATION";
107   }
108
109   @Override
110   public boolean configureProperty(String tok, String val,
111           StringBuffer warnings)
112   {
113
114     if (tok.startsWith("format"))
115     {
116       for (String fmt : new String[]
117       { CSVANNOT, JVANNOT })
118       {
119         if (val.equalsIgnoreCase(fmt))
120         {
121           format = fmt;
122           return true;
123         }
124       }
125       warnings.append("Invalid annotation file format '" + val
126               + "'. Must be one of (");
127       for (String fmt : new String[]
128       { CSVANNOT, JVANNOT })
129       {
130         warnings.append(" " + fmt);
131       }
132       warnings.append(")\n");
133     }
134     return false;
135   }
136
137   @Override
138   public List<OptionI> getOptions()
139   {
140     // TODO - consider disregarding base options here.
141     List<OptionI> lst = getBaseOptions();
142     lst.add(new Option("format", "Alignment annotation upload format",
143             true, JVANNOT, format, Arrays.asList(new String[]
144             { JVANNOT, CSVANNOT }), null));
145     return lst;
146   }
147 }