loadcif.h 7.08 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
/**
 * 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;


25

Tobias WEBER's avatar
Tobias WEBER committed
26
27
28
29
30
31
32
33
template<class t_real=double>
struct Lattice
{
	t_real a, b, c;
	t_real alpha, beta, gamma;
};


34

Tobias WEBER's avatar
Tobias WEBER committed
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
/**
 * 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;
}


62

Tobias WEBER's avatar
Tobias WEBER committed
63
64
/**
 * gets the symmetry operations from the CIF's space group
65
 * (use m::equals_all to check if space group operations are the same)
Tobias WEBER's avatar
Tobias WEBER committed
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
 */
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;
}


104

Tobias WEBER's avatar
Tobias WEBER committed
105
106
107
108
109
/**
 * 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>> 
Tobias WEBER's avatar
Tobias WEBER committed
110
load_cif(const std::string& filename, t_real eps=1e-6)
Tobias WEBER's avatar
Tobias WEBER committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
{
	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{};
Tobias WEBER's avatar
Tobias WEBER committed
128
129
130
131
132
133
	if(auto val = block.find_values("_cell_length_a"); val.length()) a = m::stoval<t_real>(val[0]);
	if(auto val = block.find_values("_cell_length_b"); val.length()) b = m::stoval<t_real>(val[0]);
	if(auto val = block.find_values("_cell_length_c"); val.length()) c = m::stoval<t_real>(val[0]);
	if(auto val = block.find_values("_cell_angle_alpha"); val.length()) alpha = m::stoval<t_real>(val[0]);
	if(auto val = block.find_values("_cell_angle_beta"); val.length()) beta = m::stoval<t_real>(val[0]);
	if(auto val = block.find_values("_cell_angle_gamma"); val.length()) gamma = m::stoval<t_real>(val[0]);
Tobias WEBER's avatar
Tobias WEBER committed
134
135
136
137
138
139
140
141
142
143
144
145
146

	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]);

Tobias WEBER's avatar
Tobias WEBER committed
147
148
149
		const t_real x = m::stoval<t_real>(tabAtoms[row][1]);
		const t_real y = m::stoval<t_real>(tabAtoms[row][2]);
		const t_real z = m::stoval<t_real>(tabAtoms[row][3]);
Tobias WEBER's avatar
Tobias WEBER committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
		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);

Tobias WEBER's avatar
Tobias WEBER committed
165
166
		// if no ops are given, just output the raw atom position
		if(!ops.size())
Tobias WEBER's avatar
Tobias WEBER committed
167
		{
Tobias WEBER's avatar
Tobias WEBER committed
168
			generatedatoms.push_back(std::vector<t_vec>{{atom}});
Tobias WEBER's avatar
Tobias WEBER committed
169
170
			continue;
		}
Tobias WEBER's avatar
Tobias WEBER committed
171

Tobias WEBER's avatar
Tobias WEBER committed
172
		std::vector<t_vec> newatoms = m::apply_ops_hom<t_vec, t_mat, t_real>(atom, ops, eps);
Tobias WEBER's avatar
Tobias WEBER committed
173
174
175
176
177
178
179
		generatedatoms.emplace_back(std::move(newatoms));
	}


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

Tobias WEBER's avatar
Tobias WEBER committed
180
181
182
183
184
185
186


/**
 * gets space group description strings and symmetry operations
 */
template<class t_mat, class t_real = typename t_mat::value_type>
std::vector<std::tuple<int, std::string, std::vector<t_mat>>>
Tobias WEBER's avatar
Tobias WEBER committed
187
get_sgs(bool bAddNr=true, bool bAddHall=true)
Tobias WEBER's avatar
Tobias WEBER committed
188
189
190
191
192
193
{
	std::vector<std::tuple<int, std::string, std::vector<t_mat>>> sgs;

	for(const auto &sg : gemmi::spacegroup_tables::main)
	{
		std::ostringstream ostrDescr;
Tobias WEBER's avatar
Tobias WEBER committed
194
195
196
		if(bAddNr) ostrDescr << "#" << sg.number << ": ";
		ostrDescr << sg.hm;
		if(bAddHall) ostrDescr << " (" << sg.hall << ")";
Tobias WEBER's avatar
Tobias WEBER committed
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223

		std::vector<t_mat> ops;
		for(const auto &op : sg.operations().all_ops_sorted())
		{
			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));
		}

		sgs.emplace_back(std::make_tuple(sg.number, ostrDescr.str(), ops));
	}

	std::stable_sort(sgs.begin(), sgs.end(), [](const auto& sg1, const auto& sg2) -> bool
	{
		return std::get<0>(sg1) < std::get<0>(sg2);
	});

	return sgs;
}


Tobias WEBER's avatar
Tobias WEBER committed
224
#endif