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