| 1 | #include "json/json.h" |
| 2 | #include "json/json-forwards.h" |
| 3 | |
| 4 | #include "SeqLib/MiniRules2.h" |
| 5 | |
| 6 | #include <cstdio> |
| 7 | #include <fstream> |
| 8 | #include <iostream> |
| 9 | #include <sstream> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | using namespace std; |
| 13 | |
| 14 | const std::vector<std::string> m_propertyNames = {"reference", "mapQuality", "isReverseStrand"}; |
| 15 | |
| 16 | bool ParseFilterObject(const string& filterName, const Json::Value& filterObject) { |
| 17 | |
| 18 | // filter object parsing variables |
| 19 | Json::Value null(Json::nullValue); |
| 20 | Json::Value propertyValue; |
| 21 | |
| 22 | // store results |
| 23 | map<string, string> propertyTokens; |
| 24 | |
| 25 | // iterate over known properties |
| 26 | vector<string>::const_iterator propertyNameIter = m_propertyNames.begin(); |
| 27 | vector<string>::const_iterator propertyNameEnd = m_propertyNames.end(); |
| 28 | for ( ; propertyNameIter != propertyNameEnd; ++propertyNameIter ) { |
| 29 | const string& propertyName = (*propertyNameIter); |
| 30 | |
| 31 | // if property defined in filter, add to token list |
| 32 | propertyValue = filterObject.get(propertyName, null); |
| 33 | if ( propertyValue != null ) { |
| 34 | std::cerr << "[" << propertyName << "] -- " << propertyValue.asString() << std::endl; |
| 35 | propertyTokens.insert( make_pair(propertyName, propertyValue.asString()) ); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // add this filter to engin |
| 40 | //m_filterEngine.addFilter(filterName); |
| 41 | |
| 42 | // add token list to this filter |
| 43 | //return AddPropertyTokensToFilter(filterName, propertyTokens); |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | const string GetScriptContents(string script) { |
| 48 | |
| 49 | // open script for reading |
| 50 | FILE* inFile = fopen(script.c_str(), "rb"); |
| 51 | if ( !inFile ) { |
| 52 | cerr << "bamtools filter ERROR: could not open script: " |
| 53 | << script << " for reading" << endl; |
| 54 | return string(); |
| 55 | } |
| 56 | |
| 57 | // read in entire script contents |
| 58 | char buffer[1024]; |
| 59 | ostringstream docStream(""); |
| 60 | while ( true ) { |
| 61 | |
| 62 | // peek ahead, make sure there is data available |
| 63 | char ch = fgetc(inFile); |
| 64 | ungetc(ch, inFile); |
| 65 | if( feof(inFile) ) |
| 66 | break; |
| 67 | |
| 68 | // read next block of data |
| 69 | if ( fgets(buffer, 1024, inFile) == 0 ) { |
| 70 | cerr << "bamtools filter ERROR: could not read script contents" << endl; |
| 71 | return string(); |
| 72 | } |
| 73 | |
| 74 | docStream << buffer; |
| 75 | } |
| 76 | |
| 77 | // close script file |
| 78 | fclose(inFile); |
| 79 | |
| 80 | // import buffer contents to document, return |
| 81 | return docStream.str(); |
| 82 | } |
| 83 | |
| 84 | int main(int argc, char** argv) { |
| 85 | |
| 86 | std::string script = "/xchip/gistic/Jeremiah/GIT/SeqLib/test.json"; |
| 87 | const string document = GetScriptContents(script); |
| 88 | |
| 89 | // set up JsonCPP reader and attempt to parse script |
| 90 | Json::Value root; |
| 91 | Json::Reader reader; |
| 92 | if ( !reader.parse(document, root) ) { |
| 93 | // use built-in error reporting mechanism to alert user what was wrong with the script |
| 94 | cerr << "bamtools filter ERROR: failed to parse script - see error message(s) below" << endl; |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | // see if root object contains multiple filters |
| 99 | const Json::Value filters = root["filters"]; |
| 100 | |
| 101 | cerr << " root size " << root.size() << std::endl; |
| 102 | cerr << " filters size " << filters.size() << std::endl; |
| 103 | |
| 104 | // iterate over any filters found |
| 105 | int filterIndex = 0; |
| 106 | Json::Value::const_iterator filtersIter = filters.begin(); |
| 107 | Json::Value::const_iterator filtersEnd = filters.end(); |
| 108 | for ( ; filtersIter != filtersEnd; ++filtersIter, ++filterIndex ) { |
| 109 | Json::Value filter = (*filtersIter); |
| 110 | |
| 111 | // convert filter index to string |
| 112 | string filterName; |
| 113 | |
| 114 | // if id tag supplied |
| 115 | const Json::Value id = filter["id"]; |
| 116 | if ( !id.isNull() ) |
| 117 | filterName = id.asString(); |
| 118 | |
| 119 | |
| 120 | // use array index |
| 121 | else { |
| 122 | stringstream convert; |
| 123 | convert << filterIndex; |
| 124 | filterName = convert.str(); |
| 125 | } |
| 126 | |
| 127 | cerr << "filter " << filterName << std::endl; |
| 128 | // create & parse filter |
| 129 | bool success = true; |
| 130 | success &= ParseFilterObject(filterName, filter); |
| 131 | } |
| 132 | |
| 133 | /// make mini rules |
| 134 | cerr << " ... maing mini rules " << endl; |
| 135 | |
| 136 | SeqLib::MiniRulesCollection mrc(script); |
| 137 | |
| 138 | |
| 139 | } |