Skip to content

Commit

Permalink
Added rename_column
Browse files Browse the repository at this point in the history
Added item_alias
Rename columns in reconstruct
  • Loading branch information
mhekkel committed Jan 22, 2024
1 parent f450643 commit 5d4534f
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 5 deletions.
3 changes: 3 additions & 0 deletions include/cif++/category.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,9 @@ class category
*/
void remove_column(std::string_view column_name);

/** @brief Rename column @a from_name to @a to_name */
void rename_column(std::string_view from_name, std::string_view to_name);

/// @brief Return whether a column with name @a name exists in this category
/// @param name The name of the column
/// @return True if the column exists
Expand Down
16 changes: 15 additions & 1 deletion include/cif++/validate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ struct type_validator
int compare(std::string_view a, std::string_view b) const;
};

/** @brief Item alias, items can be renamed over time
*/

struct item_alias
{
std::string m_name; ///< The alias_name
std::string m_dict; ///< The dictionary in which it was known
std::string m_vers; ///< The version of the dictionary
};

/**
* @brief An item_validator binds a type_validator to an item in
* a category along with other information found in the dictionary.
Expand All @@ -163,6 +173,7 @@ struct item_validator
cif::iset m_enums; ///< If filled, the set of allowed values
std::string m_default; ///< If filled, a default value for this item
category_validator *m_category = nullptr; ///< The category_validator this item_validator belongs to
std::vector<item_alias> m_aliases; ///< The aliases for this item

/// @brief Compare based on the name
bool operator<(const item_validator &rhs) const
Expand Down Expand Up @@ -191,7 +202,7 @@ struct category_validator
{
std::string m_name; ///< The name of the category
std::vector<std::string> m_keys; ///< The list of items that make up the key
cif::iset m_groups; ///< The category groups this category belongs to
cif::iset m_groups; ///< The category groups this category belongs to
cif::iset m_mandatory_fields; ///< The mandatory fields for this category
std::set<item_validator> m_item_validators; ///< The item validators for the items in this category

Expand All @@ -206,6 +217,9 @@ struct category_validator

/// @brief Return the item_validator for item @a tag, may return nullptr
const item_validator *get_validator_for_item(std::string_view tag) const;

/// @brief Return the item_validator for an item that has as alias name @a tag, may return nullptr
const item_validator *get_validator_for_aliased_item(std::string_view tag) const;
};

/**
Expand Down
14 changes: 14 additions & 0 deletions src/category.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,20 @@ void category::remove_column(std::string_view column_name)
}
}

void category::rename_column(std::string_view from_name, std::string_view to_name)
{
for (size_t ix = 0; ix < m_columns.size(); ++ix)
{
if (not iequals(from_name, m_columns[ix].m_name))
continue;

m_columns[ix].m_name = to_name;
m_columns[ix].m_validator = m_cat_validator ? m_cat_validator->get_validator_for_item(to_name) : nullptr;

break;
}
}

iset category::get_columns() const
{
iset result;
Expand Down
9 changes: 8 additions & 1 deletion src/dictionary_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ class dictionary_parser : public parser
// }
// }

std::vector<item_alias> aliases;
for (const auto &[alias_name, dictionary, version] :
dict["item_aliases"].rows<std::string,std::string,std::string>("alias_name", "dictionary", "version"))
{
aliases.emplace_back(alias_name, dictionary, version);
}

// collect the dict from our dataBlock and construct validators
for (auto i : dict["item"])
{
Expand All @@ -245,7 +252,7 @@ class dictionary_parser : public parser

auto vi = find(ivs.begin(), ivs.end(), item_validator{ item_name });
if (vi == ivs.end())
ivs.push_back(item_validator{ item_name, iequals(mandatory, "yes"), tv, ess, defaultValue /*, defaultIsNull*/ });
ivs.push_back(item_validator{ item_name, iequals(mandatory, "yes"), tv, ess, defaultValue, nullptr, std::move(aliases) });
else
{
// need to update the itemValidator?
Expand Down
20 changes: 17 additions & 3 deletions src/pdb/reconstruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,9 +683,6 @@ void comparePolySeqSchemes(datablock &db)
}
}
}

if (ndb_poly_seq_scheme.empty())
db.erase(std::remove(db.begin(), db.end(), ndb_poly_seq_scheme), db.end());
}

void reconstruct_pdbx(file &file, std::string_view dictionary)
Expand Down Expand Up @@ -732,6 +729,23 @@ void reconstruct_pdbx(file &file, std::string_view dictionary)
auto cv = validator.get_validator_for_category(cat.name());
if (not cv)
continue;

// Start by renaming columns that may have old names based on alias info

for (auto tag : cat.get_columns())
{
auto iv = cv->get_validator_for_item(tag);
if (iv) // know, must be OK then
continue;

iv = cv->get_validator_for_aliased_item(tag);
if (not iv)
continue;

if (cif::VERBOSE > 0)
std::clog << "Renaming " << tag << " to " << iv->m_tag << " in category " << cat.name() << '\n';
cat.rename_column(tag, iv->m_tag);
}

for (auto link : validator.get_links_for_child(cat.name()))
{
Expand Down
22 changes: 22 additions & 0 deletions src/validate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,28 @@ const item_validator *category_validator::get_validator_for_item(std::string_vie
return result;
}

const item_validator *category_validator::get_validator_for_aliased_item(std::string_view tag) const
{
const item_validator *result = nullptr;

for (auto &iv : m_item_validators)
{
for (auto &ai : iv.m_aliases)
{
const auto &[cat, name] = split_tag_name(ai.m_name);
if (name == tag and cat == m_name)
{
result = &iv;
break;
}
}
if (result)
break;
}

return result;
}

// --------------------------------------------------------------------

void validator::add_type_validator(type_validator &&v)
Expand Down

0 comments on commit 5d4534f

Please sign in to comment.