Skip to content

Commit

Permalink
Add functions that allow getting chain atom and residue ranges based …
Browse files Browse the repository at this point in the history
…on string-valued auth_asym_id/chain-labels.

Related to #96.
  • Loading branch information
mittinatten committed Dec 10, 2023
1 parent 4ff453c commit a8a15cc
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 15 deletions.
44 changes: 44 additions & 0 deletions src/freesasa.h
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,28 @@ int freesasa_structure_chain_atoms(const freesasa_structure *structure,
int *first,
int *last);

/**
\private
Get indices of first and last atoms of a chain
Uses long form chain labels.
This is an intermediate function added in the process of migrating to long chain labels.
It is not meant to be part of the public API for now.
@param structure A structure.
@param chain The chain label.
@param first First atom of `chain` will be stored here.
@param last Last atom of `chain` will be stored here.
@return ::FREESASA_SUCCESS. ::FREESASA_FAIL if `chain` not found.
@ingroup structure
*/
int freesasa_structure_chain_atoms_lcl(const freesasa_structure *structure,
const char *chain,
int *first,
int *last);

/**
Get indices of first and last residues of a chain
Expand All @@ -1328,6 +1350,28 @@ int freesasa_structure_chain_residues(const freesasa_structure *structure,
int *first,
int *last);

/**
\private
Get indices of first and last residues of a chain
Uses long form chain labels.
This is an intermediate function added in the process of migrating to long chain labels.
It is not meant to be part of the public API for now.
@param structure A structure.
@param chain The chain label.
@param first First residue of `chain` will be stored here.
@param last Last residue of `chain` will be stored here.
@return ::FREESASA_SUCCESS. ::FREESASA_FAIL if `chain` not found.
@ingroup structure
*/
int freesasa_structure_chain_residues_lcl(const freesasa_structure *structure,
const char *chain,
int *first,
int *last);

/**
Name of classifier used to generate structure.
Expand Down
60 changes: 45 additions & 15 deletions src/structure.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,8 @@ guess_symbol(char *symbol,

int structure_has_chain(freesasa_structure *s, const chain_label_t chain_label)
{
for (int i = 0; i < s->chains.n; ++i) {
int i;
for (i = 0; i < s->chains.n; ++i) {
if (strncmp(s->chains.labels[i], chain_label, sizeof(chain_label_t)) == 0) {
return 1;
}
Expand Down Expand Up @@ -1197,29 +1198,40 @@ int freesasa_structure_n_chains(const freesasa_structure *structure)
return structure->chains.n;
}

int freesasa_structure_chain_index(const freesasa_structure *structure,
char chain)
int freesasa_structure_chain_index_lcl(const freesasa_structure *structure,
const char *chain)
{
int i;

assert(structure);

for (i = 0; i < structure->chains.n; ++i) {
if (structure->chains.labels[i][0] == chain) return i;
if (strncmp(structure->chains.labels[i], chain, sizeof(chain_label_t)) == 0) {
return i;
}
}
return fail_msg("chain %c not found", chain);

return fail_msg("chain '%s' not found", chain);
}

int freesasa_structure_chain_atoms(const freesasa_structure *structure,
char chain,
int *first,
int *last)
/** Not public */
int freesasa_structure_chain_index(const freesasa_structure *structure,
char chain)
{
chain_label_t chain_label = {chain, '\0'};
return freesasa_structure_chain_index_lcl(structure, chain_label);
}

int freesasa_structure_chain_atoms_lcl(const freesasa_structure *structure,
const char *chain,
int *first,
int *last)
{
int c_i, n;

assert(structure);

c_i = freesasa_structure_chain_index(structure, chain);
c_i = freesasa_structure_chain_index_lcl(structure, chain);
n = freesasa_structure_n_chains(structure);

if (c_i < 0) return fail_msg("");
Expand All @@ -1234,16 +1246,25 @@ int freesasa_structure_chain_atoms(const freesasa_structure *structure,
return FREESASA_SUCCESS;
}

int freesasa_structure_chain_residues(const freesasa_structure *structure,
char chain,
int *first,
int *last)
int freesasa_structure_chain_atoms(const freesasa_structure *structure,
char chain,
int *first,
int *last)
{
chain_label_t chain_label = {chain, '\0'};
return freesasa_structure_chain_atoms_lcl(structure, chain_label, first, last);
}

int freesasa_structure_chain_residues_lcl(const freesasa_structure *structure,
const char *chain,
int *first,
int *last)
{
int first_atom, last_atom;

assert(structure);

if (freesasa_structure_chain_atoms(structure, chain, &first_atom, &last_atom))
if (freesasa_structure_chain_atoms_lcl(structure, chain, &first_atom, &last_atom))
return fail_msg("");

*first = structure->atoms.atom[first_atom]->res_index;
Expand All @@ -1252,6 +1273,15 @@ int freesasa_structure_chain_residues(const freesasa_structure *structure,
return FREESASA_SUCCESS;
}

int freesasa_structure_chain_residues(const freesasa_structure *structure,
char chain,
int *first,
int *last)
{
chain_label_t chain_label = {chain, '\0'};
return freesasa_structure_chain_residues_lcl(structure, chain_label, first, last);
}

const char *
freesasa_structure_classifier_name(const freesasa_structure *structure)
{
Expand Down
9 changes: 9 additions & 0 deletions tests/test_structure.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ END_TEST

START_TEST(test_cif)
{
int first, last;
s = freesasa_structure_new();
for (int i = 0; i < N; ++i) {
struct freesasa_cif_atom_lcl atom = {
Expand Down Expand Up @@ -151,6 +152,14 @@ START_TEST(test_cif)
ck_assert_int_eq(freesasa_structure_residue_chain(s, 0), lcl[0][0]);
ck_assert_str_eq(freesasa_structure_residue_chain_lcl(s, 0), lcl[0]);

ck_assert_int_eq(freesasa_structure_chain_atoms_lcl(s, "AAA", &first, &last), FREESASA_SUCCESS);
ck_assert_int_eq(first, 0);
ck_assert_int_eq(last, N - 1);

ck_assert_int_eq(freesasa_structure_chain_residues_lcl(s, "AAA", &first, &last), FREESASA_SUCCESS);
ck_assert_int_eq(first, 0);
ck_assert_int_eq(last, 1);

struct freesasa_cif_atom_lcl atom = {
.group_PDB = "",
.auth_asym_id = lcl[0],
Expand Down

0 comments on commit a8a15cc

Please sign in to comment.