JAL-3076 refactor for more efficient scan of 'gene' features
[jalview.git] / src / jalview / ext / ensembl / EnsemblGenome.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
27 import java.util.ArrayList;
28 import java.util.List;
29
30 /**
31  * A client to fetch genomic sequence from Ensembl
32  * 
33  * TODO: not currently used - delete?
34  * 
35  * @author gmcarstairs
36  *
37  */
38 public class EnsemblGenome extends EnsemblSeqProxy
39 {
40   /*
41    * fetch transcript features on genomic sequence (to identify the transcript 
42    * regions) and cds, exon and variation features (to retain)
43    */
44   private static final EnsemblFeatureType[] FEATURES_TO_FETCH = {
45       EnsemblFeatureType.transcript, EnsemblFeatureType.exon,
46       EnsemblFeatureType.cds, EnsemblFeatureType.variation };
47
48   /**
49    * Default constructor (to use rest.ensembl.org)
50    */
51   public EnsemblGenome()
52   {
53     super();
54   }
55
56   /**
57    * Constructor given the target domain to fetch data from
58    * 
59    * @param d
60    */
61   public EnsemblGenome(String d)
62   {
63     super(d);
64   }
65
66   @Override
67   public String getDbName()
68   {
69     return "ENSEMBL (Genomic)";
70   }
71
72   @Override
73   protected EnsemblSeqType getSourceEnsemblType()
74   {
75     return EnsemblSeqType.GENOMIC;
76   }
77
78   @Override
79   protected EnsemblFeatureType[] getFeaturesToFetch()
80   {
81     return FEATURES_TO_FETCH;
82   }
83
84   /**
85    * Answers true unless the feature type is 'transcript' (or a sub-type of
86    * transcript in the Sequence Ontology), or has a parent other than the given
87    * accession id. Transcript features are only retrieved in order to identify
88    * the transcript sequence range, and are redundant information on the
89    * transcript sequence itself.
90    */
91   @Override
92   protected boolean retainFeature(SequenceFeature sf, String accessionId)
93   {
94     if (isTranscript(sf.getType()))
95     {
96       return false;
97     }
98     return featureMayBelong(sf, accessionId);
99   }
100
101   /**
102    * Answers a list of sequence features (if any) whose type is 'transcript' (or
103    * a subtype of transcript in the Sequence Ontology), and whose ID is the
104    * accession we are retrieving.
105    * <p>
106    * Note we also include features of type "NMD_transcript_variant", although
107    * not strictly 'transcript' in the SO, as they used in Ensembl as if they
108    * were.
109    */
110   @Override
111   protected List<SequenceFeature> getIdentifyingFeatures(SequenceI seq,
112           String accId)
113   {
114     List<SequenceFeature> result = new ArrayList<>();
115     List<SequenceFeature> sfs = seq.getFeatures().getFeaturesByOntology(
116             SequenceOntologyI.TRANSCRIPT,
117             SequenceOntologyI.NMD_TRANSCRIPT_VARIANT);
118     for (SequenceFeature sf : sfs)
119     {
120       // NB features as gff use 'ID'; rest services return as 'id'
121       String id = (String) sf.getValue("ID");
122       if (("transcript:" + accId).equals(id))
123       {
124         result.add(sf);
125       }
126     }
127     return result;
128   }
129
130 }