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