/** Removes comments and strips leading and trailing whitespace. Returns the length of the stripped line on success, FREESASA_FAIL if malloc/realloc fails. */ int strip_line(char **line, const char *input) { char *linebuf = malloc(strlen(input)+1), *line_bkp, *comment, *first, *last; if (linebuf == NULL) return mem_fail(); strcpy(linebuf,input); comment = strchr(linebuf,'#'); if (comment) *comment = '\0'; // skip comments first = linebuf; last = linebuf + strlen(linebuf) - 1; while (*first == ' ' || *first == '\t') ++first; if (last > first) while (*last == ' ' || *last == '\t' || *last == '\n') --last; line_bkp = *line; *line = realloc(*line,strlen(first)+1); if (*line == NULL) { free(linebuf); free(line_bkp); return mem_fail(); } if (first >= last) { **line = '\0'; free(linebuf); return 0; } *(last+1) = '\0'; strcpy(*line,first); free(linebuf); return strlen(*line); }
static expression * expression_new() { struct expression *e = malloc(sizeof(expression)); if (e == NULL) mem_fail(); else { e->type = E_SELECTION; e->left = NULL; e->right = NULL; e->value = NULL; } return e; }
static struct selection * selection_new(int n) { struct selection *selection = malloc(sizeof(struct selection)); int i; if (selection == NULL) { mem_fail(); } else { selection->size = n; selection->atom = malloc(sizeof(int)*n); if (selection->atom == NULL) { free(selection); mem_fail(); selection = NULL; } else { for (i = 0; i < n; ++i) selection->atom[i] = 0; } } return selection; }
/** Add atom to residue. Returns index of the new atom on success. FREESASA_FAIL if memory allocation fails. FREESASA_WARN if the atom has already been added. */ static int add_atom(struct classifier_residue *res, const char *name, double radius, int the_class) { int n; char **an = res->atom_name; double *ar = res->atom_radius; int *ac = res->atom_class; if (find_string(res->atom_name, name, res->n_atoms) >= 0) return freesasa_warn("in %s(): Ignoring duplicate entry for atom '%s %s'", __func__, res->name, name); n = res->n_atoms+1; if ((res->atom_name = realloc(res->atom_name,sizeof(char*)*n)) == NULL) { res->atom_name = an; return mem_fail(); } if ((res->atom_radius = realloc(res->atom_radius,sizeof(double)*n)) == NULL) { res->atom_radius = ar; return mem_fail(); } if ((res->atom_class = realloc(res->atom_class,sizeof(int)*n)) == NULL) { res->atom_class = ac; return mem_fail(); } if ((res->atom_name[n-1] = strdup(name)) == NULL) return mem_fail(); ++res->n_atoms; res->atom_radius[n-1] = radius; res->atom_class[n-1] = the_class; return n-1; }
static freesasa_classifier* init_classifier(struct classifier_config *config) { freesasa_classifier* c = malloc(sizeof(freesasa_classifier)); if (c == NULL) { mem_fail(); return NULL; } c->config = config; c->n_classes = config->n_classes; c->radius = freesasa_classifier_config_radius; c->sasa_class = freesasa_classifier_config_class; c->class2str = freesasa_classifier_config_class2str; c->free_config = classifier_config_free; return c; }
int init_sr(sr_data *sr, double *sasa, const coord_t *xyz, const double *r, double probe_radius, int n_points) { int n_atoms = freesasa_coord_n(xyz); coord_t *srp = test_points(n_points); if (srp == NULL) return fail_msg("Failed to initialize test points."); //store parameters and reference arrays sr->n_atoms = n_atoms; sr->n_points = n_points; sr->probe_radius = probe_radius; sr->xyz = xyz; sr->srp = srp; sr->sasa = sasa; sr->nb = NULL; sr->r = malloc(sizeof(double)*n_atoms); sr->r2 = malloc(sizeof(double)*n_atoms); if (sr->r == NULL || sr->r2 == NULL) goto cleanup; for (int i = 0; i < n_atoms; ++i) { double ri = r[i] + probe_radius; sr->r[i] = ri; sr->r2[i] = ri * ri; } //calculate distances sr->nb = freesasa_nb_new(xyz, sr->r); if (sr->nb == NULL) goto cleanup; return FREESASA_SUCCESS; cleanup: release_sr(sr); return mem_fail(); }
int freesasa_shrake_rupley(double *sasa, const coord_t *xyz, const double *r, double probe_radius, int n_points, int n_threads) { assert(sasa); assert(xyz); assert(r); assert(n_threads > 0); int n_atoms = freesasa_coord_n(xyz); int return_value = FREESASA_SUCCESS; sr_data sr; if (n_atoms == 0) return freesasa_warn("%s(): empty coordinates", __func__); if (init_sr(&sr,sasa,xyz,r,probe_radius,n_points)) return mem_fail(); //calculate SASA if (n_threads > 1) { #if USE_THREADS sr_do_threads(n_threads, sr); #else return_value = freesasa_warn("%s: program compiled for single-threaded use, " "but multiple threads were requested. Will " "proceed in single-threaded mode.\n", __func__); n_threads = 1; #endif } if (n_threads == 1) { // don't want the overhead of generating threads if only one is used for (int i = 0; i < n_atoms; ++i) { sasa[i] = sr_atom_area(i,sr); } } release_sr(sr); return return_value; }
expression * freesasa_selection_create(expression *selection, const char* id) { expression *e = expression_new(); assert(id); if (e == NULL) expression_free(selection); else { e->type = E_SELECTION; e->left = selection; e->value = strdup(id); if (e->value == NULL) { mem_fail(); expression_free(e); e = NULL; } } return e; }
static xmlNodePtr atom2xml(const freesasa_node *node, int options) { xmlNodePtr xml_node = NULL; const freesasa_nodearea *area = freesasa_node_area(node); const char *name = freesasa_node_name(node); char *trim_name, buf[20]; assert(node); area = freesasa_node_area(node); name = freesasa_node_name(node); trim_name = malloc(strlen(name) + 1); if (!trim_name) { mem_fail(); goto cleanup; } sscanf(name, "%s", trim_name); xml_node = xmlNewNode(NULL, BAD_CAST "atom"); if (xml_node == NULL) { fail_msg(""); return NULL; } if (xmlNewProp(xml_node, BAD_CAST "name", BAD_CAST trim_name) == NULL) { fail_msg(""); goto cleanup; } sprintf(buf, "%.3f", area->total); if (xmlNewProp(xml_node, BAD_CAST "area", BAD_CAST buf) == NULL) { fail_msg(""); goto cleanup; } sprintf(buf, "%s", freesasa_node_atom_is_polar(node) == FREESASA_ATOM_POLAR ? "yes" : "no"); if (xmlNewProp(xml_node, BAD_CAST "isPolar", BAD_CAST buf) == NULL) { fail_msg(""); goto cleanup; } sprintf(buf, "%s", freesasa_atom_is_backbone(name) ? "yes" : "no"); if (xmlNewProp(xml_node, BAD_CAST "isMainChain", BAD_CAST buf) == NULL) { fail_msg(""); goto cleanup; } sprintf(buf, "%.3f", freesasa_node_atom_radius(node)); if (xmlNewProp(xml_node, BAD_CAST "radius", BAD_CAST buf) == NULL) { fail_msg(""); goto cleanup; } free(trim_name); return xml_node; cleanup: free(trim_name); xmlFreeNode(xml_node); return NULL; }
static xmlNodePtr residue2xml(const freesasa_node *node, int options) { xmlNodePtr xml_node = NULL, xml_area = NULL, xml_relarea = NULL; const char *name, *number; const freesasa_nodearea *abs, *reference; char *trim_number, *trim_name; freesasa_nodearea rel; assert(node); name = freesasa_node_name(node); number = freesasa_node_residue_number(node); abs = freesasa_node_area(node); reference = freesasa_node_residue_reference(node); trim_number = malloc(strlen(number) + 1); trim_name = malloc(strlen(name) + 1); if (!trim_number || ! trim_name) { mem_fail(); goto cleanup; } sscanf(number, "%s", trim_number); sscanf(name, "%s", trim_name); xml_node = xmlNewNode(NULL, BAD_CAST "residue"); if (xml_node == NULL) { fail_msg(""); return NULL; } if (xmlNewProp(xml_node, BAD_CAST "name", BAD_CAST trim_name) == NULL || xmlNewProp(xml_node, BAD_CAST "number", BAD_CAST trim_number) == NULL) { fail_msg(""); goto cleanup; } xml_area = nodearea2xml(abs, "area"); if (xml_area == NULL) { fail_msg(""); goto cleanup; } if (xmlAddChild(xml_node, xml_area) == NULL) { xmlFreeNode(xml_area); fail_msg(""); goto cleanup; } if ((reference != NULL) && !(options & FREESASA_OUTPUT_SKIP_REL)) { freesasa_residue_rel_nodearea(&rel, abs, reference); xml_relarea = nodearea2xml(&rel, "relativeArea"); if (xml_relarea == NULL) { fail_msg(""); goto cleanup; } if (xmlAddChild(xml_node, xml_relarea) == NULL) { xmlFreeNode(xml_relarea); fail_msg(""); goto cleanup; } } free(trim_name); free(trim_number); return xml_node; cleanup: free(trim_name); free(trim_number); xmlFreeNode(xml_node); return NULL; }