Bsoft  2.1.0
Bernard's software package
bmoledit.cpp
Go to the documentation of this file.
1 
9 #include "rwmolecule.h"
10 #include "mol_symmetry.h"
11 #include "mol_transform.h"
12 #include "mol_edit.h"
13 #include "mol_util.h"
14 #include "utilities.h"
15 #include "options.h"
16 #include "timer.h"
17 
18 // Declaration of global variables
19 extern int verbose; // Level of output to the screen
20 
21 long molgroup_trim(Bmolgroup* molgroup, Vector3<double> box);
22 
23 /* Usage assistance */
24 const char* use[] = {
25 " ",
26 "Usage: bmoledit [options] in.pdb out.pdb",
27 "----------------------------------------",
28 "Manipulates atomic coordinate files.",
29 " ",
30 "Actions:",
31 "-rename D Rename molecules from the given letter.",
32 "-renumber 52 Renumber residues from the given number.",
33 "-select CA Atom selection (default all).",
34 "-elements Use elements as identifiers for atom types.",
35 "-split Split molecule groups, output multiple numbered files.",
36 "-slices 23.7 Split coordinates into slices of this thickness, output multiple numbered files.",
37 "-translate 0,-50,22 Translate (angstrom).",
38 "-trim 200,230,180 Trim to new box size (use with -translate).",
39 "-noH Remove all hydrogens.",
40 "-SS 2.2 Add disulfide bonds within the given separation.",
41 "-nobonds Remove all bond specifications.",
42 "-bonds 2.1 Add bonds between atoms closer than the given distance.",
43 "-pseudobonds 3 Add pseudo-atoms on bonds given the number per bond.",
44 "-random 1.2 Add a random displacement to each coordinate within a maximum (angstrom).",
45 "-B 23.4 Add a random displacement to each coordinate based on a B-factor (angstrom^2).",
46 "-prune Remove overlapping molecules.",
47 "-untangle 3.5,0.2 Eliminate overlaps by moving molecules apart (sampling and damping factor).",
48 " ",
49 "Parameters:",
50 "-verbose 7 Verbosity of output.",
51 "-distance 3.5 Distance between overlapping atoms allowed (default 3 angstrom, use with -insert).",
52 " ",
53 "Input:",
54 "-parameters parm.star Atomic properties parameter file (default atom_prop.star).",
55 "-insert in.pdb Insert this into the input file, deleting overlapping atoms in the input file.",
56 " (use with -distance to indicate cutoff to eliminate atoms).",
57 " ",
58 NULL
59 };
60 
61 int main(int argc, char **argv)
62 {
63  /* Initialize variables */
64  int elements(0); // Flag to use element names
65  char first_name(0); // First molecule name for renaming
66  int first_res(0); // Don't renumber
67  int split(0); // Flag to split molecule groups
68  double slice_thickness(0); // Slice thickness for splitting up coordinates
69  Transform t; // Transformation details
70  Vector3<double> trim; // New enclosing box
71  int removeH(0); // Flag to remove hydrogens
72  double SSdistance(0); // Maximum distance between sulfur atoms
73  int removeBonds(0); // Flag to remove bonds
74  double addBonds(0); // Cutoff distance to add bonds, 0=don't add
75  int pseudobonds(0); // Pseudo-atoms to add
76  Bstring insert; // Molecules to insert
77  double random_max(0); // Random displacement maximum in angstrom
78  double B(0); // B-factor in angstrom^2
79  double distance(0); // Distance allowed between overlapping atoms in angstrom
80  int set_wrap(0); // Flag for wrapping coordinates
81  int prune(0); // Flag to remove overlapping molecules
82  double untangle(0); // Sampling for moving overlapping molecules
83  double lambda(0.1); // Damping factor for moving overlapping molecules
84  Vector3<double> box; // Enclosing box
85  Bstring atom_select("ALL");
86  Bstring paramfile; // Use default parameter file
87 
88  int optind;
89  Boption* option = get_option_list(use, argc, argv, optind);
90  Boption* curropt;
91  for ( curropt = option; curropt; curropt = curropt->next ) {
92  if ( curropt->tag == "select" ) {
93  atom_select = curropt->value;
94  if ( atom_select.length() < 1 )
95  cerr << "-select: A selection must be specified!" << endl;
96  }
97  if ( curropt->tag == "elements" ) elements = 1;
98  if ( curropt->tag == "split" ) split = 1;
99  if ( curropt->tag == "rename" )
100  if ( ( first_name = curropt->value[0] ) < 1 )
101  cerr << "-rename: The first molecule name must be specified!" << endl;
102  if ( curropt->tag == "renumber" )
103  if ( ( first_res = curropt->value.integer() ) < 1 )
104  cerr << "-renumber: The first residue number must be specified!" << endl;
105  if ( curropt->tag == "slices" ) {
106  if ( ( slice_thickness = curropt->value.real() ) < 1 )
107  cerr << "-slices: A slice thickness must be specified!" << endl;
108  else
109  if ( slice_thickness <= 0 ) slice_thickness = 0;
110  }
111  if ( curropt->tag == "translate" )
112  t.trans = curropt->vector3();
113  if ( curropt->tag == "trim" )
114  trim = curropt->size();
115  if ( curropt->tag == "noH" )
116  removeH = 1;
117  if ( curropt->tag == "SS" )
118  if ( ( SSdistance = curropt->value.real() ) < 0.1 )
119  cerr << "-SS: A maximum separation distance must be specified!" << endl;
120  if ( curropt->tag == "nobonds" )
121  removeBonds = 1;
122  if ( curropt->tag == "bonds" )
123  if ( ( addBonds = curropt->value.real() ) < 0.1 )
124  cerr << "-bonds: A maximum bond length must be specified!" << endl;
125  if ( curropt->tag == "pseudobonds" )
126  if ( ( pseudobonds = curropt->value.integer() ) < 1 )
127  cerr << "-pseudobonds: A number of pseudo-atoms must be specified!" << endl;
128  if ( curropt->tag == "insert" )
129  insert = curropt->filename();
130  if ( curropt->tag == "random" )
131  if ( ( random_max = curropt->value.real() ) < 0.1 )
132  cerr << "-random: A random displacement maximum must be specified!" << endl;
133  if ( curropt->tag == "B" )
134  if ( ( B = curropt->value.real() ) < 0.1 )
135  cerr << "-B: A B-factor must be specified!" << endl;
136  if ( curropt->tag == "prune" ) prune = 1;
137  if ( curropt->tag == "untangle" )
138  if ( curropt->values(untangle, lambda) < 1 )
139  cerr << "-untangle: Grid sampling in angstrom must be specified!" << endl;
140  if ( curropt->tag == "distance" )
141  if ( ( distance = curropt->value.real() ) < 1 )
142  cerr << "-distance: A separation distance must be specified!" << endl;
143  if ( curropt->tag == "wrap" ) {
144  box = curropt->size();
145  set_wrap = 1; // Wrap within periodic boundaries
146  }
147  if ( curropt->tag == "parameters" )
148  paramfile = curropt->filename();
149  }
150  option_kill(option);
151 
152  if ( optind >= argc ) bexit(-1);
153 
154  double ti = timer_start();
155 
156  Bstring filename = argv[optind++];
157  Bmolgroup* molgroup = read_molecule(filename, atom_select, paramfile);
158  if ( !molgroup ) {
159  cerr << "Error: Coordinate file not read!" << endl;
160  bexit(-1);
161  }
162 
163  if ( elements )
165 
166  if ( first_name )
167  molgroup_rename(molgroup, first_name);
168 
169  if ( first_res > 0 )
170  molgroup_residue_renumber(molgroup, first_res);
171 
172  if ( t.trans.length() ) molgroup_coor_shift(molgroup, t.trans);
173 
174  if ( trim.volume() ) molgroup_trim(molgroup, trim);
175  else if ( set_wrap ) molgroup->box = box;
176 
177  if ( removeH )
178  molgroup_remove_hydrogens(molgroup);
179 
180  if ( SSdistance )
181  molgroup_add_disulfides(molgroup, SSdistance);
182 
183  if ( removeBonds ) {
184  bond_kill(molgroup->bond);
185  molgroup->bond = NULL;
186  }
187 
188  if ( addBonds )
189  molgroup_bond_list_generate(molgroup, addBonds, set_wrap);
190 
191  if ( pseudobonds )
192  molgroup_bond_pseudo_atoms(molgroup, pseudobonds, set_wrap);
193 
194  Bmolgroup* molinsert = NULL;
195  if ( insert.length() ) {
196  molinsert = read_molecule(insert, atom_select, paramfile);
197  molgroup_insert(molgroup, molinsert, distance); // Remember that the group inserted is deallocated
198  molgroup_kill(molinsert);
199  }
200 
201  if ( random_max )
202  molgroup_randomize(molgroup, random_max);
203 
204  if ( B )
205  molgroup_randomize_B(molgroup, B);
206 
207  if ( prune )
208  molgroup_prune_molecules(molgroup);
209 
210  if ( untangle )
211  molgroup_untangle_molecules(molgroup, untangle, lambda);
212 
213  int i, num(0);
214  Bmolgroup** molgroup_set;
215  if ( optind < argc ) {
216  filename = argv[optind];
217  molecule_update_comment(molgroup, argc, argv);
218  if ( split ) {
219  molecules_to_molgroups(molgroup);
220  molgroup_list_write(filename, molgroup);
221  } else if ( slice_thickness > 0 ) {
222  molgroup_set = molgroup_split_into_slices(molgroup, slice_thickness, num);
223  for ( i=0; i<num; i++ ) {
224  filename = filename.pre_rev('.') + Bstring(i+1, "_%03d.") + filename.post_rev('.');
225  write_molecule(filename, molgroup_set[i]);
226  molgroup_kill(molgroup_set[i]);
227  }
228  delete[] molgroup_set;
229  } else {
230  i = write_molecule(argv[optind], molgroup);
231  if ( i < 1 ) {
232  cerr << "Error: No output file written!" << endl;
233  bexit(-1);
234  }
235  }
236  }
237 
238  molgroup_kill(molgroup);
239 
240  if ( verbose & VERB_TIME )
241  timer_report(ti);
242 
243  bexit(0);
244 }
245 
253 {
254  if ( box.volume() < 1 ) return 0;
255 
256  long n(0), nsel(0);
257  Bmolecule* mol;
258  Bresidue* res;
259  Batom* atom;
260  Vector3<double> start, end(box);
261 
262  if ( verbose & VERB_PROCESS )
263  cout << "Trimming to a box of size: " << box << endl;
264 
265  for ( mol = molgroup->mol; mol; mol = mol->next ) {
266  for( res = mol->res; res; res = res->next ) {
267  for ( atom = res->atom; atom; atom = atom->next, n++ ) {
268  if ( atom->coord.within(start, end) ) {
269  atom->sel = 1;
270  nsel++;
271  } else {
272  atom->sel = 0;
273  }
274  }
275  }
276  }
277 
278  long ndel = molgroup_delete_deselected_atoms(molgroup);
279 
280  if ( n != nsel + ndel )
281  cerr << "Error: There is a discrepancy between the the total number of atoms ("
282  << n << ") and the sum of selected (" << nsel
283  << ") and deleted (" << ndel << ") atoms!" << endl;
284 
285  molgroup->box = box;
286 
287  if ( verbose & VERB_PROCESS )
288  cout << "Number of atoms remaining: " << nsel << endl;
289 
290  return nsel;
291 }
Boption
A class for a command line input option.
Definition: options.h:26
Vector3::within
bool within(Vector3 &v1, Vector3 &v2)
Definition: Vector3.h:281
molgroup_kill
int molgroup_kill(Bmolgroup *molgroup)
Destroys a molecule group.
Definition: rwmolecule.cpp:374
Bresidue::next
Bresidue * next
Definition: rwmolecule.h:50
molecule_update_comment
int molecule_update_comment(Bmolgroup *molgroup, int n, char **strings)
Puts a set of strings and time in the main comment of a molecule group.
Definition: rwmolecule.cpp:1467
Bstring
Definition: Bstring.h:29
molgroup_add_disulfides
int molgroup_add_disulfides(Bmolgroup *molgroup, double distance)
Adds disulfide bonds when sulfur atoms are close to each other.
Definition: mol_edit.cpp:101
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:436
Bresidue::atom
Batom * atom
Definition: rwmolecule.h:54
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::next
Boption * next
Definition: options.h:28
Bmolgroup::mol
Bmolecule * mol
Definition: rwmolecule.h:189
Vector3< double >
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
get_option_list
Boption * get_option_list(const char *use[], int argc, char *argv[], int &optind)
Parses command line arguments based on a template.
Definition: options.cpp:40
molgroup_randomize_B
int molgroup_randomize_B(Bmolgroup *molgroup, double B)
Applies random displacements to coordinates.
Definition: mol_edit.cpp:403
Bstring::real
double real()
Definition: Bstring.cpp:553
mol_edit.h
Library routines used for atomic coordinates.
read_molecule
Bmolgroup * read_molecule(const char *filename, const char *select, const char *paramfile)
The generalized function for reading molecular files.
Definition: rwmolecule.cpp:818
options.h
Header file for option handlers.
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
mol_transform.h
Library routines used for atomic coordinate transformations.
Batom::next
Batom * next
Definition: rwmolecule.h:25
Bmolecule::res
Bresidue * res
Definition: rwmolecule.h:122
molgroup_split_into_slices
Bmolgroup ** molgroup_split_into_slices(Bmolgroup *molgroup, double slice_thickness, int &nslices)
Splits a set of coordinates into slices with a given thickness.
Definition: mol_edit.cpp:158
molgroup_trim
long molgroup_trim(Bmolgroup *molgroup, Vector3< double > box)
Deletes atoms outside the box.
Definition: bmoledit.cpp:252
molgroup_bond_pseudo_atoms
int molgroup_bond_pseudo_atoms(Bmolgroup *molgroup, int atoms_per_bond, int wrap)
Places pseudo-atoms on bonds.
Definition: mol_edit.cpp:533
Boption::tag
Bstring tag
Definition: options.h:29
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
insert
string insert(string &filename, string ins)
Definition: string_util.cpp:48
use
const char * use[]
Definition: bmoledit.cpp:24
verbose
int verbose
rwmolecule.h
Header file for reading reflection files.
Boption::value
Bstring value
Definition: options.h:30
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:421
Bmolecule::next
Bmolecule * next
Definition: rwmolecule.h:108
Bmolecule
Definition: rwmolecule.h:107
molgroup_remove_hydrogens
int molgroup_remove_hydrogens(Bmolgroup *molgroup)
Removes all hydrogens from a molecular system.
Definition: mol_edit.cpp:52
Bmolgroup::box
Vector3< double > box
Definition: rwmolecule.h:184
timer.h
Utilities for timing functions.
Bmolgroup::bond
Bbond * bond
Definition: rwmolecule.h:190
molgroup_set_atom_types_to_elements
long molgroup_set_atom_types_to_elements(Bmolgroup *molgroup)
Sets atom types to element names.
Definition: mol_edit.cpp:29
distance
double distance(vector< T1 > &v1, vector< T2 > &v2)
Definition: Vector3.h:538
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
molgroup_residue_renumber
int molgroup_residue_renumber(Bmolgroup *molgroup, int first)
Renumbers residues.
Definition: mol_util.cpp:182
molecules_to_molgroups
int molecules_to_molgroups(Bmolgroup *molgroup)
Converts molecules in a molecule group to individual molecule groups.
Definition: rwmolecule.cpp:1744
molgroup_coor_shift
int molgroup_coor_shift(Bmolgroup *molgroup, Vector3< double > shift)
Translates a molecule group.
Definition: mol_transform.cpp:28
molgroup_list_write
int molgroup_list_write(Bstring &filename, Bmolgroup *molgroup)
Writes a molecule group list.
Definition: rwmolecule.cpp:1053
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
utilities.h
Header file for general utilities.
Boption::size
Vector3< long > size()
Definition: options.h:163
split
vector< string > split(const string &s)
Splits a string on whitespace.
Definition: string_util.cpp:92
molgroup_prune_molecules
int molgroup_prune_molecules(Bmolgroup *molgroup)
Deletes overlapping molecules.
Definition: mol_edit.cpp:606
Boption::vector3
Vector3< double > vector3()
Definition: options.h:148
molgroup_untangle_molecules
int molgroup_untangle_molecules(Bmolgroup *molgroup, double sampling, double lambda)
Moves overlapping molecules away from each other.
Definition: mol_edit.cpp:802
Bstring::length
long length() const
Definition: Bstring.cpp:263
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
write_molecule
int write_molecule(char *filename, Bmolgroup *molgroup)
Writes a molecule group.
Definition: rwmolecule.cpp:975
molgroup_insert
int molgroup_insert(Bmolgroup *molgroup, Bmolgroup *molinsert, double distance)
Inserts one set of molecules into another.
Definition: mol_edit.cpp:244
Vector3::volume
double volume()
Definition: Vector3.h:237
main
int main(int argc, char **argv)
Definition: bmoledit.cpp:61
Transform::trans
Vector3< double > trans
Definition: Transform.h:30
Batom::sel
int sel
Definition: rwmolecule.h:34
molgroup_rename
int molgroup_rename(Bmolgroup *molgroup, char first_name)
Renames molecules.
Definition: mol_util.cpp:161
mol_symmetry.h
Library routines used for symmetry operations on atomic coordinates.
Bresidue
Definition: rwmolecule.h:49
Boption::filename
Bstring filename()
Definition: options.h:85
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
molgroup_bond_list_generate
Bbond * molgroup_bond_list_generate(Bmolgroup *molgroup, double maxlength, int wrap)
Generates a bond list based on atom separation.
Definition: rwmolecule.cpp:1560
Bmolgroup
Definition: rwmolecule.h:170
molgroup_randomize
int molgroup_randomize(Bmolgroup *molgroup, double random_max)
Applies random displacements to coordinates.
Definition: mol_edit.cpp:369
molgroup_delete_deselected_atoms
long molgroup_delete_deselected_atoms(Bmolgroup *molgroup)
Deletes deselected atoms.
Definition: mol_edit.cpp:720
Transform
Definition: Transform.h:24
Vector3::length
double length()
Definition: Vector3.h:222
bond_kill
int bond_kill(Bbond *bond)
Dealocates a list of bonds.
Definition: rwmolecule.cpp:480
Batom::coord
Vector3< double > coord
Definition: rwmolecule.h:30
Batom
Definition: rwmolecule.h:24
mol_util.h
Library routines used for atomic coordinates.