update author list in license for (JAL-826)
[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  * @author JimP
40  *
41  */
42 public class AnnotationFile extends InputType {
43   public AnnotationFile()
44   {
45     super(new Class[] { AlignmentI.class} );
46   }
47   /**
48    * standard jalview annotation file
49    */
50   final String JVANNOT="JalviewAnnotation";
51   /**
52    * export annotation row as simple csv
53    */
54   final String CSVANNOT="CsvAnnotationRow";
55   /**
56    * format of annotation file
57    */
58   String format=JVANNOT;
59   // TODO verify annotation file format enumeration
60   @Override
61   public ContentBody formatForInput(RestJob rj) throws UnsupportedEncodingException, NoValidInputDataException
62   {
63     AlignmentI al = rj.getAlignmentForInput(token,molType.MIX);
64     if (format.equals(JVANNOT))
65       {return new StringBody(new jalview.io.AnnotationFile().printAnnotations(al.getAlignmentAnnotation(),
66             al.getGroups(),al.getProperties()));
67       } else {
68         if (!format.equals(CSVANNOT))
69         {
70           throw new UnsupportedEncodingException("Unrecognised format for exporting Annotation ("+format+")");
71         }
72         return new StringBody(new jalview.io.AnnotationFile().printCSVAnnotations(al.getAlignmentAnnotation()));
73       }
74   }
75   @Override
76   public List<String> getURLEncodedParameter()
77   {
78     ArrayList<String> prms = new ArrayList<String>();
79     super.addBaseParams(prms);
80     prms.add("format='"+format+"'");
81     return prms;     
82   }
83   @Override
84   public String getURLtokenPrefix()
85   {
86     return "ALANNOTATION";
87   }
88   @Override
89   public boolean configureProperty(String tok, String val,
90           StringBuffer warnings)
91   {
92
93     if (tok.startsWith("format"))
94     {
95       for (String fmt : new String[] { CSVANNOT, JVANNOT})
96       {
97         if (val.equalsIgnoreCase(fmt))
98         {
99           format = fmt;
100           return true;
101         }
102       }
103       warnings.append("Invalid annotation file format '" + val
104               + "'. Must be one of (");
105       for (String fmt : new String[] { CSVANNOT, JVANNOT})
106       {
107         warnings.append(" " + fmt);
108       }
109       warnings.append(")\n");
110     }
111     return false;
112   }
113   @Override
114   public List<OptionI> getOptions()
115   {
116     // TODO - consider disregarding base options here.
117     List<OptionI> lst = getBaseOptions();
118     lst.add(new Option("format",
119             "Alignment annotation upload format", true, JVANNOT,
120             format, Arrays.asList(new String[]
121             { JVANNOT, CSVANNOT}), null));
122     return lst;
123   }
124 }