677ac1728f148d8b17404709119767b201b5228d
[alexxy/gromacs-spacetimecorr.git] / src / corrtype.cpp
1 #include "corrtype.h"
2
3 // деструктор класса
4 correlationType::~correlationType() {}
5
6 // конструктор класса для инициализации
7 correlationType::correlationType() {}
8
9 correlationType::correlationType(const std::vector< RVec > &ref, int wnd, int taau, int tau_st, float crlUp, float effRad, int mod,
10                                  const std::string &out, const std::vector< int > &indx,
11                                  const std::vector< std::vector < std::vector < size_t > > > &sels,
12                                  const std::vector< std::string > &rsNames) {
13     setDefaults(ref, wnd, taau, tau_st, crlUp, effRad, mod, out, indx, sels, rsNames);
14 }
15
16 void correlationType::setDefaults(const std::vector< RVec > &ref, int wnd, int taau, int tau_st, float crlUp, float effRad, int mod,
17                                   const std::string &out, const std::vector< int > &indx,
18                                   const std::vector< std::vector < std::vector < size_t > > > &sels,
19                                   const std::vector< std::string > &rsNames) {
20     // очень странная штука в индуском стиле... зачем столько ифов?!!!
21     if (ref.size() != 0) {reference = ref;}
22     if (wnd != -1) {window = wnd;}
23     if (taau != -1) {tau = taau;}
24     if (tau_st != -1) {tauStep = tau_st;}
25     if (crlUp != -1) {crlUpBorder = crlUp;}
26     if (effRad != -1) {effRadius = effRad;}
27     if (mod != -1) {mode = mod;}
28     if (out != "") {outputName = out;}
29     if (sels.size() != 0) {selections = sels;}
30     if (rsNames.size() > 0) {resNames = rsNames;}
31     if (indx.size() > 0) {index = indx;}
32     subGraphRouts.resize(0);
33     trajectoryPartition();
34 }
35
36 void correlationType::update(const int frameNum, const std::vector< RVec > &curFrame) {
37     std::cout << "\t ola001" << std::endl;
38     trajectory.push_back(curFrame);
39     std::cout << "\t ola002" << std::endl;
40     int temp = window + tau;
41     std::cout << "\t ola003" << std::endl;
42     if (trajectory.size() == temp + 1) {
43         std::cout << "\t ola003.1" << std::endl;
44         trajectory.erase(trajectory.begin());
45         std::cout << "\t ola003.2" << std::endl;
46     }
47     std::cout << "\t ola004" << std::endl;
48     if (mode == 1) {
49         std::cout << "\t ola005" << std::endl;
50         if ( ((frameNum - temp + 1) >= 0) && ((frameNum - temp + 1) % tauStep == 0)) {
51             std::cout << "\t ola006" << std::endl;
52             correlationEval();
53             std::cout << "\t ola007" << std::endl;
54             selections.erase(selections.begin());
55             std::cout << "\t ola008" << std::endl;
56             std::cout << "\n\t\tcorrEval successful\n" << std::endl;
57         }
58         std::cout << "\t ola009" << std::endl;
59     }
60 }
61
62 void correlationType::readEval() {
63     if (mode == 0) {
64         readWriteCorrelations(0);
65         subGraphRouts.resize(subGraphRouts.size() + 1);
66         graphCalculations(1, static_cast< size_t >(tau));
67         graphBackBoneEvaluation();
68     }
69 }
70
71 void correlationType::printData() {
72     printOutputData();
73 }
74
75 void correlationType::trajectoryPartition() {
76     std::vector< bool > temp1;
77     std::pair< size_t, size_t > temp2;
78     float temp3;
79     std::vector< std::vector < std::vector < size_t > > > selectionsTemp;
80     selectionsTemp.resize(selections.size());
81     for (size_t i1 {0}; i1 < selections.size(); i1++) {
82         selectionsTemp[i1].resize(selections[i1].size());
83         for (size_t i2 {0}; i2 < selections[i1].size(); i2++) {
84             selectionsTemp[i1][i2].resize(0);
85             for (size_t i3 {0}; i3 < selections[i1][i2].size(); i3++) {
86                 for (size_t i4 {0}; i4 < index.size(); i4++) {
87                     if (selections[i1][i2][i3] == index[i4]) {
88                         selectionsTemp[i1][i2].push_back(i4);
89                         break;
90                     }
91                 }
92             }
93         }
94     }
95     selections = selectionsTemp;
96     for (auto &k : selections) {
97         temp1.resize(0);
98         temp1.resize(index.size(), true);
99         for (auto &i : k) {
100             for (auto &j : i) {
101                 temp1[j] = false;
102             }
103         }
104         for (size_t i {0}; i < temp1.size(); i++) {
105             if (temp1[i]) {
106                 temp3 = std::numeric_limits<float>::max();
107                 for (size_t f {0}; f < k.size(); f++) {
108                     for (size_t j {0}; j < k[f].size(); j++) {
109                         if (float temp4 {(reference[k[f][j]] - reference[i]).norm()}; temp3 > temp4) {
110                             temp3 = temp4;
111                             temp2 = std::make_pair(f, j);
112                         }
113                     }
114                 }
115                 k[temp2.first].push_back(i);
116             }
117         }
118     }
119 }
120
121 void correlationType::readWriteCorrelations(int rwMode) {
122     FILE *file;
123     if (rwMode == 1) {
124         file = std::fopen((outputName + "-matrixData").c_str(), "a");
125         for (size_t i {0}; i < matrixes.size(); i++) {
126             std::fprintf(file, "%d %lud\n", count, i);
127             for (size_t j {0}; j < matrixes[i].size(); j++) {
128                 for (size_t f {0}; f < matrixes[i][j].size(); f++) {
129                     std::fprintf(file, "%.4f ", matrixes[i][j][f]); //~16
130                 }
131                 std::fprintf(file, "\n");
132             }
133         }
134         std::fclose(file);
135       }
136     if (rwMode == 0) {
137         file = std::fopen((outputName + "-matrixData").c_str(), "r+");
138         matrixes.resize(0);
139         matrixes.resize(static_cast< unsigned int >(tau + 1));
140         for (size_t i {0}; i < static_cast< size_t >(tau + 1); i++) {
141             int t0, t1, t2 = std::fscanf(file, "%d %d\n", &t0, &t1);
142             matrixes[i].resize(0);
143             matrixes[i].resize(index.size());
144             for (size_t j {0}; j < index.size(); j++) {
145                 matrixes[i][j].resize(index.size());
146                 for (size_t k {0}; k < index.size(); k++) {
147                     t2 = std::fscanf(file, "%lf ", &matrixes[i][j][k]);
148                 }
149             }
150         }
151     }
152 }
153
154 inline void correlationType::trajectoryFitting() {
155     std::vector< std::vector< std::pair< size_t, size_t > > > pairs;
156     pairs.resize(0);
157     pairs.resize(selections.front().size());
158     for (size_t i {0}; i < selections.front().size(); i++) {
159         pairs[i].resize(0);
160         for (size_t j {0}; j < selections.front()[i].size(); j++) {
161             pairs[i].push_back(std::make_pair(selections.front()[i][j], selections.front()[i][j]));
162         }
163     }
164     fitTrajectory.resize(0);
165     fitTrajectory.resize(selections.front().size(), trajectory);
166     #pragma omp parallel for schedule(dynamic) firstprivate(reference)
167     for (size_t i = 0; i < selections.front().size(); i++) {
168         for (size_t j {0}; j < fitTrajectory[i].size(); j++) {
169             MyFitNew(reference, fitTrajectory[i][j], pairs[i], 0);
170         }
171     }
172     #pragma omp barrier
173     for (size_t i {0}; i < selections.front().size(); i++) {
174         for (size_t j {0}; j < selections.front()[i].size(); j++) {
175             for (size_t k {0}; k < fitTrajectory[i].size(); k++) {
176                 trajectory[k][selections.front()[i][j]] = fitTrajectory[i][k][selections.front()[i][j]];
177             }
178         }
179     }
180 }
181
182 inline void correlationType::matrixNullFitting() {
183     matrixes.resize(0);
184     matrixes.resize(static_cast< size_t >(tau + 1));
185     for (auto &i : matrixes) {
186         i.resize(0);
187         i.resize(index.size());
188         for (auto &j : i) {
189             j.resize(0);
190             j.resize(index.size());
191             for (auto &k : j) {
192                 k = 0.;
193             }
194         }
195     }
196 }
197
198 void correlationType::correlationEval() {
199     trajectoryFitting();
200     matrixNullFitting();
201     std::vector< std::vector< double > > a, b, c;
202     std::vector< double > d;
203     d.resize(index.size(), 0.);
204     #pragma omp parallel for ordered schedule(dynamic) shared(matrixes) firstprivate(trajectory, reference)
205     for (size_t i = 0; i <= static_cast< size_t >(tau); i++) {
206         a.resize(0); b.resize(0); c.resize(0);
207         a.resize(index.size(), d); b.resize(index.size(), d); c.resize(index.size(), d);
208         for (size_t j {0}; j < static_cast< size_t >(window); j++) {
209             for (size_t k1 {0}; k1 < index.size(); k1++) {
210                 for (size_t k2 {0}; k2 < index.size(); k2++) {
211                     RVec temp1, temp2;
212                     temp1 = trajectory[j][k1]     - reference[k1];
213                     temp2 = trajectory[j + i][k2] - reference[k2];
214                     a[k1][k2] +=   (static_cast<double>(temp1[0]) * static_cast<double>(temp2[0]) +
215                                     static_cast<double>(temp1[1]) * static_cast<double>(temp2[1]) +
216                                     static_cast<double>(temp1[2]) * static_cast<double>(temp2[2]));
217                     b[k1][k2] +=   (static_cast<double>(temp1[0]) * static_cast<double>(temp1[0]) +
218                                     static_cast<double>(temp1[1]) * static_cast<double>(temp1[1]) +
219                                     static_cast<double>(temp1[2]) * static_cast<double>(temp1[2]));
220                     c[k1][k2] +=   (static_cast<double>(temp2[0]) * static_cast<double>(temp2[0]) +
221                                     static_cast<double>(temp2[1]) * static_cast<double>(temp2[1]) +
222                                     static_cast<double>(temp2[2]) * static_cast<double>(temp2[2]));
223                 }
224             }
225         }
226         for (size_t j {0}; j < index.size(); j++) {
227             for (size_t k {0}; k < index.size(); k++) {
228                 matrixes[i][j][k] = a[j][k] / (std::sqrt(b[j][k] * c[j][k]));
229             }
230         }
231     }
232     #pragma omp barrier
233     for (size_t i {0}; i < matrixes.size(); i++) {
234         for (size_t j {0}; j < matrixes[i].size(); j++) {
235             for (size_t k {0}; k < matrixes[i][j].size(); k++) {
236                 matrixes[i][j][k] = std::round(matrixes[i][j][k] * 10000) / 10000;
237             }
238         }
239     }
240     readWriteCorrelations(1);
241     count++;
242 }
243
244 void correlationType::graphCalculations(size_t tauStart, size_t tauEnd) {
245     graph.resize(0);
246     graph.resize(index.size());
247     subGraphPoints.resize(0);
248     subGraphRbr.resize(0);
249     for (size_t i {0}; i < index.size(); i++) {
250         graph[i].resize(index.size(), std::make_pair(0, -1));
251     }
252     RVec temp;
253     for (size_t i {tauStart}; i <= tauEnd; i++) {
254         for (size_t j {0}; j < index.size(); j++) {
255             for (size_t k {j}; k < index.size(); k++) {
256                 temp = reference[j] - reference[k];
257                 if (double tempIf {std::max(std::abs(matrixes[i][j][k]), std::abs(matrixes[i][k][j]))}; (tempIf >= static_cast< double >(crlUpBorder)) &&
258                     (static_cast< float >(norm(temp)) <= effRadius) && (std::abs(graph[j][k].first) < tempIf)) {
259                     graph[j][k].first = tempIf;
260                     graph[j][k].second = static_cast< int >(i);
261                 }
262             }
263         }
264     }
265     std::vector< bool > graph_flags;
266     graph_flags.resize(0);
267     graph_flags.resize(index.size(), true);
268     std::vector< size_t > a;
269     std::vector< std::pair< size_t, size_t > > b;
270     a.resize(0);
271     b.resize(0);
272     std::vector< size_t > width1, width2, tempSubGraph;
273     for (size_t i {0}; i < index.size(); i++) {
274         if (graph_flags[i]) {
275             subGraphPoints.push_back(a);
276             subGraphRbr.push_back(b);
277             width1.resize(0);
278             width2.resize(0);
279             tempSubGraph.resize(0);
280             width1.push_back(i);
281             tempSubGraph.push_back(i);
282             graph_flags[i] = false;
283             while(width1.size() > 0) {
284                 width2.resize(0);
285                 for (size_t j {0}; j < width1.size(); j++) {
286                     for (size_t k {0}; k < index.size(); k++) {
287                         if ((graph[width1[j]][k].second > -1) && graph_flags[k]) {
288                             width2.push_back(k);
289                             graph_flags[k] = false;
290                         }
291                     }
292                 }
293                 width1 = width2;
294                 for (size_t j {0}; j < width2.size(); j++) {
295                     tempSubGraph.push_back(width2[j]);
296                 }
297             }
298             subGraphPoints.back() = tempSubGraph;
299             for (size_t j {0}; j < tempSubGraph.size(); j++) {
300                 for (size_t k {0}; k < index.size(); k++) {
301                     if (graph[tempSubGraph[j]][k].second > -1) {
302                         subGraphRbr.back().push_back(std::make_pair(tempSubGraph[j], k));
303                     }
304                 }
305             }
306         }
307     }
308 }
309
310 bool correlationType::myComparisonFunction (const std::pair< int, double > i, const std::pair< int, double > j) {
311     return i.second < j.second;
312 }
313
314 void correlationType::graphBackBoneEvaluation() {
315     std::vector< double >                       key;
316     std::vector< long >                         path;
317     std::vector< std::pair< size_t, double > >  que;
318     std::vector< std::pair< size_t, size_t > >  a;
319     size_t                                      v;
320     a.resize(0);
321     subGraphRouts.back().resize(0);
322     for (size_t i {0}; i < subGraphPoints.size(); i++) {
323         key.resize(0);
324         path.resize(0);
325         que.resize(0);
326         v = 0;
327         if (subGraphPoints[i].size() > 2) {
328             key.resize(index.size(), 2);
329             path.resize(index.size(), -1);
330             key[subGraphPoints[i][0]] = 0;
331             for (size_t j {0}; j < subGraphPoints[i].size(); j++) {
332                 que.push_back(std::make_pair(subGraphPoints[i][j], key[subGraphPoints[i][j]]));
333             }
334             std::sort(que.begin(), que.end(), myComparisonFunction);
335             while (!que.empty()) {
336                 v = que.front().first;
337                 que.erase(que.begin());
338                 for (size_t j {0}; j < subGraphRbr[i].size(); j++) {
339                     long u {-1};
340                     if (subGraphRbr[i][j].first == v) {
341                         u = subGraphRbr[i][j].second;
342                     } else if (subGraphRbr[i][j].second == v) {
343                         u = subGraphRbr[i][j].first;
344                     }
345                     bool flag {false};
346                     size_t pos {0};
347                     for (size_t k {0}; k < que.size(); k++) {
348                         if (que[k].first == u) {
349                             flag = true;
350                             pos = k;
351                             k = que.size() + 1;
352                         }
353                     }
354                     if (double tempIf {1. - std::abs(graph[v][static_cast< size_t >(u)].first)};
355                             flag && (key[static_cast< size_t >(u)] > tempIf)) {
356                         path[static_cast< size_t >(u)] = v;
357                         key[static_cast< size_t >(u)] = tempIf;
358                         que[pos].second = key[static_cast< size_t >(u)];
359                         sort(que.begin(), que.end(), myComparisonFunction);
360                     }
361                 }
362             }
363             subGraphRouts.back().push_back(a);
364             for (size_t j {0}; j < index.size(); j++) {
365                 if (path[j] != -1) {
366                     subGraphRouts.back().back().push_back(std::make_pair(path[j], j));
367                 }
368             }
369         }
370     }
371 }
372
373 void correlationType::printOutputData() {
374     FILE *file {std::fopen((outputName + "-arrowsData.txt").c_str(), "w+")};
375     size_t same, pre {0};
376     std::vector< std::tuple< int, int, std::vector< int > > > table;
377     table.resize(0);
378     std::vector< int > a;
379     for (size_t i {0}; i < subGraphRouts.size(); i++) {
380         same = i;
381         for (size_t j {i + 1}; j < subGraphRouts.size(); j++) {
382             if (subGraphRouts[j] == subGraphRouts[i]) {
383                 same = j;
384             } else {
385                 break;
386             }
387         }
388         if (i == same) {
389             std::fprintf(file, "\n Starting time point = %d | correlations >= %0.2f | tau = %d | window = %d\n\n", static_cast< int >(i) * tauStep, static_cast< double >(crlUpBorder), tau, window);
390         } else {
391             std::fprintf(file, "\n Starting time point = [%d ; %d] | correlations >= %0.2f | tau = %d | window = %d\n\n", static_cast< int >(i) * tauStep, static_cast< int >(same) * tauStep, static_cast< double >(crlUpBorder), tau, window);
392         }
393         for (size_t j {0}; j < subGraphRouts[i].size(); j++) {
394             for (size_t k {0}; k < subGraphRouts[i][j].size(); k++) {
395                 std::fprintf(file, "cgo_arrow (id %3d), (id %3d), radius=0.05\n", index[subGraphRouts[i][j][k].first] + 1, index[subGraphRouts[i][j][k].second] + 1);
396             }
397             std::fprintf(file, "\n");
398         }
399         table.resize(0);
400         for (auto &j : subGraphRouts[i]) {
401             for (auto &k : j) {
402                 bool flag1 {true}, flag2 {true};
403                 for (size_t m {0}; m < table.size(); m++) {
404                     if (std::get<0>(table[m]) == index[k.first]) {
405                         std::get<1>(table[m])++;
406                         std::get<2>(table[m]).push_back(index[k.second]);
407                         flag1 = false;
408                     }
409                     if (std::get<0>(table[m]) == index[k.second]) {
410                         std::get<1>(table[m])++;
411                         std::get<2>(table[m]).push_back(index[k.first]);
412                         flag2 = false;
413                     }
414                 }
415                 if (flag1) {
416                     a.resize(0);
417                     a.push_back(index[k.second]);
418                     table.push_back(std::make_tuple(index[k.first], 1, a));
419                 }
420                 if (flag2) {
421                     a.resize(0);
422                     a.push_back(index[k.first]);
423                     table.push_back(std::make_tuple(index[k.second], 1, a));
424                 }
425             }
426         }
427         for (size_t j {0}; j < table.size(); j++) {
428             std::fprintf(file, "residue %s connections %d | ", (resNames[static_cast< size_t >(std::find(index.begin(), index.end(), std::get<0>(table[j])) - index.begin())]).c_str(), std::get<1>(table[j]));
429             for (size_t k {0}; k < std::get<2>(table[j]).size(); k++) {
430                 std::fprintf(file, "%s ", (resNames[static_cast< size_t >(std::find(index.begin(), index.end(), std::get<2>(table[j])[k]) - index.begin())]).c_str());
431             }
432             std::fprintf(file, "\n");
433         }
434         if (pre != 0) {
435             std::vector< std::vector< std::pair< size_t, size_t > > > temp01, temp02;
436             std::vector< std::pair< size_t, size_t > > temp03, temp04, temp05;
437             temp01 = subGraphRouts[pre];
438             temp02 = subGraphRouts[same];
439             temp03.resize(0);
440             temp04.resize(0);
441             temp05.resize(0);
442             for (auto &j : temp01) {
443                 for (auto &k : j) {
444                     temp03.push_back(k);
445                 }
446             }
447             for (auto &j : temp02) {
448                 for (auto &k : j) {
449                     temp04.push_back(k);
450                 }
451             }
452             std::sort(temp03.begin(), temp03.end());
453             std::sort(temp04.begin(), temp04.end());
454             std::set_difference(temp03.begin(), temp03.end(), temp04.begin(), temp04.end(), std::inserter(temp05, temp05.begin()));
455             std::fprintf(file, "minus:\n");
456             for (auto &j : temp05) {
457                 std::fprintf(file, "cgo_arrow (id %3d), (id %3d), radius=0.05\n", index[j.first] + 1, index[j.second] + 1);
458             }
459             temp05.resize(0);
460             std::set_difference(temp04.begin(), temp04.end(), temp03.begin(), temp03.end(), std::inserter(temp05, temp05.begin()));
461             std::fprintf(file, "plus:\n");
462             for (auto &j : temp05) {
463                 std::fprintf(file, "cgo_arrow (id %3d), (id %3d), radius=0.05\n", index[j.first] + 1, index[j.second] + 1);
464             }
465         }
466         pre = same;
467         i = same;
468     }
469     std::fclose(file);
470 }