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