JAL-3193 removal of rest.ensemblgenomes.org
[jalview.git] / src / jalview / ext / ensembl / EnsemblSequenceFetcher.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.ext.ensembl;
22
23 import jalview.analysis.AlignmentUtils;
24 import jalview.bin.Cache;
25 import jalview.datamodel.DBRefSource;
26 import jalview.ws.seqfetcher.DbSourceProxyImpl;
27
28 import com.stevesoft.pat.Regex;
29
30 /**
31  * A base class for Ensembl sequence fetchers
32  * 
33  * @author gmcarstairs
34  */
35 abstract class EnsemblSequenceFetcher extends DbSourceProxyImpl
36 {
37   // domain property lookup key
38   protected static final String ENSEMBL_BASEURL = "ENSEMBL_BASEURL";
39
40   // domain property default value
41   protected static final String DEFAULT_ENSEMBL_BASEURL = "https://rest.ensembl.org";
42
43   /*
44    * accepts ENSG/T/E/P with 11 digits
45    * or ENSMUSP or similar for other species
46    * or CCDSnnnnn.nn with at least 3 digits
47    */
48   private static final Regex ACCESSION_REGEX = new Regex(
49           "(ENS([A-Z]{3}|)[GTEP]{1}[0-9]{11}$)" + "|"
50                   + "(CCDS[0-9.]{3,}$)");
51
52   protected static final String OBJECT_TYPE_TRANSLATION = "Translation";
53
54   protected static final String OBJECT_TYPE_TRANSCRIPT = "Transcript";
55
56   protected static final String OBJECT_TYPE_GENE = "Gene";
57
58   protected static final String PARENT = "Parent";
59
60   protected static final String JSON_ID = AlignmentUtils.VARIANT_ID; // "id";
61
62   protected static final String OBJECT_TYPE = "object_type";
63
64   /*
65    * possible values for the 'feature' parameter of the /overlap REST service
66    * @see http://rest.ensembl.org/documentation/info/overlap_id
67    */
68   protected enum EnsemblFeatureType
69   {
70     gene, transcript, cds, exon, repeat, simple, misc, variation,
71     somatic_variation, structural_variation, somatic_structural_variation,
72     constrained, regulatory
73   }
74
75   private String domain;
76
77   /**
78    * Constructor
79    */
80   public EnsemblSequenceFetcher()
81   {
82     /*
83      * the default domain names may be overridden in .jalview_properties;
84      * this allows an easy change from http to https in future if needed
85      */
86     domain = Cache.getDefault(ENSEMBL_BASEURL,
87             DEFAULT_ENSEMBL_BASEURL).trim();
88   }
89
90   @Override
91   public String getDbSource()
92   {
93     // NB ensure Uniprot xrefs are canonicalised from "Ensembl" to "ENSEMBL"
94     return DBRefSource.ENSEMBL;
95   }
96
97   @Override
98   public String getAccessionSeparator()
99   {
100     return " ";
101   }
102
103   /**
104    * Ensembl accession are ENST + 11 digits for human transcript, ENSG for human
105    * gene. Other species insert 3 letters e.g. ENSMUST..., ENSMUSG...
106    * 
107    * @see http://www.ensembl.org/Help/View?id=151
108    */
109   @Override
110   public Regex getAccessionValidator()
111   {
112     return ACCESSION_REGEX;
113   }
114
115   @Override
116   public boolean isValidReference(String accession)
117   {
118     return getAccessionValidator().search(accession);
119   }
120
121   @Override
122   public int getTier()
123   {
124     return 0;
125   }
126
127   /**
128    * Default test query is a transcript
129    */
130   @Override
131   public String getTestQuery()
132   {
133     // has CDS on reverse strand:
134     return "ENST00000288602";
135     // ENST00000461457 // forward strand
136   }
137
138   @Override
139   public boolean isDnaCoding()
140   {
141     return true;
142   }
143
144   /**
145    * Returns the domain name to query e.g. http://rest.ensembl.org
146    * 
147    * @return
148    */
149   protected String getDomain()
150   {
151     return domain;
152   }
153
154   protected void setDomain(String d)
155   {
156     domain = d == null ? null : d.trim();
157   }
158 }