1 // ***************************************************************************
2 // bamtools_filter_engine.cpp (c) 2010 Derek Barnett, Erik Garrison
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 18 September 2010
7 // ---------------------------------------------------------------------------
9 // ***************************************************************************
12 #include "bamtools_filter_engine.h"
14 using namespace BamTools;
16 // ---------------------------------------------------------
17 // FilterValue implementation
19 // checks a string query against filter (value, compare type)
20 bool PropertyFilterValue::check(const string& query) const {
22 // ensure filter value & query are same type
23 if ( !Value.is_type<string>() ) {
24 cerr << "Cannot compare different types!" << endl;
28 // localize string version of our filter value
29 const string& valueString = Value.get<string>();
31 // string matching based on our filter type
33 case ( PropertyFilterValue::CONTAINS) : return ( query.find(valueString) != string::npos );
34 case ( PropertyFilterValue::ENDS_WITH) : return ( query.find(valueString) == (query.length() - valueString.length()) );
35 case ( PropertyFilterValue::EXACT) : return ( query == valueString );
36 case ( PropertyFilterValue::GREATER_THAN) : return ( query > valueString );
37 case ( PropertyFilterValue::GREATER_THAN_EQUAL) : return ( query >= valueString );
38 case ( PropertyFilterValue::LESS_THAN) : return ( query < valueString );
39 case ( PropertyFilterValue::LESS_THAN_EQUAL) : return ( query <= valueString );
40 case ( PropertyFilterValue::NOT) : return ( query != valueString );
41 case ( PropertyFilterValue::STARTS_WITH) : return ( query.find(valueString) == 0 );
42 default : BAMTOOLS_ASSERT_UNREACHABLE;
47 // ---------------------------------------------------------
48 // FilterEngine implementation
50 // static FilterEngine data members
51 FilterMap FilterEngine::m_filters;
52 vector<Property> FilterEngine::m_properties;
54 // creates a new filter set, returns true if created, false if error or already exists
55 bool FilterEngine::addFilter(const string& filterName) {
56 return (m_filters.insert(make_pair(filterName, PropertyFilter()))).second;
59 // return list of current filter names
60 const vector<string> FilterEngine::filterNames(void) {
62 names.reserve(m_filters.size());
63 FilterMap::const_iterator mapIter = m_filters.begin();
64 FilterMap::const_iterator mapEnd = m_filters.end();
65 for ( ; mapIter != mapEnd; ++mapIter )
66 names.push_back( (*mapIter).first );
70 // add a new known property (& type) to engine
71 bool FilterEngine::addProperty(const string& propertyName) {
72 const vector<string> propertyNames = allPropertyNames();
73 bool found = binary_search( propertyNames.begin(), propertyNames.end(), propertyName );
74 if ( found ) return false;
75 m_properties.push_back( Property(propertyName) );
76 sort( m_properties.begin(), m_properties.end() );
81 // returns list of all properties known by FilterEngine ( any created using addProperty() )
82 const vector<string> FilterEngine::allPropertyNames(void) {
84 names.reserve(m_properties.size());
85 vector<Property>::const_iterator propIter = m_properties.begin();
86 vector<Property>::const_iterator propEnd = m_properties.end();
87 for ( ; propIter != propEnd; ++propIter )
88 names.push_back( (*propIter).Name );
92 // returns list of property names that are 'enabled' ( only those touched by setProperty() )
93 const vector<string> FilterEngine::enabledPropertyNames(void) {
95 names.reserve(m_properties.size());
96 vector<Property>::const_iterator propIter = m_properties.begin();
97 vector<Property>::const_iterator propEnd = m_properties.end();
98 for ( ; propIter != propEnd; ++propIter )
99 if ( (*propIter).IsEnabled ) names.push_back( (*propIter).Name );