]> git.donarmstrong.com Git - bamtools.git/blob - src/utils/bamtools_filter_properties.h
c810ba24c5a20b47570a73f13890ca79801ccd68
[bamtools.git] / src / utils / bamtools_filter_properties.h
1 // ***************************************************************************
2 // bamtools_filter_properties.h (c) 2010 Derek Barnett, Erik Garrison
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 19 November 2010
6 // ---------------------------------------------------------------------------
7 // Provides support data structures & methods for FilterEngine
8 //
9 // The FilterEngine consists, most importantly, of :
10 //
11 //     a list of possible properties (each tagged whether it has been 'enabled' as a filter)
12 //     a map of filterName => propertySet
13 //     queue for compound rule expression (i.e. "(filter1 AND filter2) OR !filter3" )
14 //     
15 // Each propertySet is a list of properties enabled for this particular filter object
16 //
17 //     Implemented as a map of propertyNames to propertyFilterValue
18 //     ( "property1" => pfv1 
19 //       "property2" => pfv2 
20 //       "property4" => pfv4
21 //       etc. )  
22 //
23 //     Any properties that are 'possible', via FilterEngine::addProperty(), but not enabled 
24 //     via FilterEngine::setProperty() (in our example, say "property3"), evaluate to true 
25 //     for any query.  Meaning that if a property is not set on this filter, we don't care 
26 //     about it here, so it passes though OK.
27 //
28 // A propertyFilterValue contains a value and comparison type
29 //
30 //    ( pfv1: Value = 50,    Type = GREATER_THAN_EQUAL
31 //      pfv2: Value = "foo", Type = STARTS_WITH
32 //      pfv4: Value = "bar", Type = CONTAINS
33 //      etc. )  
34 //
35 //    This allows for more complex queries (than simple isEqual?) against a variety of data types.
36 //
37 // ***************************************************************************
38
39 #ifndef BAMTOOLS_FILTER_PROPERTIES_H
40 #define BAMTOOLS_FILTER_PROPERTIES_H
41
42 #include <utils/bamtools_utilities.h>
43 #include <utils/bamtools_variant.h>
44 #include <utils/utils_global.h>
45 #include <iostream>
46 #include <map>
47 #include <string>
48
49 namespace BamTools {
50
51 // ----------------------------------------------------------
52 // PropertyFilterValue
53   
54 struct UTILS_EXPORT PropertyFilterValue {
55   
56     // define valid ValueCompareTypes
57     enum ValueCompareType { CONTAINS = 0
58                           , ENDS_WITH
59                           , EXACT
60                           , GREATER_THAN
61                           , GREATER_THAN_EQUAL
62                           , LESS_THAN
63                           , LESS_THAN_EQUAL
64                           , NOT
65                           , STARTS_WITH
66                           };
67                    
68     // ctor
69     PropertyFilterValue(const Variant& value = Variant(),
70                         const ValueCompareType& type = PropertyFilterValue::EXACT)
71         : Value(value)
72         , Type(type)
73     { }
74           
75     // filter check methods      
76     template<typename T>
77     bool check(const T& query) const;
78     bool check(const std::string& query) const;
79              
80     // data members
81     Variant Value;
82     ValueCompareType Type;
83 };
84
85 // checks a query against a filter (value, compare type)
86 template<typename T>
87 bool PropertyFilterValue::check(const T& query) const {
88   
89     // ensure filter value & query are same type
90     if ( !Value.is_type<T>() ) { 
91         std::cerr << "Cannot compare different types!" << std::endl;
92         return false;
93     }
94     
95     // string matching
96     if ( Value.is_type<std::string>() ) {
97         std::cerr << "Cannot compare different types - query is a string!" << std::endl;
98         return false;
99     } 
100     
101     // numeric matching based on our filter type
102     switch ( 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;
110     }
111     return false;
112 }
113
114 // checks a string query against filter (value, compare type)
115 inline
116 bool PropertyFilterValue::check(const std::string& query) const {
117   
118     // ensure filter value & query are same type
119     if ( !Value.is_type<std::string>() ) {
120         std::cerr << "Cannot compare different types!" << std::endl;
121         return false;
122     }
123   
124     // localize string version of our filter value
125     const std::string& valueString = Value.get<std::string>();
126     
127     // string matching based on our filter type
128     switch ( 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;
139     }
140     return false;
141 }
142
143 inline
144 const std::string toString(const PropertyFilterValue::ValueCompareType& type) {
145   
146     switch ( 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;
157     }
158     return std::string();
159 }
160
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;
164
165 // ----------------------------------------------------------
166 // PropertyFilter
167
168 struct UTILS_EXPORT PropertyFilter {
169     // data members
170     PropertyMap Properties;
171 };
172
173 // filter name => properties  
174 // ('filter1' => properties1, 'filter2' => properties2, etc...)
175 typedef std::map<std::string, PropertyFilter> FilterMap;
176   
177 // ----------------------------------------------------------
178 // Property
179   
180 // used to store properties known to engine & keep track of enabled state
181 struct UTILS_EXPORT Property {
182     std::string Name;
183     bool IsEnabled;
184     Property(const std::string& name, bool isEnabled = false) 
185         : Name(name)
186         , IsEnabled(isEnabled) 
187     { }
188 };
189
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; }
192
193 } // namespace BamTools
194
195 #endif // BAMTOOLS_FILTER_PROPERTIES_H