1554a0b1e806524fe66b766efa33896e43df68ae
[jalview.git] / src / jalview / ext / ensembl / EnsemblProtein.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.datamodel.AlignmentI;
24 import jalview.datamodel.SequenceFeature;
25
26 import java.util.List;
27
28 import com.stevesoft.pat.Regex;
29
30 /**
31  * A client to fetch protein translated sequence for an Ensembl identifier
32  * 
33  * @author gmcarstairs
34  *
35  */
36 public class EnsemblProtein extends EnsemblSeqProxy
37 {
38   /*
39    * accepts ENSP with 11 digits
40    * or ENSMUSP or similar for other species
41    * or CCDSnnnnn.nn with at least 3 digits
42    */
43   private static final Regex ACCESSION_REGEX = new Regex(
44           "(ENS([A-Z]{3}|)P[0-9]{11}$)" + "|" + "(CCDS[0-9.]{3,}$)");
45
46   /**
47    * Default constructor (to use rest.ensembl.org)
48    */
49   public EnsemblProtein()
50   {
51     super();
52   }
53
54   /**
55    * Constructor given the target domain to fetch data from
56    * 
57    * @param d
58    */
59   public EnsemblProtein(String d)
60   {
61     super(d);
62   }
63
64   @Override
65   public String getDbName()
66   {
67     return "ENSEMBL (Protein)";
68   }
69
70   @Override
71   protected EnsemblSeqType getSourceEnsemblType()
72   {
73     return EnsemblSeqType.PROTEIN;
74   }
75
76   /**
77    * Returns false, as this fetcher does not retrieve DNA sequences.
78    */
79   @Override
80   public boolean isDnaCoding()
81   {
82     return false;
83   }
84
85   /**
86    * Test query is to the protein translation of transcript ENST00000288602
87    */
88   @Override
89   public String getTestQuery()
90   {
91     return "ENSP00000288602";
92   }
93
94   /**
95    * Overrides base class method to do nothing - genomic features are not
96    * applicable to the protein product sequence
97    */
98   @Override
99   protected void addFeaturesAndProduct(String accId, AlignmentI alignment)
100   {
101   }
102
103   @Override
104   protected EnsemblFeatureType[] getFeaturesToFetch()
105   {
106     // not applicable - can't fetch genomic features for a protein sequence
107     return null;
108   }
109
110   @Override
111   protected boolean identifiesSequence(SequenceFeature sf, String accId)
112   {
113     // not applicable - protein sequence is not a 'subset' of genomic sequence
114     return false;
115   }
116
117   @Override
118   public Regex getAccessionValidator()
119   {
120     return ACCESSION_REGEX;
121   }
122
123   /**
124    * Returns an accession id for a query, including conversion of ENST* to
125    * ENSP*. This supports querying for the protein sequence for a transcript
126    * (ENST identifier) and returning the ENSP identifier.
127    */
128   @Override
129   public String getAccessionIdFromQuery(String query)
130   {
131     String accId = super.getAccessionIdFromQuery(query);
132
133     /*
134      * ensure last character before (11) digits is P
135      * ENST00000288602 -> ENSP00000288602
136      * ENSMUST00000288602 -> ENSMUSP00000288602
137      */
138     if (accId != null && accId.length() >= 12)
139     {
140       char[] chars = accId.toCharArray();
141       chars[chars.length - 12] = 'P';
142       accId = new String(chars);
143     }
144     return accId;
145   }
146
147 }