Debian Codegraph @

avogadro/io/mmtfformat.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "mmtfformat.h"
7 #include <avogadro/core/array.h>
8 #include <avogadro/core/crystaltools.h>
9 #include <avogadro/core/cube.h>
10 #include <avogadro/core/elements.h>
11 #include <avogadro/core/gaussianset.h>
12 #include <avogadro/core/molecule.h>
13 #include <avogadro/core/residue.h>
14 #include <avogadro/core/spacegroups.h>
15 #include <avogadro/core/unitcell.h>
16 #include <avogadro/core/utilities.h>
17
18 #include <mmtf.hpp>
19
20 #include <iostream>
21
22 namespace Avogadro::Io {
23
24 using std::string;
25 using std::vector;
26
27 using Core::Array;
28 using Core::Elements;
29 using Core::lexicalCast;
30 using Core::Molecule;
31
32 namespace {
33 // from latest MMTF code, under the MIT license
34 // https://github.com/rcsb/mmtf-cpp/blob/master/include/mmtf/structure_data.hpp
35 #if MMTF_SPEC_VERSION_MAJOR <= 1 && MMTF_SPEC_VERSION_MINOR < 1
36 bool is_polymer(const unsigned int chain_index,
37 const std::vector<mmtf::Entity>& entity_list)
38 {
39 for (const auto& i : entity_list) {
40 if (std::find(i.chainIndexList.begin(), i.chainIndexList.end(),
41 chain_index) != i.chainIndexList.end()) {
42 return (i.type == "polymer" || i.type == "POLYMER");
43 }
44 }
45 return false;
46 }
47 #endif
48 } // namespace
49
50 bool MMTFFormat::read(std::istream& file, Molecule& molecule)
51 {
52 mmtf::StructureData structure;
53 try {
54 mmtf::decodeFromStream(structure, file);
55 } catch (...) { // if decoding failed, the file is broken
56 return false;
57 }
58
59 // This controls which model we load, currently just the first?
60 size_t modelIndex = 0;
61 size_t atomSkip = 0;
62
63 size_t chainIndex = 0;
64 size_t groupIndex = 0;
65 size_t atomIndex = 0;
66
67 molecule.setData("name", structure.title);
68
69 if (structure.unitCell.size() == 6) {
70 Real a = static_cast<Real>(structure.unitCell[0]);
71 Real b = static_cast<Real>(structure.unitCell[1]);
72 Real c = static_cast<Real>(structure.unitCell[2]);
73 Real alpha = static_cast<Real>(structure.unitCell[3]) * DEG_TO_RAD;
74 Real beta = static_cast<Real>(structure.unitCell[4]) * DEG_TO_RAD;
75 Real gamma = static_cast<Real>(structure.unitCell[5]) * DEG_TO_RAD;
76
77 auto* unitCellObject = new Core::UnitCell(a, b, c, alpha, beta, gamma);
78 if (!unitCellObject->isRegular()) {
79 appendError("cell matrix is singular");
80 delete unitCellObject;
81 return false;
82 }
83 molecule.setUnitCell(unitCellObject);
84 }
85 // spaceGroup
86 if (structure.spaceGroup.size() > 0) {
87 unsigned short hall = 0;
88 hall = Core::SpaceGroups::hallNumber(structure.spaceGroup);
89
90 if (hall != 0) {
91 molecule.setHallNumber(hall);
92 }
93 }
94
95 auto modelChainCount =
96 static_cast<Index>(structure.chainsPerModel[modelIndex]);
97
98 auto entityList = structure.entityList;
99 auto secStructList = structure.secStructList;
100
101 Array<size_t> rawToAtomId;
102 Array<size_t> altAtomIds;
103 Array<int> altAtomCoordSets;
104 Array<char> altAtomLocs;
105 std::set<char> altLocs;
106 Array<Vector3> altAtomPositions;
107
108 for (Index j = 0; j < modelChainCount; j++) {
109
110 auto chainGroupCount =
111 static_cast<Index>(structure.groupsPerChain[chainIndex]);
112
113 bool ok;
114 std::string chainid_string = structure.chainIdList[chainIndex];
115 char chainid = lexicalCast<char>(chainid_string.substr(0, 1), ok);
116
117 bool isPolymer = is_polymer(chainIndex, entityList);
118
119 // A group is like a residue or other molecule in a PDB file.
120 for (size_t k = 0; k < chainGroupCount; k++) {
121
122 auto groupType = static_cast<Index>(structure.groupTypeList[groupIndex]);
123
124 const auto& group = structure.groupList[groupType];
125
126 auto groupId = static_cast<Index>(structure.groupIdList[groupIndex]);
127 auto resname = group.groupName;
128
129 auto& residue = molecule.addResidue(resname, groupId, chainid);
130 // Stores if the group / residue is a heterogen
131 //
132 if (!isPolymer || mmtf::is_hetatm(group.chemCompType.c_str()))
133 residue.setHeterogen(true);
134
135 // Unfortunately, while the spec says secondary structure
136 // is (optionally) in groups, the code doesn't make it available.
137 // group.secStruct is a binary type
138 // https://github.com/rcsb/mmtf/blob/master/spec.md#secstructlist
139 // 0 = pi helix, 1 = bend, 2 = alpha helix, 3 = extended beta, 4 = 3-10
140 // helix, etc.
141 // residue.setSecondaryStructure(group.secStruct);
142 //
143 // instead, we'll get it from secStructList
144 auto secStructure = structure.secStructList[groupIndex];
145 residue.setSecondaryStructure(
146 static_cast<Avogadro::Core::Residue::SecondaryStructure>(secStructure));
147
148 // Save the offset before we go changing it
149 Index atomOffset = atomIndex - atomSkip;
150 Index groupSize = group.atomNameList.size();
151
152 for (Index l = 0; l < groupSize; l++) {
153 Vector3 pos(static_cast<Real>(structure.xCoordList[atomIndex]),
154 static_cast<Real>(structure.yCoordList[atomIndex]),
155 static_cast<Real>(structure.zCoordList[atomIndex]));
156 if (structure.altLocList[atomIndex] != '\0' &&
157 structure.altLocList[atomIndex] != 'A') {
158 rawToAtomId.push_back(-1);
159 altAtomIds.push_back(molecule.atomCount() - 1);
160 altAtomLocs.push_back(structure.altLocList[atomIndex]);
161 altLocs.insert(structure.altLocList[atomIndex]);
162 altAtomPositions.push_back(pos);
163 atomIndex++;
164 continue;
165 }
166
167 auto atom = molecule.addAtom(
168 Elements::atomicNumberFromSymbol(group.elementList[l]));
169
170 atom.setFormalCharge(group.formalChargeList[l]);
171 atom.setPosition3d(pos);
172
173 std::string atomName = group.atomNameList[l];
174 residue.addResidueAtom(atomName, atom);
175 rawToAtomId.push_back(molecule.atomCount() - 1);
176 atomIndex++;
177 }
178
179 // Intra-residue bonds
180 for (size_t l = 0; l < group.bondOrderList.size(); l++) {
181
182 auto atom1 = static_cast<Index>(
183 rawToAtomId[atomOffset + group.bondAtomList[l * 2]]);
184 auto atom2 = static_cast<Index>(
185 rawToAtomId[atomOffset + group.bondAtomList[l * 2 + 1]]);
186
187 char bo = static_cast<char>(group.bondOrderList[l]);
188
189 if (atom1 < molecule.atomCount() && atom2 < molecule.atomCount())
190 molecule.addBond(atom1, atom2, bo);
191 }
192
193 // This is the original PDB Chain name
194 // if (!structure_.chainNameList.empty()) {
195 // structure.chainNameList[chainIndex_];
196 //}
197
198 groupIndex++;
199 }
200
201 chainIndex++;
202 }
203
204 // Use this eventually for multi-model formats
205 modelIndex++;
206
207 // These are for inter-residue bonds
208 for (size_t i = 0; i < structure.bondAtomList.size() / 2; i++) {
209
210 auto atom1 =
211 static_cast<size_t>(rawToAtomId[structure.bondAtomList[i * 2]]);
212 auto atom2 =
213 static_cast<size_t>(rawToAtomId[structure.bondAtomList[i * 2 + 1]]);
214
215 /* Code for multiple models
216 // We are below the atoms we care about
217 if (atom1 < atomSkip || atom2 < atomSkip) {
218 continue;
219 }
220
221 // We are above the atoms we care about
222 if (atom1 > atomIndex || atom2 > atomIndex) {
223 continue;
224 } */
225
226 if (atom1 < molecule.atomCount() && atom2 < molecule.atomCount())
227 molecule.addBond(atom1, atom2, 1); // Always a single bond
228 }
229
230 for (char l : altLocs) {
231 Array<Vector3> coordinateSet = molecule.atomPositions3d();
232 bool found = false;
233 for (size_t i = 0; i < altAtomLocs.size(); i++) {
234 if (altAtomLocs[i] == l) {
235 found = true;
236 coordinateSet[altAtomIds[i]] = altAtomPositions[i];
237 }
238 }
239 if (found) {
240 molecule.setCoordinate3d(coordinateSet, molecule.coordinate3dCount()
241 ? molecule.coordinate3dCount()
242 : 1);
243 }
244 }
245
246 return true;
247 }
248
249 bool MMTFFormat::write(std::ostream&, const Core::Molecule&)
250 {
251 return false;
252 }
253
254 vector<std::string> MMTFFormat::fileExtensions() const
255 {
256 vector<std::string> ext;
257 ext.emplace_back("mmtf");
258 return ext;
259 }
260
261 vector<std::string> MMTFFormat::mimeTypes() const
262 {
263 vector<std::string> mime;
264 mime.emplace_back("chemical/x-mmtf");
265 return mime;
266 }
267
268 } // namespace Avogadro::Io