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: 30 August 2010
7 // ---------------------------------------------------------------------------
9 // ***************************************************************************
12 #include "bamtools_filter_engine.h"
15 using namespace BamTools;
17 // ---------------------------------------------------------
18 // FilterValue implementation
20 // checks a string query against filter (value, compare type)
21 bool PropertyFilterValue::check(const string& query) const {
23 // ensure filter value & query are same type
24 if ( !Value.is_type<string>() ) {
25 cerr << "Cannot compare different types!" << endl;
29 // localize string version of our filter value
30 const string& valueString = Value.get<string>();
32 // string matching based on our filter type
34 case ( PropertyFilterValue::CONTAINS) : return ( query.find(valueString) != string::npos );
35 case ( PropertyFilterValue::ENDS_WITH) : return ( query.find(valueString) == (query.length() - valueString.length()) );
36 case ( PropertyFilterValue::EXACT) : return ( query == valueString );
37 case ( PropertyFilterValue::GREATER_THAN) : return ( query > valueString );
38 case ( PropertyFilterValue::GREATER_THAN_EQUAL) : return ( query >= valueString );
39 case ( PropertyFilterValue::LESS_THAN) : return ( query < valueString );
40 case ( PropertyFilterValue::LESS_THAN_EQUAL) : return ( query <= valueString );
41 case ( PropertyFilterValue::NOT) : return ( query != valueString );
42 case ( PropertyFilterValue::STARTS_WITH) : return ( query.find(valueString) == 0 );
43 default : BAMTOOLS_ASSERT_UNREACHABLE;
48 // ---------------------------------------------------------
49 // FilterEngine implementation
51 // static FilterEngine data members
52 FilterMap FilterEngine::m_filters;
53 vector<Property> FilterEngine::m_properties;
55 // creates a new filter set, returns true if created, false if error or already exists
56 bool FilterEngine::addFilter(const string& filterName) {
57 return (m_filters.insert(make_pair(filterName, PropertyFilter()))).second;
60 // return list of current filter names
61 const vector<string> FilterEngine::filterNames(void) {
63 names.reserve(m_filters.size());
64 FilterMap::const_iterator mapIter = m_filters.begin();
65 FilterMap::const_iterator mapEnd = m_filters.end();
66 for ( ; mapIter != mapEnd; ++mapIter )
67 names.push_back( (*mapIter).first );
71 // add a new known property (& type) to engine
72 bool FilterEngine::addProperty(const string& propertyName) {
73 const vector<string> propertyNames = allPropertyNames();
74 bool found = binary_search( propertyNames.begin(), propertyNames.end(), propertyName );
75 if ( found ) return false;
76 m_properties.push_back( Property(propertyName) );
77 sort( m_properties.begin(), m_properties.end() );
82 // returns list of all properties known by FilterEngine ( any created using addProperty() )
83 const vector<string> FilterEngine::allPropertyNames(void) {
85 names.reserve(m_properties.size());
86 vector<Property>::const_iterator propIter = m_properties.begin();
87 vector<Property>::const_iterator propEnd = m_properties.end();
88 for ( ; propIter != propEnd; ++propIter )
89 names.push_back( (*propIter).Name );
93 // returns list of property names that are 'enabled' ( only those touched by setProperty() )
94 const vector<string> FilterEngine::enabledPropertyNames(void) {
96 names.reserve(m_properties.size());
97 vector<Property>::const_iterator propIter = m_properties.begin();
98 vector<Property>::const_iterator propEnd = m_properties.end();
99 for ( ; propIter != propEnd; ++propIter )
100 if ( (*propIter).IsEnabled ) names.push_back( (*propIter).Name );