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