loadcif.h 6.03 KB
Newer Older
Tobias WEBER's avatar
Tobias WEBER committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
 * get atom positions from cif
 * @author Tobias Weber <tweber@ill.fr>
 * @date Jan-2019
 * @license GPLv3, see 'LICENSE' file
 */

#ifndef __LOAD_CIF_H__
#define __LOAD_CIF_H__

#include <vector>
#include <string>
#include <fstream>
#include <sstream>

#include <boost/algorithm/string.hpp>

#include <gemmi/cif.hpp>
#include <gemmi/symmetry.hpp>

#include "libs/_cxx20/math_algos.h"
using namespace m_ops;


template<class t_real=double>
struct Lattice
{
	t_real a, b, c;
	t_real alpha, beta, gamma;
};


/**
 * gets the symmetry operations from the CIF
 */
template<class t_vec, class t_mat, class t_real = typename t_vec::value_type>
std::vector<t_mat> get_cif_ops(gemmi::cif::Block& block)
{
	std::vector<t_mat> ops;

	auto colOps = block.find_values("_symmetry_equiv_pos_as_xyz");
	for(std::size_t row=0; row<colOps.length(); ++row)
	{
		auto op = gemmi::parse_triplet(colOps[row])/*.wrap()*/;
		auto M = op.float_seitz();

		t_mat mat = m::create<t_mat>({
			std::get<0>(std::get<0>(M)), std::get<1>(std::get<0>(M)), std::get<2>(std::get<0>(M)), std::get<3>(std::get<0>(M)),
			std::get<0>(std::get<1>(M)), std::get<1>(std::get<1>(M)), std::get<2>(std::get<1>(M)), std::get<3>(std::get<1>(M)),
			std::get<0>(std::get<2>(M)), std::get<1>(std::get<2>(M)), std::get<2>(std::get<2>(M)), std::get<3>(std::get<2>(M)),
			std::get<0>(std::get<3>(M)), std::get<1>(std::get<3>(M)), std::get<2>(std::get<3>(M)), std::get<3>(std::get<3>(M)) });

		ops.emplace_back(std::move(mat));
	}

	return ops;
}


/**
 * gets the symmetry operations from the CIF's space group
 */
template<class t_vec, class t_mat, class t_real = typename t_vec::value_type>
std::vector<t_mat> get_cif_sg_ops(gemmi::cif::Block& block)
{
	std::vector<t_mat> ops;

	if(auto val = block.find_values("_symmetry_space_group_name_H-M"); val.length())
	{
		std::string sgname = boost::trim_copy(val[0]);

		// remove quotation marks
		if(sgname[0] == '\'' || sgname[0] == '\"')
			sgname.erase(sgname.begin());
		if(sgname[sgname.size()-1] == '\'' || sgname[sgname.size()-1] == '\"')
			sgname.erase(sgname.begin()+sgname.size()-1);

		if(auto sg = gemmi::find_spacegroup_by_name(sgname))
		{
			auto symops = sg->operations().all_ops_sorted();
			for(const auto &op : symops)
			{
				auto M = op.float_seitz();

				t_mat mat = m::create<t_mat>({
					std::get<0>(std::get<0>(M)), std::get<1>(std::get<0>(M)), std::get<2>(std::get<0>(M)), std::get<3>(std::get<0>(M)),
					std::get<0>(std::get<1>(M)), std::get<1>(std::get<1>(M)), std::get<2>(std::get<1>(M)), std::get<3>(std::get<1>(M)),
					std::get<0>(std::get<2>(M)), std::get<1>(std::get<2>(M)), std::get<2>(std::get<2>(M)), std::get<3>(std::get<2>(M)),
					std::get<0>(std::get<3>(M)), std::get<1>(std::get<3>(M)), std::get<2>(std::get<3>(M)), std::get<3>(std::get<3>(M)) });

				ops.emplace_back(std::move(mat));
			}
		}
	}

	return ops;
}


/**
 * loads the lattice parameters and the atom positions from a CIF
 */
template<class t_vec, class t_mat, class t_real = typename t_vec::value_type>
std::tuple<const char*, std::vector<t_vec>, std::vector<std::vector<t_vec>>, std::vector<std::string>, Lattice<t_real>> 
load_cif(const std::string& filename)
{
	auto ifstr = std::ifstream(filename);
	if(!ifstr)
		return std::make_tuple("Cannot open CIF.", std::vector<t_vec>{}, std::vector<std::vector<t_vec>>{}, std::vector<std::string>{}, Lattice{});

	// load CIF
	auto cif = gemmi::cif::read_istream(ifstr, 4096, filename.c_str());

	if(!cif.blocks.size())
		return std::make_tuple("No blocks in CIF.", std::vector<t_vec>{}, std::vector<std::vector<t_vec>>{}, std::vector<std::string>{}, Lattice{});

	// get the block
	/*const*/ auto& block = cif.sole_block();


	// lattice
	t_real a{}, b{}, c{}, alpha{}, beta{}, gamma{};
	if(auto val = block.find_values("_cell_length_a"); val.length()) std::istringstream{val[0]} >> a;
	if(auto val = block.find_values("_cell_length_b"); val.length()) std::istringstream{val[0]} >> b;
	if(auto val = block.find_values("_cell_length_c"); val.length()) std::istringstream{val[0]} >> c;
	if(auto val = block.find_values("_cell_angle_alpha"); val.length()) std::istringstream{val[0]} >> alpha;
	if(auto val = block.find_values("_cell_angle_beta"); val.length()) std::istringstream{val[0]} >> beta;
	if(auto val = block.find_values("_cell_angle_gamma"); val.length()) std::istringstream{val[0]} >> gamma;

	Lattice<t_real> latt{.a=a, .b=b, .c=c, .alpha=alpha, .beta=beta, .gamma=gamma};


	// fractional atom positions
	std::vector<t_vec> atoms;
	auto tabAtoms = block.find("_atom_site", {"_type_symbol", "_fract_x", "_fract_y", "_fract_z"});

	std::vector<std::string> atomnames;
	for(std::size_t row=0; row<tabAtoms.length(); ++row)
	{
		atomnames.push_back(tabAtoms[row][0]);

		t_real x{}, y{}, z{};
		std::istringstream{tabAtoms[row][1]} >> x;
		std::istringstream{tabAtoms[row][2]} >> y;
		std::istringstream{tabAtoms[row][3]} >> z;
		atoms.emplace_back(t_vec{{x, y, z}});
	}


	// generate all atoms using symmetry ops
	std::vector<std::vector<t_vec>> generatedatoms;
	auto ops = get_cif_ops<t_vec, t_mat, t_real>(block);
	if(!ops.size()) // if ops are not directly given, use standard ops from space group
		ops = get_cif_sg_ops<t_vec, t_mat, t_real>(block);

	for(t_vec atom : atoms)
	{
		// make homogeneuous 4-vector
		if(atom.size() == 3) atom.push_back(1);

		std::vector<t_vec> newatoms;

		for(const auto& op : ops)
		{
			auto newatom = op*atom;
			newatom.resize(3);

			for(int i=0; i<3; ++i)
			{
				newatom[i] = std::fmod(newatom[i], 1);
				while(newatom[i] < 0) newatom[i] += 1;
				while(newatom[i] >= 1) newatom[i] -= 1;

				newatom[i] -= 0.5;
			}


			// position already occupied?
			if(std::find_if(newatoms.begin(), newatoms.end(), [&newatom](const t_vec& vec)->bool
			{
				return m::equals<t_vec>(vec, newatom);
			}) == newatoms.end())
			{
				newatoms.emplace_back(std::move(newatom));
			}
		}

Tobias WEBER's avatar
Tobias WEBER committed
188
189
190
191
		// if no ops are given, just output the raw atom position
		if(!ops.size())
			generatedatoms.push_back(std::vector<t_vec>{{atom}});

Tobias WEBER's avatar
Tobias WEBER committed
192
193
194
195
196
197
198
199
		generatedatoms.emplace_back(std::move(newatoms));
	}


	return std::make_tuple(nullptr, atoms, generatedatoms, atomnames, latt);
}

#endif