File: XYZFileParser.cpp

package info (click to toggle)
rdkit 202503.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220,160 kB
  • sloc: cpp: 399,240; python: 77,453; ansic: 25,517; java: 8,173; javascript: 4,005; sql: 2,389; yacc: 1,565; lex: 1,263; cs: 1,081; makefile: 580; xml: 229; fortran: 183; sh: 105
file content (184 lines) | stat: -rw-r--r-- 5,239 bytes parent folder | download | duplicates (2)
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
//
//  Copyright (C) 2022 Sreya Gogineni and other RDKit contributors
//
//   @@ All Rights Reserved @@
//  This file is part of the RDKit.
//  The contents are covered by the terms of the BSD license
//  which is included in the file license.txt, found at the root
//  of the RDKit source tree.
//

#include <RDGeneral/BoostStartInclude.h>
#include <boost/lexical_cast.hpp>
#include <RDGeneral/BoostEndInclude.h>

#include "FileParsers.h"
#include "FileParserUtils.h"
#include <RDGeneral/StreamOps.h>

#include <RDGeneral/FileParseException.h>
#include <RDGeneral/BadFileException.h>
#include <exception>

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <vector>

namespace RDKit {

void ParseExtraLine(const std::string &extraLine) {
  std::string whitespace{" \t"};
  if (extraLine.find_first_not_of(whitespace) != std::string::npos) {
    std::ostringstream errout;
    errout << "More lines than expected" << std::endl;
    throw FileParseException(errout.str());
  }
}

Atom *ParseXYZFileAtomLine(const std::string &atomLine, RDGeom::Point3D &pos,
                           unsigned int line) {
  std::string whitespace{" \t"};
  size_t delims[8];
  size_t prev = 0;
  for (unsigned int i = 0; i < 7; i++) {
    if (i % 2 == 0) {
      delims[i] = atomLine.find_first_not_of(whitespace, prev);
    } else {
      delims[i] = atomLine.find_first_of(whitespace, prev);
    }
    if (delims[i] == std::string::npos) {
      std::ostringstream errout;
      errout << "Missing coordinates on line " << line << std::endl;
      throw FileParseException(errout.str());
    }
    prev = delims[i];
  }
  delims[7] = atomLine.find_last_not_of(whitespace) + 1;

  // set conformer
  try {
    pos.x = FileParserUtils::toDouble(
        atomLine.substr(delims[2], delims[3] - delims[2]), false);
  } catch (boost::bad_lexical_cast &) {
    std::ostringstream errout;
    errout << "Cannot convert '"
           << atomLine.substr(delims[2], delims[3] - delims[2])
           << "' to double on line " << line << std::endl;
    throw FileParseException(errout.str());
  }

  try {
    pos.y = FileParserUtils::toDouble(
        atomLine.substr(delims[4], delims[5] - delims[4]), false);
  } catch (boost::bad_lexical_cast &) {
    std::ostringstream errout;
    errout << "Cannot convert '"
           << atomLine.substr(delims[4], delims[5] - delims[4])
           << "' to double on line " << line << std::endl;
    throw FileParseException(errout.str());
  }

  try {
    pos.z = FileParserUtils::toDouble(
        atomLine.substr(delims[6], delims[7] - delims[6]), false);
  } catch (boost::bad_lexical_cast &) {
    std::ostringstream errout;
    errout << "Cannot convert '"
           << atomLine.substr(delims[6], delims[7] - delims[6])
           << "' to double on line " << line << std::endl;
    throw FileParseException(errout.str());
  }

  std::string symb{atomLine.substr(delims[0], delims[1] - delims[0])};
  if (symb.size() == 2 && symb[1] >= 'A' && symb[1] <= 'Z') {
    symb[1] = static_cast<char>(tolower(symb[1]));
  }

  Atom *atom;
  try {
    atom = new Atom(PeriodicTable::getTable()->getAtomicNumber(symb));
  } catch (const Invar::Invariant &e) {
    throw FileParseException(e.what());
  }

  return atom;
}

namespace v2 {
namespace FileParsers {

std::unique_ptr<RWMol> MolFromXYZDataStream(std::istream &inStream) {
  unsigned int numAtoms = 0;

  std::string num{getLine(inStream)};
  try {
    numAtoms = FileParserUtils::toUnsigned(num);
  } catch (boost::bad_lexical_cast &) {
    std::ostringstream errout;
    errout << "Unable to recognize the number of atoms: cannot convert '" << num
           << "' to unsigned int on line 0" << std::endl;
    throw FileParseException(errout.str());
  }

  std::string comment{getLine(inStream)};

  auto mol = std::make_unique<RWMol>();
  if (numAtoms) {
    Conformer *conf = new Conformer(numAtoms);
    if (!comment.empty()) {
      mol->setProp("_FileComments", comment);
    }
    for (unsigned int i = 0; i < numAtoms; i++) {
      if (inStream.eof()) {
        throw FileParseException("EOF hit while reading atoms");
      }
      RDGeom::Point3D pos;
      std::string atomLine{getLine(inStream)};
      Atom *atom = ParseXYZFileAtomLine(atomLine, pos, i + 2);
      unsigned int idx = mol->addAtom(atom, false, true);
      conf->setAtomPos(idx, pos);
    }
    mol->addConformer(conf);
  }

  while (!inStream.eof()) {
    std::string extraLine{getLine(inStream)};
    ParseExtraLine(extraLine);
  }

  return mol;
}

std::unique_ptr<RWMol> MolFromXYZBlock(const std::string &xyzBlock) {
  std::istringstream xyz(xyzBlock);

  xyz.peek();
  if (!xyz.eof()) {
    return MolFromXYZDataStream(xyz);
  } else {
    return nullptr;
  }
}

std::unique_ptr<RWMol> MolFromXYZFile(const std::string &fName) {
  std::ifstream xyzFile(fName);
  if (!xyzFile || (xyzFile.bad())) {
    std::ostringstream errout;
    errout << "Bad input file " << fName;
    throw BadFileException(errout.str());
  }

  xyzFile.peek();
  if (!xyzFile.eof()) {
    return MolFromXYZDataStream(xyzFile);
  } else {
    return nullptr;
  }
}
}  // namespace FileParsers
}  // namespace v2
}  // namespace RDKit