| 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 "gromacsformat.h" |
| 7 |
|
| 8 |
#include <avogadro/core/avogadrocore.h> |
| 9 |
|
| 10 |
#include <avogadro/core/atom.h> |
| 11 |
#include <avogadro/core/elements.h> |
| 12 |
#include <avogadro/core/matrix.h> |
| 13 |
#include <avogadro/core/molecule.h> |
| 14 |
#include <avogadro/core/residue.h> |
| 15 |
#include <avogadro/core/unitcell.h> |
| 16 |
#include <avogadro/core/utilities.h> |
| 17 |
|
| 18 |
#include <iostream> |
| 19 |
|
| 20 |
#include <string> |
| 21 |
|
| 22 |
using namespace std::string_literals; |
| 23 |
|
| 24 |
namespace Avogadro::Io { |
| 25 |
|
| 26 |
using Core::Atom; |
| 27 |
using Core::Elements; |
| 28 |
using Core::lexicalCast; |
| 29 |
using Core::Molecule; |
| 30 |
using Core::Residue; |
| 31 |
using Core::split; |
| 32 |
using Core::trimmed; |
| 33 |
using Core::UnitCell; |
| 34 |
|
| 35 |
std::vector<std::string> GromacsFormat::fileExtensions() const |
| 36 |
{ |
| 37 |
return std::vector<std::string>(1, "gro"s); |
| 38 |
} |
| 39 |
|
| 40 |
std::vector<std::string> GromacsFormat::mimeTypes() const |
| 41 |
{ |
| 42 |
return std::vector<std::string>(1, "chemical/x-gro"s); |
| 43 |
} |
| 44 |
|
| 45 |
bool GromacsFormat::read(std::istream& in, Molecule& molecule) |
| 46 |
{ |
| 47 |
// Allow ADL ofr string |
| 48 |
using std::string; |
| 49 |
|
| 50 |
string buffer; |
| 51 |
string value; |
| 52 |
Residue* r = nullptr; |
| 53 |
size_t currentResidueId = 0; |
| 54 |
|
| 55 |
// Title |
| 56 |
std::getline(in, buffer); |
| 57 |
if (!buffer.empty()) |
| 58 |
molecule.setData("name", trimmed(buffer)); |
| 59 |
|
| 60 |
// Atom count |
| 61 |
std::getline(in, buffer); |
| 62 |
buffer = trimmed(buffer); |
| 63 |
bool ok; |
| 64 |
auto numAtoms = lexicalCast<size_t>(buffer, ok); |
| 65 |
if (buffer.empty() || !ok) { |
| 66 |
appendError("Number of atoms (line 2) invalid."); |
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
// read atom info: |
| 71 |
using AtomTypeMap = std::map<string, unsigned char>; |
| 72 |
AtomTypeMap atomTypes; |
| 73 |
unsigned char customElementCounter = CustomElementMin; |
| 74 |
Vector3 pos; |
| 75 |
while (numAtoms-- > 0) { |
| 76 |
std::getline(in, buffer); |
| 77 |
// Figure out the distance between decimal points, implement support for |
| 78 |
// variable precision as specified: |
| 79 |
// "any number of decimal places, the format will then be n+5 positions with |
| 80 |
// n decimal places (n+1 for velocities) in stead of 8 with 3 (with 4 for |
| 81 |
// velocities)". |
| 82 |
size_t decimal1 = buffer.find(".", 20); |
| 83 |
size_t decimal2 = string::npos; |
| 84 |
int decimalSep = 0; |
| 85 |
if (decimal1 != string::npos) |
| 86 |
decimal2 = buffer.find(".", decimal1 + 1); |
| 87 |
if (decimal2 != string::npos) |
| 88 |
decimalSep = decimal2 - decimal1; |
| 89 |
if (decimalSep == 0) { |
| 90 |
appendError("Decimal separation of 0 found in atom positions: " + buffer); |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
if (buffer.size() < static_cast<size_t>(20 + 3 * decimalSep)) { |
| 95 |
appendError("Error reading atom specification -- line too short: " + |
| 96 |
buffer); |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
// Format of buffer is: (all indices start at 1, variable dp throws this). |
| 101 |
// Offset: 0 format: %5i value: Residue number |
| 102 |
// Offset: 5 format: %-5s value: Residue name |
| 103 |
// Offset: 10 format: %5s value: Atom name |
| 104 |
// Offset: 15 format: %5i value: Atom number |
| 105 |
// Offset: 20 format: %8.3f value: x coordinate (nm) |
| 106 |
// Offset: 28 format: %8.3f value: y coordinate (nm) |
| 107 |
// Offset: 36 format: %8.3f value: z coordinate (nm) |
| 108 |
// Offset: 44 format: %8.4f value: x velocity (nm/ps, a.k.a. km/s) |
| 109 |
// Offset: 52 format: %8.4f value: y velocity (nm/ps, a.k.a. km/s) |
| 110 |
// Offset: 60 format: %8.4f value: z velocity (nm/ps, a.k.a. km/s) |
| 111 |
|
| 112 |
auto residueId = lexicalCast<size_t>(buffer.substr(0, 5), ok); |
| 113 |
if (!ok) { |
| 114 |
appendError("Failed to parse residue sequence number: " + |
| 115 |
buffer.substr(0, 5)); |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
if (residueId != currentResidueId) { |
| 120 |
currentResidueId = residueId; |
| 121 |
|
| 122 |
auto residueName = lexicalCast<string>(buffer.substr(5, 5), ok); |
| 123 |
if (!ok) { |
| 124 |
appendError("Failed to parse residue name: " + buffer.substr(5, 5)); |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
// gro files do not have a chain ID. So we use a makeshift dummy ID |
| 129 |
char dummyChainId = '0'; |
| 130 |
r = &molecule.addResidue(residueName, currentResidueId, dummyChainId); |
| 131 |
} |
| 132 |
|
| 133 |
// Atom name: |
| 134 |
value = trimmed(buffer.substr(10, 5)); |
| 135 |
Atom atom; |
| 136 |
int atomicNum = 0; |
| 137 |
if (r != nullptr) |
| 138 |
r->atomicNumber(value); |
| 139 |
if (atomicNum) { |
| 140 |
atom = molecule.addAtom(atomicNum); |
| 141 |
} else { |
| 142 |
unsigned char atomicNumFromSymbol = |
| 143 |
Elements::atomicNumberFromSymbol(value); |
| 144 |
if (atomicNumFromSymbol != 255) { |
| 145 |
atom = molecule.addAtom(atomicNumFromSymbol); |
| 146 |
} else { |
| 147 |
auto it = atomTypes.find(value); |
| 148 |
if (it == atomTypes.end()) { |
| 149 |
atomTypes.insert(std::make_pair(value, customElementCounter++)); |
| 150 |
it = atomTypes.find(value); |
| 151 |
if (customElementCounter > CustomElementMax) { |
| 152 |
appendError("Custom element type limit exceeded."); |
| 153 |
return false; |
| 154 |
} |
| 155 |
} |
| 156 |
atom = molecule.addAtom(it->second); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
// Coords |
| 161 |
for (int i = 0; i < 3; ++i) { |
| 162 |
value = trimmed(buffer.substr(20 + i * decimalSep, decimalSep)); |
| 163 |
pos[i] = lexicalCast<Real>(value, ok); |
| 164 |
if (!ok || value.empty()) { |
| 165 |
appendError( |
| 166 |
"Error reading atom specification -- invalid coordinate: '" + buffer + |
| 167 |
"' (bad coord: '" + value + "')"); |
| 168 |
return false; |
| 169 |
} |
| 170 |
} |
| 171 |
atom.setPosition3d(pos * static_cast<Real>(10.0)); // nm --> Angstrom |
| 172 |
if (r) { |
| 173 |
r->addResidueAtom(value, atom); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
// Set the custom element map if needed: |
| 178 |
if (!atomTypes.empty()) { |
| 179 |
Molecule::CustomElementMap elementMap; |
| 180 |
for (const auto& atomType : atomTypes) { |
| 181 |
elementMap.insert(std::make_pair(atomType.second, atomType.first)); |
| 182 |
} |
| 183 |
molecule.setCustomElementMap(elementMap); |
| 184 |
} |
| 185 |
|
| 186 |
// Box description: |
| 187 |
// v1(x) v2(y) v3(z) [v1(y) v1(z) v2(x) v2(z) v3(x) v3(y)] |
| 188 |
// The last six values may be omitted, set all non-specified values to 0. |
| 189 |
// v1(y) == v1(z) == v2(z) == 0 always. |
| 190 |
std::getline(in, buffer); |
| 191 |
std::vector<string> tokens(split(buffer, ' ', true)); |
| 192 |
if (tokens.size() > 0) { |
| 193 |
if (tokens.size() != 3 && tokens.size() != 9) { |
| 194 |
appendError("Invalid box specification -- need either 3 or 9 values: '" + |
| 195 |
buffer + "'"); |
| 196 |
return false; |
| 197 |
} |
| 198 |
|
| 199 |
// Index arrays for parsing loop: |
| 200 |
const int rows[] = { 0, 1, 2, 1, 2, 0, 2, 0, 1 }; |
| 201 |
const int cols[] = { 0, 1, 2, 0, 0, 1, 1, 2, 2 }; |
| 202 |
|
| 203 |
Matrix3 cellMatrix = Matrix3::Zero(); |
| 204 |
for (size_t i = 0; i < tokens.size(); ++i) { |
| 205 |
cellMatrix(rows[i], cols[i]) = lexicalCast<Real>(tokens[i], ok); |
| 206 |
if (!ok || tokens[i].empty()) { |
| 207 |
appendError("Invalid box specification -- bad value: '" + tokens[i] + |
| 208 |
"'"); |
| 209 |
return false; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
auto* cell = new UnitCell; |
| 214 |
cell->setCellMatrix(cellMatrix * static_cast<Real>(10)); // nm --> Angstrom |
| 215 |
if (!cell->isRegular()) { |
| 216 |
appendError("box vectors are not linear independent"); |
| 217 |
delete cell; |
| 218 |
return false; |
| 219 |
} |
| 220 |
molecule.setUnitCell(cell); |
| 221 |
} |
| 222 |
|
| 223 |
return true; |
| 224 |
} |
| 225 |
|
| 226 |
bool GromacsFormat::write(std::ostream&, const Core::Molecule&) |
| 227 |
{ |
| 228 |
return false; |
| 229 |
} |
| 230 |
|
| 231 |
} // namespace Avogadro::Io |