Skip to content

Commit

Permalink
Change altloc from str1 to std::string for future flexibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
olegsobolev committed Nov 3, 2023
1 parent fc6b80f commit ab6e2b3
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 80 deletions.
24 changes: 18 additions & 6 deletions iotbx/pdb/atom_selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,26 @@ namespace {
}
}

void
s_map_array_transfer(
std::map<std::string, std::vector<unsigned> >& map_s,
std::map<std::string, std::vector<unsigned> >& map)
{
typedef typename std::map<std::string, std::vector<unsigned> >::iterator it;
it i_end = map_s.end();
for(it i=map_s.begin();i!=i_end;i++) {
map[i->first].swap(i->second);
}
}

} // namespace <anonymous>

atom_selection_cache::atom_selection_cache(
hierarchy::root const& root,
bool altloc_only)
{
std::map<str4, std::vector<unsigned> > name_s;
std::map<str1, std::vector<unsigned> > altloc_s;
std::map<std::string, std::vector<unsigned> > altloc_s;
std::map<str4, std::vector<unsigned> > resseq_s;
std::map<str1, std::vector<unsigned> > icode_s;
std::map<str5, std::vector<unsigned> > resid_s;
Expand Down Expand Up @@ -106,11 +118,11 @@ namespace {
}
n_seq = i_seq;
if (!altloc_only) {
if (altloc_s.find(str1("")) != altloc_s.end()) {
std::vector<unsigned> const& s0 = altloc_s[str1("")];
std::vector<unsigned>& s1 = altloc_s[str1(" ")];
if (altloc_s.find("") != altloc_s.end()) {
std::vector<unsigned> const& s0 = altloc_s[""];
std::vector<unsigned>& s1 = altloc_s[" "];
s1.insert(s1.end(), s0.begin(), s0.end());
altloc_s.erase(str1(""));
altloc_s.erase("");
}
map_array_transfer(name_s, name);
map_array_transfer(resseq_s, resseq);
Expand All @@ -120,7 +132,7 @@ namespace {
map_array_transfer(element_s, element);
map_array_transfer(charge_s, charge);
}
map_array_transfer(altloc_s, altloc);
s_map_array_transfer(altloc_s, altloc);
}

af::shared<unsigned> get_resid_sequence (
Expand Down
51 changes: 26 additions & 25 deletions iotbx/pdb/hierarchy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ namespace {
altloc = resname = resseq = icode = chain_id = model_id = 0;
}
else {
altloc = ag->altloc.elems;
altloc = ag->altloc.c_str();
resname = ag->resname.c_str();
shared_ptr<residue_group_data> rg_lock = ag->parent.lock();
const residue_group_data* rg = rg_lock.get();
Expand Down Expand Up @@ -694,7 +694,7 @@ namespace {
atom_with_labels const& self,
atom_label_columns_formatter& label_formatter)
{
label_formatter.altloc = self.altloc.elems;
label_formatter.altloc = self.altloc.c_str();
label_formatter.resname = self.resname.c_str();
label_formatter.resseq = self.resseq.elems;
label_formatter.icode = self.icode.elems;
Expand Down Expand Up @@ -1111,7 +1111,7 @@ namespace {
std::string chain_id;
str4 resseq;
str1 icode;
str1 altloc;
std::string altloc;
std::string resname;
boost::optional<atom_group> ag = parent();
if (ag) {
Expand All @@ -1137,7 +1137,7 @@ namespace {
chain_id.c_str(),
resseq.elems,
icode.elems,
altloc.elems,
altloc.c_str(),
resname.c_str(),
/* is_first_in_chain */ false,
/* is_first_after_break */ false);
Expand Down Expand Up @@ -1330,7 +1330,7 @@ namespace {
atom_group::confid() const
{
std::string result;
result += (data->altloc.size() == 0) ? " " : data->altloc.elems;
result += (data->altloc.size() == 0) ? " " : data->altloc.c_str();
result += (data->resname.size() == 0) ? " " : boost::algorithm::trim_copy(data->resname);
return result;
}
Expand Down Expand Up @@ -1583,8 +1583,8 @@ namespace {
typedef std::vector<atom_group>::const_iterator agi_t;
agi_t ag_end = data->atom_groups.end();
for(agi_t agi=data->atom_groups.begin();agi!=ag_end;agi++) {
char altloc = agi->data->altloc.elems[0];
if (altloc != '\0' && altloc != blank_altloc_char) {
std::string altloc = agi->data->altloc;
if (altloc != "" && altloc != blank_altloc_string) {
return true;
}
}
Expand Down Expand Up @@ -1629,8 +1629,8 @@ namespace {
unsigned n_ag = atom_groups_size();
for(unsigned i_ag=0;i_ag<n_ag;i_ag++) {
atom_group const& ag = data->atom_groups[i_ag];
char altloc = ag.data->altloc.elems[0];
if (altloc == '\0' || altloc == blank_altloc_char) {
std::string altloc = ag.data->altloc;
if (altloc == "" || altloc == blank_altloc_string) {
if (i_ag != n_blank_altloc_atom_groups) {
atom_group ag_by_value = ag;
remove_atom_group(i_ag);
Expand All @@ -1656,7 +1656,7 @@ namespace {
unsigned i_ag = 0;
for(;i_ag<n_blank_altloc_atom_groups;i_ag++) {
atom_group& ag = data->atom_groups[i_ag];
ag.data->altloc.elems[0] = '\0';
ag.data->altloc = "";
ss4& blank_name_set = blank_name_sets[ag.data->resname];
unsigned n_atoms = ag.atoms_size();
for(unsigned i_atom=0;i_atom<n_atoms;i_atom++) {
Expand Down Expand Up @@ -1713,7 +1713,7 @@ namespace {
if (new_atom_group == 0) {
unsigned i = n_blank_altloc_atom_groups
+ n_blank_but_alt_atom_groups;
atom_group new_ag(blank_altloc_cstr, ag.data->resname.c_str());
atom_group new_ag(blank_altloc_string, ag.data->resname.c_str());
insert_atom_group(i, new_ag);
new_atom_group = &data->atom_groups[i];
n_blank_but_alt_atom_groups++;
Expand Down Expand Up @@ -1755,7 +1755,7 @@ namespace {
" (this chain must be the parent).");
}
typedef std::map<std::string, atom_group> s3ag;
typedef std::map<str1, s3ag> s1s3ag;
typedef std::map<std::string, s3ag> s1s3ag;
s1s3ag altloc_resname_dict;
unsigned n_ag = primary.atom_groups_size();
for(unsigned i=0;i<n_ag;i++) {
Expand Down Expand Up @@ -1844,9 +1844,9 @@ namespace {
std::vector<unsigned> const& i_rgs = mrii->second;
unsigned n_i_rgs = static_cast<unsigned>(i_rgs.size());
if (n_i_rgs == 1U) continue;
std::set<str1> altlocs;
altlocs.insert('\0');
altlocs.insert(blank_altloc_char);
std::set<std::string> altlocs;
altlocs.insert("");
altlocs.insert(blank_altloc_string);
unsigned altlocs_size = 2U;
for(unsigned i_i_rgs=0;i_i_rgs<n_i_rgs;i_i_rgs++) {
unsigned i_rg = i_rgs[i_i_rgs];
Expand Down Expand Up @@ -1910,7 +1910,7 @@ namespace {
unsigned n_ags = rg.atom_groups_size();
std::vector<atom_group> const& ags = rg.atom_groups();
bool is_pure_altloc = ( n_ags != 0
&& ags[0].data->altloc.elems[0] != '\0');
&& ags[0].data->altloc != "");
if (common_residue_name_class_only != 0) {
skip = 1;
for(unsigned i_ag=0;i_ag<n_ags;i_ag++) {
Expand Down Expand Up @@ -1997,9 +1997,10 @@ namespace {
const residue_group* residue_groups,
unsigned residue_groups_size)
{
const char nulc = '\0';
std::vector<char> altlocs;
typedef std::map<char, unsigned> mcu;
// const char nulc = '\0';
std::string nulc = "";
std::vector<std::string> altlocs;
typedef std::map<std::string, unsigned> mcu;
mcu altloc_indices;
bool have_at_least_one_atom_group = false;
for(unsigned i_rg=0;i_rg<residue_groups_size;i_rg++) {
Expand All @@ -2008,7 +2009,7 @@ namespace {
if (n_ag != 0) have_at_least_one_atom_group = true;
std::vector<atom_group> const& ags = rg.atom_groups();
for(unsigned i_ag=0;i_ag<n_ag;i_ag++) {
char altloc = ags[i_ag].data->altloc.elems[0];
std::string altloc = ags[i_ag].data->altloc;
if (altloc == nulc) continue;
if (altloc_indices.find(altloc) == altloc_indices.end()) {
altlocs.push_back(altloc);
Expand All @@ -2026,10 +2027,10 @@ namespace {
af::shared<conformer> result((af::reserve(n_cf)));
for(unsigned i_cf=0;i_cf<n_cf;i_cf++) {
if (chain != 0) {
result.push_back(conformer(*chain, str1(altlocs[i_cf]).elems));
result.push_back(conformer(*chain, altlocs[i_cf]));
}
else {
result.push_back(conformer(str1(altlocs[i_cf]).elems));
result.push_back(conformer(altlocs[i_cf]));
}
}
std::vector<std::string> resnames; // allocate once
Expand All @@ -2046,7 +2047,7 @@ namespace {
std::vector<atom_group> const& ags = rg.atom_groups();
for(unsigned i_ag=0;i_ag<n_ag;i_ag++) {
atom_group const& ag = ags[i_ag];
char altloc = ag.data->altloc.elems[0];
std::string altloc = ag.data->altloc;
if (altloc == nulc) {
for(unsigned i_cf=0;i_cf<n_cf;i_cf++) {
altloc_ags[i_cf].push_back(ag);
Expand Down Expand Up @@ -2079,7 +2080,7 @@ namespace {
}
}
atoms->insert(atoms->end(), ag.atoms().begin(), ag.atoms().end());
if (ag.data->altloc.elems[0] != nulc) {
if (ag.data->altloc != nulc) {
resnames_with_altloc.insert(ag.data->resname);
}
}
Expand Down Expand Up @@ -2526,7 +2527,7 @@ namespace {
chain_id.c_str(),
resseq.elems,
icode.elems,
altloc.elems,
altloc.c_str(),
resname.c_str(),
is_first_in_chain,
is_first_after_break);
Expand Down
43 changes: 21 additions & 22 deletions iotbx/pdb/hierarchy.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ A residue object is NOT a parent of the atoms.

namespace hierarchy {

static const char blank_altloc_char = ' ';
static const char blank_altloc_cstr[2] = {blank_altloc_char, '\0'};
static const std::string blank_altloc_string(" ");

class root_data;
class root;
Expand Down Expand Up @@ -203,21 +202,21 @@ namespace hierarchy {
friend struct atom_label_columns_formatter;
weak_ptr<residue_group_data> parent;
public:
str1 altloc;
std::string altloc;
std::string resname;
protected:
std::vector<atom> atoms;

inline
atom_group_data(
weak_ptr<residue_group_data> const& parent_,
const char* altloc_,
const char* resname_);
std::string altloc_,
std::string resname_);

inline
atom_group_data(
const char* altloc_,
const char* resname_);
std::string altloc_,
std::string resname_);

inline
atom_group_data(
Expand Down Expand Up @@ -863,13 +862,13 @@ namespace hierarchy {
explicit
atom_group(
residue_group const& parent,
const char* altloc="",
const char* resname="");
std::string altloc="",
std::string resname="");

explicit
atom_group(
const char* altloc="",
const char* resname="")
std::string altloc="",
std::string resname="")
:
data(new atom_group_data(altloc, resname))
{}
Expand All @@ -878,8 +877,8 @@ namespace hierarchy {
atom_group(const atom_group_data* other)
:
data(new atom_group_data(
other->altloc.elems,
other->resname.c_str()))
other->altloc,
other->resname))
{}

atom_group(
Expand Down Expand Up @@ -1683,7 +1682,7 @@ namespace hierarchy {
std::string chain_id;
str4 resseq;
str1 icode;
str1 altloc;
std::string altloc;
std::string resname;
bool is_first_in_chain;
bool is_first_after_break;
Expand Down Expand Up @@ -1856,21 +1855,21 @@ namespace hierarchy {
inline
atom_group_data::atom_group_data(
weak_ptr<residue_group_data> const& parent_,
const char* altloc_,
const char* resname_)
std::string altloc_,
std::string resname_)
:
parent(parent_),
altloc(altloc_),
resname((resname_ == 0) ? "" : resname_)
resname(resname_)
{}

inline
atom_group_data::atom_group_data(
const char* altloc_,
const char* resname_)
std::string altloc_,
std::string resname_)
:
altloc(altloc_),
resname((resname_ == 0) ? "" : resname_)
resname(resname_)
{}

inline
Expand All @@ -1886,8 +1885,8 @@ namespace hierarchy {
inline
atom_group::atom_group(
residue_group const& parent,
const char* altloc,
const char* resname)
std::string altloc,
std::string resname)
:
data(new atom_group_data(parent.data, altloc, resname))
{}
Expand Down
3 changes: 2 additions & 1 deletion iotbx/pdb/hierarchy_atom_bpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ namespace {
/* HY36_WIDTH_4_MAX */ 2436111)
IOTBX_LOC_GET(resseq)
IOTBX_LOC_GET_SET(icode)
IOTBX_LOC_GET_SET(altloc)
// IOTBX_LOC_GET_SET(altloc)
// IOTBX_LOC_GET_SET(resname)

#undef IOTBX_LOC_GET
Expand Down Expand Up @@ -484,6 +484,7 @@ namespace {
IOTBX_LOC_GET_SET_STD_STRING(model_id)
IOTBX_LOC_GET_SET_STD_STRING(chain_id)
IOTBX_LOC_GET_SET_STD_STRING(resname)
IOTBX_LOC_GET_SET_STD_STRING(altloc)

#undef IOTBX_LOC_GET_STD_STRING
#undef IOTBX_LOC_SET_STD_STRING
Expand Down
2 changes: 1 addition & 1 deletion iotbx/pdb/hierarchy_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace {
{
typedef atom_group w_t;

IOTBX_PDB_HIERARCHY_DATA_WRAPPERS_SMALL_STR_GET_SET(altloc)
IOTBX_PDB_HIERARCHY_DATA_WRAPPERS_STD_STRING_GET_SET(altloc)
IOTBX_PDB_HIERARCHY_DATA_WRAPPERS_STD_STRING_GET_SET(resname)

static
Expand Down
2 changes: 1 addition & 1 deletion iotbx/pdb/hierarchy_select.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace iotbx { namespace pdb { namespace hierarchy {
for(unsigned i_ag=0;i_ag<n_ags;i_ag++) { \
atom_group const& ag = ags[i_ag]; \
unsigned n_ats = ag.atoms_size(); \
atom_group r_ag(ag.data->altloc.elems, ag.data->resname.c_str()); \
atom_group r_ag(ag.data->altloc, ag.data->resname); \
std::vector<atom> const& atoms = ag.atoms(); \
boost::scoped_array<atom> r_atoms(new atom[n_ats]); \
unsigned r_n_ats = 0;
Expand Down
4 changes: 2 additions & 2 deletions iotbx/pdb/hierarchy_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace iotbx { namespace pdb { namespace hierarchy {
unsigned n_ag = rg.atom_groups_size();
for(unsigned i_ag=0;i_ag<n_ag;i_ag++) {
atom_group const& ag = rg.atom_groups()[i_ag];
label_formatter.altloc = ag.data->altloc.elems;
label_formatter.altloc = ag.data->altloc.c_str();
label_formatter.resname = ag.data->resname.c_str();
typedef std::vector<atom> va;
va const& atoms = ag.atoms();
Expand All @@ -55,7 +55,7 @@ namespace iotbx { namespace pdb { namespace hierarchy {
for(unsigned i_at=0;i_at<n_at;i_at++) {
hierarchy::atom const* atom = &ats[i_at];
shared_ptr<atom_group_data> ag_data = atom->parent_ptr();
label_formatter.altloc = ag_data->altloc.elems;
label_formatter.altloc = ag_data->altloc.c_str();
label_formatter.resname = ag_data->resname.c_str();
IOTBX_LOC
}
Expand Down
Loading

0 comments on commit ab6e2b3

Please sign in to comment.