1 // ***************************************************************************
2 // bamtools_filter_properties.h (c) 2010 Derek Barnett, Erik Garrison
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 30 August 2010
7 // ---------------------------------------------------------------------------
8 // Provides support data structures & methods for FilterEngine
10 // The FilterEngine consists, most importantly, of :
12 // a list of possible properties (each tagged whether it has been 'enabled' as a filter)
13 // a map of filterName => propertySet
14 // queue for compound rule expression (i.e. "(filter1 AND filter2) OR !filter3" )
16 // Each propertySet is a list of properties enabled for this particular filter object
18 // Implemented as a map of propertyNames to propertyFilterValue
19 // ( "property1" => pfv1
20 // "property2" => pfv2
21 // "property4" => pfv4
24 // Any properties that are 'possible', via FilterEngine::addProperty(), but not enabled
25 // via FilterEngine::setProperty() (in our example, say "property3"), evaluate to true
26 // for any query. Meaning that if a property is not set on this filter, we don't care
27 // about it here, so it passes though OK.
29 // A propertyFilterValue contains a value and comparison type
31 // ( pfv1: Value = 50, Type = GREATER_THAN_EQUAL
32 // pfv2: Value = "foo", Type = STARTS_WITH
33 // pfv4: Value = "bar", Type = CONTAINS
36 // This allows for more complex queries (than simple isEqual?) against a variety of data types.
38 // ***************************************************************************
40 #ifndef BAMTOOLS_FILTER_PROPERTIES_H
41 #define BAMTOOLS_FILTER_PROPERTIES_H
46 #include "bamtools_utilities.h"
47 #include "bamtools_variant.h"
51 // ----------------------------------------------------------
52 // PropertyFilterValue
54 struct PropertyFilterValue {
56 // define valid ValueCompareTypes
57 enum ValueCompareType { CONTAINS = 0
69 PropertyFilterValue(const Variant& value = Variant(),
70 const ValueCompareType& type = PropertyFilterValue::EXACT)
75 // filter check methods
77 bool check(const T& query) const;
78 bool check(const std::string& query) const;
82 ValueCompareType Type;
85 // checks a query against a filter (value, compare type)
87 bool PropertyFilterValue::check(const T& query) const {
89 // ensure filter value & query are same type
90 if ( !Value.is_type<T>() ) {
91 std::cerr << "Cannot compare different types!" << std::endl;
96 if ( Value.is_type<std::string>() ) {
97 std::cerr << "Cannot compare different types - query is a string!" << std::endl;
101 // numeric matching based on our filter type
103 case ( PropertyFilterValue::EXACT) : return ( query == Value.get<T>() );
104 case ( PropertyFilterValue::GREATER_THAN) : return ( query > Value.get<T>() );
105 case ( PropertyFilterValue::GREATER_THAN_EQUAL) : return ( query >= Value.get<T>() );
106 case ( PropertyFilterValue::LESS_THAN) : return ( query < Value.get<T>() );
107 case ( PropertyFilterValue::LESS_THAN_EQUAL) : return ( query <= Value.get<T>() );
108 case ( PropertyFilterValue::NOT) : return ( query != Value.get<T>() );
109 default : BAMTOOLS_ASSERT_UNREACHABLE;
114 // checks a string query against filter (value, compare type)
116 bool PropertyFilterValue::check(const std::string& query) const {
118 // ensure filter value & query are same type
119 if ( !Value.is_type<std::string>() ) {
120 std::cerr << "Cannot compare different types!" << std::endl;
124 // localize string version of our filter value
125 const std::string& valueString = Value.get<std::string>();
127 // string matching based on our filter type
129 case ( PropertyFilterValue::CONTAINS) : return ( query.find(valueString) != std::string::npos );
130 case ( PropertyFilterValue::ENDS_WITH) : return ( query.find(valueString) == (query.length() - valueString.length()) );
131 case ( PropertyFilterValue::EXACT) : return ( query == valueString );
132 case ( PropertyFilterValue::GREATER_THAN) : return ( query > valueString );
133 case ( PropertyFilterValue::GREATER_THAN_EQUAL) : return ( query >= valueString );
134 case ( PropertyFilterValue::LESS_THAN) : return ( query < valueString );
135 case ( PropertyFilterValue::LESS_THAN_EQUAL) : return ( query <= valueString );
136 case ( PropertyFilterValue::NOT) : return ( query != valueString );
137 case ( PropertyFilterValue::STARTS_WITH) : return ( query.find(valueString) == 0 );
138 default : BAMTOOLS_ASSERT_UNREACHABLE;
144 const std::string toString(const PropertyFilterValue::ValueCompareType& type) {
147 case ( PropertyFilterValue::CONTAINS ) : return std::string( "CONTAINS");
148 case ( PropertyFilterValue::ENDS_WITH ) : return std::string( "ENDS_WITH");
149 case ( PropertyFilterValue::EXACT ) : return std::string( "EXACT");
150 case ( PropertyFilterValue::GREATER_THAN ) : return std::string( "GREATER_THAN");
151 case ( PropertyFilterValue::GREATER_THAN_EQUAL ) : return std::string( "GREATER_THAN_EQUAL");
152 case ( PropertyFilterValue::LESS_THAN ) : return std::string( "LESS_THAN");
153 case ( PropertyFilterValue::LESS_THAN_EQUAL ) : return std::string( "LESS_THAN_EQUAL");
154 case ( PropertyFilterValue::NOT ) : return std::string( "NOT");
155 case ( PropertyFilterValue::STARTS_WITH ) : return std::string( "STARTS_WITH");
156 default : BAMTOOLS_ASSERT_UNREACHABLE;
158 return std::string();
161 // property name => property filter value
162 // ('name' => ('SSR', STARTS_WITH), 'mapQuality' => (50, GREATER_THAN_EQUAL), etc...)
163 typedef std::map<std::string, PropertyFilterValue> PropertyMap;
165 // ----------------------------------------------------------
168 struct PropertyFilter {
170 PropertyMap Properties;
173 // filter name => properties
174 // ('filter1' => properties1, 'filter2' => properties2, etc...)
175 typedef std::map<std::string, PropertyFilter> FilterMap;
177 // ----------------------------------------------------------
180 // used to store properties known to engine & keep track of enabled state
184 Property(const std::string& name, bool isEnabled = false)
186 , IsEnabled(isEnabled)
190 inline bool operator< (const Property& lhs, const Property& rhs) { return lhs.Name < rhs.Name; }
191 inline bool operator== (const Property& lhs, const Property& rhs) { return lhs.Name == rhs.Name; }
193 } // namespace BamTools
195 #endif // BAMTOOLS_FILTER_PROPERTIES_H