File: addHydrogens.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 239,924 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; javascript: 164; makefile: 88
file content (128 lines) | stat: -rw-r--r-- 3,720 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
// $Id: addHydrogens.C,v 1.14 2004/05/27 19:49:57 oliver Exp $
//

#include <BALL/FORMAT/PDBFile.h>
#include <BALL/MOLMEC/AMBER/amber.h>
#include <BALL/MOLMEC/MINIMIZATION/conjugateGradient.h>
#include <BALL/MOLMEC/COMMON/assignTypes.h>
#include <BALL/KERNEL/selector.h>
#include <BALL/STRUCTURE/defaultProcessors.h>
#include <BALL/STRUCTURE/residueChecker.h>
#include <BALL/STRUCTURE/fragmentDB.h>

using namespace BALL;
using namespace std;

int main(int argc, char** argv)
{
	Log.setPrefix(cout, "[%T] ");
	Log.setPrefix(cerr, "[%T] ERROR: ");

	// issue a usage hint if called without parameters
	if (argc != 3)
	{
		Log << argv[0] << " <PDB infile> <PDB outfile>" << endl;
		return 1;
	}


	// open a PDB file with the name of the first argument
	PDBFile file(argv[1]);
	if (!file)
	{
		// if file does not exist: complain and abort
		Log.error() << "error opening " << argv[1] << " for input." << endl;
		return 2;
	}
	
	// create a system and read the contents of the PDB file
	System S;
	file >> S;
	file.close();

	// print the number of atoms read from the file
	Log << "read " << S.countAtoms() << " atoms." << endl;


	// now we open a fragment database
	Log << "reading fragment DB..." << endl;
	FragmentDB fragment_db("");

	// and normalize the atom names, i.e. we convert different
	// naming standards to the PDB naming scheme - just in case!
	Log << "normalizing names..." << endl;
	S.apply(fragment_db.normalize_names);

	// now we add any missing hydrogens to the residues
	// the data on the hydrogen positions stems from the
	// fragment database. However the hydrogen positions 
	// created in this way are only good estimates
	Log << "creating missing atoms..." << endl;
	S.apply(fragment_db.add_hydrogens);	
	Log << "added " << fragment_db.add_hydrogens.getNumberOfInsertedAtoms() 
			<< " atoms" << endl;

	// now we create the bonds between the atoms (PDB files hardly
  // ever contain a complete set of CONECT records)																							
	Log << "building bonds..." << endl;
	S.apply(fragment_db.build_bonds);

	// now we check whether the model we built is consistent
	// The ResidueChecker checks for charges, bond lengths,
	// and missing atoms
	Log << "checking the built model..." << endl;
	ResidueChecker checker(fragment_db);
	S.apply(checker);
	
	// now we create an AMBER force field 
	Log << "setting up force field..." << endl;
	AmberFF FF;

	// we then select all hydrogens (element(H))
	// using a specialized processor (Selector)
	S.deselect();
	FF.setup(S);
	Selector selector("element(H)");
	S.apply(selector);
	
	// just for curiosity: check how many atoms we are going
	// to optimize
	Log << "optimizing " << FF.getNumberOfMovableAtoms() << " out of " << S.countAtoms() << " atoms" << endl;
	
	// now we create a minimizer object that uses a conjugate 
	// gradient algorithm to optimize the atom positions
	ConjugateGradientMinimizer minimizer;

	// calculate the total energy of the system
	float initial_energy = FF.updateEnergy();
	Log << "initial energy: " << initial_energy << " kJ/mol" << endl;

	// initialize the minimizer and perform (up to)
	// 1000 optimization steps
	minimizer.setup(FF);
	minimizer.setEnergyOutputFrequency(1);
	minimizer.minimize(50);

	// calculate the terminal energy and print it
	float terminal_energy = FF.getEnergy();

	Log << "energy before/after minimization: " << initial_energy << "/" << terminal_energy << " kJ/mol" << endl;

	// write the optimized structure to a file whose
	// name is given as the second command line argument
	Log << "writing PBD file " << argv[2] << endl;
	file.open(argv[2], ios::out);
	file << S;
	file.close();

	// done.
	return 0;
}