| Bsoft 2.0.5 Bernard's software package

rwXPLOR.cpp
Go to the documentation of this file.
1
9 #include "rwXPLOR.h"
10 #include "linked_list.h"
11 #include "Complex.h"
12 #include "utilities.h"
13 #include <fstream>
14
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17
18
19 // Internal function prototypes
20 int readXPLORmap(Bimage* p, int readdata);
21 int readXPLORsf(Bimage* p, int readdata);
22 int writeXPLORmap(Bimage* p);
23 int writeXPLORsf(Bimage* p);
24
32 int readXPLOR(Bimage* p, int readdata)
33 {
34 int err = 0;
35 string ext(extension(p->file_name()));
36
37 if ( ext.find("xpl") != string::npos || ext.find("cns") != string::npos ) err = readXPLORmap(p, readdata);
38 else if ( ext.find("rfl") != string::npos ) err = readXPLORsf(p, readdata);
39 else err = -1;
40
41 return err;
42 }
43
44 /*
45 @brief Reading a XPLOR map file format.
46 A map text format used in X-ray crystallography.
47 @param *p the image structure.
48 @param readdata flag to activate reading of image data.
49 @return int error code (<0 means failure).
50 **/
51 int readXPLORmap(Bimage* p, int readdata)
52 {
53 ifstream fimg(p->file_name());
54
55 if ( fimg.fail() ) return -1;
56
57 char aline[MAXLINELEN];
58 long i, j, x, y, z, xs(1), xf(1), ys(1), yf(1), zs(1), zf(1), n(0);
59 double avg, std;
60 UnitCell uc;
61
62 p->images(1);
63
64 fimg.getline(aline, MAXLINELEN); // First line blank
65 fimg.getline(aline, MAXLINELEN); // Second line with number of title lines
66 sscanf(aline, "%ld", &n);
67 for ( i=0; i<n; i++ ) {
68 fimg.getline(aline, MAXLINELEN); // Title line
69 p->label(p->label() + aline);
70 }
71 fimg.getline(aline, MAXLINELEN); // Dimensions and origin
72 sscanf(aline, "%ld %ld %ld %ld %ld %ld %ld %ld %ld", &x, &xs, &xf,
73 &y, &ys, &yf, &z, &zs, &zf);
74 fimg.getline(aline, MAXLINELEN); // Unit cell
75 sscanf(aline, "%lf %lf %lf %lf %lf %lf",
76 &uc[0], &uc[1], &uc[2], &uc[3], &uc[4], &uc[5]);
77 fimg.getline(aline, MAXLINELEN); // Line with "ZYX"
78
79 p->size(xf-xs+1, yf-ys+1, zf-zs+1);
80 p->channels(1);
81 // p->image->origin(-xs, -ys, -zs);
82 p->origin(-xs, -ys, -zs);
83 p->data_type(Float);
84 uc.degrees_to_radians();
85 p->unit_cell(uc);
86 p->sampling(uc.a()/x, uc.b()/y, uc.c()/z);
87
88 if ( verbose & VERB_DEBUG )
89 cout << "DEBUG readXPLORmap: size=" << p->size() << endl;
90
91 if ( !readdata ) {
92 fimg.close();
93 return 0;
94 }
95
96 long slice_size = p->sizeX()*p->sizeY();
97 float* data = (float *) p->data_alloc();
98
99 for ( i=z=0; z<p->sizeZ(); z++ ) {
100 fimg.getline(aline, MAXLINELEN); // Slice number
101 // cout << "reading slice:" << z << " " << aline << endl;
102 for ( j=0; j<slice_size; j+=6 ) {
103 fimg.getline(aline, MAXLINELEN);
104 // cout << "reading data:" << i << " " << aline << endl;
105 if ( !fimg.fail() ) {
106 n = sscanf(aline, "%f %f %f %f %f %f", &data[i], &data[i+1],
107 &data[i+2], &data[i+3], &data[i+4], &data[i+5]);
108 i += n;
109 }
110 }
111 }
112
113 fimg.getline(aline, MAXLINELEN); // -9999
114 fimg.getline(aline, MAXLINELEN); // Average and standard deviation
115 sscanf(aline, "%lf %lf", &avg, &std);
116 p->average(avg);
117 p->standard_deviation(std);
118
119 if ( verbose & VERB_DEBUG )
120 cout << "DEBUG readXPLORmap: avg=" << p->average() << " " << p->standard_deviation() << endl;
121
122 fimg.close();
123
124 return 0;
125 }
126
127 /*
128 @brief Reading a XPLOR structure factor file format.
129 A structure factor text format used in X-ray crystallography.
130 A minimal implementation of the XPLOR reflection file format which
131 recognizes only the indices, amplitudes and phases on a line.
132 @param *p the image structure.
133 @param readdata flag to activate reading of image data.
134 @return int error code (<0 means failure).
135 **/
136 int readXPLORsf(Bimage* p, int readdata)
137 {
138 long i = 0, j, h, k, l, x, y, z, n = 0;
139 long hmin = 0, hmax = 0, kmin = 0, kmax = 0, lmin = 0, lmax = 0;
140 float amp, phi;
141
142 ifstream fhkl(p->file_name());
143
144 if ( fhkl.fail() ) return -1;
145
146 char aline[MAXLINELEN], tag[20];
147 fhkl.getline(aline, MAXLINELEN); // First line with number of reflections
148 sscanf( aline, "%s %ld", tag, &n);
149
150 fhkl.getline(aline, MAXLINELEN); // Second line with anomalous tag
151
152 while ( fhkl.getline(aline, MAXLINELEN) ) {
153 if ( strstr(aline, "INDE") || strstr(aline, "inde") ) {
154 i = sscanf( aline, "%s %ld %ld %ld %s %f %f", tag, &h, &k, &l,
155 tag, &amp, &phi ); // A reflection line
156 if ( i ) {
157 if ( hmin > h ) hmin = h;
158 if ( kmin > k ) kmin = k;
159 if ( lmin > l ) lmin = l;
160 if ( hmax < h ) hmax = h;
161 if ( kmax < k ) kmax = k;
162 if ( lmax < l ) lmax = l;
163 n++;
164 }
165 }
166 }
167
168 p->size(1,1,1);
169 if ( p->sizeX() < (long)2*hmax + 1 ) p->sizeX(2*(hmax + 1));
170 if ( p->sizeX() < 1 - (long)2*hmin ) p->sizeX(-2*(hmin - 1));
171 if ( p->sizeY() < (long)2*kmax + 1 ) p->sizeY(2*(kmax + 1));
172 if ( p->sizeY() < 1 - (long)2*kmin ) p->sizeY(-2*(kmin - 1));
173 if ( p->sizeZ() < (long)2*lmax + 1 ) p->sizeZ(2*(lmax + 1));
174 if ( p->sizeZ() < 1 - (long)2*lmin ) p->sizeZ(-2*(lmin - 1));
175
176 p->data_type(Float);
177 p->compound_type(TComplex);
178 p->channels(2);
179
180 fhkl.close();
181
182 if ( !readdata ) return 0;
183
184 Complex<float>* data = (Complex<float>*) p->data_alloc();
185
186 fhkl.open(p->file_name());
187
188 if ( fhkl.fail() ) return -1;
189
190 fhkl.getline(aline, MAXLINELEN); // Ignore the header line
191
192 while ( fhkl.getline(aline, MAXLINELEN) ) {
193 if ( strstr(aline, "INDE") || strstr(aline, "inde") ) {
194 i = sscanf( aline, "%s %ld %ld %ld %s %f %f", tag, &h, &k, &l,
195 tag, &amp, &phi ); // A reflection line
196 if ( i ) {
197 x = h;
198 if ( x < 0 ) x += p->sizeX();
199 y = k;
200 if ( y < 0 ) y += p->sizeY();
201 z = l;
202 if ( z < 0 ) z += p->sizeZ();
203 j = (z*p->sizeY() + y)*p->sizeX() + x;
204 phi *= M_PI/180;
205 data[j] = Complex<float>(amp*cos(phi), amp*sin(phi));
206 }
207 }
208 }
209
210 fhkl.close();
211
212 return 0;
213 }
214
221 int writeXPLOR(Bimage* p)
222 {
223 int err(0);
224 string ext(extension(p->file_name()));
225
226 if ( ext.find("xpl") != string::npos || ext.find("cns") != string::npos ) err = writeXPLORmap(p);
227 else if ( ext.find("rfl") != string::npos ) err = writeXPLORsf(p);
228
229 return err;
230 }
231
232 /*
233 @brief Writing a XPLOR structure factor file format.
234 A structure factor text format used in X-ray crystallography.
235 @param *p the image structure.
236 @return int error code (<0 means failure).
237 **/
238 int writeXPLORmap(Bimage* p)
239 {
240 ofstream fimg(p->file_name());
241
242 if ( fimg.fail() ) return -1;
243
244 long i, j, x, y, z, n;
245 long xs, xf, ys, yf, zs, zf;
246 long slice_size = p->sizeX()*p->sizeY();
247
248 UnitCell uc = p->unit_cell();
249
250 x = (long) (uc.a()/p->sampling()[0] + 0.5);
251 y = (long) (uc.b()/p->sampling()[1] + 0.5);
252 z = (long) (uc.c()/p->sampling()[2] + 0.5);
253 // xs = (long) -(0.5 + p->image->origin()[0]);
254 // ys = (long) -(0.5 + p->image->origin()[1]);
255 // zs = (long) -(0.5 + p->image->origin()[2]);
256 xs = (long) -p->image->origin()[0];
257 ys = (long) -p->image->origin()[1];
258 zs = (long) -p->image->origin()[2];
259 xf = (long) (xs + p->sizeX() - 1);
260 yf = (long) (ys + p->sizeY() - 1);
261 zf = (long) (zs + p->sizeZ() - 1);
262
263 fimg << endl; // First line blank
264 vector<string> slist = split(p->label(), '\n');
265 n = slist.size();
266 fimg << setw(8) << n << endl; // Second line with number of title lines
267 for ( auto it = slist.begin(); it != slist.end(); ++it )
268 fimg << "REMARKS " << *it << endl; // Title line
269 fimg << right << setw(8) << x << setw(8) << xs << setw(8) << xf
270 << setw(8) << y << setw(8) << ys << setw(8) << yf
271 << setw(8) << z << setw(8) << zs << setw(8) << zf << endl; // Dimensions and origin
272 fimg << setw(12) << uc.a() << setw(12) << uc.b() << setw(12) << uc.c()
273 << setw(12) << uc.alpha()*180/M_PI << setw(12) << uc.beta()*180/M_PI
274 << setw(12) << uc.gamma()*180/M_PI << endl; // Unit cell
275 fimg << "ZYX" << endl; // Line with "ZYX"
276
277 for ( i=z=0; z<p->sizeZ(); z++ ) {
278 fimg << setw(8) << z << endl;
279 for ( j=n=0; j<slice_size; j++, i++ ) {
280 fimg << setw(12) << (*p)[i];
281 n++;
282 if ( n == 6 ) {
283 n = 0;
284 fimg << endl;
285 }
286 }
287 if ( n > 0 ) fimg << endl;
288 }
289
290 fimg << "-9999" << endl; // -9999
291 fimg << setw(12) << p->average() << setw(12) << p->standard_deviation() << endl; // Average and standard deviation
292
293 fimg.close();
294
295 return 0;
296 }
297
298 /*
299 @brief Writing a XPLOR structure factor file format.
300 A structure factor text format used in X-ray crystallography.
301 @param *p the image structure.
302 @return int error code (<0 means failure).
303 **/
304 int writeXPLORsf(Bimage* p)
305 {
306 long i, x, y, z, n = 0, nw = 0, freeflag = 0;
307 long data_size = p->sizeX()*p->sizeY()*p->sizeZ();
308 long h, k, l;
309 float fom1 = 1;
310 float* fom = NULL;
311 if ( p->next ) fom = (float *) p->next->data_pointer();
312 Complex<float>* data = (Complex<float> *) p->data_pointer();
313
314 ofstream fhkl(p->file_name());
315
316 if ( fhkl.fail() ) return -1;
317
318 for ( i=0; i<data_size; i++ ) if ( data[i].amp() >= 1e-10 ) n++;
319
320 fhkl << " NREFlection=" << n << endl;
321 fhkl << " ANOMalous=FALSe { equiv. to HERMitian=TRUE}" << endl;
322 /* Not necessary
323 fhkl << " DECLare NAME=FOBS DOMAin=RECIprocal TYPE=REAL END" << endl;
324 fhkl << " DECLare NAME=PHASE DOMAin=RECIprocal TYPE=REAL END" << endl;
325 fhkl << " DECLare NAME=FOM DOMAin=RECIprocal TYPE=REAL END" << endl;
326 fhkl << " DECLare NAME=TEST DOMAin=RECIprocal TYPE=INTEGER END" << endl;
327 */
328 for ( i=z=0; z<p->sizeZ(); z++ ) {
329 l = z;
330 if ( l > (long)(p->sizeZ() - 1)/2 ) l -= p->sizeZ();
331 for ( y=0; y<p->sizeY(); y++ ) {
332 k = y;
333 if ( k > (long)(p->sizeY() - 1)/2 ) k -= p->sizeY();
334 for ( x=0; x<p->sizeX(); x++, i++ ) {
335 h = x;
336 if ( h > (long)(p->sizeX() - 1)/2 ) h -= p->sizeX();
337 fom1 = 1;
338 if ( data[i].amp() < 1e-10 ) fom1 = 0;
339 if ( fom ) fom1 = fom[i];
340 if ( fom1 > 1e-10 ) {
341 fhkl << " INDEX " << h << " " << k << " " << l
342 << " FOBS=" << data[i].amp()
343 << " PHASE=" << data[i].phi()*180/M_PI
344 << " FOM=" << fom1 << " TEST=" << freeflag << endl;
345 nw++;
346 }
347 }
348 }
349 }
350
351 fhkl.close();
352
353 return 0;
354 }
UnitCell
Definition: UnitCell.h:22
extension
string extension(string filename)
Definition: string_util.cpp:32
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
UnitCell::gamma
void gamma(double d)
Definition: UnitCell.h:41
utilities.h
Header file for general utilities.
readXPLORmap
int readXPLORmap(Bimage *p, int readdata)
Definition: rwXPLOR.cpp:51
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:351
UnitCell::beta
void beta(double d)
Definition: UnitCell.h:40
Complex.h
Class for complex numbers.
UnitCell::a
void a(double d)
Definition: UnitCell.h:36
MAXLINELEN
#define MAXLINELEN
Definition: utilities.h:44
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:230
TComplex
Definition: Bimage.h:59
Float
Definition: utilities.h:157
Bimage
General image parameter class.
Definition: Bimage.h:163
writeXPLOR
int writeXPLOR(Bimage *p)
Writing a XPLOR map or structure factor file format. A text format used in X-ray crystallography.
Definition: rwXPLOR.cpp:221
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:256
M_PI
#define M_PI
Definition: utilities.h:59
linked_list.h
Header file for generalized linked list functions.
verbose
int verbose
Bimage::label
void label(string s)
Definition: Bimage.h:235
readXPLOR
int readXPLOR(Bimage *p, int readdata)
Reading a XPLOR map or structure factor file format. A minimal implementation.
Definition: rwXPLOR.cpp:32
Bimage::unit_cell
void unit_cell(UnitCell uc)
Definition: Bimage.h:601
UnitCell::b
void b(double d)
Definition: UnitCell.h:37
Complex::amp
double amp()
Definition: Complex.h:104
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:127
Bimage::channels
long channels()
Definition: Bimage.h:371
readXPLORsf
int readXPLORsf(Bimage *p, int readdata)
Definition: rwXPLOR.cpp:136
Bimage::standard_deviation
double standard_deviation()
Definition: Bimage.h:537
Bimage::next
Bimage * next
Definition: Bimage.h:165
Complex
Definition: Complex.h:23
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:261
UnitCell::alpha
void alpha(double d)
Definition: UnitCell.h:39
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
kmin
long kmin(long i, long hk)
Definition: Bimage_filter.cpp:17
UnitCell::degrees_to_radians
void degrees_to_radians()
Definition: UnitCell.h:54
kmax
long kmax(long i, long km, long m)
Definition: Bimage_filter.cpp:22
Complex::phi
double phi()
Definition: Complex.h:105
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
rwXPLOR.h
Header file for reading and writing XPLOR reflection files.
Bimage::average
double average(long cc, double xf, double yf, double zf, long nn, double iscale)
Averages in 3D when scaling is less than 1.
Definition: Bimage.cpp:1349
writeXPLORsf
int writeXPLORsf(Bimage *p)
Definition: rwXPLOR.cpp:304
Bimage::images
long images()
Definition: Bimage.h:491
split
vector< string > split(const string &s)
Splits a string on whitespace.
Definition: string_util.cpp:92
writeXPLORmap
int writeXPLORmap(Bimage *p)
Definition: rwXPLOR.cpp:238
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
UnitCell::c
void c(double d)
Definition: UnitCell.h:38
Bimage::image
Bsub_image * image
Definition: Bimage.h:188
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563