uint plua_read_asset_flags_field_impl(lua_State* L, int table, const char* key) { PLUA_BALANCE_START(L); lua_getfield(L, table, key); if (!lua_istable(L, -1)) PTHROW(script_exception, std::string("Key not table: ") + key); const auto enumMap = lookup(CAssetIntrospect::flagsStrings, key); if (!enumMap) PTHROW(script_exception, std::string("Unknown flag type: ") + key); // Read flags as table of strings. auto flagTable = lua_gettop(L); uint flag = 0; lua_pushnil(L); // First key. while (lua_next(L, flagTable)) { // Value at -1. size_t len; auto sp = lua_tolstring(L, -1, &len); auto s = std::string{sp, len}; // Transform to lowercase. std::transform(s.begin(), s.end(), s.begin(), std::ptr_fun<int, int>(std::tolower)); auto flagValue = lookup(**enumMap, s.c_str()); if (flagValue) { flag |= *flagValue; } else { warning << "Unknown flag value in field " << key << ": " << s << std::endl; } lua_pop(L, 1); // Pop value. } PLUA_BALANCE_END(L); return flag; }
void CLuaVm::Gc() { // Run with default options. lua_getglobal(L, "collectgarbage"); if (lua_pcall(L, 0, 0, 0) != 0) PTHROW(script_exception, "error collecting garbage"); }
lua_Number plua_read_number_field(lua_State* L, int table, const char* key) { PLUA_BALANCE_START(L); lua_getfield(L, table, key); if (!lua_isnumber(L, -1)) PTHROW(script_exception, std::string("Key not number: ") + key); auto i = (int) lua_tonumber(L, -1); lua_pop(L, 1); PLUA_BALANCE_END(L); return i; }
size_t CLuaVm::MemoryUsage() { lua_getglobal(L, "collectgarbage"); lua_pushliteral(L, "count"); if (lua_pcall(L, 1, 1, 0) != 0) PTHROW(script_exception, "error counting memory usage"); auto usage = static_cast<size_t>( lua_tonumber(L, -1) * 1024 ); lua_pop(L, 1); return usage; }
bool plua_read_bool_field(lua_State* L, int table, const char* key) { PLUA_BALANCE_START(L); lua_getfield(L, table, key); if (!lua_isboolean(L, -1)) PTHROW(script_exception, std::string("Key not boolean: ") + key); bool b = lua_toboolean(L, -1); lua_pop(L, 1); PLUA_BALANCE_END(L); return b; }
std::string plua_read_string_field(lua_State* L, int table, const char* key) { PLUA_BALANCE_START(L); lua_getfield(L, table, key); if (!lua_isstring(L, -1)) { PTHROW(script_exception,std::string("Key not string: ") + key); } auto len = size_t{}; auto sp = lua_tolstring(L, -1, &len); auto s = std::string{ sp, len }; lua_pop(L, 1); PLUA_BALANCE_END(L); return s; }
rgba_t<float> plua_read_rgba_field(lua_State* L, int table, const char* key) { PLUA_BALANCE_START(L); lua_getfield(L, table, key); if (!lua_istable(L, -1)) PTHROW(script_exception, std::string("Key not table: ") + key); // Read color as table. auto colorTable = lua_gettop(L); rgba_t<float> color; lua_getfield(L, colorTable, "r"); color.r = static_cast<float>(lua_tonumber(L, -1)); // Defaults to 0. lua_getfield(L, colorTable, "g"); color.g = static_cast<float>(lua_tonumber(L, -1)); lua_getfield(L, colorTable, "b"); color.b = static_cast<float>(lua_tonumber(L, -1)); lua_getfield(L, colorTable, "a"); color.a = static_cast<float>(lua_tonumber(L, -1)); lua_pop(L, 5); // 4 numbers plus table. PLUA_BALANCE_END(L); return color; }
static BLAST_Matrix * ConvertPSSMToBLASTMatrix(const CPssmWithParameters& pssm) { // TRACE_MESSAGE("converting CPssmWithParameters to BLAST_Matrix"); if (!pssm.GetPssm().IsSetFinalData()) PTHROW("ConvertPSSMToBLASTMatrix() - pssm must have finalData"); unsigned int nScores = pssm.GetPssm().GetNumRows() * pssm.GetPssm().GetNumColumns(); if (pssm.GetPssm().GetNumRows() != 28 || pssm.GetPssm().GetFinalData().GetScores().size() != nScores) PTHROW("ConvertPSSMToBLASTMatrix() - bad matrix size"); // rows and columns are reversed in pssm vs BLAST_Matrix BLAST_Matrix *matrix = new BLAST_Matrix(pssm.GetPssm().GetNumColumns() + 1, pssm.GetPssm().GetNumRows()); // set BLAST_Matrix values matrix->is_prot = pssm.GetPssm().GetIsProtein(); matrix->name = NULL; // matrix->posFreqs = NULL; if (pssm.GetPssm().GetFinalData().IsSetKappa()) matrix->karlinK = pssm.GetPssm().GetFinalData().GetKappa(); else ERROR_MESSAGE("ConvertPSSMToBLASTMatrix() - missing Kappa"); // matrix->original_matrix = NULL; // convert matrix unsigned int i, r = 0, c = 0; CPssmFinalData::TScores::const_iterator s = pssm.GetPssm().GetFinalData().GetScores().begin(); for (i=0; i<nScores; ++i, ++s) { matrix->matrix[r][c] = *s; // adjust for matrix layout in pssm if (pssm.GetPssm().GetByRow()) { ++r; if ((int)r == pssm.GetPssm().GetNumColumns()) { ++c; r = 0; } } else { ++c; if ((int)c == pssm.GetPssm().GetNumRows()) { ++r; c = 0; } } } // Set the last row to BLAST_SCORE_MIN for (i=0; (int)i<matrix->columns; i++) matrix->matrix[matrix->rows - 1][i] = BLAST_SCORE_MIN; #ifdef DEBUG_PSSM CNcbiOfstream ofs("psimsa.txt", IOS_BASE::out | IOS_BASE::app); if (ofs) { ofs << "matrix->is_prot: " << (int) matrix->is_prot << '\n' << "matrix->name: " << (matrix->name ? matrix->name : "(none)") << '\n' << "matrix->rows: " << matrix->rows << '\n' << "matrix->columns: " << matrix->columns << '\n'; for (r=0; (int)r<matrix->rows; ++r) { for (c=0; (int)c<matrix->columns; ++c) ofs << matrix->matrix[r][c] << ' '; ofs << '\n'; } } #endif return matrix; }
static void FillInAlignmentData(const BlockMultipleAlignment *bma, PSIMsa *data) { const unsigned char gap = LookupNCBIStdaaNumberFromCharacter('-'); if (data->dimensions->query_length != bma->GetMaster()->Length() || data->dimensions->num_seqs != bma->NRows() - 1) PTHROW("FillInAlignmentData() - data array size mismatch"); BlockMultipleAlignment::ConstBlockList blocks; bma->GetBlockList(blocks); unsigned int b, row, column, masterStart, masterWidth, slaveStart, slaveWidth, left=0, /*right=0,*/ middle=0; const Block::Range *range; const Sequence *seq; for (b=0; b<blocks.size(); ++b) { const Block& block = *(blocks[b]); seq = bma->GetSequenceOfRow(0); range = block.GetRangeOfRow(0); if (range->from < 0 || range->from > (int)seq->Length() || range->to < -1 || range->to >= (int)seq->Length() || range->to < range->from - 1 || range->from != ((b == 0) ? 0 : (blocks[b - 1]->GetRangeOfRow(0)->to + 1)) || (b == blocks.size() - 1 && range->to != (int)seq->Length() - 1)) PTHROW("FillInAlignmentData() - master range error"); masterStart = range->from; masterWidth = range->to - range->from + 1; for (row=0; row<bma->NRows(); row++) { seq = bma->GetSequenceOfRow(row); range = block.GetRangeOfRow(row); if (range->from < 0 || range->from > (int)seq->Length() || range->to < -1 || range->to >= (int)seq->Length() || range->to < range->from - 1 || range->from != ((b == 0) ? 0 : (blocks[b - 1]->GetRangeOfRow(row)->to + 1)) || (b == blocks.size() - 1 && range->to != (int)seq->Length() - 1)) PTHROW("FillInAlignmentData() - slave range error"); slaveStart = range->from; slaveWidth = range->to - range->from + 1; for (column=0; column<masterWidth; ++column) { PSIMsaCell& cell = data->data[row][masterStart + column]; // same # residues in both master and slave if (slaveWidth == masterWidth) { cell.letter = LookupNCBIStdaaNumberFromCharacter(seq->m_sequenceString[slaveStart + column]); cell.is_aligned = true; } // left tail else if (b == 0) { // truncate left end of sequence if (slaveWidth > masterWidth) { cell.letter = LookupNCBIStdaaNumberFromCharacter(seq->m_sequenceString[slaveWidth - masterWidth + column]); cell.is_aligned = true; } else { // residues to the right if (column >= masterWidth - slaveWidth) { cell.letter = LookupNCBIStdaaNumberFromCharacter(seq->m_sequenceString[column - (masterWidth - slaveWidth)]); cell.is_aligned = true; } // pad left with unaligned gaps else { cell.letter = gap; cell.is_aligned = false; } } } // right tail else if (b == blocks.size() - 1) { // truncate right end of sequence if (slaveWidth > masterWidth) { cell.letter = LookupNCBIStdaaNumberFromCharacter(seq->m_sequenceString[slaveStart + column]); cell.is_aligned = true; } else { // residues to the left if (column < slaveWidth) { cell.letter = LookupNCBIStdaaNumberFromCharacter(seq->m_sequenceString[slaveStart + column]); cell.is_aligned = true; } // pad left with unaligned gaps else { cell.letter = gap; cell.is_aligned = false; } } } // more residues in master than slave: split and pad middle with (aligned) gaps else if (slaveWidth < masterWidth) { if (column == 0) { left = (slaveWidth + 1) / 2; // +1 means left gets more in uneven split middle = masterWidth - slaveWidth; //right = slaveWidth - left; } if (column < left) cell.letter = LookupNCBIStdaaNumberFromCharacter(seq->m_sequenceString[slaveStart + column]); else if (column < left + middle) cell.letter = gap; else cell.letter = LookupNCBIStdaaNumberFromCharacter(seq->m_sequenceString[slaveStart + column - middle]); cell.is_aligned = true; // even gaps are aligned in these regions } // more residues in slave than master: truncate middle of slave else { // slaveWidth > masterWidth if (column == 0) { left = (masterWidth + 1) / 2; // +1 means left gets more in uneven split //right = masterWidth - left; } if (column < left) cell.letter = LookupNCBIStdaaNumberFromCharacter(seq->m_sequenceString[slaveStart + column]); else cell.letter = LookupNCBIStdaaNumberFromCharacter( seq->m_sequenceString[slaveStart + slaveWidth - masterWidth + column]); cell.is_aligned = true; } } } } }