2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2013,2014,2015, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Implements gmx::analysismodules::Freevolume.
39 * \author Titov Anatoly <Wapuk-cobaka@yandex.ru>
40 * \ingroup module_trajectoryanalysis
51 #include <gromacs/trajectoryanalysis.h>
52 #include <gromacs/utility/smalloc.h>
53 #include <gromacs/math/do_fit.h>
54 #include <gromacs/trajectoryanalysis/topologyinformation.h>
60 void make_correlation_matrix_file(std::vector< std::vector< std::vector< float > > > correlations, const char* file_name, int start)
63 file = std::fopen(file_name, "w+");
64 for (int i = start; i < correlations.size(); i++) {
65 if (correlations.size() - start > 1) {
66 std::fprintf(file, "correlation between 'n' and 'n + %d' frames\n", i);
69 std::cout << "correlation between 'n' and 'n + " << i << "' frames\n";
71 for (int j = 0; j < correlations[i].size(); j++) {
72 for (int f = 0; f < correlations[i][j].size(); f++) {
73 std::fprintf(file, "%3.4f ", correlations[i][j][f]);
75 std::fprintf(file, "\n");
81 void make_correlation_pairs_file(std::vector< std::vector< std::vector< float > > > correlations, const char* file_name, int start)
84 file = std::fopen(file_name, "w+");
85 for (int i = 0; i < correlations.front().size(); i++) {
86 for (int j = 0; j < correlations.front().front().size(); j++) {
87 //std::fprintf(file, "correlation between point '%d' and point '%d'\n", i, j);
88 std::fprintf(file, "%d %d\n", i, j);
89 for (int k = 0; k < correlations.size(); k++) {
90 std::fprintf(file, "%3.4f ", correlations[k][i][j]);
92 std::fprintf(file, "\n");
95 std::cout << "correlations in row " << i << " out of " << correlations.front().size() << " completed\n";
101 void make_rout_file(float crl_border, std::vector< int > indx, std::vector< std::vector< std::pair< int, int > > > rout, const char* file_name)
104 file = std::fopen(file_name, "w+");
105 std::fprintf(file, "correlations >= %0.2f\n\n", crl_border);
106 for (int i = 0; i < rout.size(); i++) {
107 for (int j = 0; j < rout[i].size(); j++) {
108 std::fprintf(file, "cgo_arrow (id %3d), (id %3d), radius=0.15\n", indx[rout[i][j].first]/* + 1*/, indx[rout[i][j].second]/* + 1*/);
110 std::fprintf(file, "\n\n");
115 void make_best_corrs_graphics(std::vector< std::vector< std::vector< float > > > correlations,
116 std::vector< std::vector< std::pair< int, int > > > rout_pairs,
117 std::vector< int > indx,
118 const char* file_name)
121 file = std::fopen(file_name, "w+");
122 for (int i = 0; i < rout_pairs.size(); i++) {
123 for (int j = 0; j < rout_pairs[i].size(); j++) {
124 std::fprintf(file, "%3d %3d\n", indx[rout_pairs[i][j].first]/* + 1*/, indx[rout_pairs[i][j].second]/* + 1*/);
125 for (int k = 0; k < correlations.size(); k++) {
126 std::fprintf(file, "%3.5f ", correlations[k][rout_pairs[i][j].first][rout_pairs[i][j].second]);
128 std::fprintf(file, "\n");
134 void make_diffusion_file(const char* file_name, std::vector< float > D)
137 file = std::fopen(file_name, "w+");
138 for (int i = 0; i < D.size(); i++) {
139 std::fprintf(file, "%f ", D[i]);
144 bool mysortfunc (std::vector< int > a, std::vector< int > b) {
145 return (a.size() > b.size());
148 bool isitsubset (std::vector< int > a, std::vector< int > b) {
152 std::sort(a.begin(), a.end());
153 std::sort(b.begin(), b.end());
155 for (int i = 0; i < a.size(); i++) {
167 void correlation_evaluation(std::vector< RVec > ref, std::vector< std::vector< RVec > > traj, int b_frame, std::vector< std::vector< std::vector< float > > > &crl, int tauS, int tauE) {
168 crl.resize(tauE + 1);
169 for (int i = 0; i < crl.size(); i++) {
170 crl[i].resize(traj.front().size());
171 for (int j = 0; j < crl[i].size(); j++) {
172 crl[i][j].resize(traj.front().size(), 0);
180 std::vector< float > d;
181 d.resize(traj.front().size(), 0);
183 #pragma omp parallel for schedule(dynamic)
184 for (int i = tauS; i <= tauE; i += 1) {
186 std::vector< std::vector< float > > a, b, c;
187 a.resize(traj.front().size(), d);
188 b.resize(traj.front().size(), d);
189 c.resize(traj.front().size(), d);
190 for (int j = 0; j < traj.size() - i - 1; j++) {
191 for (int f1 = 0; f1 < traj.front().size(); f1++) {
192 for (int f2 = 0; f2 < traj.front().size(); f2++) {
193 temp1 = traj[j][f1] - ref[f1];
194 temp2 = traj[j + i][f2] - ref[f2];
195 a[f1][f2] += (temp1[0] * temp2[0] + temp1[1] * temp2[1] + temp1[2] * temp2[2]);
196 b[f1][f2] += (temp1[0] * temp1[0] + temp1[1] * temp1[1] + temp1[2] * temp1[2]);
197 c[f1][f2] += (temp2[0] * temp2[0] + temp2[1] * temp2[1] + temp2[2] * temp2[2]);
201 for (int j = 0; j < traj.front().size(); j++) {
202 for (int f = 0; f < traj.front().size(); f++) {
203 crl[i][j][f] = a[j][f] / (std::sqrt(b[j][f] * c[j][f]));
206 std::cout << i << " corr done\n";
211 void graph_calculation(std::vector< std::vector< std::pair< float, int > > > &graph, std::vector< std::vector< int > > &s_graph, std::vector< std::vector< std::pair< int, int > > > &s_graph_rbr,
212 std::vector< std::vector< RVec > > traj, int b_frame,
213 std::vector< std::vector< std::vector< float > > > crl, float crl_b, float e_rad, int tauE) {
214 graph.resize(traj.front().size());
215 for (int i = 0; i < traj.front().size(); i++) {
216 graph[i].resize(traj.front().size(), std::make_pair(0, -1));
219 for (int i = 1; i <= tauE; i++) {
220 for (int j = 0; j < traj.front().size(); j++) {
221 for (int f = j; f < traj.front().size(); f++) {
222 temp = traj[b_frame][j] - traj[b_frame][f];
223 if (std::max(std::abs(crl[i][j][f]), std::abs(crl[i][f][j])) >= crl_b && norm(temp) <= e_rad && std::abs(graph[j][f].first) < std::max(std::abs(crl[i][j][f]), std::abs(crl[i][f][j]))) {
224 if (std::abs(crl[i][j][f]) > std::abs(crl[i][f][j])) {
225 graph[j][f].first = crl[i][j][f];
227 graph[j][f].first = crl[i][f][j];
229 graph[j][f].second = i;
234 std::cout << "crl analysed\n";
235 std::vector< bool > graph_flags;
236 graph_flags.resize(traj.front().size(), true);
237 std::vector< int > a;
239 std::vector< std::pair< int, int > > b;
241 std::vector< int > que1, que2, que3;
242 for (int i = 0; i < traj.front().size(); i++) {
243 if (graph_flags[i]) {
244 s_graph.push_back(a);
245 s_graph_rbr.push_back(b);
251 graph_flags[i] = false;
252 while(que1.size() > 0) {
254 for (int k = 0; k < que1.size(); k++) {
255 for (int j = 0; j < traj.front().size(); j++) {
256 if (graph[que1[k]][j].second > -1 && graph_flags[j]) {
258 graph_flags[j] = false;
263 for (int j = 0; j < que2.size(); j++) {
264 que3.push_back(que2[j]);
267 s_graph.back() = que3;
268 for (int j = 0; j < que3.size(); j++) {
269 for (int k = 0; k < traj.front().size(); k++) {
270 if (graph[que3[j]][k].second > -1) {
271 s_graph_rbr.back().push_back(std::make_pair(que3[j], k));
275 //std::cout << s_graph.back().size() << " ";
280 bool myfunction (const std::pair< int, float > i, const std::pair< int, float > j) {
281 return i.second < j.second;
284 void graph_back_bone_evaluation(std::vector< std::vector < std::pair< int, int > > > &rout_n, int indxSize,
285 std::vector< std::vector< std::pair< float, int > > > graph, std::vector< std::vector< int > > s_graph, std::vector< std::vector< std::pair< int, int > > > s_graph_rbr) {
286 std::vector< float > key;
287 std::vector< int > path;
288 std::vector< std::pair< int, float > > que;
289 std::vector< std::pair< int, int > > a;
291 for (int i = 0; i < s_graph.size(); i++) {
296 if (s_graph[i].size() > 2) {
297 key.resize(indxSize, 2);
298 path.resize(indxSize, -1);
299 key[s_graph[i][0]] = 0;
300 for (int j = 0; j < s_graph[i].size(); j++) {
301 que.push_back(std::make_pair(s_graph[i][j], key[s_graph[i][j]]));
303 std::sort(que.begin(), que.end(), myfunction);
304 while (!que.empty()) {
306 que.erase(que.begin());
307 for (int j = 0; j < s_graph_rbr[i].size(); j++) {
309 if (s_graph_rbr[i][j].first == v) {
310 u = s_graph_rbr[i][j].second;
311 } else if (s_graph_rbr[i][j].second == v) {
312 u = s_graph_rbr[i][j].first;
316 for (int k = 0; k < que.size(); k++) {
317 if (que[k].first == u) {
323 if (flag && key[u] > 1 - std::abs(graph[v][u].first)) {
325 key[u] = 1 - std::abs(graph[v][u].first);
326 que[pos].second = key[u];
327 sort(que.begin(), que.end(), myfunction);
333 for (int j = 0; j < indxSize; j++) {
335 rout_n.back().push_back(std::make_pair(j, path[j]));
342 gmx::RVec evaluate_com(std::vector< RVec > frame) {
347 for (int i = 0; i < frame.size(); i++) {
350 temp[0] /= frame.size();
351 temp[1] /= frame.size();
352 temp[2] /= frame.size();
356 void evaluate_diffusion(std::vector< std::vector< RVec > > trj, std::vector< float > &D/*, int max_frame_depth*/) {
357 D.resize(trj.size() * 0.9 - 1/*max_frame_depth*/);
359 for (int i = 1; i < trj.size() * 0.9 /*max_frame_depth*/; i++) {
361 for (int j = 0; j < trj.size() - 1 - i /*trj.size() - 1 - max_frame_depth*/; j++) {
362 temp += (evaluate_com(trj[j]) - evaluate_com(trj[j + i])).norm2();
363 //D[i][j] = (evaluate_com(trj[j]) - evaluate_com(trj[j + i])).norm2() / (2 * i);
365 D[i - 1] = temp / (trj.size() - 1 - i) / (2 * i * 0.000001);
370 * \ingroup module_trajectoryanalysis
373 class SpaceTimeCorr : public TrajectoryAnalysisModule
378 virtual ~SpaceTimeCorr();
380 //! Set the options and setting
381 virtual void initOptions(IOptionsContainer *options,
382 TrajectoryAnalysisSettings *settings);
384 //! First routine called by the analysis framework
385 // virtual void initAnalysis(const t_trxframe &fr, t_pbc *pbc);
386 virtual void initAnalysis(const TrajectoryAnalysisSettings &settings,
387 const TopologyInformation &top);
389 virtual void initAfterFirstFrame(const TrajectoryAnalysisSettings &settings,
390 const t_trxframe &fr);
392 //! Call for each frame of the trajectory
393 // virtual void analyzeFrame(const t_trxframe &fr, t_pbc *pbc);
394 virtual void analyzeFrame(int frnr, const t_trxframe &fr, t_pbc *pbc,
395 TrajectoryAnalysisModuleData *pdata);
397 //! Last routine called by the analysis framework
398 // virtual void finishAnalysis(t_pbc *pbc);
399 virtual void finishAnalysis(int nframes);
401 //! Routine to write output, that is additional over the built-in
402 virtual void writeOutput();
408 std::vector< std::vector< RVec > > trajectory;
409 std::vector< RVec > reference;
411 std::vector< int > index;
414 int tau = 0; // selectable
415 float crl_border = 0; // selectable
416 float eff_rad = 1.5; // selectable
417 std::string OutPutName; // selectable
418 // Copy and assign disallowed by base.
421 SpaceTimeCorr::SpaceTimeCorr(): TrajectoryAnalysisModule()
425 SpaceTimeCorr::~SpaceTimeCorr()
430 SpaceTimeCorr::initOptions(IOptionsContainer *options,
431 TrajectoryAnalysisSettings *settings)
433 static const char *const desc[] = {
434 "[THISMODULE] to be done"
436 // Add the descriptive text (program help text) to the options
437 settings->setHelpText(desc);
438 // Add option for output file name
439 //options->addOption(FileNameOption("on").filetype(eftIndex).outputFile()
440 // .store(&fnNdx_).defaultBasename("domains")
441 // .description("Index file from the domains"));
442 // Add option for tau constant
443 options->addOption(gmx::IntegerOption("tau")
445 .description("number of frames for time travel"));
446 // Add option for crl_border constant
447 options->addOption(FloatOption("crl")
449 .description("make graph based on corrs > constant"));
450 // Add option for eff_rad constant
451 options->addOption(FloatOption("ef_rad")
453 .description("effective radius for atoms to evaluate corrs"));
454 // Add option for selection list
455 options->addOption(SelectionOption("select_domains_and_residue").storeVector(&sel_)
456 .required().dynamicMask().multiValue()
457 .description("Domains to form rigid skeleton"));
458 options->addOption(StringOption("out_put")
460 .description("<your name here> + <local file tag>.txt"));
461 // Control input settings
462 settings->setFlags(TrajectoryAnalysisSettings::efNoUserPBC);
463 settings->setFlag(TrajectoryAnalysisSettings::efUseTopX);
464 settings->setPBC(true);
468 SpaceTimeCorr::initAnalysis(const TrajectoryAnalysisSettings &settings,
469 const TopologyInformation &top)
471 ArrayRef< const int > atomind = sel_[0].atomIndices();
473 for (ArrayRef< const int >::iterator ai = atomind.begin(); (ai < atomind.end()); ai++) {
474 index.push_back(*ai);
476 trajectory.resize(0);
479 if (top.hasFullTopology()) {
480 for (int i = 0; i < index.size(); i++) {
481 reference.push_back(top.x().at(index[i]));
487 SpaceTimeCorr::initAfterFirstFrame(const TrajectoryAnalysisSettings &settings,
488 const t_trxframe &fr)
492 // -s '/home/toluk/Develop/samples/reca_rd/reca_rd.mono.xtc' -f '/home/toluk/Develop/samples/reca_rd/reca_rd.mono.xtc' -n '/home/toluk/Develop/samples/reca_rd/test6.ndx' -sf '/home/toluk/Develop/samples/reca_rd/SelectionList5' -tau 5 -crl 0.10
493 // -s '/home/toluk/Develop/samples/reca_rd/reca_rd.mono.tpr' -f '/home/toluk/Develop/samples/reca_rd/reca_rd.mono.xtc' -n '/home/toluk/Develop/samples/reca_rd/CorrsTestDomainsNfit.ndx' -sf '/home/toluk/Develop/samples/reca_rd/SelectionListDomainsNFit' -tau 5000 -crl 0.75 -ef_rad 1
494 // -s '/home/toluk/Develop/samples/reca_rd/reca_rd.mono.tpr' -f '/home/toluk/Develop/samples/reca_rd/reca_rd.mono.xtc' -n '/home/toluk/Develop/samples/reca_rd/TestCa.ndx' -sf '/home/toluk/Develop/samples/reca_rd/SelListCa' -tau 100 -crl 0.75 -ef_rad 1
495 // -s '/home/toluk/Develop/samples/reca_rd/reca_rd.mono.tpr' -f '/home/toluk/Develop/samples/reca_rd/reca_rd.mono.xtc' -n '/home/toluk/Develop/samples/reca_rd/testCa.ndx' -sf '/home/toluk/Develop/samples/reca_rd/SelListCa' -tau 9000 -crl 0.60 -ef_rad 1
497 // -s '/home/toluk/Develop/samples/CubeTermal/CUBETermalTest1.pdb' -f '/home/toluk/Develop/samples/CubeTermal/CUBETermalTest.pdb' -n '/home/toluk/Develop/samples/CubeTermal/testCa.ndx' -sf '/home/toluk/Develop/samples/CubeTermal/SelListCa' -tau 900 -crl 0.20 -ef_rad 1
498 // cube.000.000.10k.10.3.1stfrm
499 // -s '/home/toluk/Develop/samples/JustCube/cube.000.000.10k.10.3.1stfrm.pdb' -f '/home/toluk/Develop/samples/JustCube/cube.000.000.10k.10.3.pdb' -n '/home/toluk/Develop/samples/JustCube/system.ndx' -sf '/home/toluk/Develop/samples/JustCube/SLsystem' -tau 1000 -crl 0.20 -ef_rad 9000 -out_put OLA
501 // -s '*.pdb' -f '*.pdb' -n '*.ndx' -sf 'name' -tau 1000 -crl 0.20 -ef_rad 9000 -out_put OLA
502 // -s '*.tpr' -f '*.xtc' -n '*.ndx' -sf 'name' -tau 1000 -crl 0.30 -ef_rad 20 -out_put 'test_run'
505 SpaceTimeCorr::analyzeFrame(int frnr, const t_trxframe &fr, t_pbc *pbc,
506 TrajectoryAnalysisModuleData *pdata)
508 trajectory.resize(trajectory.size() + 1);
509 trajectory.back().resize(index.size());
510 for (int i = 0; i < index.size(); i++) {
511 trajectory.back()[i] = fr.x[index[i]];
517 SpaceTimeCorr::finishAnalysis(int nframes)
519 std::vector< std::vector< std::vector< float > > > crltns;
520 std::vector< std::vector< std::pair< float, int > > > graph;
521 std::vector< std::vector< int > > sub_graph;
522 std::vector< std::vector< std::pair< int, int > > > sub_graph_rbr, rout_new;
528 std::cout << "\nCorrelation's evaluation - start\n";
530 correlation_evaluation(reference, trajectory, basic_frame, crltns, m, k);
532 make_correlation_matrix_file(crltns, (OutPutName + "_matrix.txt").c_str(), 0);
533 std::cout << "corelation matrix printed\n";
535 make_correlation_pairs_file(crltns, (OutPutName + "_pairs.txt").c_str(), 0);
536 std::cout << "corelation pairs printed\n";
538 std::cout << "Correlation's evaluation - end\n" << "graph evaluation: start\n";
540 graph_calculation(graph, sub_graph, sub_graph_rbr, trajectory, basic_frame, crltns, crl_border, eff_rad, k);
541 std::cout << "graph evaluation: end\n" << "routs evaluation: start\n";
543 graph_back_bone_evaluation(rout_new, index.size(), graph, sub_graph, sub_graph_rbr);
545 std::cout << "routs evaluation: end\n";
547 make_rout_file(crl_border, index, rout_new, (OutPutName + "_routs.txt").c_str());
548 std::cout << "corelation routs printed\n";
550 make_best_corrs_graphics(crltns, rout_new, index, (OutPutName + "_routs_graphics.txt").c_str());
551 std::cout << "corelation routs' pairs' graphics printed\n";
554 /*std::cout << "extra params\n";
555 std::vector< float > diffusion;
556 evaluate_diffusion(trajectory, diffusion);
557 make_diffusion_file((OutPutName + "_diffusion.txt").c_str(), diffusion);*/
559 std::cout << "Finish Analysis - end\n\n";
563 SpaceTimeCorr::writeOutput()
569 * The main function for the analysis template.
572 main(int argc, char *argv[])
574 return gmx::TrajectoryAnalysisCommandLineRunner::runAsMain<SpaceTimeCorr>(argc, argv);