Debian Codegraph @

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

raw symbols SCIP
1 // SPDX-License-Identifier: LGPL-3.0-or-later
2 // Author: Kristian Lytje
3
4 #include <io/detail/structure/PDBWriter.h>
5 #include <io/pdb/PDBStructure.h>
6 #include <io/File.h>
7 #include <io/pdb/Terminate.h>
8 #include <io/pdb/PDBAtom.h>
9 #include <io/pdb/PDBWater.h>
10 #include <utility/Exceptions.h>
11 #include <settings/GeneralSettings.h>
12
13 #include <fstream>
14
15 using namespace ausaxs;
16 using namespace ausaxs::io::detail::pdb;
17 using namespace ausaxs::io::pdb;
18
19 std::vector<std::string> as_pdb(const PDBStructure& f) {
20 std::vector<std::string> files;
21 std::string s = f.header.get();
22 Terminate t = f.terminate;
23
24 unsigned int count = 0;
25 int i_ter = t.serial;
26 bool printed_ter = i_ter == -1;
27 for (unsigned int i = 0; i < f.atoms.size(); i++) {
28 if (static_cast<int>(i) == i_ter) { // check if this is where the terminate is supposed to go
29 t.set_serial(t.serial % 100000);
30 s += t.as_pdb(); // write it if so
31 printed_ter = true;
32 }
33 s += f.atoms[i].as_pdb();
34 count++;
35 if (count == 100000) {
36 count = 0;
37 files.push_back(std::move(s));
38 s = "";
39 }
40 }
41
42 // print terminate if missing
43 if (!printed_ter) {
44 t.set_serial(t.serial % 100000);
45 s += t.as_pdb();
46 }
47
48 // print hetatoms
49 for (unsigned int i = 0; i < f.waters.size(); i++) {
50 s += f.waters[i].as_pdb();
51 count++;
52 if (count == 100000) {
53 count = 0;
54 files.push_back(std::move(s));
55 s = "";
56 }
57 }
58
59 s += f.footer.get();
60 files.push_back(std::move(s));
61 return files;
62 }
63
64 void io::detail::pdb::write(const PDBStructure& s, const io::File& path) {
65 path.directory().create();
66
67 auto content = as_pdb(s);
68 if (content.size() == 1) {
69 std::ofstream output(path);
70 if (!output.is_open()) {throw std::ios_base::failure("PDBWriter::write: Could not open file \"" + path.str() + "\"");}
71 output << content.at(0) << std::flush;
72 output.close();
73 if (settings::general::verbose) {std::cout << "Output written to file " + path.str() + "." << std::endl;}
74 }
75 else {
76 for (unsigned int i = 0; i < content.size(); i++) {
77 auto nfile = path.append("_part" + std::to_string(i+1));
78 if (settings::general::verbose) {std::cout << "Output written to file " + nfile.str() << std::endl;}
79 std::ofstream output(nfile);
80 if (!output.is_open()) {throw std::ios_base::failure("PDBWriter::write: Could not open file \"" + path.str() + "\"");}
81 output << content.at(i) << std::flush;
82 output.close();
83 }
84 }
85 }