Debian Codegraph @

source/core/io/detail/structure/PDBReader.cpp

raw symbols SCIP
1 // SPDX-License-Identifier: LGPL-3.0-or-later
2 // Author: Kristian Lytje
3
4 #include <io/detail/structure/PDBReader.h>
5 #include <io/ExistingFile.h>
6 #include <io/pdb/Terminate.h>
7 #include <io/pdb/PDBAtom.h>
8 #include <io/pdb/PDBWater.h>
9 #include <utility/Exceptions.h>
10 #include <utility/StringUtils.h>
11 #include <settings/GeneralSettings.h>
12 #include <settings/MoleculeSettings.h>
13 #include <constants/ConstantsFwd.h>
14 #include <utility/StringUtils.h>
15 #include <utility/Console.h>
16
17 #include <fstream>
18
19 using namespace ausaxs;
20 using namespace ausaxs::io::pdb;
21
22 auto parse_single_file = [] (const io::ExistingFile& file, io::pdb::PDBStructure& collection) -> void {
23 // check if file was succesfully opened
24 std::ifstream input(file);
25 if (!input.is_open()) {throw except::io_error("PDBReader::read: Could not open file \"" + file.str() + "\"");}
26
27 unsigned int discarded_hydrogens = 0;
28 std::string line;
29 while(getline(input, line)) {
30 if (utility::remove_all(line, " \n\r").empty()) {continue;}
31 std::string type = line.substr(0, std::min(6, int(line.size()))); // read the first 6 characters
32 switch(Record::get_type(type)) {
33 case RecordType::ATOM: {
34 // first just parse it as an atom; we can reuse it anyway even if it is a water molecule
35 PDBAtom atom;
36 atom.parse_pdb(line);
37
38 // check if this is a hydrogen atom
39 if (atom.element == constants::atom_t::H && !settings::general::keep_hydrogens) {
40 discarded_hydrogens++;
41 continue;
42 }
43
44 // check if this is a water molecule
45 if (atom.is_water()) {collection.add(PDBWater(std::move(atom)));}
46 else {collection.add(std::move(atom));}
47 break;
48 } case RecordType::TERMINATE: {
49 Terminate term;
50 term.parse_pdb(line);
51 collection.add(term);
52 break;
53 } case RecordType::HEADER: {
54 collection.add(RecordType::HEADER, line);
55 break;
56 } case RecordType::FOOTER: {
57 collection.add(RecordType::FOOTER, line);
58 break;
59 } case RecordType::NOTYPE: {
60 break;
61 } default: {
62 throw except::io_error("PDBReader::read: Malformed input file - unrecognized type \"" + type + "\".");
63 }
64 };
65 }
66 input.close();
67
68 if (!settings::molecule::use_occupancy) {
69 for (auto& a : collection.atoms) {a.occupancy = 1.0;}
70 }
71
72 if (discarded_hydrogens != 0) {
73 console::print_text("Discarded " + std::to_string(discarded_hydrogens) + " explicit hydrogen atoms.");
74 }
75 };
76
77 io::pdb::PDBStructure io::detail::pdb::read(const io::File& path) {
78 console::print_info("Reading PDB file from \"" + path.str() + "\"");
79 console::indent();
80
81 io::pdb::PDBStructure res;
82 if (path.append("_part1").exists()) {
83 console::print_text("File is split into multiple parts.");
84 unsigned int i = 1;
85 while (path.append("_part" + std::to_string(i)).exists()) {
86 console::print_text("\tParsed file " + path.append("_part" + std::to_string(i)).str());
87 parse_single_file(path.append("_part" + std::to_string(i)), res);
88 i++;
89 }
90 } else {
91 parse_single_file(path, res);
92 }
93
94 unsigned int n_pa = res.atoms.size();
95 unsigned int n_ha = res.waters.size();
96
97 console::print_text("Successfully read " + std::to_string(n_pa + n_ha) + " atomic records.");
98 if (n_ha != 0) {console::print_text("\t" + std::to_string(res.waters.size()) + " of these are hydration atoms.");}
99 console::unindent();
100 return res;
101 }