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
53 #include <gromacs/trajectoryanalysis.h>
54 #include <gromacs/utility/smalloc.h>
55 #include <gromacs/math/do_fit.h>
56 #include <gromacs/trajectoryanalysis/topologyinformation.h>
62 const std::vector< std::vector< std::vector< double > > > read_correlation_matrix_file(const char* file_name, unsigned long size, unsigned int length)
65 std::vector< std::vector< std::vector< double > > > crrlts;
66 file = std::fopen(file_name, "r+");
67 std::vector< double > c;
69 std::vector< std::vector< double > > b;
71 crrlts.resize(length + 1, b);
73 std::cout << "reading correlations from a file:\n";
74 for (unsigned int i = 0; i < length + 1; i++) {
75 //char *t1 = std::fgets(a, 100, file);
76 int t0 = 0, t1 = std::fscanf(file, "%d\n", &t0);
77 std::cout << t0 << " | ";
81 for (unsigned int j = 0; j < size; j++) {
82 for (unsigned int f = 0; f < size; f++) {
83 int t2 = std::fscanf(file, "%lf ", &crrlts[i][j][f]);
92 void make_correlation_matrix_file(const std::vector< std::vector< std::vector< double > > > correlations, const char* file_name)
95 file = std::fopen(file_name, "w+");
96 std::cout << "writing correlation matrixes: \n";
97 for (unsigned int i = 0; i < correlations.size(); i++) {
98 std::fprintf(file, "%d\n", i);
99 for (unsigned int j = 0; j < correlations[i].size(); j++) {
100 for (unsigned int f = 0; f < correlations[i][j].size(); f++) {
101 std::fprintf(file, "%.6f ", correlations[i][j][f]); //~16
103 std::fprintf(file, "\n");
105 std::cout << i << " | ";
111 void make_correlation_pairs_file(std::vector< std::vector< std::vector< double > > > correlations, const char* file_name)
114 file = std::fopen(file_name, "w+");
115 for (unsigned int i = 0; i < correlations.front().size(); i++) {
116 for (unsigned int j = 0; j < correlations.front().front().size(); j++) {
117 //std::fprintf(file, "correlation between point '%d' and point '%d'\n", i, j);
118 std::fprintf(file, "%d %d\n", i, j);
119 for (unsigned int k = 0; k < correlations.size(); k++) {
120 std::fprintf(file, "%3.4f ", correlations[k][i][j]);
122 std::fprintf(file, "\n");
125 std::cout << "correlations in row " << i << " out of " << correlations.front().size() << " completed\n";
131 void make_rout_file(double crl_border, std::vector< int > indx, std::vector< std::vector< std::pair< unsigned int, unsigned int > > > rout, const char* file_name)
134 file = std::fopen(file_name, "w+");
135 std::fprintf(file, "correlations >= %0.2f\n\n", crl_border);
136 for (unsigned int i = 0; i < rout.size(); i++) {
137 for (unsigned int j = 0; j < rout[i].size(); j++) {
138 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*/);
140 std::fprintf(file, "\n\n");
145 void make_best_corrs_graphics(std::vector< std::vector< std::vector< double > > > correlations,
146 std::vector< std::vector< std::pair< unsigned int, unsigned int > > > rout_pairs,
147 std::vector< int > indx,
148 const char* file_name)
151 file = std::fopen(file_name, "w+");
152 for (unsigned int i = 0; i < rout_pairs.size(); i++) {
153 for (unsigned int j = 0; j < rout_pairs[i].size(); j++) {
154 std::fprintf(file, "%3d %3d\n", indx[rout_pairs[i][j].first]/* + 1*/, indx[rout_pairs[i][j].second]/* + 1*/);
155 for (unsigned int k = 0; k < correlations.size(); k++) {
156 std::fprintf(file, "%3.5f ", correlations[k][rout_pairs[i][j].first][rout_pairs[i][j].second]);
158 std::fprintf(file, "\n");
164 void make_diffusion_file(const char* file_name, std::vector< double > D)
167 file = std::fopen(file_name, "w+");
168 for (unsigned int i = 0; i < D.size(); i++) {
169 std::fprintf(file, "%f ", D[i]);
174 bool mysortfunc (std::vector< int > a, std::vector< int > b) {
175 return (a.size() > b.size());
178 bool isitsubset (std::vector< int > a, std::vector< int > b) {
182 std::sort(a.begin(), a.end());
183 std::sort(b.begin(), b.end());
185 for (unsigned int i = 0; i < a.size(); i++) {
197 const std::vector< std::vector< std::vector< double > > > correlation_evaluation(std::vector< RVec > ref, std::vector< std::vector< RVec > > traj, unsigned int tauE) {
198 std::vector< std::vector< std::vector< double > > > crl;
199 crl.resize(tauE + 1);
200 for (unsigned int i = 0; i < crl.size(); i++) {
201 crl[i].resize(traj.front().size());
202 for (unsigned int j = 0; j < crl[i].size(); j++) {
203 crl[i][j].resize(traj.front().size(), 0);
211 std::vector< double > d;
212 d.resize(traj.front().size(), 0);
214 #pragma omp parallel for schedule(dynamic)
215 for (unsigned int i = 0; i <= tauE; i++) {
217 std::vector< std::vector< double > > a, b, c;
218 a.resize(traj.front().size(), d);
219 b.resize(traj.front().size(), d);
220 c.resize(traj.front().size(), d);
221 for (unsigned int j = 0; j < traj.size() - i - 1; j++) {
222 for (unsigned int f1 = 0; f1 < traj.front().size(); f1++) {
223 for (unsigned int f2 = 0; f2 < traj.front().size(); f2++) {
224 temp1 = traj[j][f1] - ref[f1];
225 temp2 = traj[j + i][f2] - ref[f2];
226 a[f1][f2] += (static_cast<double>(temp1[0]) * static_cast<double>(temp2[0]) +
227 static_cast<double>(temp1[1]) * static_cast<double>(temp2[1]) +
228 static_cast<double>(temp1[2]) * static_cast<double>(temp2[2]));
229 b[f1][f2] += (static_cast<double>(temp1[0]) * static_cast<double>(temp1[0]) +
230 static_cast<double>(temp1[1]) * static_cast<double>(temp1[1]) +
231 static_cast<double>(temp1[2]) * static_cast<double>(temp1[2]));
232 c[f1][f2] += (static_cast<double>(temp2[0]) * static_cast<double>(temp2[0]) +
233 static_cast<double>(temp2[1]) * static_cast<double>(temp2[1]) +
234 static_cast<double>(temp2[2]) * static_cast<double>(temp2[2]));
238 for (unsigned int j = 0; j < traj.front().size(); j++) {
239 for (unsigned int f = 0; f < traj.front().size(); f++) {
240 crl[i][j][f] = a[j][f] / (std::sqrt(b[j][f] * c[j][f]));
243 std::cout << i << " | ";
253 void graph_calculation( std::vector< std::vector< std::pair< double, long int > > > &graph, std::vector< std::vector< unsigned int > > &s_graph,
254 std::vector< std::vector< std::pair< unsigned int, unsigned int > > > &s_graph_rbr,
255 std::vector< std::vector< RVec > > traj, std::vector< RVec > ref,
256 const std::vector< std::vector< std::vector<double > > > crl, double crl_b, double e_rad, unsigned int tauB, unsigned int tauE) {
257 graph.resize(traj.front().size());
258 for (unsigned int i = 0; i < traj.front().size(); i++) {
259 graph[i].resize(traj.front().size(), std::make_pair(0, -1));
262 for (unsigned int i = tauB; i <= tauE; i++) {
263 for (unsigned int j = 0; j < traj.front().size(); j++) {
264 for (unsigned int f = j; f < traj.front().size(); f++) {
265 temp = ref[i] - ref[f];
266 if (std::max(std::abs(crl[i][j][f]), std::abs(crl[i][f][j])) >= crl_b &&
267 static_cast<double>(norm(temp)) <= e_rad && std::abs(graph[j][f].first) < std::max(std::abs(crl[i][j][f]), std::abs(crl[i][f][j]))) {
268 if (std::abs(crl[i][j][f]) > std::abs(crl[i][f][j])) {
269 graph[j][f].first = crl[i][j][f];
271 graph[j][f].first = crl[i][f][j];
273 graph[j][f].second = i;
278 std::cout << "crl analysed\n";
279 std::vector< bool > graph_flags;
280 graph_flags.resize(traj.front().size(), true);
281 std::vector< unsigned int > a;
283 std::vector< std::pair< unsigned int, unsigned int > > b;
285 std::vector< unsigned int > que1, que2, que3;
286 for (unsigned int i = 0; i < traj.front().size(); i++) {
287 if (graph_flags[i]) {
288 s_graph.push_back(a);
289 s_graph_rbr.push_back(b);
295 graph_flags[i] = false;
296 while(que1.size() > 0) {
298 for (unsigned int k = 0; k < que1.size(); k++) {
299 for (unsigned int j = 0; j < traj.front().size(); j++) {
300 if (graph[que1[k]][j].second > -1 && graph_flags[j]) {
302 graph_flags[j] = false;
307 for (unsigned int j = 0; j < que2.size(); j++) {
308 que3.push_back(que2[j]);
311 s_graph.back() = que3;
312 for (unsigned int j = 0; j < que3.size(); j++) {
313 for (unsigned int k = 0; k < traj.front().size(); k++) {
314 if (graph[que3[j]][k].second > -1) {
315 s_graph_rbr.back().push_back(std::make_pair(que3[j], k));
319 //std::cout << s_graph.back().size() << " ";
324 bool myfunction (const std::pair< int, double > i, const std::pair< int, double > j) {
325 return i.second < j.second;
328 void graph_back_bone_evaluation(std::vector< std::vector < std::pair< unsigned int, unsigned int > > > &rout_n, unsigned long indxSize,
329 std::vector< std::vector< std::pair< double, long > > > graph, std::vector< std::vector< unsigned int > > s_graph,
330 std::vector< std::vector< std::pair< unsigned int, unsigned int > > > s_graph_rbr) {
331 std::vector< double > key;
332 std::vector< long > path;
333 std::vector< std::pair< unsigned int, double > > que;
334 std::vector< std::pair< unsigned int, unsigned int > > a;
336 for (unsigned int i = 0; i < s_graph.size(); i++) {
341 if (s_graph[i].size() > 2) {
342 key.resize(indxSize, 2);
343 path.resize(indxSize, -1);
344 key[s_graph[i][0]] = 0;
345 for (unsigned int j = 0; j < s_graph[i].size(); j++) {
346 que.push_back(std::make_pair(s_graph[i][j], key[s_graph[i][j]]));
348 std::sort(que.begin(), que.end(), myfunction);
349 while (!que.empty()) {
351 que.erase(que.begin());
352 for (unsigned int j = 0; j < s_graph_rbr[i].size(); j++) {
354 if (s_graph_rbr[i][j].first == v) {
355 u = s_graph_rbr[i][j].second;
356 } else if (s_graph_rbr[i][j].second == v) {
357 u = s_graph_rbr[i][j].first;
360 unsigned long pos = 0;
361 for (unsigned long k = 0; k < que.size(); k++) {
362 if (que[k].first == u) {
368 if (flag && key[static_cast< unsigned long >(u)] > 1 - std::abs(graph[v][static_cast< unsigned long >(u)].first)) {
369 path[static_cast< unsigned long >(u)] = v;
370 key[static_cast< unsigned long >(u)] = 1 - std::abs(graph[v][static_cast< unsigned long >(u)].first);
371 que[pos].second = key[static_cast< unsigned long >(u)];
372 sort(que.begin(), que.end(), myfunction);
378 for (unsigned int j = 0; j < indxSize; j++) {
380 rout_n.back().push_back(std::make_pair(j, path[j]));
387 gmx::RVec evaluate_com(std::vector< RVec > frame) {
392 for (unsigned int i = 0; i < frame.size(); i++) {
395 temp[0] /= frame.size();
396 temp[1] /= frame.size();
397 temp[2] /= frame.size();
401 void evaluate_diffusion(std::vector< std::vector< RVec > > trj, std::vector< double > &D/*, int max_frame_depth*/) {
402 D.resize(static_cast< unsigned int >(trj.size() * 0.9 - 1)/*max_frame_depth*/);
404 for (unsigned int i = 1; i < trj.size() * 0.9 /*max_frame_depth*/; i++) {
406 for (unsigned int j = 0; j < trj.size() - 1 - i /*trj.size() - 1 - max_frame_depth*/; j++) {
407 temp += static_cast< unsigned int >((evaluate_com(trj[j]) - evaluate_com(trj[j + i])).norm2());
408 //D[i][j] = (evaluate_com(trj[j]) - evaluate_com(trj[j + i])).norm2() / (2 * i);
410 D[i - 1] = temp / (trj.size() - 1 - i) / (2 * i * 0.000001);
415 * \ingroup module_trajectoryanalysis
418 class SpaceTimeCorr : public TrajectoryAnalysisModule
423 virtual ~SpaceTimeCorr();
425 //! Set the options and setting
426 virtual void initOptions(IOptionsContainer *options,
427 TrajectoryAnalysisSettings *settings);
429 //! First routine called by the analysis framework
430 // virtual void initAnalysis(const t_trxframe &fr, t_pbc *pbc);
431 virtual void initAnalysis(const TrajectoryAnalysisSettings &settings,
432 const TopologyInformation &top);
434 virtual void initAfterFirstFrame(const TrajectoryAnalysisSettings &settings,
435 const t_trxframe &fr);
437 //! Call for each frame of the trajectory
438 // virtual void analyzeFrame(const t_trxframe &fr, t_pbc *pbc);
439 virtual void analyzeFrame(int frnr, const t_trxframe &fr, t_pbc *pbc,
440 TrajectoryAnalysisModuleData *pdata);
442 //! Last routine called by the analysis framework
443 // virtual void finishAnalysis(t_pbc *pbc);
444 virtual void finishAnalysis(int nframes);
446 //! Routine to write output, that is additional over the built-in
447 virtual void writeOutput();
453 std::vector< std::vector< RVec > > trajectory;
454 std::vector< RVec > reference;
456 std::vector< int > index;
458 int tau = 0; // selectable
459 float crl_border = 0; // selectable
460 float eff_rad = 1.5; // selectable
461 std::string OutPutName; // selectable
462 int mode = 0; // selectable
463 std::string MtrxNm; // selectable
464 // Copy and assign disallowed by base.
467 SpaceTimeCorr::SpaceTimeCorr(): TrajectoryAnalysisModule()
471 SpaceTimeCorr::~SpaceTimeCorr()
476 SpaceTimeCorr::initOptions(IOptionsContainer *options,
477 TrajectoryAnalysisSettings *settings)
479 static const char *const desc[] = {
480 "[THISMODULE] to be done"
482 // Add the descriptive text (program help text) to the options
483 settings->setHelpText(desc);
484 // Add option for output file name
485 //options->addOption(FileNameOption("on").filetype(eftIndex).outputFile()
486 // .store(&fnNdx_).defaultBasename("domains")
487 // .description("Index file from the domains"));
488 // Add option for working mode
489 options->addOption(gmx::IntegerOption("mode")
491 .description("default 0 | rdy correlation matrixes 1, need extra params"));
492 // Add option for Matrix Input file names
493 options->addOption(StringOption("Mtrx_in_put")
495 .description("mandatory if work mode == 1"));
496 // Add option for tau constant
497 options->addOption(gmx::IntegerOption("tau")
499 .description("number of frames for time travel"));
500 // Add option for crl_border constant
501 options->addOption(FloatOption("crl")
503 .description("make graph based on corrs > constant"));
504 // Add option for eff_rad constant
505 options->addOption(FloatOption("ef_rad")
507 .description("effective radius for atoms to evaluate corrs"));
508 // Add option for selection list
509 options->addOption(SelectionOption("select_domains_and_residue").storeVector(&sel_)
510 .required().dynamicMask().multiValue()
511 .description("Domains to form rigid skeleton"));
512 // Add option for output file names
513 options->addOption(StringOption("out_put")
515 .description("<your name here> + <local file tag>.txt"));
516 // Control input settings
517 settings->setFlags(TrajectoryAnalysisSettings::efNoUserPBC);
518 settings->setFlag(TrajectoryAnalysisSettings::efUseTopX);
519 settings->setPBC(true);
523 SpaceTimeCorr::initAnalysis(const TrajectoryAnalysisSettings &settings, const TopologyInformation &top)
525 ArrayRef< const int > atomind = sel_[0].atomIndices();
527 for (ArrayRef< const int >::iterator ai = atomind.begin(); (ai < atomind.end()); ai++) {
528 index.push_back(*ai);
530 trajectory.resize(0);
533 if (top.hasFullTopology()) {
534 for (unsigned int i = 0; i < index.size(); i++) {
535 reference.push_back(top.x().at(index[i]));
541 SpaceTimeCorr::initAfterFirstFrame(const TrajectoryAnalysisSettings &settings, const t_trxframe &fr)
545 // -s '*.pdb' -f '*.pdb' -n '*.ndx' -sf 'name' -tau 1000 -crl 0.20 -ef_rad 9000 -out_put OLA
546 // -s '*.tpr' -f '*.xtc' -n '*.ndx' -sf 'name' -tau 1000 -crl 0.30 -ef_rad 20 -out_put 'test_run'
549 SpaceTimeCorr::analyzeFrame(int frnr, const t_trxframe &fr, t_pbc *pbc, TrajectoryAnalysisModuleData *pdata)
551 trajectory.resize(trajectory.size() + 1);
552 trajectory.back().resize(index.size());
553 for (unsigned int i = 0; i < index.size(); i++) {
554 trajectory.back()[i] = fr.x[index[i]];
560 SpaceTimeCorr::finishAnalysis(int nframes)
562 std::vector< std::vector< std::vector< double > > > crltns, crltns_test;
563 std::vector< std::vector< std::pair< double, long int > > > graph;
564 std::vector< std::vector< unsigned int > > sub_graph;
565 std::vector< std::vector< std::pair< unsigned int, unsigned int > > > sub_graph_rbr;
566 std::vector< std::vector< std::pair< unsigned int, unsigned int > > > rout_new;
567 unsigned int k = 1000, m = 0;
569 k = static_cast< unsigned int>(tau);
573 std::cout << "\nCorrelation's evaluation - start\n";
575 crltns = correlation_evaluation(reference, trajectory, k);
577 for (unsigned int i1 = 0; i1 < crltns.size(); i1++) {
578 for (unsigned int i2 = 0; i2 < crltns[i1].size(); i2++) {
579 for (unsigned int i3 = 0; i3 < crltns[i1][i2].size(); i3++) {
580 crltns[i1][i2][i3] = std::round(crltns[i1][i2][i3] * 10000) / 10000;
585 make_correlation_matrix_file(crltns, (OutPutName + "_matrix.txt").c_str());
586 std::cout << "corelation matrix printed\n";
588 //make_correlation_pairs_file(crltns, (OutPutName + "_pairs.txt").c_str(), 0);
589 //std::cout << "corelation pairs printed\n";
591 std::cout << "Correlation's evaluation - end\n" << "graph evaluation: start\n";
592 } else if (mode == 1) {
593 crltns = read_correlation_matrix_file((MtrxNm).c_str(), index.size(), k);
595 for (unsigned int i1 = 0; i1 < crltns.size(); i1++) {
596 for (unsigned int i2 = 0; i2 < crltns[i1].size(); i2++) {
597 for (unsigned int i3 = 0; i3 < crltns[i1][i2].size(); i3++) {
598 crltns[i1][i2][i3] = std::round(crltns[i1][i2][i3] * 10000) / 10000;
607 for (int i = 0; i < crltns.size(); i++) {
608 for (int j = 0; j < crltns[i].size(); j++) {
609 for (int o = 0; o < crltns[i][j].size(); o++) {
610 if (std::fabs(crltns[i][j][o] - crltns_test[i][j][o]) > 0) {
611 std::cout << "error " << i << " " << j << " " << o << " " << std::fabs(crltns[i][j][o] - crltns_test[i][j][o]) << "\n";
618 std::cout << "ola001 = " << ola001 << "\n";
621 graph_calculation(graph, sub_graph, sub_graph_rbr, trajectory, reference, crltns, static_cast< double >(crl_border), static_cast< double >(eff_rad), m, k);
622 std::cout << "graph evaluation: end\n" << "routs evaluation: start\n";
624 graph_back_bone_evaluation(rout_new, index.size(), graph, sub_graph, sub_graph_rbr);
626 std::cout << "routs evaluation: end\n";
628 make_rout_file(static_cast< double >(crl_border), index, rout_new, (OutPutName + "_routs.txt").c_str());
629 std::cout << "corelation routs printed\n";
631 make_best_corrs_graphics(crltns, rout_new, index, (OutPutName + "_routs_graphics.txt").c_str());
632 std::cout << "corelation routs' pairs' graphics printed\n";
634 /*std::cout << "extra params\n";
635 std::vector< double > diffusion;
636 evaluate_diffusion(trajectory, diffusion);
637 make_diffusion_file((OutPutName + "_diffusion.txt").c_str(), diffusion);*/
639 std::cout << "Finish Analysis - end\n\n";
643 SpaceTimeCorr::writeOutput()
649 * The main function for the analysis template.
652 main(int argc, char *argv[])
654 return gmx::TrajectoryAnalysisCommandLineRunner::runAsMain<SpaceTimeCorr>(argc, argv);