Skip to content

Commit

Permalink
Rename chain_list -> chain_group.
Browse files Browse the repository at this point in the history
Use lcl chain_groups in CLI.
  • Loading branch information
mittinatten committed May 26, 2024
1 parent 0250f3b commit a204a1d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 54 deletions.
6 changes: 3 additions & 3 deletions src/freesasa.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,13 @@ typedef struct freesasa_cif_atom freesasa_cif_atom;
typedef struct freesasa_cif_atom_lcl freesasa_cif_atom_lcl;
#endif

struct freesasa_chain_list {
struct freesasa_chain_group {
const char **chains;
size_t n;
};

#ifndef __cplusplus
typedef struct freesasa_chain_list freesasa_chain_list;
typedef struct freesasa_chain_group freesasa_chain_group;
#endif

/**
Expand Down Expand Up @@ -1025,7 +1025,7 @@ freesasa_structure_get_chains(const freesasa_structure *structure,
*/
freesasa_structure *
freesasa_structure_get_chains_lcl(const freesasa_structure *structure,
const freesasa_chain_list *chains,
const freesasa_chain_group *chains,
const freesasa_classifier *classifier,
int options);

Expand Down
78 changes: 37 additions & 41 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,37 @@ struct cli_state {
int static_classifier;
int cif;
int no_rel;
/* chain groups */
int n_chain_groups;
char **chain_groups;
std::vector<freesasa_chain_group> chain_groups;

/* selection commands */
int n_select;
char **select_cmd;

/* output settings */
int output_format, output_depth;

/* Files */
char *output_filename;
FILE *input, *output, *errlog;

cli_state()
{
parameters = freesasa_default_parameters;
classifier_from_file = NULL;
classifier = NULL;
structure_options = 0;
static_classifier = 0;
no_rel = 0;
chain_groups = std::vector<freesasa_chain_group>();
n_select = 0;
select_cmd = 0;
output_format = 0;
output_depth = FREESASA_OUTPUT_CHAIN;
output_filename = NULL;
output = NULL;
errlog = NULL;
cif = 0;
}
};

struct analysis_results {
Expand All @@ -113,40 +133,17 @@ struct analysis_results {
int n_structures;
};

/* Defaults */
static void
init_state(struct cli_state *state)
{
state->parameters = freesasa_default_parameters;
state->classifier_from_file = NULL;
state->classifier = NULL;
state->structure_options = 0;
state->static_classifier = 0;
state->no_rel = 0;
state->n_chain_groups = 0;
state->chain_groups = NULL;
state->n_select = 0;
state->select_cmd = 0;
state->output_format = 0;
state->output_depth = FREESASA_OUTPUT_CHAIN;
state->output_filename = NULL;
state->output = NULL;
state->errlog = NULL;
state->cif = 0;
}

static void
release_state(struct cli_state *state)
{
int i;

if (state->classifier_from_file)
freesasa_classifier_free(state->classifier_from_file);
if (state->chain_groups) {
for (i = 0; i < state->n_chain_groups; ++i) {
free(state->chain_groups[i]);
}
for (i = 0; i < state->chain_groups.size(); ++i) {
free(state->chain_groups[i].chains);
}

if (state->select_cmd) {
for (i = 0; i < state->n_select; ++i) {
free(state->select_cmd[i]);
Expand Down Expand Up @@ -291,19 +288,19 @@ get_structures(std::FILE *input,
}

/* get chain-groups (if requested) */
if (state->n_chain_groups > 0) {
if (state->chain_groups.size() > 0) {
n2 = n;
for (int i = 0; i < state->n_chain_groups; ++i) {
for (int i = 0; i < state->chain_groups.size(); ++i) {
for (int j = 0; j < n; ++j) {
// TODO make this function pdb and cif compatible.
tmp = freesasa_structure_get_chains(structures[j], state->chain_groups[i],
state->classifier, state->structure_options);
tmp = freesasa_structure_get_chains_lcl(structures[j], &state->chain_groups[i],
state->classifier, state->structure_options);
if (tmp != NULL) {
++n2;
structures.reserve(n2);
structures.push_back(tmp);
} else {
abort_msg("at least one of chain(s) '%s' not found", state->chain_groups[i]);
abort_msg("at least one of the requested chains not found in structure");
}
}
}
Expand Down Expand Up @@ -404,12 +401,13 @@ state_add_chain_groups(const char *cmd, struct cli_state *state)
str = strdup(cmd);
token = strtok(str, "+");
while (token) {
++state->n_chain_groups;
state->chain_groups = (char **)realloc(state->chain_groups, sizeof(char *) * state->n_chain_groups);
if (state->chain_groups == NULL) {
abort_msg("out of memory");
size_t n = strlen(token);
const char **chains = (const char **)malloc(n * sizeof(char *));
for (size_t i = 0; i < n; ++i) {
const char chain[2] = {token[i], '\0'};
chains[i] = strdup(chain);
}
state->chain_groups[state->n_chain_groups - 1] = strdup(token);
state->chain_groups.push_back({.n = n, .chains = chains});
token = strtok(0, "+");
}
free(str);
Expand Down Expand Up @@ -731,8 +729,6 @@ int main(int argc,
freesasa_node *tree = freesasa_tree_new(), *tmp;
if (tree == NULL) abort_msg("error initializing calculation");

init_state(&state);

optind = parse_arg(argc, argv, &state);
std::vector<freesasa_structure *> structures;

Expand Down
6 changes: 3 additions & 3 deletions src/structure.c
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ freesasa_structure_get_chains(const freesasa_structure *structure,
return NULL;
}

static int chain_list_has_chain(const freesasa_chain_list *chains, chain_label_t chain)
static int chain_group_has_chain(const freesasa_chain_group *chains, chain_label_t chain)
{
assert(chains);

Expand All @@ -1024,7 +1024,7 @@ static int chain_list_has_chain(const freesasa_chain_list *chains, chain_label_t

freesasa_structure *
freesasa_structure_get_chains_lcl(const freesasa_structure *structure,
const freesasa_chain_list *chains,
const freesasa_chain_group *chains,
const freesasa_classifier *classifier,
int options)
{
Expand All @@ -1050,7 +1050,7 @@ freesasa_structure_get_chains_lcl(const freesasa_structure *structure,
for (i = 0; i < structure->atoms.n; ++i) {
ai = structure->atoms.atom[i];
c = ai->chain_label;
if (chain_list_has_chain(chains, c)) {
if (chain_group_has_chain(chains, c)) {
v = freesasa_coord_i(structure->xyz, i);
res = structure_add_atom_wopt_impl(new_s, ai->atom_name,
ai->res_name, ai->res_number, ai->symbol,
Expand Down
14 changes: 7 additions & 7 deletions tests/test_structure.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,33 +490,33 @@ START_TEST(test_get_chains_lcl)
freesasa_structure *s = freesasa_structure_from_pdb(pdb, NULL, 0);
int first, last;

freesasa_chain_list selection = {.chains = NULL, .n = 0};
freesasa_chain_group selection = {.chains = NULL, .n = 0};
freesasa_structure *s2 = freesasa_structure_get_chains_lcl(s, &selection, NULL, 0);
ck_assert(s2 == NULL);

const char *chains2[] = {"X"};
freesasa_chain_list selection2 = {.chains = chains2, .n = 1};
freesasa_chain_group selection2 = {.chains = chains2, .n = 1};
s2 = freesasa_structure_get_chains_lcl(s, &selection2, NULL, 0);
ck_assert(s2 == NULL);

const char *chains3[] = {"A"};
freesasa_chain_list selection3 = {.chains = chains3, .n = 1};
freesasa_chain_group selection3 = {.chains = chains3, .n = 1};
s2 = freesasa_structure_get_chains_lcl(s, &selection3, NULL, 0);
ck_assert(freesasa_structure_n(s2) == 129);
ck_assert(freesasa_structure_atom_chain(s2, 0) == 'A');
ck_assert_str_eq(freesasa_structure_chain_labels(s2), "A");
freesasa_structure_free(s2);

const char *chains4[] = {"D"};
freesasa_chain_list selection4 = {.chains = chains4, .n = 1};
freesasa_chain_group selection4 = {.chains = chains4, .n = 1};
s2 = freesasa_structure_get_chains_lcl(s, &selection4, NULL, 0);
ck_assert(freesasa_structure_n(s2) == 129);
ck_assert(freesasa_structure_atom_chain(s2, 0) == 'D');
ck_assert_str_eq(freesasa_structure_chain_labels(s2), "D");
freesasa_structure_free(s2);

const char *chains5[] = {"A", "C"};
freesasa_chain_list selection5 = {.chains = chains5, .n = 2};
freesasa_chain_group selection5 = {.chains = chains5, .n = 2};
s2 = freesasa_structure_get_chains_lcl(s, &selection5, NULL, 0);
ck_assert(freesasa_structure_n(s2) == 2 * 129);
ck_assert(freesasa_structure_atom_chain(s2, 0) == 'A');
Expand All @@ -525,12 +525,12 @@ START_TEST(test_get_chains_lcl)
freesasa_structure_free(s2);

const char *chains6[] = {"E"};
freesasa_chain_list selection6 = {.chains = chains6, .n = 1};
freesasa_chain_group selection6 = {.chains = chains6, .n = 1};
s2 = freesasa_structure_get_chains_lcl(s, &selection6, NULL, 0);
ck_assert_ptr_eq(s2, NULL);

const char *chains7[] = {"A", "E"};
freesasa_chain_list selection7 = {.chains = chains7, .n = 2};
freesasa_chain_group selection7 = {.chains = chains7, .n = 2};
s2 = freesasa_structure_get_chains_lcl(s, &selection7, NULL, 0);
ck_assert_ptr_eq(s2, NULL);

Expand Down

0 comments on commit a204a1d

Please sign in to comment.