JAL-1705 tests added, minor bugfix and refactoring
[jalview.git] / src / jalview / ext / ensembl / EnsemblCds.java
1 package jalview.ext.ensembl;
2
3 import jalview.datamodel.SequenceFeature;
4 import jalview.datamodel.SequenceI;
5 import jalview.io.gff.SequenceOntology;
6
7 import java.util.List;
8
9 public class EnsemblCds extends EnsemblSeqProxy
10 {
11   /*
12    * fetch cds features on genomic sequence (to identify the CDS regions)
13    * and exon and variation features (to retain for display)
14    */
15   private static final EnsemblFeatureType[] FEATURES_TO_FETCH = {
16       EnsemblFeatureType.cds, EnsemblFeatureType.exon,
17       EnsemblFeatureType.variation };
18
19   /**
20    * Constructor
21    */
22   public EnsemblCds()
23   {
24     super();
25   }
26
27   @Override
28   public String getDbName()
29   {
30     return "ENSEMBL (CDS)";
31   }
32
33   @Override
34   protected EnsemblSeqType getSourceEnsemblType()
35   {
36     return EnsemblSeqType.CDS;
37   }
38
39   @Override
40   protected EnsemblFeatureType[] getFeaturesToFetch()
41   {
42     return FEATURES_TO_FETCH;
43   }
44
45   /**
46    * Answers true unless the feature type is 'CDS' (or a sub-type of CDS in the
47    * Sequence Ontology). CDS features are only retrieved in order to identify
48    * the cds sequence range, and are redundant information on the cds sequence
49    * itself.
50    */
51   @Override
52   protected boolean retainFeature(SequenceFeature sf, String accessionId)
53   {
54     if (SequenceOntology.getInstance().isA(sf.getType(),
55             SequenceOntology.CDS))
56     {
57       return false;
58     }
59     return featureMayBelong(sf, accessionId);
60   }
61
62   /**
63    * Answers true if the sequence feature type is 'CDS' (or a subtype of CDS in
64    * the Sequence Ontology), and the Parent of the feature is the transcript we
65    * are retrieving
66    */
67   @Override
68   protected boolean identifiesSequence(SequenceFeature sf, String accId)
69   {
70     if (SequenceOntology.getInstance().isA(sf.getType(),
71             SequenceOntology.CDS))
72     {
73       String parentFeature = (String) sf.getValue(PARENT);
74       if (("transcript:" + accId).equals(parentFeature))
75       {
76         return true;
77       }
78     }
79     return false;
80   }
81
82   /**
83    * Overrides this method to trivially return a range which is the whole of the
84    * nucleotide sequence. This is both faster than scanning for CDS features,
85    * and also means we don't need to keep CDS features on CDS sequence (where
86    * they are redundant information).
87    */
88   @Override
89   protected int getCdsRanges(SequenceI dnaSeq, List<int[]> ranges)
90   {
91     int len = dnaSeq.getLength();
92     ranges.add(new int[] { 1, len });
93     return len;
94   }
95
96 }