コード例 #1
0
struct atomgrp* join_atomgrps (struct atomgrp** ags)
{
	struct atomgrp* ag = (struct atomgrp*) _mol_calloc (1, sizeof (struct atomgrp));
	ag->natoms = 100; // just a guess, realloc as necessary
	ag->atoms = (struct atom*) _mol_malloc (sizeof (struct atom) * ag->natoms);

	int agai = 0; // ag atom index
	int agi = 0; // index into ags
	while (ags[agi] != NULL)
	{
		int agsai; // ags atom index
		for (agsai = 0; agsai < ags[agi]->natoms; agsai++, agai++)
		{
			if (agai+1 > ag->natoms)
			{
				ag->natoms *= 2;
				ag->atoms = (struct atom*) _mol_realloc (ag->atoms, sizeof (struct atom) * ag->natoms);
			}

			copy_atom (&ags[agi]->atoms[agsai], &ag->atoms[agai]);
		}

		agi++;
	}


	// final realloc of the arrays to make them tight
	ag->natoms = agai;
	ag->atoms = (struct atom*) _mol_realloc (ag->atoms, sizeof (struct atom) * ag->natoms);

	return ag;
}
コード例 #2
0
ファイル: sasa.cpp プロジェクト: ampereira/F2Dock
int* read_sasa (const char* path)
{
	FILE* fp = myfopen (path, "r");

	int nsasas = 100; // just a guess, realloc as necessary
	int* sasas = (int*) _mol_malloc (sizeof (int) * nsasas);

	char* line = NULL;
	size_t len = 0;


	int i = 0;
	while (getline (&line, &len, fp) != -1)
	{
		if (i+1 > nsasas)
		{
			nsasas *= 2;
			sasas = (int*) _mol_realloc (sasas, sizeof (int) * nsasas);
		}

		if (sscanf (line, "%d", &sasas[i]) < 1)
		{
			fprintf (stderr, "error: in file %s line %s: incorrect sasa line\n", path, line);
			exit (EXIT_FAILURE);
		}
		if (sasas[i] != 0 && sasas[i] != 1)
		{
			fprintf (stderr, "error: in file %s line %s integer should be 0 or 1\n", path, line);
			exit (EXIT_FAILURE);
		}

		i++;
	}
	if (line)
		free (line);
	myfclose (fp);

	// final realloc of the arrays to make them tight
	nsasas = i;
	sasas = (int*) _mol_realloc (sasas, sizeof (int) * (nsasas+1)); // one extra for -1
	sasas[nsasas] = -1;

	return sasas;
}
コード例 #3
0
struct atomgrp* exrm_type (struct atomgrp* ag, const char* type, struct prm* prm, int direction)
{
	int atomn; // loop var
	int natoms; // type atom number

	struct atomgrp* exag = (struct atomgrp*) _mol_calloc (1, sizeof (struct atomgrp)); // resultant extracted atomgrp
	exag->atoms = (struct atom*) _mol_malloc (sizeof (struct atom) * ag->natoms); // allocate memory for the array of atoms, realloc later to make it tight

	natoms = 0;
	for (atomn = 0; atomn < ag->natoms; atomn++)
	{
		if (
				(direction == 0 && strcmp (type, prm->atoms[ag->atoms[atomn].atom_typen].typemin) == 0) // extract type
				||
				(direction == 1 && ! (strcmp (type, prm->atoms[ag->atoms[atomn].atom_typen].typemin) == 0)) // rm type
		   )
		{
			exag->atoms[natoms].atom_typen = ag->atoms[atomn].atom_typen;
			exag->atoms[natoms].sa = ag->atoms[atomn].sa;
			exag->atoms[natoms].X = ag->atoms[atomn].X;
			exag->atoms[natoms].Y = ag->atoms[atomn].Y;
			exag->atoms[natoms].Z = ag->atoms[atomn].Z;
			/*
			exag->atoms[natoms].bonds[0] = ag->atoms[atomn].bonds[0];
			exag->atoms[natoms].bonds[1] = ag->atoms[atomn].bonds[1];
			exag->atoms[natoms].bonds[2] = ag->atoms[atomn].bonds[2];
			exag->atoms[natoms].bonds[3] = ag->atoms[atomn].bonds[3];
			*/
			natoms++;
		}
	}

	if (natoms == 0)
	{
		if (direction == 0)
		{
			fprintf (stderr, "there are no atoms of type %s to extract\n", type);
		}
		if (direction == 1)
		{
			fprintf (stderr, "removing atoms of type %s leaves no remaining atoms\n", type);
		}
		exit (EXIT_FAILURE);
	}

	exag->atoms = (struct atom*) _mol_realloc (exag->atoms, sizeof (struct atom) * natoms); // realloc exag->atoms to make it tight
	exag->natoms = natoms; // attach number of atoms

	return exag;
}
コード例 #4
0
ファイル: version.cpp プロジェクト: ampereira/F2Dock
void
mol_svn_version (char** lineptr, size_t *n)
{
    size_t length = strlen(_SVN_VERSION_);

    if (*lineptr == NULL) {
        *lineptr = (char*)_mol_malloc( sizeof(char) * length );
        *n = length;
    } else if( *n < length) {
        *lineptr = (char*)_mol_realloc(*lineptr, sizeof(char) * length );
        *n = length;
    }

	sprintf(*lineptr, "%s", _SVN_VERSION_);
}
コード例 #5
0
struct atomgrp *read_json_ag(const char *json_file)
{
	struct atomgrp *ag =
	    (struct atomgrp *)_mol_calloc(1, sizeof(struct atomgrp));
	json_error_t json_file_error;
	json_t *base = json_load_file(json_file, 0, &json_file_error);

	if (!base) {
		fprintf(stderr,
			"error reading json file %s on line %d column %d: %s\n",
			json_file, json_file_error.line, json_file_error.column,
			json_file_error.text);
	}

	if (!json_is_object(base)) {
		fprintf(stderr, "json file not an object %s\n", json_file);
	}

	json_t *atoms, *bonds, *angles, *torsions, *impropers;
	atoms = json_object_get(base, "atoms");
	if (!json_is_array(atoms)) {
		fprintf(stderr, "json atoms are not an array %s\n", json_file);
	}
	size_t natoms = json_array_size(atoms);
	ag->natoms = natoms;
	ag->atoms = (struct atom *)_mol_calloc(ag->natoms, sizeof(struct atom));

	ag->num_atom_types = 0;

	char *prev_segment = _mol_calloc(1, sizeof(char));
	char *prev_residue = _mol_calloc(1, sizeof(char));
	int prev_residue_seq = -107;
	ag->nres = 0;
	int alloc_res = 250;
	ag->iares = _mol_malloc(alloc_res*sizeof(int));
	for (size_t i = 0; i < natoms; i++) {
		json_t *atom = json_array_get(atoms, i);
		if (!json_is_object(atom)) {
			fprintf(stderr,
				"Atom %zd not an object in json file %s\n", i,
				json_file);
		}
		json_t *ace_volume, *ftype_index, *ftype_name, *eps03;
		json_t *name, *radius03, *eps, *acp_type, *residue_name;
		json_t *charge, *radius, *element;
		json_t *x, *y, *z;
		json_t *yeti_type, *sybyl_type;
		json_t *backbone, *hb_acceptor, *hb_donor, *hb_weight;
		json_t *segment, *residue;


		segment = json_object_get(atom, "segment");
		residue = json_object_get(atom, "residue");
		if ((segment != NULL) && (residue != NULL)) {
			if (!json_is_string(segment)) {
				fprintf(stderr,
					"json segment is not string for atom %zd in json_file %s\n",
					i, json_file);
			}
			if (!json_is_string(residue)) {
				fprintf(stderr,
					"json residue is not string for atom %zd in json_file %s\n",
					i, json_file);
			}

			const char *cur_segment = json_string_value(segment);
			const char *cur_residue = json_string_value(residue);

			if (strcmp(cur_segment, prev_segment) != 0) {
				prev_residue_seq += 100;
				free(prev_segment);
				prev_segment = strdup(cur_segment);
			}
			if (strcmp(cur_residue, prev_residue) != 0) {
				int cur_residue_int = atoi(cur_residue);
				int prev_residue_int = atoi(prev_residue);
				if ((cur_residue_int - prev_residue_int) > 1) {
					prev_residue_seq +=
					    (cur_residue_int -
					     prev_residue_int);
				} else {
					prev_residue_seq += 1;
				}
				free(prev_residue);
				prev_residue = strdup(cur_residue);

				if (ag->nres +1 == alloc_res) {
					alloc_res *= 2;
					ag->iares = _mol_realloc(ag->iares, alloc_res * i);
				}
				ag->iares[ag->nres] = i;
				ag->nres++;
			}


			ag->atoms[i].comb_res_seq = prev_residue_seq;
		} else {
			ag->atoms[i].comb_res_seq = prev_residue_seq;
		}

		ace_volume = json_object_get(atom, "ace_volume");
		if (!json_is_real(ace_volume)) {
			fprintf(stderr,
				"json ace volume is not floating point for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].acevolume = json_real_value(ace_volume);

		ftype_index = json_object_get(atom, "ftype_index");
		if (!json_is_integer(ftype_index)) {
			fprintf(stderr,
				"json ftype index is not integer for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].atom_ftypen = json_integer_value(ftype_index);
		if (ag->atoms[i].atom_ftypen > ag->num_atom_types) {
			ag->num_atom_types = ag->atoms[i].atom_ftypen;
		}

		ftype_name = json_object_get(atom, "ftype_name");
		if (!json_is_string(ftype_name)) {
			fprintf(stderr,
				"json ftype name is not string for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].ftype_name = strdup(json_string_value(ftype_name));

		element = json_object_get(atom, "element");
		if (!json_is_string(element)) {
			fprintf(stderr,
				"json element name is not string for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].element = strdup(json_string_value(element));

		eps = json_object_get(atom, "eps");
		if (!json_is_real(eps)) {
			fprintf(stderr,
				"json eps is not floating point for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].eps = sqrt(-json_real_value(eps));

		eps03 = json_object_get(atom, "eps03");
		if (!json_is_real(eps03)) {
			fprintf(stderr,
				"json eps03 is not floating point for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].eps03 = sqrt(-json_real_value(eps03));

		radius = json_object_get(atom, "radius");
		if (!json_is_real(radius)) {
			fprintf(stderr,
				"json radius is not floating point for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].rminh = json_real_value(radius);

		radius03 = json_object_get(atom, "radius03");
		if (!json_is_real(radius03)) {
			fprintf(stderr,
				"json radius03 is not floating point for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].rminh03 = json_real_value(radius03);

		charge = json_object_get(atom, "charge");
		if (!json_is_real(radius03)) {
			fprintf(stderr,
				"json charge is not floating point for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].chrg = json_real_value(charge);

		name = json_object_get(atom, "name");
		if (!json_is_string(name)) {
			fprintf(stderr,
				"json name is not string for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].name = strdup(json_string_value(name));

		residue_name = json_object_get(atom, "residue_name");
		if (residue_name != NULL) {
			if (!json_is_string(residue_name)) {
				fprintf(stderr,
					"json residue_name is not string for atom %zd in json_file %s\n",
					i, json_file);
			}
			ag->atoms[i].residue_name =
			    strdup(json_string_value(residue_name));
		} else {
			ag->atoms[i].residue_name = NULL;
		}

		x = json_object_get(atom, "x");
		if (!json_is_real(x)) {
			fprintf(stderr,
				"json coordinate x is not floating point for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].X = json_real_value(x);

		y = json_object_get(atom, "y");
		if (!json_is_real(y)) {
			fprintf(stderr,
				"json coordinate y is not floating point for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].Y = json_real_value(y);

		z = json_object_get(atom, "z");
		if (!json_is_real(z)) {
			fprintf(stderr,
				"json coordinate z is not floating point for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].Z = json_real_value(z);

		acp_type = json_object_get(atom, "acp_type");
		if (acp_type != NULL) {
			if (!json_is_integer(acp_type)) {
				fprintf(stderr,
					"json acp_type index is not integer for atom %zd in json_file %s\n",
					i, json_file);
			}
			ag->atoms[i].acp_type = json_integer_value(acp_type);
		} else {
			ag->atoms[i].acp_type = -1;
		}

		yeti_type = json_object_get(atom, "yeti_type");
		if (yeti_type != NULL) {
			const char *yeti_type_string;
			if (!json_is_string(yeti_type)) {
				fprintf(stderr,
					"json yeti_type is not string for atom %zd in json_file %s\n",
					i, json_file);
			}
			yeti_type_string = json_string_value(yeti_type);
			if (strcmp("carbonyl", yeti_type_string) == 0) {
				ag->atoms[i].yeti_type = MOL_YETI_CARBONYL;
			} else if (strcmp("hydroxyl", yeti_type_string) == 0) {
				ag->atoms[i].yeti_type = MOL_YETI_HYDROXYL;
			} else if (strcmp("sulfonamide", yeti_type_string) == 0) {
				ag->atoms[i].yeti_type = MOL_YETI_SULFONAMIDE;
			} else if (strcmp("N5_aromatic", yeti_type_string) == 0) {
				ag->atoms[i].yeti_type = MOL_YETI_N5_AROMATIC;
			} else if (strcmp("N6_aromatic", yeti_type_string) == 0) {
				ag->atoms[i].yeti_type = MOL_YETI_N6_AROMATIC;
			} else {
				fprintf(stderr,
					"unknown json yeti_type %s for atom %zd in json_file %s\n",
					yeti_type_string, i, json_file);
				ag->atoms[i].yeti_type = MOL_YETI_NONE;
			}
		} else {
			ag->atoms[i].yeti_type = MOL_YETI_NONE;
		}

		sybyl_type = json_object_get(atom, "sybyl_type");
		if (sybyl_type != NULL) {
			const char *sybyl_type_string;
			if (!json_is_string(sybyl_type)) {
				fprintf(stderr,
					"json sybyl_type is not string for atom %zd in json_file %s\n",
					i, json_file);
			}
			sybyl_type_string = json_string_value(sybyl_type);
			ag->atoms[i].hybridization =
			    mol_hydridization_from_sybyl(sybyl_type_string);
		} else {
			ag->atoms[i].hybridization = UNKNOWN_HYBRID;
		}

		backbone = json_object_get(atom, "backbone");
		if (backbone != NULL) {
			if (!json_is_boolean(backbone)) {
				fprintf(stderr,
					"json backbone is not boolean for atom %zd in json_file %s\n",
					i, json_file);
			}
			ag->atoms[i].backbone = json_is_true(backbone);
		} else {
			ag->atoms[i].backbone = false;
		}

		ag->atoms[i].hprop = 0;
		hb_acceptor = json_object_get(atom, "hb_acceptor");
		if (hb_acceptor != NULL) {
			if (!json_is_boolean(hb_acceptor)) {
				fprintf(stderr,
					"json hb_acceptor is not boolean for atom %zd in json_file %s\n",
					i, json_file);
			}
			if (json_is_true(hb_acceptor)) {
				ag->atoms[i].hprop |= HBOND_ACCEPTOR;
			}
		}
		hb_donor = json_object_get(atom, "hb_donor");
		if (hb_donor != NULL) {
			if (!json_is_boolean(hb_donor)) {
				fprintf(stderr,
					"json hb_donor is not boolean for atom %zd in json_file %s\n",
					i, json_file);
			}
			if (json_is_true(hb_donor)) {
				ag->atoms[i].hprop |= DONATABLE_HYDROGEN;
			}
		}
		ag->atoms[i].hbond_weight = 1.0;
		hb_weight = json_object_get(atom, "hb_weight");
		if (hb_weight != NULL) {
			if (!json_is_real(hb_weight)) {
				fprintf(stderr,
					"json hb_weight is not floating point for atom %zd in json_file %s\n",
					i, json_file);
			}
			ag->atoms[i].hbond_weight = json_real_value(hb_weight);
		}

		ag->atoms[i].nbonds = 0;
		ag->atoms[i].nangs = 0;
		ag->atoms[i].ntors = 0;
		ag->atoms[i].nimps = 0;
		ag->atoms[i].fixed = 0;
		ag->atoms[i].sa = -1;
		ag->atoms[i].base = -1;
		ag->atoms[i].base2 = -1;
	}
	ag->iares[ag->nres] = ag->natoms;
	ag->iares = _mol_realloc(ag->iares, (ag->nres+1) * sizeof(int));
	free(prev_segment);
	free(prev_residue);

	bonds = json_object_get(base, "bonds");
	if (!json_is_array(bonds)) {
		fprintf(stderr, "json bonds are not an array %s\n", json_file);
	}
	size_t nbonds = json_array_size(bonds);
	ag->nbonds = nbonds;
	ag->bonds = _mol_calloc(nbonds, sizeof(struct atombond));
	for (size_t i = 0; i < nbonds; i++) {
		json_t *bond = json_array_get(bonds, i);
		if (!json_is_object(bond)) {
			fprintf(stderr,
				"Bond %zd not an object in json file %s\n", i,
				json_file);
		}
		json_t *length, *atom1, *atom2, *spring_constant, *sdf_type;

		atom1 = json_object_get(bond, "atom1");
		if (!json_is_integer(atom1)) {
			fprintf(stderr,
				"json atom1 is not integer for bond %zd in json_file %s\n",
				i, json_file);
		}
		int i1 = json_integer_value(atom1) - 1;
		ag->bonds[i].a0 = &(ag->atoms[i1]);
		(ag->atoms[i1].nbonds)++;

		atom2 = json_object_get(bond, "atom2");
		if (!json_is_integer(atom2)) {
			fprintf(stderr,
				"json atom2 is not integer for bond %zd in json_file %s\n",
				i, json_file);
		}
		int i2 = json_integer_value(atom2) - 1;
		ag->bonds[i].a1 = &(ag->atoms[i2]);
		(ag->atoms[i2].nbonds)++;

		length = json_object_get(bond, "length");
		if (!json_is_real(length)) {
			fprintf(stderr,
				"json length is not floating point for bond %zd in json_file %s\n",
				i, json_file);
		}
		ag->bonds[i].l0 = json_real_value(length);

		spring_constant = json_object_get(bond, "spring_constant");
		if (!json_is_real(spring_constant)) {
			fprintf(stderr,
				"json spring_constant is not floating point for bond %zd in json_file %s\n",
				i, json_file);
		}
		ag->bonds[i].k = json_real_value(spring_constant);

		sdf_type = json_object_get(bond, "sdf_type");
		if (sdf_type != NULL) {
			if (!json_is_integer(sdf_type)) {
				fprintf(stderr,
					"json sdf_type is not integer for bond %zd in json_file %s\n",
					i, json_file);
			}
			ag->bonds[i].sdf_type = json_integer_value(sdf_type);
		} else {
			ag->bonds[i].sdf_type = 0;
		}
	}

	angles = json_object_get(base, "angles");
	if (!json_is_array(angles)) {
		fprintf(stderr, "json angles are not an array %s\n", json_file);
	}
	size_t nangles = json_array_size(angles);
	ag->nangs = nangles;
	ag->angs = _mol_calloc(nangles, sizeof(struct atomangle));
	for (size_t i = 0; i < nangles; i++) {
		json_t *angle = json_array_get(angles, i);
		if (!json_is_object(angle)) {
			fprintf(stderr,
				"Angle %zd not an object in json file %s\n", i,
				json_file);
		}
		json_t *theta, *atom1, *atom2, *atom3, *spring_constant;

		atom1 = json_object_get(angle, "atom1");
		if (!json_is_integer(atom1)) {
			fprintf(stderr,
				"json atom1 is not integer for angle %zd in json_file %s\n",
				i, json_file);
		}
		int i1 = json_integer_value(atom1) - 1;
		ag->angs[i].a0 = &(ag->atoms[i1]);
		(ag->atoms[i1].nangs)++;

		atom2 = json_object_get(angle, "atom2");
		if (!json_is_integer(atom2)) {
			fprintf(stderr,
				"json atom2 is not integer for angle %zd in json_file %s\n",
				i, json_file);
		}
		int i2 = json_integer_value(atom2) - 1;
		ag->angs[i].a1 = &(ag->atoms[i2]);
		(ag->atoms[i2].nangs)++;

		atom3 = json_object_get(angle, "atom3");
		if (!json_is_integer(atom3)) {
			fprintf(stderr,
				"json atom3 is not integer for angle %zd in json_file %s\n",
				i, json_file);
		}
		int i3 = json_integer_value(atom3) - 1;
		ag->angs[i].a2 = &(ag->atoms[i3]);
		(ag->atoms[i3].nangs)++;

		theta = json_object_get(angle, "theta");
		if (!json_is_real(theta)) {
			fprintf(stderr,
				"json theta is not floating point for angle %zd in json_file %s\n",
				i, json_file);
		}
		ag->angs[i].th0 = json_real_value(theta);

		spring_constant = json_object_get(angle, "spring_constant");
		if (!json_is_real(spring_constant)) {
			fprintf(stderr,
				"json spring_constant is not floating point for angle %zd in json_file %s\n",
				i, json_file);
		}
		ag->angs[i].k = json_real_value(spring_constant);
	}

	torsions = json_object_get(base, "torsions");
	if (!json_is_array(torsions)) {
		fprintf(stderr, "json torsions are not an array %s\n",
			json_file);
	}
	size_t ntorsions = json_array_size(torsions);
	ag->ntors = ntorsions;
	ag->tors = _mol_calloc(ntorsions, sizeof(struct atomtorsion));
	for (size_t i = 0; i < ntorsions; i++) {
		json_t *torsion = json_array_get(torsions, i);
		if (!json_is_object(torsion)) {
			fprintf(stderr,
				"Torsion %zd not an object in json file %s\n",
				i, json_file);
		}
		json_t *atom1, *atom2, *atom3, *atom4, *minima, *delta_constant,
		    *spring_constant;

		atom1 = json_object_get(torsion, "atom1");
		if (!json_is_integer(atom1)) {
			fprintf(stderr,
				"json atom1 is not integer for torsion %zd in json_file %s\n",
				i, json_file);
		}
		int i1 = json_integer_value(atom1) - 1;
		ag->tors[i].a0 = &(ag->atoms[i1]);
		(ag->atoms[i1].ntors)++;

		atom2 = json_object_get(torsion, "atom2");
		if (!json_is_integer(atom2)) {
			fprintf(stderr,
				"json atom2 is not integer for torsion %zd in json_file %s\n",
				i, json_file);
		}
		int i2 = json_integer_value(atom2) - 1;
		ag->tors[i].a1 = &(ag->atoms[i2]);
		(ag->atoms[i2].ntors)++;

		atom3 = json_object_get(torsion, "atom3");
		if (!json_is_integer(atom3)) {
			fprintf(stderr,
				"json atom3 is not integer for torsion %zd in json_file %s\n",
				i, json_file);
		}
		int i3 = json_integer_value(atom3) - 1;
		ag->tors[i].a2 = &(ag->atoms[i3]);
		(ag->atoms[i3].ntors)++;

		atom4 = json_object_get(torsion, "atom4");
		if (!json_is_integer(atom4)) {
			fprintf(stderr,
				"json atom4 is not integer for torsion %zd in json_file %s\n",
				i, json_file);
		}
		int i4 = json_integer_value(atom4) - 1;
		ag->tors[i].a3 = &(ag->atoms[i4]);
		(ag->atoms[i4].ntors)++;

		minima = json_object_get(torsion, "minima");
		if (!json_is_integer(minima)) {
			fprintf(stderr,
				"json minima is not integer for torsion %zd in json_file %s\n",
				i, json_file);
		}
		ag->tors[i].n = json_integer_value(minima);

		delta_constant = json_object_get(torsion, "delta_constant");
		if (!json_is_real(delta_constant)) {
			fprintf(stderr,
				"json delta_constant is not floating point for torsion %zd in json_file %s\n",
				i, json_file);
		}
		ag->tors[i].d = json_real_value(delta_constant);

		spring_constant = json_object_get(torsion, "spring_constant");
		if (!json_is_real(spring_constant)) {
			fprintf(stderr,
				"json spring_constant is not floating point for torsion %zd in json_file %s\n",
				i, json_file);
		}
		ag->tors[i].k = json_real_value(spring_constant);
	}

	impropers = json_object_get(base, "impropers");
	if (!json_is_array(impropers)) {
		fprintf(stderr, "json impropers are not an array %s\n",
			json_file);
	}
	size_t nimpropers = json_array_size(impropers);
	ag->nimps = nimpropers;
	ag->imps = _mol_calloc(nimpropers, sizeof(struct atomimproper));
	for (size_t i = 0; i < nimpropers; i++) {
		json_t *improper = json_array_get(impropers, i);
		if (!json_is_object(improper)) {
			fprintf(stderr,
				"Improper %zd not an object in json file %s\n",
				i, json_file);
		}
		json_t *atom1, *atom2, *atom3, *atom4, *phi, *spring_constant;

		atom1 = json_object_get(improper, "atom1");
		if (!json_is_integer(atom1)) {
			fprintf(stderr,
				"json atom1 is not integer for improper %zd in json_file %s\n",
				i, json_file);
		}
		int i1 = json_integer_value(atom1) - 1;
		ag->imps[i].a0 = &(ag->atoms[i1]);
		(ag->atoms[i1].nimps)++;

		atom2 = json_object_get(improper, "atom2");
		if (!json_is_integer(atom2)) {
			fprintf(stderr,
				"json atom2 is not integer for improper %zd in json_file %s\n",
				i, json_file);
		}
		int i2 = json_integer_value(atom2) - 1;
		ag->imps[i].a1 = &(ag->atoms[i2]);
		(ag->atoms[i2].nimps)++;

		atom3 = json_object_get(improper, "atom3");
		if (!json_is_integer(atom3)) {
			fprintf(stderr,
				"json atom3 is not integer for improper %zd in json_file %s\n",
				i, json_file);
		}
		int i3 = json_integer_value(atom3) - 1;
		ag->imps[i].a2 = &(ag->atoms[i3]);
		(ag->atoms[i3].nimps)++;

		atom4 = json_object_get(improper, "atom4");
		if (!json_is_integer(atom4)) {
			fprintf(stderr,
				"json atom4 is not integer for improper %zd in json_file %s\n",
				i, json_file);
		}
		int i4 = json_integer_value(atom4) - 1;
		ag->imps[i].a3 = &(ag->atoms[i4]);
		(ag->atoms[i4].nimps)++;

		phi = json_object_get(improper, "phi");
		if (!json_is_real(phi)) {
			fprintf(stderr,
				"json phi is not floating point for improper %zd in json_file %s\n",
				i, json_file);
		}
		ag->imps[i].psi0 = json_real_value(phi);

		spring_constant = json_object_get(improper, "spring_constant");
		if (!json_is_real(spring_constant)) {
			fprintf(stderr,
				"json spring_constant is not floating point for improper %zd in json_file %s\n",
				i, json_file);
		}
		ag->imps[i].k = json_real_value(spring_constant);
	}

	json_decref(base);

//allocate atom arrays of pointers to parameters
	for (size_t i = 0; i < natoms; i++) {
		int i1 = ag->atoms[i].nbonds;
		ag->atoms[i].bonds = _mol_calloc(i1, sizeof(struct atombond *));
		ag->atoms[i].nbonds = 0;
		i1 = ag->atoms[i].nangs;
		ag->atoms[i].angs = _mol_calloc(i1, sizeof(struct atomangle *));
		ag->atoms[i].nangs = 0;
		i1 = ag->atoms[i].ntors;
		ag->atoms[i].tors =
		    _mol_calloc(i1, sizeof(struct atomtorsion *));
		ag->atoms[i].ntors = 0;
		i1 = ag->atoms[i].nimps;
		ag->atoms[i].imps =
		    _mol_calloc(i1, sizeof(struct atomimproper *));
		ag->atoms[i].nimps = 0;
	}
	struct atom *atm;
//fill bonds
	for (int i = 0; i < ag->nbonds; i++) {
		atm = ag->bonds[i].a0;
		atm->bonds[(atm->nbonds)++] = &(ag->bonds[i]);
		atm = ag->bonds[i].a1;
		atm->bonds[(atm->nbonds)++] = &(ag->bonds[i]);
	}
//fill angles
	for (int i = 0; i < ag->nangs; i++) {
		atm = ag->angs[i].a0;
		atm->angs[(atm->nangs)++] = &(ag->angs[i]);
		atm = ag->angs[i].a1;
		atm->angs[(atm->nangs)++] = &(ag->angs[i]);
		atm = ag->angs[i].a2;
		atm->angs[(atm->nangs)++] = &(ag->angs[i]);
	}
//fill torsions
	for (int i = 0; i < ag->ntors; i++) {
		atm = ag->tors[i].a0;
		atm->tors[(atm->ntors)++] = &(ag->tors[i]);
		atm = ag->tors[i].a1;
		atm->tors[(atm->ntors)++] = &(ag->tors[i]);
		atm = ag->tors[i].a2;
		atm->tors[(atm->ntors)++] = &(ag->tors[i]);
		atm = ag->tors[i].a3;
		atm->tors[(atm->ntors)++] = &(ag->tors[i]);
	}
//fill impropers
	for (int i = 0; i < ag->nimps; i++) {
		atm = ag->imps[i].a0;
		atm->imps[(atm->nimps)++] = &(ag->imps[i]);
		atm = ag->imps[i].a1;
		atm->imps[(atm->nimps)++] = &(ag->imps[i]);
		atm = ag->imps[i].a2;
		atm->imps[(atm->nimps)++] = &(ag->imps[i]);
		atm = ag->imps[i].a3;
		atm->imps[(atm->nimps)++] = &(ag->imps[i]);
	}
//atom indices in the group
	fill_ingrp(ag);

	ag->is_psf_read = true;

	// copy vals from deprecated to new data structures
	int atomsi;
	for (atomsi = 0; atomsi < ag->natoms; atomsi++) {
		_mol_atom_create_bond_indices(&ag->atoms[atomsi],
					      ag->atoms[atomsi].nbonds);
	}
	_mol_atom_group_copy_from_deprecated(ag);
	return ag;
}
コード例 #6
0
struct atomgrp *read_pdb(const char *path, struct prm *prm)
{
    FILE *fp = myfopen(path, "r");

    struct atomgrp *ag =
        (struct atomgrp *)_mol_calloc(1, sizeof(struct atomgrp));

    char *line = NULL;
    size_t len = 0;

    char atypemaj[5];
    char atypemin[5];

    // read every line of the pdb file
    int atomi = 0;
    ag->natoms = 100;	// just a guess, realloc as necessary
    ag->atoms =
        (struct atom *)_mol_malloc(sizeof(struct atom) * ag->natoms);
    while (getline(&line, &len, fp) != -1) {
        if (strncmp(line, "ATOM  ", 6) != 0)	// check for ATOM line
            continue;

        if (atomi + 1 > ag->natoms) {
            ag->natoms *= 2;
            ag->atoms =
                (struct atom *)_mol_realloc(ag->atoms,
                                            sizeof(struct atom) *
                                            ag->natoms);
        }

        /*
           // init bonds
           ag->atoms[atomi].bonds[0] = -1;
           ag->atoms[atomi].bonds[1] = -1;
           ag->atoms[atomi].bonds[2] = -1;
           ag->atoms[atomi].bonds[3] = -1;
         */

        // init sa
        ag->atoms[atomi].sa = -1;
        // init mask
        ag->atoms[atomi].mask = 0;
        // init attl
        ag->atoms[atomi].attl = 0.0;
        ag->atoms[atomi].nbondis = 0;
        ag->atoms[atomi].nbonds = 0;
        ag->atoms[atomi].nangs = 0;
        ag->atoms[atomi].ntors = 0;
        ag->atoms[atomi].nimps = 0;

        if (sscanf(line, "%*s %*d %4s %4s", atypemin, atypemaj) < 2) {
            fprintf(stderr,
                    "error: in file %s line %s: incorrect atom line\n",
                    path, line);
            exit(EXIT_FAILURE);
        }

        ag->atoms[atomi].atom_typen = atomid(prm, atypemaj, atypemin);
        if (ag->atoms[atomi].atom_typen == -1)	// val not found
        {
            if (atypemin[0] == 'H')	// try one more time for hydrogen
            {
                atypemin[1] = '\0';
                ag->atoms[atomi].atom_typen =
                    atomid(prm, atypemaj, atypemin);
            }
            if (ag->atoms[atomi].atom_typen == -1)	// val still not found
            {
                fprintf(stderr,
                        "error: in file %s line %s: atom type number of %s %s not defined in prm\n",
                        path, line, atypemaj, atypemin);
                exit(EXIT_FAILURE);
            }
        }

        if (!strcmp(atypemin, "C") || !strcmp(atypemin, "CA")
                || !strcmp(atypemin, "N") || !strcmp(atypemin, "O")
                || !strcmp(atypemin, "H"))
            ag->atoms[atomi].backbone = true;
        else
            ag->atoms[atomi].backbone = false;

        sscanf(&line[30], "%8lf%8lf%8lf",
               &ag->atoms[atomi].X, &ag->atoms[atomi].Y,
               &ag->atoms[atomi].Z);
        sscanf(&line[60], "%6lf", &ag->atoms[atomi].B);

        ag->atoms[atomi].icode = line[26];
        line[26] = 0;
        errno = 0;
        ag->atoms[atomi].res_seq = atoi(&line[22]);
        if (errno) {
            perror("atoi");
            exit(EXIT_FAILURE);
        }

        ag->atoms[atomi].base = ag->atoms[atomi].base2 = -1;

        atomi++;
    }
    if (line)
        free(line);
    myfclose(fp);

    // final realloc of the arrays to make them tight
    ag->natoms = atomi;
    ag->atoms =
        (struct atom *)_mol_realloc(ag->atoms,
                                    sizeof(struct atom) * ag->natoms);

    ag->is_psf_read = false;
    ag->prm = prm;

//      check_atomgrp (ag, prm);

    return ag;
}
コード例 #7
0
struct atomgrp *read_pdb_nopar(const char *path)
{
    FILE *fp = myfopen(path, "r");

    struct atomgrp *ag =
        (struct atomgrp *)_mol_calloc(1, sizeof(struct atomgrp));

    char *line = NULL;
    size_t len = 0;

    // read every line of the pdb file
    int atomi = 0;
    ag->natoms = 100;	// just a guess, realloc as necessary
    ag->atoms =
        (struct atom *)_mol_malloc(sizeof(struct atom) * ag->natoms);
    while (getline(&line, &len, fp) != -1) {
        char *atom_name;
        if (strncmp(line, "ATOM  ", 6) != 0
                && strncmp(line, "HETATM", 6) != 0)
            continue;
        if (atomi + 1 > ag->natoms) {
            ag->natoms *= 2;
            ag->atoms =
                (struct atom *)_mol_realloc(ag->atoms,
                                            sizeof(struct atom) *
                                            ag->natoms);
        }
        // init bonds
        /*
                        ag->atoms[atomi].bonds[0] = -1;
                        ag->atoms[atomi].bonds[1] = -1;
                        ag->atoms[atomi].bonds[2] = -1;
                        ag->atoms[atomi].bonds[3] = -1;
        */

        // init sa
        ag->atoms[atomi].sa = -1;
        // init mask
//                ag->atoms[atomi].mask = 0;
        // init attl
//                ag->atoms[atomi].attl = 0.0;

        ag->atoms[atomi].atom_typen = 1;

        if (!strncmp(line + 13, "C ", 2)
                || !strncmp(line + 13, "CA ", 3)
                || !strncmp(line + 13, "N ", 2)
                || !strncmp(line + 13, "O ", 2)
                || !strncmp(line + 13, "H ", 2))
            ag->atoms[atomi].backbone = true;
        else
            ag->atoms[atomi].backbone = false;

        atom_name = _mol_calloc(5, sizeof(char));
        strncpy(atom_name, line + 12, 4);
        rstrip(atom_name);
        while (isspace(*atom_name)) {
            memmove(atom_name, atom_name + 1, 4);
        }
        ag->atoms[atomi].name = atom_name;

        sscanf(&line[30], "%8lf%8lf%8lf",
               &ag->atoms[atomi].X, &ag->atoms[atomi].Y,
               &ag->atoms[atomi].Z);
        sscanf(&line[60], "%6lf", &ag->atoms[atomi].B);

        ag->atoms[atomi].icode = line[26];
        line[26] = 0;
        errno = 0;
        ag->atoms[atomi].res_seq = atoi(&line[22]);
        if (errno) {
            perror("atoi");
            exit(EXIT_FAILURE);
        }

        ag->atoms[atomi].base = ag->atoms[atomi].base2 = -1;
        ag->atoms[atomi].element = NULL;

        atomi++;
    }
    if (line)
        free(line);
    myfclose(fp);

    // final realloc of the arrays to make them tight
    ag->natoms = atomi;
    ag->atoms =
        (struct atom *)_mol_realloc(ag->atoms,
                                    sizeof(struct atom) * ag->natoms);

    ag->is_psf_read = false;
    ag->prm = NULL;

    return ag;
}
コード例 #8
0
struct atomgrp* read_ms (const char* path, struct prm* prm)
{
	FILE* fp = myfopen (path, "r");

	struct atomgrp* ag = (struct atomgrp*) _mol_calloc (1, sizeof (struct atomgrp));

	char* line = NULL;
	size_t len = 0;

	// tmp scanf vals
	char atypemaj[5];
	char atypemin[5];

	// read every line of the pdb file
	int atomi = 0;
	ag->natoms = 100; // just a guess, realloc as necessary
	ag->atoms = (struct atom*) _mol_malloc (sizeof (struct atom) * ag->natoms);
	while (getline (&line, &len, fp) != -1)
	{
		if (strncmp (line, "ATOM  ", 6) != 0) // check for ATOM line
			continue;

		if (atomi+1 > ag->natoms)
		{
			ag->natoms *= 2;
			ag->atoms = (struct atom*) _mol_realloc (ag->atoms, sizeof (struct atom) * ag->natoms);
		}

		/*
		// init bonds
		ag->atoms[atomi].bonds[0] = -1;
		ag->atoms[atomi].bonds[1] = -1;
		ag->atoms[atomi].bonds[2] = -1;
		ag->atoms[atomi].bonds[3] = -1;
		*/

		// init sa
		ag->atoms[atomi].sa = -1;
		// init mask
		ag->atoms[atomi].mask = 0;
		ag->atoms[atomi].nbondis = 0;
		ag->atoms[atomi].nbonds = 0;
		ag->atoms[atomi].nangs = 0;
		ag->atoms[atomi].ntors = 0;
		ag->atoms[atomi].nimps = 0;

		if (sscanf (line, "%*s %*d %4s %4s", atypemin, atypemaj) < 2)
		{
			fprintf (stderr, "begin error\n");
			fprintf (stderr, "in function read_ms\n");
			fprintf (stderr, "incorrect format for ATOM line\n");
			fprintf (stderr, "at file:\n");
			fprintf (stderr, "%s\n", path);
			fprintf (stderr, "at line:\n");
			fprintf (stderr, "%s\n", line);
			fprintf (stderr, "end error\n");
			exit (EXIT_FAILURE);
		}

		if (strncmp (atypemin, "HT", 2) == 0)
			continue; // ignore terminal hydrogens
		if (strncmp (atypemin, "OCT1", 4) == 0)
		{
			atypemin[1] = '\0';
		}
		if (strncmp (atypemin, "OCT2", 4) == 0)
			continue;


		ag->atoms[atomi].atom_typen = atomid (prm, atypemaj, atypemin);
		if (ag->atoms[atomi].atom_typen == -1) // val not found
		{
			if (atypemin[0] == 'H') // try one more time for hydrogen
			{
				atypemin[1] = '\0';
				ag->atoms[atomi].atom_typen = atomid (prm, atypemaj, atypemin);
			}
			if (ag->atoms[atomi].atom_typen == -1) // val still not found
			{
				fprintf (stderr, "error: in file %s line %s: atom type number of %s %s not defined in prm\n", path, line, atypemaj, atypemin);
				exit (EXIT_FAILURE);
			}
		}

		ag->atoms[atomi].X = atof (&line[30]);
		ag->atoms[atomi].Y = atof (&line[38]);
		ag->atoms[atomi].Z = atof (&line[46]);
		//if (line[57] != '0' && line[57] != '1') // verify that sa is 1 or 0
		if (! (line[57] >= '0' && line[57] <= '9')) // verify that sa is 1 or 0
		{
			fprintf (stderr, "error: file %s does not appear to be an ms file\n", path);
			exit (EXIT_FAILURE);
		}
		//ag->atoms[atomi].sa = atoi (&line[57]);
		ag->atoms[atomi].attl = atof (&line[57]);
		if (ag->atoms[atomi].attl > 0.0)
		{
			ag->atoms[atomi].sa = 1;
		}
		else
		{
			ag->atoms[atomi].sa = 0;
		}

		atomi++;
	}
	if (line)
		free (line);
	myfclose (fp);

	// final realloc of the arrays to make them tight
	ag->natoms = atomi;
	assert (ag->natoms > 0);
	ag->atoms = (struct atom*) _mol_realloc (ag->atoms, sizeof (struct atom) * ag->natoms);

	check_atomgrp (ag, prm);

	return ag;
}
コード例 #9
0
struct atomgrp* read_ms_nopar (const char* path)
{
	FILE* fp = myfopen (path, "r");

	struct atomgrp* ag = (struct atomgrp*) _mol_calloc (1, sizeof (struct atomgrp));

	char* line = NULL;
	size_t len = 0;

	// read every line of the pdb file
	int atomi = 0;
	ag->natoms = 100; // just a guess, realloc as necessary
	ag->atoms = (struct atom*) _mol_malloc (sizeof (struct atom) * ag->natoms);
	while (getline (&line, &len, fp) != -1)
	{
		if (strncmp (line, "ATOM  ", 6) != 0) // check for ATOM line
			continue;

		if (atomi+1 > ag->natoms)
		{
			ag->natoms *= 2;
			ag->atoms = (struct atom*) _mol_realloc (ag->atoms, sizeof (struct atom) * ag->natoms);
		}

		/*
		// init bonds
		ag->atoms[atomi].bonds[0] = -1;
		ag->atoms[atomi].bonds[1] = -1;
		ag->atoms[atomi].bonds[2] = -1;
		ag->atoms[atomi].bonds[3] = -1;
		*/

		// init sa
		ag->atoms[atomi].sa = -1;
		// init mask
		ag->atoms[atomi].mask = 0;

        ag->atoms[atomi].atom_typen = 1;
		ag->atoms[atomi].X = atof (&line[30]);
		ag->atoms[atomi].Y = atof (&line[38]);
		ag->atoms[atomi].Z = atof (&line[46]);
		//if (line[57] != '0' && line[57] != '1') // verify that sa is 1 or 0
		if (! (line[57] >= '0' && line[57] <= '9')) // verify that sa is 1 or 0
		{
			fprintf (stderr, "error: file %s does not appear to be an ms file\n", path);
			exit (EXIT_FAILURE);
		}
		//ag->atoms[atomi].sa = atoi (&line[57]);
		ag->atoms[atomi].attl = atof (&line[57]);
		if (ag->atoms[atomi].attl > 0.0)
		{
			ag->atoms[atomi].sa = 1;
		}
		else
		{
			ag->atoms[atomi].sa = 0;
		}

		atomi++;
	}
	if (line)
		free (line);
	myfclose (fp);

	// final realloc of the arrays to make them tight
	ag->natoms = atomi;
	ag->atoms = (struct atom*) _mol_realloc (ag->atoms, sizeof (struct atom) * ag->natoms);

	return ag;
}