JAL-2418 source formatting
[jalview.git] / src / jalview / ws / rest / params / AnnotationFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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[] { 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(new jalview.io.AnnotationFile()
75               .printAnnotationsForAlignment(al));
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(new jalview.io.AnnotationFile()
86               .printCSVAnnotations(al.getAlignmentAnnotation()));
87     }
88   }
89
90   @Override
91   public List<String> getURLEncodedParameter()
92   {
93     ArrayList<String> prms = new ArrayList<String>();
94     super.addBaseParams(prms);
95     prms.add("format='" + format + "'");
96     return prms;
97   }
98
99   @Override
100   public String getURLtokenPrefix()
101   {
102     return "ALANNOTATION";
103   }
104
105   @Override
106   public boolean configureProperty(String tok, String val,
107           StringBuffer warnings)
108   {
109
110     if (tok.startsWith("format"))
111     {
112       for (String fmt : new String[] { CSVANNOT, JVANNOT })
113       {
114         if (val.equalsIgnoreCase(fmt))
115         {
116           format = fmt;
117           return true;
118         }
119       }
120       warnings.append("Invalid annotation file format '" + val
121               + "'. Must be one of (");
122       for (String fmt : new String[] { CSVANNOT, JVANNOT })
123       {
124         warnings.append(" " + fmt);
125       }
126       warnings.append(")\n");
127     }
128     return false;
129   }
130
131   @Override
132   public List<OptionI> getOptions()
133   {
134     // TODO - consider disregarding base options here.
135     List<OptionI> lst = getBaseOptions();
136     lst.add(new Option("format", "Alignment annotation upload format", true,
137             JVANNOT, format, Arrays.asList(new String[]
138             { JVANNOT, CSVANNOT }), null));
139     return lst;
140   }
141 }