Residue class

class Residue

A Residue is a group of atoms belonging to the same logical unit. They can be molecules, amino-acids in a protein, monomers in polymers, etc.

Iterating over a Residue gives the indexes of the atoms in the associated Topology.

auto residue = Residue("CLU");

residue.add_atom(56);
residue.add_atom(22);
residue.add_atom(31);

std::vector<size_t> atoms;
for (auto atom: residue) {
    atoms.push_back(atom);
}

assert(atoms == std::vector<size_t>({22, 31, 56}));

Public Functions

Residue(std::string name)

Create a new residue with a given name and no residue id.

auto residue = Residue("ALA");

assert(residue.name() == "ALA");
assert(residue.id() == nullopt);

Residue(std::string name, uint64_t resid)

Create a new residue with a given name and residue id resid.

auto residue = Residue("ALA", 456);

assert(residue.name() == "ALA");
assert(residue.id().value() == 456);

const std::string &name() const

Get the name of the residue

auto residue = Residue("FOO");
assert(residue.name() == "FOO");

optional<uint64_t> id() const

Get the residue identifier if it exists.

This function returna an chemfiles::optional value that is close to C++17 std::optional.

auto residue = Residue("FOO");
assert(!residue.id());

residue = Residue("BAR", 33);
assert(residue.id());
assert(residue.id().value() == 33);

size_t size() const

Get the size of the residue, i.e. the number of atoms in this residue.

auto residue = Residue("FOO");
assert(residue.size() == 0);

residue.add_atom(2);
residue.add_atom(22);
residue.add_atom(42);
residue.add_atom(36);

assert(residue.size() == 4);

void add_atom(size_t i)

Add an atom with index i to this residue

If the atom is already in the residue, this does nothing.

auto residue = Residue("FOO");
assert(residue.size() == 0);

residue.add_atom(2);
residue.add_atom(22);
residue.add_atom(42);
residue.add_atom(36);

assert(residue.size() == 4);

residue.add_atom(36);
assert(residue.size() == 4);

bool contains(size_t i) const

Check if the residue contains a given atom with index i

auto residue = Residue("FOO");

residue.add_atom(2);
residue.add_atom(22);
residue.add_atom(42);
residue.add_atom(36);

assert(residue.contains(22) == true);
assert(residue.contains(23) == false);

const property_map &properties() const

Get the map of properties asociated with this residue. This map might be iterated over to list the properties of the residue, or directly accessed.

auto frame = Frame();

frame.set("a string", "the lazy fox");
frame.set("a number", 122);

// Iterator over properties in the frame
for (auto it: frame.properties()) {
    if (it.first == "a string") {
        assert(it.second.as_string() == "the lazy fox");
    } else if (it.first == "a number") {
        assert(it.second.as_double() == 122);
    }
}

void set(std::string name, Property value)

Set an arbitrary Property for this residue with the given name and value. If a property with this name already exist, it is replaced with the new value.

auto residue = Residue("ALA");

residue.set("first", "alanine group");
residue.set("second", 42.5);

assert(residue.get("second")->as_double() == 42.5);
assert(residue.get("first")->as_string() == "alanine group");

// Typed access to properties
assert(residue.get<Property::STRING>("first").value() == "alanine group");
assert(!residue.get<Property::BOOL>("first"));

assert(!residue.get("non-existant property"));

// Override the "first" property
residue.set("first", false);
assert(residue.get("first")->as_bool() == false);

optional<const Property&> get(const std::string &name) const

Get the Property with the given name for this residue if it exists.

If no property with the given name is found, this function returns nullopt.

This function returns an chemfiles::optional value that is close to C++17 std::optional.

auto residue = Residue("ALA");

residue.set("first", "alanine group");
residue.set("second", 42.5);

assert(residue.get("second")->as_double() == 42.5);
assert(residue.get("first")->as_string() == "alanine group");

// Typed access to properties
assert(residue.get<Property::STRING>("first").value() == "alanine group");
assert(!residue.get<Property::BOOL>("first"));

assert(!residue.get("non-existant property"));

// Override the "first" property
residue.set("first", false);
assert(residue.get("first")->as_bool() == false);

template<Property::Kind kind>
optional<typename property_metadata<kind>::type> get(const std::string &name) const

Get the Property with the given name for this residue if it exists, and check that it has the required kind.

If no property with the given name is found, this function returns nullopt.

If a property with the given name is found, but has a different kind, this function emits a warning and returns nullopt.

This function returns an chemfiles::optional value that is close to C++17 std::optional.

auto residue = Residue("ALA");

residue.set("first", "alanine group");
residue.set("second", 42.5);

assert(residue.get("second")->as_double() == 42.5);
assert(residue.get("first")->as_string() == "alanine group");

// Typed access to properties
assert(residue.get<Property::STRING>("first").value() == "alanine group");
assert(!residue.get<Property::BOOL>("first"));

assert(!residue.get("non-existant property"));

// Override the "first" property
residue.set("first", false);
assert(residue.get("first")->as_bool() == false);