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