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
50 #include <gromacs/trajectoryanalysis.h>
51 #include <gromacs/pbcutil/pbc.h>
52 #include <gromacs/pbcutil/rmpbc.h>
53 #include <gromacs/utility/smalloc.h>
54 #include <gromacs/math/vectypes.h>
55 #include <gromacs/math/vec.h>
56 #include <gromacs/math/do_fit.h>
58 #include "gromacs/analysisdata/analysisdata.h"
59 #include "gromacs/analysisdata/modules/average.h"
60 #include "gromacs/analysisdata/modules/histogram.h"
61 #include "gromacs/analysisdata/modules/plot.h"
62 #include "gromacs/math/vec.h"
63 #include "gromacs/options/basicoptions.h"
64 #include "gromacs/options/filenameoption.h"
65 #include "gromacs/options/ioptionscontainer.h"
66 #include "gromacs/pbcutil/pbc.h"
67 #include "gromacs/selection/selection.h"
68 #include "gromacs/selection/selectionoption.h"
69 #include "gromacs/trajectory/trajectoryframe.h"
70 #include "gromacs/trajectoryanalysis/analysissettings.h"
71 #include "gromacs/utility/arrayref.h"
72 #include "gromacs/utility/exceptions.h"
73 #include "gromacs/utility/stringutil.h"
79 void make_pdb_trajectory(std::vector< std::vector< RVec > > trajectory, const char* file_name)
82 file = std::fopen(file_name, "w+");
83 for (int i = 1; i < trajectory.size(); i++) {
84 std::fprintf(file, "MODEL%9d\n", i);
85 for (int j = 0; j < trajectory[i].size(); j++) {
86 std::fprintf(file, "ATOM %5d CA LYS 1 %8.3f%8.3f%8.3f 1.00 0.00\n", j, trajectory[i][j][0] * 10, trajectory[i][j][1] * 10, trajectory[i][j][2] * 10);
88 std::fprintf(file, "ENDMDL\n");
92 1 - 6 Record name "ATOM "
93 7 - 11 Integer Atom serial number.
94 13 - 16 Atom Atom name.
95 17 Character Alternate location indicator.
96 18 - 20 Residue name Residue name.
97 22 Character Chain identifier.
98 23 - 26 Integer Residue sequence number.
99 27 AChar Code for insertion of residues.
100 31 - 38 Real(8.3) Orthogonal coordinates for X in Angstroms.
101 39 - 46 Real(8.3) Orthogonal coordinates for Y in Angstroms.
102 47 - 54 Real(8.3) Orthogonal coordinates for Z in Angstroms.
103 55 - 60 Real(6.2) Occupancy.
104 61 - 66 Real(6.2) Temperature factor (Default = 0.0).
105 73 - 76 LString(4) Segment identifier, left-justified.
106 77 - 78 LString(2) Element symbol, right-justified.
107 79 - 80 LString(2) Charge on the atom.
110 void make_correlation_file(std::vector< std::vector< std::vector< double > > > correlations, char* file_name, int start)
113 file = std::fopen(file_name, "w+");
114 for (int i = start; i < correlations.size(); i++) {
115 if (correlations.size() - start > 1) {
116 std::fprintf(file, "correlation between 'n' and 'n + %d' frames\n", i);
119 std::cout << "correlation between 'n' and 'n + " << i << "' frames\n";
121 for (int j = 0; j < correlations[i].size(); j++) {
122 for (int f = 0; f < correlations[i][j].size(); f++) {
123 std::fprintf(file, "%3.2f ", correlations[i][j][f]);
125 std::fprintf(file, "\n");
130 void make_rout_file(std::vector< int > indx, std::vector< std::vector< int > > rout, const char* file_name)
133 file = std::fopen(file_name, "w+");
134 for (int i = 0; i < rout.size(); i++) {
135 for (int j = 0; j < rout[i].size(); j++) {
136 std::fprintf(file, "%3d ", indx[rout[i][j]]);
138 std::fprintf(file, "\n");
142 int dive_in(std::vector< std::vector< std::pair< int, int > > > graph, int start, int steps, int frames, int depth, int depth_go, std::vector< std::pair< int, int > > path)
144 int steps_old = 0, steps_new = 0;
145 if (depth > frames) {
149 for (int i = 0; i < path.size(); i++) {
150 if (path[i].first == start) {
155 path.push_back(std::make_pair(start, depth_go));
157 for (int i = 0; i < graph[start].size(); i++) {
159 for (int j = 0; j < path.size(); j++) {
160 if (path[j].first == graph[start][i].first) {
165 steps_new = dive_in(graph, graph[start][i].first, steps + 1, frames, depth + graph[start][i].second, graph[start][i].second, path);
166 if (steps_old < steps_new) {
167 steps_old = steps_new;
171 return (std::max(steps_old, steps));
174 bool mysortfunc (std::vector<int> a, std::vector<int> b) { return (a.size() > b.size()); }
177 * Class used to compute free volume in a simulations box.
179 * Inherits TrajectoryAnalysisModule and all functions from there.
180 * Does not implement any new functionality.
182 * \ingroup module_trajectoryanalysis
186 class Domains : public TrajectoryAnalysisModule
193 //! Set the options and setting
194 virtual void initOptions(IOptionsContainer *options,
195 TrajectoryAnalysisSettings *settings);
197 //! First routine called by the analysis framework
198 // virtual void initAnalysis(const t_trxframe &fr, t_pbc *pbc);
199 virtual void initAnalysis(const TrajectoryAnalysisSettings &settings,
200 const TopologyInformation &top);
202 virtual void initAfterFirstFrame(const TrajectoryAnalysisSettings &settings,
203 const t_trxframe &fr);
205 //! Call for each frame of the trajectory
206 // virtual void analyzeFrame(const t_trxframe &fr, t_pbc *pbc);
207 virtual void analyzeFrame(int frnr, const t_trxframe &fr, t_pbc *pbc,
208 TrajectoryAnalysisModuleData *pdata);
210 //! Last routine called by the analysis framework
211 // virtual void finishAnalysis(t_pbc *pbc);
212 virtual void finishAnalysis(int nframes);
214 //! Routine to write output, that is additional over the built-in
215 virtual void writeOutput();
222 std::vector< std::vector< int > > domains;
223 std::vector< std::vector< int > > domains_local;
224 std::vector< std::vector< RVec > > trajectory;
225 std::vector< std::vector< RVec > > frankenstein_trajectory;
228 std::vector< int > index;
229 std::vector< int > numbers;
233 double crl_border = 0;
237 // Copy and assign disallowed by base.
240 Domains::Domains(): TrajectoryAnalysisModule()
249 Domains::initOptions(IOptionsContainer *options,
250 TrajectoryAnalysisSettings *settings)
252 static const char *const desc[] = {
253 "[THISMODULE] to be done"
255 // Add the descriptive text (program help text) to the options
256 settings->setHelpText(desc);
257 // Add option for output file name
258 options->addOption(FileNameOption("on").filetype(eftIndex).outputFile()
259 .store(&fnNdx_).defaultBasename("domains")
260 .description("Index file from the domains"));
261 // Add option for tau constant
262 options->addOption(gmx::IntegerOption("tau")
264 .description("number of frames for time travel"));
265 // Add option for crl_border constant
266 options->addOption(DoubleOption("crl")
268 .description("make graph based on corrs > constant"));
269 // Add option for selection list
270 options->addOption(SelectionOption("select_domains_and_residue").storeVector(&sel_)
271 .required().dynamicMask().multiValue()
272 .description("Domains to form rigid skeleton"));
273 // Control input settings
274 settings->setFlags(TrajectoryAnalysisSettings::efNoUserPBC);
275 settings->setPBC(true);
279 Domains::initAnalysis(const TrajectoryAnalysisSettings &settings,
280 const TopologyInformation &top)
282 domains_ePBC = top.ePBC();
286 Domains::initAfterFirstFrame(const TrajectoryAnalysisSettings &settings,
287 const t_trxframe &fr)
289 ConstArrayRef< int > atomind = sel_[0].atomIndices();
291 for (ConstArrayRef<int>::iterator ai = atomind.begin(); (ai < atomind.end()); ai++) {
292 index.push_back(*ai);
295 trajectory.resize(1);
296 trajectory.back().resize(index.size());
298 for (int i = 0; i < index.size(); i++) {
299 trajectory.back()[i] = fr.x[index[i]];
302 domains.resize(sel_.size());
303 for (int i = 1; i < sel_.size(); i++) {
304 atomind = sel_[i].atomIndices();
305 for (ConstArrayRef<int>::iterator ai = atomind.begin(); (ai < atomind.end()); ai++) {
306 domains[i].push_back(*ai);
310 for (int i = 0; i < domains.size(); i++) {
311 for (int j = 0; j < domains[i].size(); j++) {
313 while (index[k] != domains[i][j]) {
320 for (int i = 0; i < domains.size(); i++) {
321 if (domains[i].size() >= 2) {
322 domains_local.push_back(domains[i]);
323 for (int k = 0; k < domains[i].size(); k++) {
324 for (int j = i + 1; j < domains.size(); j++) {
325 for (int x = 0; x < domains[j].size(); x++) {
326 if (domains[j][x] == domains[i][k]) {
327 domains[j].erase(domains[j].begin() + x);
336 domains = domains_local;
338 std::vector< bool > flags;
339 flags.resize(index.size(), true);
341 for (int i = 0; i < domains.size(); i++) {
342 for (int j = 0; j < domains[i].size(); j++) {
343 flags[domains[i][j]] = false;
347 for (int i = 0; i < index.size(); i++) {
349 int a, b, dist = 90000001;
351 for (int j = 0; j < domains.size(); j++) {
352 for (int k = 0; k < domains[j].size(); k++) {
353 rvec_sub(trajectory.back()[i], trajectory.back()[domains[j][k]], temp);
354 if (norm(temp) <= dist) {
361 domains_local[a].push_back(i);
366 frankenstein_trajectory.resize(trajectory.size());
367 frankenstein_trajectory.back() = trajectory.back();
373 Domains::analyzeFrame(int frnr, const t_trxframe &fr, t_pbc *pbc,
374 TrajectoryAnalysisModuleData *pdata)
376 trajectory.resize(trajectory.size() + 1);
377 trajectory.back().resize(index.size());
379 for (int i = 0; i < index.size(); i++) {
380 trajectory.back()[i] = fr.x[index[i]];
383 snew(w_rls, domains_local.size() + 1);
384 for (int i = 0; i < domains_local.size(); i++) {
385 snew(w_rls[i], index.size());
386 for (int j = 0; j < index.size(); j++) {
389 for (int j = 0; j < domains_local[i].size(); j++) {
390 w_rls[i][domains_local[i][j]] = 1;
393 snew(w_rls[domains_local.size()], index.size());
394 for (int i = 0; i < index.size(); i++) {
395 w_rls[domains_local.size()][i] = 1;
398 frankenstein_trajectory.resize(trajectory.size());
399 frankenstein_trajectory.back().resize(index.size());
401 for (int i = 0; i < domains_local.size(); i++) {
403 snew(basic, index.size());
404 for (int k = 0; k < index.size(); k++) {
405 copy_rvec(trajectory[basic_frame][k].as_vec(), basic[k]);
407 snew(traj, index.size());
408 for (int k = 0; k < index.size(); k++) {
409 copy_rvec(trajectory.back()[k].as_vec(), traj[k]);
411 reset_x(index.size(), NULL, index.size(), NULL, basic, w_rls[i]);
412 reset_x(index.size(), NULL, index.size(), NULL, traj, w_rls[i]);
413 do_fit(index.size(), w_rls[i], basic, traj);
415 for (int j = 0; j < index.size(); j++) {
416 if (w_rls[i][j] == 0) {
417 copy_rvec(basic[j], traj[j]);
420 reset_x(index.size(), NULL, index.size(), NULL, traj, w_rls[domains_local.size()]);
422 for (int j = 0; j < domains_local[i].size(); j++) {
423 frankenstein_trajectory.back()[domains_local[i][j]] = traj[domains_local[i][j]];
433 //spacetimecorr -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/test.ndx'
436 Domains::finishAnalysis(int nframes)
438 //make_pdb_trajectory(frankenstein_trajectory, "/home/toluk/Develop/samples/reca_rd/10k_frames.pdb");
440 //frankenstein_trajectory = trajectory;
442 std::vector< std::vector< std::vector< double > > > crltns;
447 crltns.resize(k + 1);
448 for (int i = 0; i < crltns.size(); i++) {
449 crltns[i].resize(index.size());
450 for (int j = 0; j < crltns[i].size(); j++) {
451 crltns[i][j].resize(index.size(), 0);
458 #pragma omp for schedule(dynamic)
459 for (int i = m; i <= k; i += 1) {
460 std::cout << " k = " << i << " ";
462 rvec *medx, *medy, temp_zero;
463 snew(medx, index.size());
464 snew(medy, index.size());
468 for (int i = 0; i < index.size(); i++) {
469 copy_rvec(temp_zero, medx[i]);
470 copy_rvec(temp_zero, medy[i]);
473 for (int j = 0; j < frankenstein_trajectory.size() - i; j++) {
474 for (int f = 0; f < index.size(); f++) {
477 rvec_sub(frankenstein_trajectory[basic_frame][f], frankenstein_trajectory[j][f], temp);
478 rvec_inc(medx[f], temp);
479 rvec_sub(frankenstein_trajectory[basic_frame][f], frankenstein_trajectory[j + i][f], temp);
480 rvec_inc(medy[f], temp);
484 for (int j = 0; j < index.size(); j++) {
486 real temp2 = frankenstein_trajectory.size();
489 copy_rvec(medx[j], temp);
490 svmul(temp2, temp, medx[j]);
491 copy_rvec(medy[j], temp);
492 svmul(temp2, temp, medy[j]);
495 std::vector< std::vector< double > > a, b, c;
496 a.resize(index.size());
497 b.resize(index.size());
498 c.resize(index.size());
499 for (int j = 0; j < index.size(); j++) {
500 a[j].resize(index.size(), 0);
501 b[j].resize(index.size(), 0);
502 c[j].resize(index.size(), 0);
504 for (int j = 0; j < frankenstein_trajectory.size() - i; j++) {
505 for (int f1 = 0; f1 < index.size(); f1++) {
506 for (int f2 = f1; f2 < index.size(); f2++) {
508 rvec_sub(frankenstein_trajectory[basic_frame][f1], frankenstein_trajectory[j][f1], temp1);
509 rvec_dec(temp1, medx[f1]);
510 rvec_sub(frankenstein_trajectory[basic_frame][f2], frankenstein_trajectory[j + i][f2], temp2);
511 rvec_dec(temp2, medy[f2]);
512 a[f1][f2] += (temp1[0] * temp2[0] + temp1[1] * temp2[1] + temp1[2] * temp2[2]);
513 b[f1][f2] += (temp1[0] * temp1[0] + temp1[1] * temp1[1] + temp1[2] * temp1[2]);
514 c[f1][f2] += (temp2[0] * temp2[0] + temp2[1] * temp2[1] + temp2[2] * temp2[2]);
518 for (int j = 0; j < index.size(); j++) {
519 for (int f = j; f < index.size(); f++) {
520 crltns[i][j][f] = a[j][f] / (std::sqrt(b[j][f] * c[j][f]));
529 //make_correlation_file(crltns, "corrs.txt", m);
531 for (int i1 = 0; i1 < 100; i1++) {
533 for (int i2 = 1; i2 < crltns.size(); i2++) {
534 for (int i3 = 0; i3 < crltns[i2].size(); i3++) {
535 for (int i4 = 0; i4 < crltns[i2][i3].size(); i4++) {
536 if (crltns[i2][i3][i4] >= (double)i1 / 100) {
542 std::cout << i1 << " - " << i5 << " ";
550 std::vector< std::vector< int > > graph;
551 graph.resize(index.size());
552 for (int i = 1; i <= k; i++) {
553 for (int j = 0; j < index.size(); j++) {
554 for (int f = 0; f < index.size(); f++) {
555 if (std::abs(crltns[i][j][f]) >= crl_border) {
556 bool local_flag = true;
557 for (int f2 = 0; f2 < graph[j].size(); f2++) {
558 if (graph[j][f2] == f) {
563 graph[j].push_back(f);
570 /*for (int i = 0; i < index.size(); i++) {
571 if (graph[i].size() > 0) {
572 std::cout << i << " - " << graph[i].size() << "\n";
577 std::vector< std::vector < int > > rout_old, rout_new;
580 rout_old.resize(index.size());
581 for (int i = 0; i < index.size(); i++) {
582 rout_old[i].resize(1, i);
585 std::cout << "building routs\n\n" ;
589 std::vector < int > flag3;
590 for (long int i = 0; i < rout_old.size(); i++) {
591 if (graph[rout_old[i].back()].size() == 0 && rout_old[i].size() > 2) {
592 rout_new.push_back(rout_old[i]);
595 for (long int j = 0; j < graph[rout_old[i].back()].size(); j++) {
596 bool flag2 = true, flag22 = true;
598 for (int f = 0; f < flag3.size(); f++) {
599 if (flag3[f] == graph[rout_old[i].back()][j]) {
604 flag3.push_back(graph[rout_old[i].back()][j]);
605 for (int f = 0; f < rout_new.size(); f++) {
606 if (rout_new[f] == flag3) {
611 rout_new.push_back(flag3);
618 if (flag222 && rout_old[i].size() > 2) {
619 rout_new.push_back(rout_old[i]);
623 if (rout_new.size() != 0) {
624 std::cout << rout_old.size() << " -> " << rout_new.size() << "\n";
630 std::sort(rout_old.begin(), rout_old.end(), mysortfunc);
632 std::cout << "\nfinished routs\n\n" ;
635 graph.resize(index.size());
638 for (int i = 0; i < rout_old.size(); i++) {
640 for (int j = 0; j < rout_old[i].size() - 1; j++) {
642 for (int f = 0; f < graph[rout_old[i][j]].size(); f++) {
643 if (graph[rout_old[i][j]][f] == rout_old[i][j + 1]) {
648 graph[rout_old[i][j]].push_back(rout_old[i][j + 1]);
653 rout_new.push_back(rout_old[i]);
658 /*std::cout << "\n test old \n";
659 for (int i = 0; i < rout_old.size(); i++) {
660 for (int j = 0; j < rout_old[i].size(); j++) {
661 std::cout << index[rout_old[i][j]] << " ";
666 std::cout << "\n test new \n";*/
669 for (int i = rout_old.size() - 1; i >= 0; i--) {
671 for (int j = 0; j < i; j++) {
672 if (rout_old[i].size() < rout_old[j].size()) {
674 for (int f = 0; f < rout_old[j].size(); f++) {
675 if (rout_old[j][f] == rout_old[i][la]) {
679 if (la == rout_old[i].size()) {
685 rout_new.push_back(rout_old[i]);
689 //make_rout_file(index, rout_new, "routs.txt");
691 for (int i = 0; i < rout_new.size(); i++) {
692 for (int j = 0; j < rout_new[i].size(); j++) {
693 std::cout << index[rout_new[i][j]] << " ";
698 std::cout << "\n\n\n job done \n\n\n";
702 Domains::writeOutput()
709 * The main function for the analysis template.
712 main(int argc, char *argv[])
714 return gmx::TrajectoryAnalysisCommandLineRunner::runAsMain<Domains>(argc, argv);