Bsoft  2.1.0
Bernard's software package
rwsymop.cpp
Go to the documentation of this file.
1 
9 #include "rwsymop.h"
10 #include "rwstar.h"
11 #include "sym_tags.h"
12 #include "linked_list.h"
13 #include "utilities.h"
14 
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17 
18 // Internal function prototypes
19 char* read_symop_star(Bstring& filename, int spacegroup, int& nsym);
20 char* read_symop_lib(Bstring& filename, int spacegroup, int& nsym);
21 int write_symop_star(Bstring& filename, int spacegroup, int nsym, char* symop, int line_len);
22 int write_star_pointgroup(Bstring& filename, Bsymmetry* sym, View ref_view);
23 
34 float* read_symat(Bstring& filename, int spacegroup, int& nsym)
35 {
36  if ( verbose & VERB_DEBUG )
37  cout << "DEBUG read_symat: Getting symmetry matrices from: " << filename << endl;
38 
39  char* symop = read_symop(filename, spacegroup, nsym);
40 
41  // Set up the symmetry operator matrix
42  float* mat = sym_matrices_from_text_list(nsym, symop, 80);
43 
44  delete[] symop;
45 
46  return mat;
47 }
48 
59 char* read_symop(Bstring& filename, int spacegroup, int& nsym)
60 {
61  // No space group has more than 192 operators
62  if ( spacegroup < 1 || spacegroup > 100000 ) return 0;
63 
64  Bstring symopfile(filename);
65  if ( filename.length() )
66  symopfile = filename;
67  else {
68  symopfile = "symop.star";
69  symopfile = parameter_file_path(symopfile);
70  }
71 
72  if ( verbose & VERB_DEBUG )
73  cout << "DEBUG read_symop: Reading symmetry operator file " << symopfile << endl;
74 
75  Bstring ext = symopfile.extension();
76 
77  char* symop = NULL;
78 
79  if ( spacegroup > 1 ) {
80  if ( ext.contains(".star") || ext.contains(".cif") ) {
81  symop = read_symop_star(symopfile, spacegroup, nsym);
82  } else {
83  symop = read_symop_lib(symopfile, spacegroup, nsym);
84  }
85  if ( !symop )
86  cerr << "Error: No symmetry operator file read!" << endl;
87  }
88 
89  if ( verbose & VERB_DEBUG )
90  cout << "DEBUG read_symop: Symmetry operator file read: " << filename << endl;
91 
92  return symop;
93 }
94 
101 int write_symat(Bstring& filename, int spacegroup)
102 {
103  int nsym(0), err(0);
104  Bstring temp;
105  char* symop = read_symop(temp, spacegroup, nsym);
106  if ( !symop || !nsym ) return -1;
107 
108  Bstring symatfile = filename;
109 
110  err = write_symop_star(symatfile, spacegroup, nsym, symop, 80);
111 
112  delete[] symop;
113 
114  return err;
115 }
116 
124 int write_pointgroup(Bstring& filename, Bstring& symmetry_string, View ref_view)
125 {
126  Bsymmetry* sym = init_point_group_symmetry(symmetry_string);
127 
128  int err = write_pointgroup(filename, sym, ref_view);
129 
130  kill_symmetry(sym);
131 
132  return err;
133 }
134 
135 int write_pointgroup(Bstring& filename, Bsymmetry* sym, View ref_view)
136 {
137  int err(0);
138 
139  if ( filename.contains(".star") )
140  err = write_star_pointgroup(filename, sym, ref_view);
141  else {
142  cerr << "Error: File type for " << filename << " not supported!" << endl;
143  err = -1;
144  }
145 
146  return err;
147 }
148 
149 // Find space group label and operators in a STAR format file
150 char* read_symop_star(Bstring& filename, int spacegroup, int& nsym)
151 {
152  int i, notfound = 1;
153  char* symop = NULL;
154 
155  Bstar* star = init_star();
156  Bstar_block* block;
157  Bstar_item* item;
158  Bstring* data;
159 
160  if ( read_star(filename.c_str(), star) < 0 ) {
161  kill_star(star);
162  return NULL;
163  }
164 
165  for ( block = star->block; block && notfound; block = block->next ) {
166  if ( spacegroup == item_get_integer(block, SYMMETRY_NUMBER) ) {
168  symop = new char[nsym*80];
169  for ( i=0; i<nsym*80; i++ ) symop[i] = 0;
170  item = item_find(block, SYMMETRY_EQUIVXYZ);
171  if ( item )
172  for ( data=item->data, i=0; data && i<nsym; data=data->next, i++ )
173  strcpy(symop+80*i, data->c_str());
174  notfound = 0;
175  }
176  }
177 
178  kill_star(star);
179 
180  return symop;
181 }
182 
183 // Find space group label and operators in "symop.lib" or similar file
184 char* read_symop_lib(Bstring& filename, int spacegroup, int& nsym)
185 {
186  int i, j, k(0), notfound(1), number(0), nlines;
187  char aline[80], symall[4000];
188  for ( i=0; i<4000; i++ ) symall[i] = 0;
189 
190  if ( verbose & VERB_DEBUG )
191  cout << "DEBUG read_symop_lib: Symmetry operator library: " << filename << endl << endl;
192 
193  Bstring atfile;
194  Bstring symopfile;
195  if ( filename.empty() ) symopfile = filename;
196  else symopfile = "symop.lib";
197 
198  if ( access(symopfile.c_str(), R_OK) != 0 )
199  symopfile = parameter_file_path(symopfile);
200 
201  ifstream fsym;
202  fsym.open(symopfile.c_str());
203  if ( fsym.fail() ) return NULL;
204 
205  if ( verbose & VERB_DEBUG )
206  cout << "DEBUG read_symop_lib: Symmetry operator library: " << symopfile << endl << endl;
207 
208  while ( notfound && fsym.getline(aline, 80) ) {
209  sscanf( aline, "%d %d", &number, &nlines );
210  if ( number == spacegroup ) notfound = 0;
211  }
212 
213  if ( notfound ) {
214  fsym.close();
215  return NULL;
216  }
217 
218  for ( i=nsym=0; i<nlines; i++ ) {
219  fsym.getline(aline, 80);
220  for ( j=0; j<(int)strlen(aline); j++ ) {
221  if ( aline[j] != ' ' && aline[j] != '\n' ) {
222  symall[k] = tolower(aline[j]);
223  k++;
224  }
225  if ( aline[j] == '*' ) nsym++;
226  }
227  symall[k] = '*';
228  k++;
229  nsym++;
230  }
231 
232  if ( verbose & VERB_DEBUG )
233  cout << "DEBUG read_symop_lib: Spacegroup=" << number << " Noperators=" << nsym << endl;
234 
235  char* symop = new char[nsym*80];
236  for ( i=0; i<nsym*80; i++ ) symop[i] = 0;
237 
238  j = 0;
239  for ( i=0; i<nsym; i++ ) {
240  k = 0;
241  while( symall[j] != '*' && k<80 ) {
242  symop[i*80+k] = symall[j];
243  j++;
244  k++;
245  }
246  j++;
247  if ( verbose & VERB_DEBUG )
248  cout << "DEBUG read_symop_lib: " << &symop[80*i] << endl;
249  }
250 
251  fsym.close();
252 
253  return symop;
254 }
255 
256 int write_symop_star(Bstring& filename, int spacegroup, int nsym, char* symop, int line_len)
257 {
258  int err(0), i, loop;
259  Bstring s;
260 
261  Bstar* star = init_star();
262  Bstar_item* item;
263  Bstring* data;
264  add_item((char **)&star->block, sizeof(Bstar_block));
265 
266  item_put_integer(star->block, SYMMETRY_NUMBER, spacegroup, "%d");
267 
269  item->type = NumberItem;
270  data = NULL;
271  for ( i=0; i<nsym; i++ ) {
272  s = Bstring(i+1, "%4d");
273  data = string_add(&data, s);
274  if ( !item->data ) item->data = data;
275  }
276 
278  item->type = StringItem;
279  data = NULL;
280  for ( i=0; i<nsym; i++ ) {
281  s = Bstring(symop+line_len*i);
282  data = string_add(&data, s);
283  if ( !item->data ) item->data = data;
284  }
285 
286  loop = item_index(star->block, SYMMETRY_EQUIVID);
288 
290 
291  err = write_star(filename.c_str(), star);
292 
293  if ( err < 0 ) i = err;
294 
295  kill_star(star);
296 
297  return i;
298 }
299 
300 int write_star_pointgroup(Bstring& filename, Bsymmetry* sym, View ref_view)
301 {
302  int err(0), i, loop;
303  Bstring s;
304 
305  Matrix3 mat = ref_view.matrix();
306 
307  Bstar* star = init_star();
308  Bstar_item* item;
309  Bstring* data;
310  add_item((char **)&star->block, sizeof(Bstar_block));
311 
312  star->comment = "# Symmetry from bsym\n\n";
313 
315  item_put_integer(star->block, SYMMETRY_PG_NUMBER, sym->point, "%d");
316 
317  Vector3<double>* v = new Vector3<double>[sym->n];
318 
319  for ( i=0; i<sym->n; i++ )
320  v[i] = mat * sym->op[i].axis;
321 
323  item->type = NumberItem;
324  data = NULL;
325  for ( i=0; i<sym->n; i++ ) {
326  s = Bstring(sym->op[i].order, "%4d");
327  data = string_add(&data, s);
328  if ( !item->data ) item->data = data;
329  }
330 
331  item = item_find_or_make(star->block, SYMMETRY_AXIS_X);
332  item->type = NumberItem;
333  data = NULL;
334  for ( i=0; i<sym->n; i++ ) {
335  s = Bstring(v[i][0], "%7.4f");
336  data = string_add(&data, s);
337  if ( !item->data ) item->data = data;
338  }
339 
340  item = item_find_or_make(star->block, SYMMETRY_AXIS_Y);
341  item->type = NumberItem;
342  data = NULL;
343  for ( i=0; i<sym->n; i++ ) {
344  s = Bstring(v[i][1], "%7.4f");
345  data = string_add(&data, s);
346  if ( !item->data ) item->data = data;
347  }
348 
349  item = item_find_or_make(star->block, SYMMETRY_AXIS_Z);
350  item->type = NumberItem;
351  data = NULL;
352  for ( i=0; i<sym->n; i++ ) {
353  s = Bstring(v[i][2], "%7.4f");
354  data = string_add(&data, s);
355  if ( !item->data ) item->data = data;
356  }
357 
358  delete[] v;
359 
360  loop = item_index(star->block, SYMMETRY_AXIS_ORDER);
361  loop_set_identifier(star->block, loop, 1, "symmetry_axis");
362 
364 
365  err = write_star(filename.c_str(), star);
366 
367  kill_star(star);
368 
369  return err;
370 }
371 
rwsymop.h
Header file for reading and writing symmetry operators.
Bstar_item::data
Bstring * data
Definition: rwstar.h:43
Bstar_item
Definition: rwstar.h:36
write_pointgroup
int write_pointgroup(Bstring &filename, Bstring &symmetry_string, View ref_view)
Writing point group symmetry operators.
Definition: rwsymop.cpp:124
Bsymmetry::n
int n
Definition: symmetry.h:42
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bsymmetry::point
int point
Definition: symmetry.h:39
item_index
int item_index(Bstar_block *block, const char *tag)
Gets the STAR item associated with a tag in a STAR data block.
Definition: rwstar.cpp:1029
item_get_integer
int item_get_integer(Bstar_block *block, const char *tag)
Gets an integer value associated with a tag in a STAR data block.
Definition: rwstar.cpp:1245
Bstring
Definition: Bstring.h:29
SYMMETRY_AXIS_X
#define SYMMETRY_AXIS_X
Definition: sym_tags.h:24
add_item
char * add_item(char **list, unsigned long size)
Adds an item to a linked list.
Definition: linked_list.cpp:26
Bstring::extension
Bstring extension()
Definition: Bstring.cpp:523
write_symat
int write_symat(Bstring &filename, int spacegroup)
Writing crystallographic symmetry operators.
Definition: rwsymop.cpp:101
Vector3< double >
Bstar::comment
Bstring comment
Definition: rwstar.h:81
parameter_file_path
Bstring parameter_file_path(Bstring filename)
Finds the parameter file path.
Definition: Bstring.cpp:795
Bstring::empty
bool empty()
Definition: Bstring.h:71
loop_set_identifier
int loop_set_identifier(Bstar_block *block, int loop, int n,...)
Assigns a loop identification number to items.
Definition: rwstar.cpp:1803
star_set_string_lengths
int star_set_string_lengths(Bstar *star)
Sets the maximum string lengths for each item in each block.
Definition: rwstar.cpp:938
View
Definition: View.h:26
item_find_or_make
Bstar_item * item_find_or_make(Bstar_block *block, const char *tag)
Gets the STAR item associated with a tag in a STAR data block.
Definition: rwstar.cpp:1105
SYMMETRY_NUMBER
#define SYMMETRY_NUMBER
Definition: sym_tags.h:17
Bsymmetry
Definition: symmetry.h:38
read_symat
float * read_symat(Bstring &filename, int spacegroup, int &nsym)
Reading crystallographic symmetry operators.
Definition: rwsymop.cpp:34
SYMMETRY_POINT_GROUP
#define SYMMETRY_POINT_GROUP
Definition: sym_tags.h:21
Bsymop::axis
Vector3< double > axis
Definition: symmetry.h:25
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
write_star_pointgroup
int write_star_pointgroup(Bstring &filename, Bsymmetry *sym, View ref_view)
Definition: rwsymop.cpp:300
Matrix3
Definition: Matrix3.h:18
item_put_integer
int item_put_integer(Bstar_block *block, const char *tag, int value, const char *format)
Writes a integer into a data item associated with a specific data block and tag in a STAR data base.
Definition: rwstar.cpp:1368
StringItem
@ StringItem
Definition: rwstar.h:25
write_star
int write_star(const char *filename, Bstar *star)
Writes a STAR data base to one or more STAR format files.
Definition: rwstar.cpp:666
Bsymop::order
int order
Definition: symmetry.h:26
sym_matrices_from_text_list
float * sym_matrices_from_text_list(int nsym, char *symop, int line_len)
Calculates symmetry matrices from a list of strings.
Definition: symmetry.cpp:1063
read_symop_lib
char * read_symop_lib(Bstring &filename, int spacegroup, int &nsym)
Definition: rwsymop.cpp:184
item_get_number_for_block
long item_get_number_for_block(Bstar_block *block, const char *tag)
Gets the number of values associated with a tag in a STAR data block.
Definition: rwstar.cpp:1163
verbose
int verbose
SYMMETRY_AXIS_Z
#define SYMMETRY_AXIS_Z
Definition: sym_tags.h:26
NumberItem
@ NumberItem
Definition: rwstar.h:26
read_symop_star
char * read_symop_star(Bstring &filename, int spacegroup, int &nsym)
Definition: rwsymop.cpp:150
utilities.h
Header file for general utilities.
SYMMETRY_EQUIVID
#define SYMMETRY_EQUIVID
Definition: sym_tags.h:19
Bstring::length
long length() const
Definition: Bstring.cpp:263
read_star
int read_star(const char *filename, Bstar *star)
Reads paramaters and data into a STAR data base from a list of files.
Definition: rwstar.cpp:224
init_point_group_symmetry
Bsymmetry * init_point_group_symmetry(Bstring symmetry_string)
Sets up a symmetry structure from a string identifier.
Definition: symmetry.cpp:39
write_symop_star
int write_symop_star(Bstring &filename, int spacegroup, int nsym, char *symop, int line_len)
Definition: rwsymop.cpp:256
kill_star
int kill_star(Bstar *star)
Destroys a STAR data base.
Definition: rwstar.cpp:132
SYMMETRY_AXIS_Y
#define SYMMETRY_AXIS_Y
Definition: sym_tags.h:25
kill_symmetry
int kill_symmetry(Bsymmetry *sym)
Deallocates a symmetry structure.
Definition: symmetry.cpp:207
linked_list.h
Header file for generalized linked list functions.
sym_tags.h
All STAR file format tags for symmetry parameters.
item_find
Bstar_item * item_find(Bstar_block *block, const char *tag)
Gets the STAR item associated with a tag in a STAR data block.
Definition: rwstar.cpp:1083
Bsymmetry::op
Bsymop * op
Definition: symmetry.h:43
rwstar.h
A header file for STAR format files.
item_put_string
int item_put_string(Bstar_block *block, const char *tag, char *string)
Writes a string into a data item associated with a specific data block and tag in a STAR data base.
Definition: rwstar.cpp:1300
Bstar::block
Bstar_block * block
Definition: rwstar.h:82
View::matrix
Matrix3 matrix()
Definition: View.h:153
SYMMETRY_EQUIVXYZ
#define SYMMETRY_EQUIVXYZ
Definition: sym_tags.h:20
Bsymmetry::label
Bstring label
Definition: symmetry.h:41
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
SYMMETRY_PG_NUMBER
#define SYMMETRY_PG_NUMBER
Definition: sym_tags.h:22
Bstar_block
Definition: rwstar.h:55
Bstar_block::next
Bstar_block * next
Definition: rwstar.h:56
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713
init_star
Bstar * init_star()
Creates a STAR data base.
Definition: rwstar.cpp:111
SYMMETRY_AXIS_ORDER
#define SYMMETRY_AXIS_ORDER
Definition: sym_tags.h:23
read_symop
char * read_symop(Bstring &filename, int spacegroup, int &nsym)
Reading crystallographic symmetry operators.
Definition: rwsymop.cpp:59
Bstar_item::type
StarType type
Definition: rwstar.h:39
Bstar
Definition: rwstar.h:78