Pteros  2.0
Molecular modeling library for human beings!
file_handler.h
1 /*
2  * This file is a part of
3  *
4  * ============================================
5  * ### Pteros molecular modeling library ###
6  * ============================================
7  *
8  * https://github.com/yesint/pteros
9  *
10  * (C) 2009-2021, Semen Yesylevskyy
11  *
12  * All works, which use Pteros, should cite the following papers:
13  *
14  * 1. Semen O. Yesylevskyy, "Pteros 2.0: Evolution of the fast parallel
15  * molecular analysis library for C++ and python",
16  * Journal of Computational Chemistry, 2015, 36(19), 1480–1488.
17  * doi: 10.1002/jcc.23943.
18  *
19  * 2. Semen O. Yesylevskyy, "Pteros: Fast and easy to use open-source C++
20  * library for molecular analysis",
21  * Journal of Computational Chemistry, 2012, 33(19), 1632–1636.
22  * doi: 10.1002/jcc.22989.
23  *
24  * This is free software distributed under Artistic License:
25  * http://www.opensource.org/licenses/artistic-license-2.0.php
26  *
27 */
28 
29 #pragma once
30 
31 #include <string>
32 #include "pteros/core/system.h"
33 #include "pteros/core/selection.h"
34 #include <bitset>
35 
36 namespace pteros {
37 
38 class FileContent {
39 public:
40  FileContent(){
41  flags.reset();
42  }
43 
44  // The list of atoms and their properties
45  bool atoms() const { return flags[0]; }
46  FileContent atoms(bool val){ flags[0] = val; return *this;}
47 
48  // Single set of coordinates
49  bool coord() const { return flags[1]; }
50  FileContent coord(bool val){ flags[1] = val; return *this;}
51 
52  // Many frames
53  bool traj() const { return flags[2]; }
54  FileContent traj(bool val){ flags[2] = val; return *this;}
55 
56  // Molecular topology
57  bool top() const { return flags[3]; }
58  FileContent top(bool val){ flags[3] = val; return *this;}
59 
60  // Random access trajectory
61  bool rand() const { return flags[4]; }
62  FileContent rand(bool val){ flags[4] = val; return *this;}
63 
64 private:
65  std::bitset<5> flags;
66 };
67 
68 //------------------------------------------------------------------------------
69 
70 class FileHandler;
71 using FileHandler_ptr = std::unique_ptr<FileHandler>;
72 
74 class FileHandler {
75 public:
77  static FileHandler_ptr recognize(std::string fname);
78 
86  static FileHandler_ptr open(std::string fname, char open_mode);
87 
89  virtual void open(char open_mode) = 0;
90  virtual void close();
91 
92  virtual ~FileHandler();
93 
97  bool read(System* sys, Frame* frame, const FileContent& what);
98 
100  void write(const Selection& sel, const FileContent& what);
101 
103  virtual FileContent get_content_type() const = 0;
104 
105 protected:
106  FileHandler(std::string& file_name);
107 
108  // Stores file name
109  std::string fname;
110  // Number of atoms
111  int natoms;
112 
113  // Method to sanity check parameters send to read and write
114  void sanity_check_read(System* sys, Frame* frame, const FileContent &what) const;
115  void sanity_check_write(const Selection& sel, const FileContent& what) const;
116 
118  virtual bool do_read(System* sys, Frame* frame, const FileContent& what) = 0;
119 
121  virtual void do_write(const Selection& sel, const FileContent& what) = 0;
122 };
123 
124 //------------------------------------------------------------------------------
125 
126 class FileHandlerRandomAccess: public FileHandler {
127 protected:
128  FileHandlerRandomAccess(std::string& file_name): FileHandler(file_name) {}
129 
130 public:
131  // Seek frame
132  virtual void seek_frame(int fr) = 0;
133 
134  // Seek time
135  virtual void seek_time(float t) = 0;
136 
137  // Report current position in trajectory
138  virtual void tell_current_frame_and_time(int& step, float& t) = 0;
139 
140  // Report last position in trajectory
141  virtual void tell_last_frame_and_time(int& step, float& t) = 0;
142 };
143 
144 //------------------------------------------------------------------------------
145 
146 } // namespace pteros
147 
148 
pteros::Selection
Selection class.
Definition: selection.h:65
pteros::FileHandler
Generic API for reading and writing any molecule file formats.
Definition: file_handler.h:74
pteros::FileHandler::write
void write(const Selection &sel, const FileContent &what)
Write data from selection specidied by what.
Definition: file_handler.cpp:69
pteros::FileHandler::read
bool read(System *sys, Frame *frame, const FileContent &what)
Reads data, which are specified by what.
Definition: file_handler.cpp:64
pteros::FileHandler::open
static FileHandler_ptr open(std::string fname, char open_mode)
Recognize file extension, open file for reading or writing and return a file handler.
Definition: file_handler.cpp:131
pteros::System
The system of atoms.
Definition: system.h:93
pteros::FileHandler::recognize
static FileHandler_ptr recognize(std::string fname)
Recognizes file extension and returns a file handler.
Definition: file_handler.cpp:109
pteros::FileHandler::get_content_type
virtual FileContent get_content_type() const =0
Reports content of this file type.
pteros::Frame
Definition of single trajectory frame.
Definition: system.h:51