22faba9e318a3c0a875b476b82e3e80640334c33
[jalview.git] / src / jalview / ext / ensembl / EnsemblFeatures.java
1 package jalview.ext.ensembl;
2
3 import jalview.datamodel.Alignment;
4 import jalview.datamodel.AlignmentI;
5 import jalview.io.FeaturesFile;
6 import jalview.io.FileParse;
7
8 import java.io.IOException;
9 import java.net.MalformedURLException;
10 import java.net.URL;
11 import java.util.ArrayList;
12 import java.util.List;
13
14 /**
15  * A client for fetching and processing Ensembl feature data in GFF format by
16  * calling the overlap REST service
17  * 
18  * @author gmcarstairs
19  * @see http://rest.ensembl.org/documentation/info/overlap_id
20  */
21 class EnsemblFeatures extends EnsemblRestClient
22 {
23   /*
24    * The default features to retrieve from Ensembl
25    * can override in getSequenceRecords parameter
26    */
27   private EnsemblFeatureType[] featuresWanted = { EnsemblFeatureType.cds,
28       EnsemblFeatureType.exon, EnsemblFeatureType.variation };
29
30   @Override
31   public String getDbName()
32   {
33     return "ENSEMBL (features)";
34   }
35
36   /**
37    * Makes a query to the REST overlap endpoint for the given sequence
38    * identifier. This returns an 'alignment' consisting of one 'dummy sequence'
39    * (the genomic sequence for which overlap features are returned by the
40    * service). This sequence will have on it sequence features which are the
41    * real information of interest, such as CDS regions or sequence variations.
42    */
43   @Override
44   public AlignmentI getSequenceRecords(String query) throws IOException
45   {
46     // TODO: use a vararg String... for getSequenceRecords instead?
47     List<String> queries = new ArrayList<String>();
48     queries.add(query);
49     FileParse fp = getSequenceReader(queries);
50     FeaturesFile fr = new FeaturesFile(fp);
51     return new Alignment(fr.getSeqsAsArray());
52   }
53
54   /**
55    * Returns a URL for the REST overlap endpoint
56    * 
57    * @param ids
58    * @return
59    */
60   @Override
61   protected URL getUrl(List<String> ids) throws MalformedURLException
62   {
63     StringBuffer urlstring = new StringBuffer(128);
64     urlstring.append(ENSEMBL_REST).append("/overlap/id/")
65             .append(ids.get(0));
66
67     // @see https://github.com/Ensembl/ensembl-rest/wiki/Output-formats
68     urlstring.append("?content-type=text/x-gff3");
69
70     /*
71      * specify  features to retrieve
72      * @see http://rest.ensembl.org/documentation/info/overlap_id
73      * could make the list a configurable entry in jalview.properties
74      */
75     for (EnsemblFeatureType feature : featuresWanted)
76     {
77       urlstring.append("&feature=").append(feature.name());
78     }
79
80     return new URL(urlstring.toString());
81   }
82
83   @Override
84   protected boolean useGetRequest()
85   {
86     return true;
87   }
88
89   /**
90    * Returns the MIME type for GFF3. For GET requests the Content-type header
91    * describes the required encoding of the response.
92    */
93   @Override
94   protected String getRequestMimeType(boolean multipleIds)
95   {
96     return "text/x-gff3";
97   }
98
99   /**
100    * Returns the MIME type for GFF3.
101    */
102   @Override
103   protected String getResponseMimeType()
104   {
105     return "text/x-gff3";
106   }
107
108   /**
109    * Overloaded method that allows a list of features to retrieve to be
110    * specified
111    * 
112    * @param accId
113    * @param features
114    * @return
115    * @throws IOException
116    */
117   protected AlignmentI getSequenceRecords(String accId,
118           EnsemblFeatureType[] features) throws IOException
119   {
120     featuresWanted = features;
121     return getSequenceRecords(accId);
122   }
123 }