Pteros  2.0
Molecular modeling library for human beings!
All Classes Functions Variables Friends Pages
membrane.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 
30 #pragma once
31 #include "pteros/core/selection.h"
32 #include "pteros/core/logging.h"
33 #include "pteros/core/utilities.h"
34 #include <Eigen/Core>
35 
36 namespace pteros {
37 
38 
39 struct LipidSpecies {
40  // Symbolic name of lipid
41  std::string name;
42  // Selection for the whole lipids
43  std::string whole_str;
44  // Selection for the head marker
45  std::string head_marker_str;
46  // Selection for the tail marker
47  std::string tail_marker_str;
48  // Selection for the middle marker
49  std::string mid_marker_str;
50  // Selections for the carbons of each tail
51  std::vector<std::string> tail_carbons_str;
52 };
53 
54 
55 class LipidTail;
56 class LipidMembrane;
57 
58 class LipidMolecule {
59 friend class LipidMembrane;
60 friend class PerSpeciesProperties;
61 public:
62  LipidMolecule(const Selection& lip_mol, const LipidSpecies& sp, int ind, LipidMembrane* parent);
63 
64  void add_to_group(int gr);
65 
66  std::string name;
67  Selection whole_sel;
68  Selection head_marker_sel;
69  Selection tail_marker_sel;
70  Selection mid_marker_sel;
71 
72  // Tails
73  std::vector<LipidTail> tails;
74 
75  // General instanteneous properties
76  Eigen::Vector3f normal;
77  float tilt;
78  float area;
79  int coord_number;
80  Eigen::Vector3f dipole; // Dipole
81  float dipole_proj; // Dipole projected onto the normal
82 
83  // Curvature-related instanteneous properties
84  Eigen::Vector3f smoothed_mid_xyz;
85  float quad_fit_rms;
86  float gaussian_curvature;
87  float mean_curvature;
88 
89  // Indexes of neighbour lipids
90  std::vector<int> neib;
91 
92 private:
93 
94  int id;
95  LipidMembrane* membr_ptr;
96  // Set markers to current COM coordinates of marker seletions
97  void set_markers();
98  void unset_markers();
99 
100  // Coordinates of markers
101  Eigen::Vector3f head_marker, tail_marker, mid_marker, pos_saved;
102 
103  Selection local_sel;
104 };
105 
106 
107 struct LipidTail {
108  LipidTail(const Selection& lipid_sel, const std::string& tail_sel_str);
109  void compute(const LipidMolecule& lipid);
110  int size() const {return carbon_offsets.size();}
111 
112  // Order parameters. Size N-2
113  Eigen::ArrayXf order;
114  // Dihedral angles. Size N-3
115  Eigen::ArrayXf dihedrals;
116  // Relative offsets of carbon atoms indexes in whole lipid selection. Size N.
117  Eigen::VectorXi carbon_offsets;
118 };
119 
120 
121 class PerSpeciesProperties {
122 public:
123  PerSpeciesProperties(LipidMembrane* ptr);
124 
125  float count; // number of lipids of this species. float to avoid overflow.
126  // Area
127  Histogram area_hist;
128  Eigen::Vector2f area; // (mean,std)
129  // Tilt
130  Histogram tilt_hist;
131  Eigen::Vector2f tilt; // (mean,std)
132  // Total dipole
133  Eigen::Vector2f total_dipole; // (mean,std)
134  // Projected dipole
135  Eigen::Vector2f projected_dipole; // (mean,std)
136  // Coordination number
137  Eigen::Vector2f coord_number; // (mean,std)
138  // Trans dihedrals ratio
139  Eigen::Vector2f trans_dihedrals_ratio; // (mean,std)
140  // Curvature
141  Eigen::Vector2f gaussian_curvature;
142  Eigen::Vector2f mean_curvature;
143  // Order parameter
144  std::vector<Eigen::ArrayXf> order; //Sz order parameter identical to "gmx order -szonly"
145  // Abundance of neighboring species
146  std::map<std::string,float> around;
147 
148  // Called at each lipid on each frame
149  void add_data(const LipidMolecule& lip);
150  // Called at the end
151  void post_process(float num_frames);
152 
153  // Returns summary as a string
154  std::string summary();
155 
156  // Save order to file
157  void save_order_to_file(const std::string& fname);
158  void save_around_to_file(const std::string& fname);
159 
160  int num_tails;
161 private:
162  bool order_initialized;
163  LipidMembrane* membr_ptr;
164 };
165 
166 
167 class LipidGroup {
168 public:
169  LipidGroup(LipidMembrane* ptr, int id);
170 
171  void reset(){ lip_ids.clear(); }
172  void add_lipid_id(int i){lip_ids.push_back(i);}
173  void process_frame();
174  void post_process();
175 
176  // Returns summary as a string
177  std::string summary();
178  std::string properties_table();
179  void save_properties_table_to_file(const std::string& fname);
180 
181  // Per group averages (mean,std)
182  float num_lipids, num_frames;
183  Eigen::Vector2f trans_dihedrals_ratio;
184  // Per species averages
185  std::map<std::string,PerSpeciesProperties> species_properties;
186 
187 private:
188  // Group ID
189  int gr_id;
190  // Lipids by ID
191  std::vector<int> lip_ids;
192  // Parent ptr
193  LipidMembrane* membr_ptr;
194 };
195 
196 
197 class LipidMembrane {
198 public:
199  LipidMembrane(System *sys, const std::vector<LipidSpecies>& species, int ngroups);
200 
201  void reset_groups();
202 
203  void compute_properties(float d = 2.0,
204  bool use_external_normal = false,
205  Vector3f_const_ref external_pivot = Eigen::Vector3f::Zero(),
206  Vector3i_const_ref external_dist_dim = Eigen::Vector3i::Ones());
207 
208  void compute_averages();
209  void write_averages(std::string path=".");
210 
211  std::vector<LipidMolecule> lipids;
212  std::vector<LipidGroup> groups;
213 
214  std::vector<std::string> species_names;
215 private:
216  System* system; // Parent system
217  std::shared_ptr<spdlog::logger> log;
218 
219  Selection all_mid_sel;
220 };
221 
222 
223 } // namespace pteros
224 
225 
226 
227