Bsoft  2.1.0
Bernard's software package
bsf.cpp
Go to the documentation of this file.
1 
9 #include "molecule_to_map.h"
10 #include "mol_transform.h"
11 #include "mol_select.h"
12 #include "mol_util.h"
13 #include "rwmolecule.h"
14 #include "rwimg.h"
15 #include "ctf.h"
16 #include "utilities.h"
17 #include "options.h"
18 #include "timer.h"
19 
20 // Declaration of global variables
21 extern int verbose; // Level of output to the screen
22 
23 /* Usage assistance */
24 const char* use[] = {
25 " ",
26 "Usage: bsf [options] out.pdb out.hkl",
27 "------------------------------------",
28 "Calculates structure factors from atomic models.",
29 "A coordinate file is required.",
30 "If a structure factor file is given, it is compared to the calculated structure factors.",
31 " ",
32 "Selections:",
33 "-select CA Atom selection (default all).",
34 "-chains B,G,O,Z Select chains or molecules by ID (default all).",
35 " ",
36 "Actions:",
37 "-center Center coordinates before calculations.",
38 "-realspace Transform the output back to real space.",
39 "-scatter 300 Correct scattering intensities for the given acceleration voltage.",
40 " ",
41 "Parameters:",
42 "-verbose 7 Verbosity of output.",
43 "-size 100,80,70 Size (default automatic voxels).",
44 "-origin 0,0,0 Origin placement within image (default 0,0,0).",
45 "-sampling 2.5,2.5,2.5 Sampling (default 1 angstrom/voxel).",
46 "-resolution 3 Resolution (default 0.1 angstrom).",
47 "-wrap Wrap around periodic boundaries (default off).",
48 "-Bfactor 30 Global B-factor (default 0 angstrom squared).",
49 "-symmetry 1 Space group (default 1).",
50 "-unitcell 50,50,50,90,90,90 Unit cell parameters (angstrom & degrees).",
51 "-curves C,Au,Mn,H Selection of elements for scattering curve output.",
52 " ",
53 "Input:",
54 "-coordinates file.pdb Input coordinate file.",
55 "-hkl file.hkl Input structure factor file.",
56 "-parameters scat.star Scattering factor parameter file.",
57 " ",
58 "Output:",
59 "-output outfile.txt File for scattering curve output (use with -curves option).",
60 " ",
61 NULL
62 };
63 
64 int main(int argc, char **argv)
65 {
66  /* Initialize variables */
67  Bstring atom_select("ALL");
68  Bstring chain_select;
69  int center(0); // Flag to center coordinates
70  int set_backtransform(0); // Flag for back transformation
71  double volt(0); // In volts
72  Vector3<double> origin; // Coordinate origin placement
73  int set_origin(0); // Flag to set origin
74  Vector3<long> size; // New map size
75  Vector3<double> sam; // Sampling in angstrom/voxel side
76  double resolution(0.1); // Resolution = 0.1 angstrom
77  int wrap(0); // No wrapping as default
78  double Bfactor(0); // B-factor = 0 angstrom squared
79  int spacegroup(1);
80  UnitCell uc(0,0,0,M_PI_2,M_PI_2,M_PI_2);
81  Bstring curves; // String with elements for curve output
82  Bstring coorfile; // Atomic coordinates
83  Bstring mapfile; // Map to compare with
84  Bstring paramfile; // Use default parameter file
85  Bstring outfile; // File name for curve output
86 
87  int optind;
88  Boption* option = get_option_list(use, argc, argv, optind);
89  Boption* curropt;
90  for ( curropt = option; curropt; curropt = curropt->next ) {
91  if ( curropt->tag == "select" ) {
92  atom_select = curropt->value;
93  if ( atom_select.length() < 1 )
94  cerr << "-select: A selection must be specified!" << endl;
95  }
96  if ( curropt->tag == "chains" ) {
97  chain_select = curropt->value;
98  if ( chain_select.length() < 1 )
99  cerr << "-chains: A selection must be specified!" << endl;
100  }
101  if ( curropt->tag == "center" ) center = 1;
102  if ( curropt->tag == "realspace" )
103  set_backtransform = 1;
104  if ( curropt->tag == "scatter" ) {
105  if ( ( volt = curropt->value.real() ) < 1 )
106  cerr << "-scatter: A voltage must be specified!" << endl;
107  else
108  if ( volt < 1e3 ) volt *= 1e3; // Assume kilovolts
109  }
110  if ( curropt->tag == "size" )
111  size = curropt->size();
112  if ( curropt->tag == "origin" ) {
113  if ( curropt->value[0] == 'c' ) {
114  set_origin = 2;
115  } else {
116  origin = curropt->origin();
117  set_origin = 1;
118  }
119  }
120  if ( curropt->tag == "sampling" )
121  sam = curropt->scale();
122  if ( curropt->tag == "resolution" )
123  if ( ( resolution = curropt->value.real() ) < 0.001 )
124  cerr << "-resolution: Resolution must be specified!" << endl;
125  if ( curropt->tag == "wrap" )
126  wrap = 1;
127  if ( curropt->tag == "Bfactor" )
128  if ( ( Bfactor = curropt->value.real() ) < 0.001 )
129  cerr << "-Bfactor: The B-factor must be specified!" << endl;
130  if ( curropt->tag == "unitcell" )
131  uc = curropt->unit_cell();
132  if ( curropt->tag == "symmetry" )
133  if ( ( spacegroup = curropt->value.integer() ) < 1 )
134  cerr << "-symmetry: The space group number must be specified!" << endl;
135  if ( curropt->tag == "curves" )
136  curves = curropt->value;
137  if ( curropt->tag == "coordinates" )
138  coorfile = curropt->value;
139  if ( curropt->tag == "map" )
140  mapfile = curropt->filename();
141  if ( curropt->tag == "parameters" )
142  paramfile = curropt->filename();
143  if ( curropt->tag == "output" )
144  outfile = curropt->filename();
145  }
146  option_kill(option);
147 
148  double ti = timer_start();
149 
150  if ( outfile.length() )
151  write_scattering_curves(paramfile, outfile, curves);
152 
153  Bmolgroup* molgroup = NULL;
154  Bimage* p = NULL;
155  Bstring ext;
156  Bstring* file_list = NULL;
157  Vector3<double> box;
158 
159  if ( coorfile.length() ) {
160 // molgroup = read_molecule(coorfile, atom_select, paramfile);
161  file_list = coorfile.split(",");
162  molgroup = read_molecule(file_list, 0, box, atom_select, paramfile);
163 
164  if ( !molgroup->mol || !molgroup->mol->res ) {
165  cerr << "Error: Problem with coordinate file " << coorfile << ", exiting!" << endl;
166  bexit(-1);
167  }
168  }
169 
170  if ( molgroup == NULL ) {
171  cerr << "No coordinates read!" << endl;
172  bexit(-1);
173  }
174 
175  if ( center )
177 
178  if ( mapfile.length() ) {
179  p = read_img(mapfile, 1, -1);
180  if ( !p ) {
181  cerr << "Error: Map file " << mapfile << " not read!" << endl;
182  bexit(-1);
183  }
184  }
185 
186  // Get default parameters from the input image if possible
187  if ( p ) {
188  if ( set_origin ) {
189  if ( set_origin == 2 ) origin = p->default_origin();
190  } else origin = p->image->origin();
191  if ( sam.volume() > 0 ) p->sampling(sam);
192  if ( size[0] <= 0 || size[1] <= 0 || size[2] <= 0 ) {
193  size = {p->sizeX(), p->sizeY(), p->sizeZ()};
194  if ( !uc.a() ) uc[0] = p->real_size()[0];
195  if ( !uc.b() ) uc[1] = p->real_size()[1];
196  if ( !uc.c() ) uc[2] = p->real_size()[2];
197  }
198  }
199 
200  if ( set_origin == 2 ) origin = {double(size[0]/2), double(size[1]/2), double(size[2]/2)};
201 
202  if ( chain_select.length() )
203  molgroup_select_chains(molgroup, chain_select);
204 
205  // Calculate the structure factors
206  Bimage* pcalc = img_sf_from_molecule(molgroup, origin, size,
207  sam, resolution, spacegroup, uc, wrap, Bfactor, paramfile);
208 
209  double mw = molgroup_calculate_weight(molgroup);
210 
211 #ifdef HAVE_GCD
212  fftwf_init_threads();
213  fftwf_plan_with_nthreads(system_processors());
214 #endif
215  if ( verbose )
216  cout << "Number of threads: " << system_processors() << endl;
217 
218  if ( pcalc ) {
219  if ( volt ) {
220  double lambda = electron_wavelength(volt);
221  double b2 = beta2(volt);
222  double scale = 2*lambda/sqrt(1 - b2);
223  if ( verbose ) {
224  cout << "Scaling to correct scattering amplitudes:" << endl;
225  cout << "Voltage: " << volt*1e-3 << " kV" << endl;
226  cout << "Wavelength: " << lambda << " A" << endl;
227  cout << "Beta: " << sqrt(b2) << endl;
228  cout << "Scale: " << scale << endl;
229  }
230 // scale /= sqrt(POTPREFAC); // Remove old scaling factor
231  scale /= POTPREFAC; // Remove old scaling factor
232  pcalc->multiply(scale);
233  }
234  if ( set_backtransform ) {
235  pcalc->fft(FFTW_BACKWARD, 0);
236  pcalc->complex_to_real();
237  pcalc->mass_threshold(0, mw, RHO);
238  }
239  }
240 
241 #ifdef HAVE_GCD
242  fftwf_cleanup_threads();
243 #endif
244 
245  // Write output file(s)
246  while ( optind < argc ) {
247  ext = argv[optind];
248  ext = ext.extension();
249  if ( ext.contains("pdb") || ext.contains("gro") ) {
250  if ( molgroup ) write_molecule(argv[optind++], molgroup);
251  } else if ( pcalc ) {
252  write_img(argv[optind++], pcalc, 0);
253  }
254  }
255 
256  if ( molgroup ) molgroup_kill(molgroup);
257  if ( p ) delete p;
258  if ( pcalc ) delete pcalc;
259 
260  if ( verbose & VERB_TIME )
261  timer_report(ti);
262 
263  bexit(0);
264 }
265 
Boption
A class for a command line input option.
Definition: options.h:26
molgroup_shift_to_center_of_mass
int molgroup_shift_to_center_of_mass(Bmolgroup *molgroup)
Translates a set of coordinates to the center of mass.
Definition: mol_transform.cpp:625
Bimage::real_size
Vector3< double > real_size()
Definition: Bimage.h:500
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
RHO
#define RHO
Definition: utilities.h:95
Boption::unit_cell
UnitCell unit_cell()
Definition: options.h:279
Boption::scale
Vector3< double > scale()
Definition: options.h:190
molgroup_kill
int molgroup_kill(Bmolgroup *molgroup)
Destroys a molecule group.
Definition: rwmolecule.cpp:374
system_processors
size_t system_processors()
Returns the number of processors.
Definition: utilities.cpp:193
Bstring
Definition: Bstring.h:29
UnitCell::c
void c(double d)
Definition: UnitCell.h:38
Bstring::extension
Bstring extension()
Definition: Bstring.cpp:523
mol_select.h
Library routines to select atomic coordinates.
write_scattering_curves
int write_scattering_curves(Bstring &paramfile, Bstring &outfile, Bstring &selection)
Calculates and writes atomic scattering profiles to a file.
Definition: molecule_to_map.cpp:805
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 >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
POTPREFAC
#define POTPREFAC
Definition: molecule_to_map.h:14
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
Bimage::default_origin
Vector3< double > default_origin()
Definition: Bimage.h:592
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
Bstring::real
double real()
Definition: Bstring.cpp:553
UnitCell::b
void b(double d)
Definition: UnitCell.h:37
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.
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
mol_transform.h
Library routines used for atomic coordinate transformations.
M_PI_2
#define M_PI_2
Definition: utilities.h:66
Bmolecule::res
Bresidue * res
Definition: rwmolecule.h:122
Boption::tag
Bstring tag
Definition: options.h:29
Bimage::mass_threshold
double mass_threshold(long img_num, double mol_weight, double rho)
Finds the density threshold associated with a particular molecular weight.
Definition: Bimage_segment.cpp:820
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:77
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
Bimage::multiply
void multiply(long j, Complex< double > cv)
Definition: Bimage.h:325
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
verbose
int verbose
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
rwmolecule.h
Header file for reading reflection files.
Bimage::fft
int fft()
Definition: Bimage.h:830
molgroup_calculate_weight
double molgroup_calculate_weight(Bmolgroup *molgroup)
Calculates the weight of each molecule.
Definition: mol_util.cpp:270
Boption::value
Bstring value
Definition: options.h:30
ctf.h
Header file for CTF (contrast transfer function) functions.
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
UnitCell::a
void a(double d)
Definition: UnitCell.h:36
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
use
const char * use[]
Definition: bsf.cpp:24
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
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
Vector3::volume
double volume()
Definition: Vector3.h:237
rwimg.h
Header file for 2D and 3D image I/O.
molgroup_select_chains
int molgroup_select_chains(Bmolgroup *molgroup, Bstring chains)
Selects molecules.
Definition: mol_select.cpp:168
Boption::filename
Bstring filename()
Definition: options.h:85
electron_wavelength
double electron_wavelength(double volt)
Calculates the wavelength in angstrom from the acceleration voltage.
Definition: ctf.cpp:456
Bstring::split
Bstring * split()
Definition: Bstring.cpp:605
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
main
int main(int argc, char **argv)
Definition: bsf.cpp:64
Bmolgroup
Definition: rwmolecule.h:170
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
UnitCell
Definition: UnitCell.h:22
beta2
double beta2(double volt)
Calculates the square of the relative electron velocity.
Definition: ctf.cpp:479
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
molecule_to_map.h
Header file for functions to calculate a 3D map from atomic coordinates.
Bimage::complex_to_real
void complex_to_real()
The real part of a complex image is written to a simple image.
Definition: Bimage_complex.cpp:50
img_sf_from_molecule
Bimage * img_sf_from_molecule(Bmolgroup *molgroup, Vector3< double > origin, Vector3< long > size, Vector3< double > sam, double resolution, int spacegroup, UnitCell unit_cell, int wrap, double Bfactor, Bstring &paramfile)
Calculates a 3D set of structure factors from a set of atomic coordinates.
Definition: molecule_to_map.cpp:357
mol_util.h
Library routines used for atomic coordinates.