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