b1681a06f75cd3eff3d32c1cf486c67f48fff869
[jalview.git] / src / jalview / ws / rest / params / AnnotationFile.java
1 package jalview.ws.rest.params;
2
3 import jalview.datamodel.AlignmentI;
4 import jalview.ws.params.OptionI;
5 import jalview.ws.params.simple.Option;
6 import jalview.ws.rest.InputType;
7 import jalview.ws.rest.NoValidInputDataException;
8 import jalview.ws.rest.RestJob;
9 import jalview.ws.rest.RestServiceDescription;
10 import jalview.ws.rest.InputType.molType;
11
12 import java.io.UnsupportedEncodingException;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.List;
16
17 import org.apache.http.entity.mime.content.ContentBody;
18 import org.apache.http.entity.mime.content.StringBody;
19
20 /**
21  * format a jalview annotation file for input to a rest service.
22  * @author JimP
23  *
24  */
25 public class AnnotationFile extends InputType {
26   public AnnotationFile()
27   {
28     super(new Class[] { AlignmentI.class} );
29   }
30   /**
31    * standard jalview annotation file
32    */
33   final String JVANNOT="JalviewAnnotation";
34   /**
35    * export annotation row as simple csv
36    */
37   final String CSVANNOT="CsvAnnotationRow";
38   /**
39    * format of annotation file
40    */
41   String format=JVANNOT;
42   // TODO verify annotation file format enumeration
43   @Override
44   public ContentBody formatForInput(RestJob rj) throws UnsupportedEncodingException, NoValidInputDataException
45   {
46     AlignmentI al = rj.getAlignmentForInput(token,molType.MIX);
47     if (format.equals(JVANNOT))
48       {return new StringBody(new jalview.io.AnnotationFile().printAnnotations(al.getAlignmentAnnotation(),
49             al.getGroups(),al.getProperties()));
50       } else {
51         if (!format.equals(CSVANNOT))
52         {
53           throw new UnsupportedEncodingException("Unrecognised format for exporting Annotation ("+format+")");
54         }
55         return new StringBody(new jalview.io.AnnotationFile().printCSVAnnotations(al.getAlignmentAnnotation()));
56       }
57   }
58   @Override
59   public List<String> getURLEncodedParameter()
60   {
61     ArrayList<String> prms = new ArrayList<String>();
62     super.addBaseParams(prms);
63     prms.add("format='"+format+"'");
64     return prms;     
65   }
66   @Override
67   public String getURLtokenPrefix()
68   {
69     return "ALANNOTATION";
70   }
71   @Override
72   public boolean configureProperty(String tok, String val,
73           StringBuffer warnings)
74   {
75
76     if (tok.startsWith("format"))
77     {
78       for (String fmt : new String[] { CSVANNOT, JVANNOT})
79       {
80         if (val.equalsIgnoreCase(fmt))
81         {
82           format = fmt;
83           return true;
84         }
85       }
86       warnings.append("Invalid annotation file format '" + val
87               + "'. Must be one of (");
88       for (String fmt : new String[] { CSVANNOT, JVANNOT})
89       {
90         warnings.append(" " + fmt);
91       }
92       warnings.append(")\n");
93     }
94     return false;
95   }
96   @Override
97   public List<OptionI> getOptions()
98   {
99     // TODO - consider disregarding base options here.
100     List<OptionI> lst = getBaseOptions();
101     lst.add(new Option("format",
102             "Alignment annotation upload format", true, JVANNOT,
103             format, Arrays.asList(new String[]
104             { JVANNOT, CSVANNOT}), null));
105     return lst;
106   }
107 }