6d031b7e8219222d93a3e41e17cbf208147aab8a
[jalview.git] / src / jalview / ext / ensembl / EnsemblCdna.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.SequenceFeature;
24 import jalview.io.gff.SequenceOntologyFactory;
25 import jalview.io.gff.SequenceOntologyI;
26
27 import java.util.HashMap;
28 import java.util.Map;
29
30 import com.stevesoft.pat.Regex;
31
32 /**
33  * A client to fetch CDNA sequence from Ensembl (i.e. that part of the genomic
34  * sequence that is transcribed to RNA, but not necessarily translated to
35  * protein)
36  * 
37  * @author gmcarstairs
38  *
39  */
40 public class EnsemblCdna extends EnsemblSeqProxy
41 {
42   /*
43    * accepts ENST or ENSTG with 11 digits
44    * or ENSMUST or similar for other species
45    * or CCDSnnnnn.nn with at least 3 digits
46    */
47   private static final Regex ACCESSION_REGEX = new Regex(
48           "(ENS([A-Z]{3}|)[TG][0-9]{11}$)" + "|" + "(CCDS[0-9.]{3,}$)");
49
50   private static Map<String, String> params = new HashMap<String, String>();
51
52   static
53   {
54     params.put("object_type", "transcript");
55   }
56
57   /*
58    * fetch exon features on genomic sequence (to identify the cdna regions)
59    * and cds and variation features (to retain)
60    */
61   private static final EnsemblFeatureType[] FEATURES_TO_FETCH = {
62       EnsemblFeatureType.exon, EnsemblFeatureType.cds,
63       EnsemblFeatureType.variation };
64
65   /**
66    * Default constructor (to use rest.ensembl.org)
67    */
68   public EnsemblCdna()
69   {
70     super();
71   }
72
73   /**
74    * Constructor given the target domain to fetch data from
75    * 
76    * @param d
77    */
78   public EnsemblCdna(String d)
79   {
80     super(d);
81   }
82
83   @Override
84   public String getDbName()
85   {
86     return "ENSEMBL (CDNA)";
87   }
88
89   @Override
90   protected EnsemblSeqType getSourceEnsemblType()
91   {
92     return EnsemblSeqType.CDNA;
93   }
94
95   @Override
96   public Regex getAccessionValidator()
97   {
98     return ACCESSION_REGEX;
99   }
100
101   @Override
102   protected EnsemblFeatureType[] getFeaturesToFetch()
103   {
104     return FEATURES_TO_FETCH;
105   }
106
107   /**
108    * Answers true unless the feature type is 'transcript' (or a sub-type in the
109    * Sequence Ontology).
110    */
111   @Override
112   protected boolean retainFeature(SequenceFeature sf, String accessionId)
113   {
114     if (isTranscript(sf.getType()))
115     {
116       return false;
117     }
118     return featureMayBelong(sf, accessionId);
119   }
120
121   /**
122    * Answers true if the sequence feature type is 'exon' (or a subtype of exon
123    * in the Sequence Ontology), and the Parent of the feature is the transcript
124    * we are retrieving
125    */
126   @Override
127   protected boolean identifiesSequence(SequenceFeature sf, String accId)
128   {
129     if (SequenceOntologyFactory.getInstance().isA(sf.getType(),
130             SequenceOntologyI.EXON))
131     {
132       String parentFeature = (String) sf.getValue(PARENT);
133       if (("transcript:" + accId).equals(parentFeature))
134       {
135         return true;
136       }
137     }
138     return false;
139   }
140
141   /**
142    * Parameter object_type=cdna added to ensure cdna and not peptide is returned
143    * (JAL-2529)
144    */
145   @Override
146   protected Map<String, String> getAdditionalParameters()
147   {
148     return params;
149   }
150
151 }