// encode genotypes matrixusing a set of codes // // genotypes is markers x individuals // old_values is markers x n_new_values // new_values is vector of new values // // [[Rcpp::export(".encode_geno")]] StringMatrix encode_geno(const StringMatrix& g, const StringMatrix& old_values, const StringVector& new_values) { int n_mar = g.rows(); int n_ind = g.cols(); int n_code = old_values.cols(); StringMatrix result(n_mar,n_ind); if(old_values.rows() != n_mar) throw std::invalid_argument("nrow(g) != nrow(old_values)"); if(new_values.size() != n_code) throw std::invalid_argument("ncol(old_values) != length(new_values)"); for(int ind=0; ind<n_ind; ind++) { for(int mar=0; mar<n_mar; mar++) { if(StringVector::is_na(g(mar,ind))) result(mar,ind)=NA_STRING; else { bool unassigned=true; for(int codei=0; codei<n_code; codei++) { if(g(mar,ind)==old_values(mar,codei)) { unassigned=false; result(mar,ind) = new_values[codei]; } } if(unassigned) result(mar,ind) = NA_STRING; } } } return result; }
void loadPattern(int pattIndex) { PatternLoader p; StringMatrix pattData = p.readPattern(pattIndex); for(int i = 0; i < pattData.size(); i++) { for(int j = 0; j < pattData[i].size(); j++) { this->tracks[i].setNote(j, stoi(pattData[i][j])); } } }
void loadKit(int kitIndex) { PatternLoader p; StringMatrix kitData = p.readKit(kitIndex); for(int i = 0; i < kitData.size(); i++) { for(int j = 0; j < kitData[i].size(); j++) { this->trackSynths[i].setParameter("param" + to_string(j), stof(kitData[i][j])/238.0); } } }
// Helper function to write 'matrix' of strings to a CSV file. static void writeCSV(const StringMatrix& string_matrix, const std::string& filename) { CHECK_GE(string_matrix.size(), 1) << "Provided matrix of strings had no entries."; std::ofstream out_file_stream; out_file_stream.open(filename.c_str()); // Iterate over the rows of the string matrix and write comma-separated fields. for (StringMatrix::const_iterator it = string_matrix.begin(); it != string_matrix.end(); ++it) { CHECK_GE(it->size(), 1) << "String matrix row has no entries."; out_file_stream << it->at(0u); for (size_t i = 1u; i < it->size(); ++i) { out_file_stream << "," << it->at(i); } out_file_stream << std::endl; } out_file_stream.close(); }
StringMatrix Pattern::read(string fileName, int xlen, int ylen) { StringMatrix res; string val; ifstream file(fileName); for (int i = 0; i < ylen; ++i) { res.push_back(vector<string>()); for (int j = 0; j < xlen; ++j) { file >> val; res[i].push_back(val); } } file.close(); return res; }
bool MatlabEngine::PutCells( const string& inExp, const StringMatrix& inValue ) { int sizeDim2 = inValue.empty() ? 0 : inValue[ 0 ].size(); mxArray* mat = mxCreateCellMatrix( inValue.size(), sizeDim2 ); int indices[] = { 0, 0 }; for( size_t i = 0; i < inValue.size(); ++i ) { indices[ 0 ] = i; for( size_t j = 0; j < inValue[ i ].size(); ++j ) { indices[ 1 ] = j; int idx = mxCalcSingleSubscript( mat, 2, indices ); mxDestroyArray( mxGetCell( mat, idx ) ); mxArray* val = mxCreateString( inValue[ i ][ j ].c_str() ); mxSetCell( mat, idx, val ); } } bool success = PutMxArray( inExp, mat ); mxDestroyArray( mat ); return success; }
string Pattern::packData(StringMatrix data) { string s = ""; for(int i = 0; i < data.size(); ++i) { for(int j = 0; j < data[i].size(); ++j) { s += data[i][j] + " "; } s += "\n"; } return s; }
bool MatlabEngine::PutCells( const string& inExp, const StringMatrix& inValue ) { mwSize sizeDim2 = static_cast<mwSize>( inValue.empty() ? 0 : inValue[ 0 ].size() ); mxArray* mat = mxCreateCellMatrix_( static_cast<mwSize>( inValue.size() ), sizeDim2 ); mwIndex indices[] = { 0, 0 }; for( mwIndex i = 0; i < static_cast<mwIndex>( inValue.size() ); ++i ) { indices[ 0 ] = i; for( mwIndex j = 0; j < static_cast<mwIndex>( inValue[ i ].size() ); ++j ) { indices[ 1 ] = j; int idx = mxCalcSingleSubscript_( mat, 2, indices ); mxDestroyArray_( mxGetCell_( mat, idx ) ); mxArray* val = mxCreateString_( inValue[ i ][ j ].c_str() ); mxSetCell_( mat, idx, val ); } } bool success = PutMxArray( inExp, mat ); mxDestroyArray_( mat ); return success; }
MatlabEngine::StringMatrix MatlabEngine::GetCells( const string& inExp ) { StringMatrix result; mxArray* ans = GetMxArray( inExp ); if( ans ) { int numDims = mxGetNumberOfDimensions( ans ); const int* dims = mxGetDimensions( ans ); if( numDims != 2 ) bcierr << "Can only handle two dimensions" << endl; result.resize( dims[ 0 ], vector<string>( dims[ 1 ] ) ); int indices[] = { 0, 0 }; for( size_t i = 0; i < result.size(); ++i ) { indices[ 0 ] = i; for( size_t j = 0; j < result[ i ].size(); ++j ) { indices[ 1 ] = j; int idx = mxCalcSingleSubscript( ans, 2, indices ); mxArray* cell = mxGetCell( ans, idx ); if( cell ) { const char* s = mxArrayToString( cell ); if( s == NULL ) bcierr << "Could not read string value \"" << inExp << "\"" << endl; else { result[ i ][ j ] = s; mxFree( ( void* )s ); } } } } mxDestroyArray( ans ); } return result; }
MatLabUdmTruthTable::StringVectorManager MatLabUdmTruthTable::getStringVectorManager( std::string matrix ) { static boost::regex scPattern( "(?<!&#x[0-9A-Fa-f]{2});", boost::regex_constants::perl ); static boost::regex scCodePattern( "[Bb];", boost::regex_constants::perl ); static boost::regex cmPattern( ",", boost::regex_constants::perl ); static boost::regex cmCodePattern( "[Cc];", boost::regex_constants::perl ); static boost::regex labeledStringPattern( "\\s*(\\S+)\\s*:\\s*(.*?)\\s*\\Z", boost::regex_constants::perl | boost::regex_constants::mod_s); matrix = matrix.substr( 1, matrix.size() - 2 ); // GET RID OF ENCLOSING {} StringMatrix stringMatrix; bool scMatch = true; boost::match_results< std::string::const_iterator > results; while ( scMatch && ( boost::regex_search( matrix, results, scPattern ) || !(scMatch = false) ) ) { std::string row; if ( scMatch ) { row = results.prefix(); matrix = results.suffix(); } else { row = matrix; } row = boost::regex_replace( row, scCodePattern, ";" ); stringMatrix.push_back( StringVectorSP( new StringVector ) ); StringVector &stringVector = *stringMatrix.back(); bool cmMatch = true; while( cmMatch && ( boost::regex_search( row, results, cmPattern ) || !( cmMatch = false ) ) ) { std::string cell; if ( cmMatch ) { cell = results.prefix(); row = results.suffix(); } else { cell = row; } cell = boost::regex_replace( cell, cmCodePattern, "," ); stringVector.push_back( cell ); } } StringVectorMap stringVectorMap; for( StringMatrix::iterator stmItr = stringMatrix.begin() ; stmItr != stringMatrix.end() ; ++stmItr ) { StringVector &stringVector = **stmItr; std::string &labeledString = stringVector[1]; labeledString = RegexCommon::eliminateContinuations( labeledString ); if ( boost::regex_search( labeledString, results, labeledStringPattern ) ) { std::string key( results[1] ); std::string condition( results[2] ); stringVectorMap.insert( std::make_pair( key, *stmItr ) ); labeledString = condition; } } return StringVectorManager( stringMatrix, stringVectorMap ); }
void MatLabUdmTruthTable::setupTruthTable( void ) { State udmTruthTable = getUdmTruthTable(); udmTruthTable.Name() = udmTruthTable.name() = getUniqueName().c_str(); udmTruthTable.RefId() = getRefId(); udmTruthTable.Decomposition() = "FUNC_STATE"; udmTruthTable.Order() = "-2"; udmTruthTable.EnterAction() = RegexCommon::eliminateContinuations( MatLabEngine::globalEngine().getMxStringValue( getMatLabObject() + ".LabelString" ) ); std::string conditionTable = MatLabEngine::globalEngine().getData( getMatLabObject() + ".ConditionTable" ); StringVectorManager conditionSTM = getStringVectorManager( conditionTable ); std::string actionTable = MatLabEngine::globalEngine().getData( getMatLabObject() + ".ActionTable" ); StringVectorManager actionSTM = getStringVectorManager( actionTable ); std::string functionBody; StringMatrix conditionStringMatrix = conditionSTM.getStringMatrix(); int noConditions = (int) conditionStringMatrix[0]->size() - 2; int noConditionTerms = (int) conditionStringMatrix.size() - 1; static boost::regex crnlPattern( "\\r|\\n", boost::regex_constants::perl ); for( int ix = 0 ; ix < noConditions ; ++ix ) { if ( ix != 0 ) functionBody += "else"; functionBody += "if "; std::string condition; for( int jx = 0 ; jx < noConditionTerms ; ++jx ) { StringVector &stringVector = *conditionStringMatrix[ jx ]; std::string tf = stringVector[ ix + 2 ]; std::string rawCondition = "(" + boost::regex_replace( stringVector[1], crnlPattern, " " ) + ")"; if ( tf.find( '-' ) != std::string::npos ) continue; if ( tf.find( 'F' ) != std::string::npos ) rawCondition = "!" + rawCondition; if ( !condition.empty() ) condition += " && "; condition += rawCondition; } if ( condition.empty() ) condition = "1"; functionBody += condition + "\n"; StringVector actionVector; std::string actions = ( *conditionStringMatrix.back() )[ ix + 2 ]; static boost::regex actionPattern( "[0-9]+|[A-Za-z_][A-Za-z_0-9]*" ); boost::match_results< std::string::const_iterator > results; while ( boost::regex_search( actions, results, actionPattern ) ) { actionVector.push_back( results[0] ); actions = results.suffix(); } static boost::regex newlinePattern( "\n" ); for( StringVector::iterator stvItr = actionVector.begin() ; stvItr != actionVector.end() ; ++stvItr ) { std::string actionSpec = *stvItr; if ( isdigit( actionSpec[0] ) ) { int actionNo = boost::lexical_cast< int >( actionSpec ) - 1; std::string action = (*actionSTM.getStringMatrix()[ actionNo ])[ 1 ]; action = boost::regex_replace( action, newlinePattern, "\n " ); functionBody += " " + action + "\n"; } else { std::string action = (*actionSTM.getStringVectorMap()[ actionSpec ])[ 1 ]; action = boost::regex_replace( action, newlinePattern, "\n " ); functionBody += " " + action + "\n"; } } } functionBody += "end\n"; udmTruthTable.DuringAction() = functionBody; }