Bsoft  2.1.0
Bernard's software package
bsplitmol.cpp
Go to the documentation of this file.
1 
9 #include "rwmolecule.h"
10 #include "utilities.h"
11 #include "options.h"
12 #include "timer.h"
13 
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16 
17 // Usage assistance
18 const char* use[] = {
19 " ",
20 "Usage: bsplitmol [options] input.pdb output.pdb",
21 "-----------------------------------------------",
22 "Splits sets of coordinates to different numbered files.",
23 " ",
24 "Parameters:",
25 "-verbose 7 Verbosity of output.",
26 "-first 5 Number given to the first file (default 0).",
27 "-digits 3 Number of digits inserted before the last period in the output file name.",
28 " ",
29 "Input:",
30 "-parameters parm.star Atomic properties parameter file (default atom_prop.star).",
31 " ",
32 NULL
33 };
34 
35 int main(int argc, char **argv)
36 {
37  // Initialize variables
38  Bstring atom_select("all"); // Selection
39  int first_number(0); // Number given to first file
40  int digits(3); // File number size
41  Bstring paramfile = NULL; // Use default parameter file
42 
43  int optind;
44  Boption* option = get_option_list(use, argc, argv, optind);
45  Boption* curropt;
46  for ( curropt = option; curropt; curropt = curropt->next ) {
47  if ( curropt->tag == "first" )
48  if ( ( first_number = curropt->value.integer() ) < 0 )
49  cerr << "-first: A number must be specified!" << endl;
50  if ( curropt->tag == "digits" )
51  if ( ( digits = curropt->value.integer() ) < 1 )
52  cerr << "-digits: A number of digits must be specified!" << endl;
53  if ( curropt->tag == "parameters" )
54  paramfile = curropt->filename();
55  }
56  option_kill(option);
57 
58  double ti = timer_start();
59 
60  Bstring inputfile = argv[optind++];
61  Bmolgroup* molgroup = read_molecule(inputfile, atom_select, paramfile);
62  if ( molgroup == NULL ) {
63  cerr << "Error: No input file read!" << endl;
64  bexit(-1);
65  }
66 
67  if ( optind >= argc ) bexit(0);
68 
69  long i, nmol = 0;
70  Bmolecule* mol, *mol2;
71  for ( mol=molgroup->mol; mol; mol=mol->next ) nmol++;
72 
73  if ( verbose & VERB_LABEL )
74  cout << "Splitting " << inputfile << " up into " << nmol << " files" << endl << endl;
75 
76  Bstring outputfile = argv[optind];
77  Bstring filename;
78  char format[32];
79 
80  if ( digits < 0.44*log(1.0*nmol) + 1 ) digits = (int) (0.44*log(1.0*nmol) + 1);
81  snprintf(format, 32, "_%%0%dd.", digits);
82 
83  Bmolgroup* molgroup_out = molgroup_init();
84  molecule_update_comment(molgroup_out, argc, argv);
85  for ( i=0, mol=molgroup->mol; mol; mol=mol2, i++ ) {
86  mol2 = mol->next;
87  mol->next = NULL;
88  molgroup_out->mol = mol;
89  filename = outputfile.pre_rev('.') + Bstring(i+first_number, format) + outputfile.post_rev('.');
90  write_molecule(filename, molgroup_out);
91  molecule_kill(mol);
92  molgroup_out->mol = NULL;
93  }
94 
95  molgroup->mol = NULL;
96  molgroup_kill(molgroup);
97  molgroup_kill(molgroup_out);
98 
99  if ( verbose & VERB_TIME )
100  timer_report(ti);
101 
102  bexit(0);
103 }
104 
Boption
A class for a command line input option.
Definition: options.h:26
molgroup_init
Bmolgroup * molgroup_init()
Initializes and allocates a new molecule group.
Definition: rwmolecule.cpp:45
molgroup_kill
int molgroup_kill(Bmolgroup *molgroup)
Destroys a molecule group.
Definition: rwmolecule.cpp:374
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
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:436
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::next
Boption * next
Definition: options.h:28
Bmolgroup::mol
Bmolecule * mol
Definition: rwmolecule.h:189
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
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::tag
Bstring tag
Definition: options.h:29
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
verbose
int verbose
timer.h
Utilities for timing functions.
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
utilities.h
Header file for general utilities.
molecule_kill
int molecule_kill(Bmolecule *mol)
Destroys a molecule.
Definition: rwmolecule.cpp:417
main
int main(int argc, char **argv)
Definition: bsplitmol.cpp:35
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
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
Bmolgroup
Definition: rwmolecule.h:170
use
const char * use[]
Definition: bsplitmol.cpp:18
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110