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