]> git.donarmstrong.com Git - bamtools.git/blob - src/utils/bamtools_filter_engine.cpp
Merge branch 'master' of http://github.com/pezmaster31/bamtools
[bamtools.git] / src / utils / bamtools_filter_engine.cpp
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 // ---------------------------------------------------------------------------
8 // 
9 // ***************************************************************************
10
11 #include <iostream>
12 #include "bamtools_filter_engine.h"
13 using namespace std;
14 using namespace BamTools;
15
16 // ---------------------------------------------------------
17 // FilterValue implementation
18
19 // checks a string query against filter (value, compare type)
20 bool PropertyFilterValue::check(const string& query) const {
21   
22     // ensure filter value & query are same type
23     if ( !Value.is_type<string>() ) {
24         cerr << "Cannot compare different types!" << endl;
25         return false;
26     }
27   
28     // localize string version of our filter value
29     const string& valueString = Value.get<string>();
30     
31     // string matching based on our filter type
32     switch ( 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;
43     }
44     return false;
45 }
46
47 // ---------------------------------------------------------
48 // FilterEngine implementation
49
50 // static FilterEngine data members
51 FilterMap FilterEngine::m_filters;
52 vector<Property> FilterEngine::m_properties;
53
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;
57 }
58
59 // return list of current filter names
60 const vector<string> FilterEngine::filterNames(void) {
61     vector<string> names;
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 ); 
67     return names;
68 }
69
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() );
77     return true;
78 }
79
80
81 // returns list of all properties known by FilterEngine  ( any created using addProperty() )
82 const vector<string> FilterEngine::allPropertyNames(void) {
83     vector<string> names;
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 );    
89     return names;
90 }
91
92 // returns list of property names that are 'enabled' ( only those touched by setProperty() )
93 const vector<string> FilterEngine::enabledPropertyNames(void) {
94     vector<string> names;
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 );    
100     return names;
101 }