JAL-2446 various get and find methods added with test coverage
[jalview.git] / src / jalview / datamodel / features / SequenceFeatures.java
1 package jalview.datamodel.features;
2
3 import jalview.datamodel.SequenceFeature;
4
5 import java.util.ArrayList;
6 import java.util.Collections;
7 import java.util.HashMap;
8 import java.util.List;
9 import java.util.Map;
10
11 /**
12  * A class that stores sequence features in a way that supports efficient
13  * querying by type and location (overlap). Intended for (but not limited to)
14  * storage of features for one sequence.
15  * 
16  * @author gmcarstairs
17  *
18  */
19 public class SequenceFeatures
20 {
21
22   /*
23    * map from feature type to structured store of features for that type
24    * null types are permitted (but not a good idea!)
25    */
26   private Map<String, FeatureStore> featureStore;
27
28   /**
29    * Constructor
30    */
31   public SequenceFeatures()
32   {
33     featureStore = new HashMap<String, FeatureStore>();
34   }
35
36   /**
37    * Add one sequence feature to the store
38    * 
39    * @param sf
40    */
41   public void add(SequenceFeature sf)
42   {
43     String type = sf.getType();
44
45     if (featureStore.get(type) == null)
46     {
47       featureStore.put(type, new FeatureStore());
48     }
49     featureStore.get(type).addFeature(sf);
50   }
51
52   /**
53    * Returns a (possibly empty) list of features of the given type which overlap
54    * the (inclusive) sequence position range
55    * 
56    * @param type
57    * @param from
58    * @param to
59    * @return
60    */
61   public List<SequenceFeature> findFeatures(String type, int from,
62           int to)
63   {
64     FeatureStore features = featureStore.get(type);
65     if (features == null)
66     {
67       return Collections.emptyList();
68     }
69     return features.findOverlappingFeatures(from, to);
70   }
71
72   /**
73    * Answers a list of all features stored (including non-positional), in no
74    * particular guaranteed order
75    * 
76    * @return
77    */
78   public List<SequenceFeature> getFeatures()
79   {
80     List<SequenceFeature> result = new ArrayList<SequenceFeature>();
81     for (FeatureStore featureSet : featureStore.values())
82     {
83       result.addAll(featureSet.getFeatures());
84     }
85     return result;
86   }
87
88   /**
89    * Answers a list of all non-positional features stored, in no particular
90    * guaranteed order
91    * 
92    * @return
93    */
94   public List<SequenceFeature> getNonPositionalFeatures()
95   {
96     List<SequenceFeature> result = new ArrayList<SequenceFeature>();
97     for (FeatureStore featureSet : featureStore.values())
98     {
99       result.addAll(featureSet.getNonPositionalFeatures());
100     }
101     return result;
102   }
103
104   /**
105    * Answers a list of all contact features stored, in no particular guaranteed
106    * order
107    * 
108    * @return
109    */
110   public List<SequenceFeature> getContactFeatures()
111   {
112     List<SequenceFeature> result = new ArrayList<SequenceFeature>();
113     for (FeatureStore featureSet : featureStore.values())
114     {
115       result.addAll(featureSet.getContactFeatures());
116     }
117     return result;
118   }
119
120   /**
121    * Answers a list of all features of the given type (including
122    * non-positional), in no particular guaranteed order
123    * 
124    * @return
125    */
126   public List<SequenceFeature> getFeatures(String type)
127   {
128     List<SequenceFeature> result = new ArrayList<SequenceFeature>();
129     FeatureStore featureSet = featureStore.get(type);
130     if (featureSet != null)
131     {
132       result.addAll(featureSet.getFeatures());
133     }
134     return result;
135   }
136
137   /**
138    * Answers a list of all contact features of the given type, in no particular
139    * guaranteed order
140    * 
141    * @return
142    */
143   public List<SequenceFeature> getContactFeatures(String type)
144   {
145     List<SequenceFeature> result = new ArrayList<SequenceFeature>();
146     FeatureStore featureSet = featureStore.get(type);
147     if (featureSet != null)
148     {
149       result.addAll(featureSet.getContactFeatures());
150     }
151     return result;
152   }
153
154   /**
155    * Answers a list of all non-positional features of the given type, in no
156    * particular guaranteed order
157    * 
158    * @return
159    */
160   public List<SequenceFeature> getNonPositionalFeatures(String type)
161   {
162     List<SequenceFeature> result = new ArrayList<SequenceFeature>();
163     FeatureStore featureSet = featureStore.get(type);
164     if (featureSet != null)
165     {
166       result.addAll(featureSet.getNonPositionalFeatures());
167     }
168     return result;
169   }
170 }