Debian Codegraph @

auxil/strand-analysis/strand-analysis.cc

raw
1
2 #include <sys/types.h>
3 #ifdef _MSC_VER
4 #include "compat/dirent.h"
5 #else
6 #include <dirent.h>
7 #endif
8 #include <string.h>
9 #include <stdlib.h>
10
11 #include <unistd.h>
12 #include <iostream>
13 #include "strand-analysis.hh"
14
15 int main(int argc, char **argv) {
16
17 if (argc > 1) {
18 std::string dirname = argv[1];
19 int strand_length = 9;
20 if (argc > 2)
21 strand_length = atoi(argv[2]);
22 read_dir(dirname, strand_length);
23 } else {
24 std::cout << "Usage: " << argv[0] << " <reference-structure-dir-name>"
25 << std::endl;
26 }
27 return 0;
28 }
29
30 void read_dir(const std::string &dir_name, int strand_length) {
31
32 coot::strands_t strands;
33
34 std::vector<std::string> v = get_reference_pdb_list(dir_name);
35 std::cout << "" << std::endl;
36
37 for (int i=0; i<v.size(); i++) {
38 strands.analyse_pdb_file(v[i]);
39 }
40
41 strands.post_read_analysis(strand_length);
42 }
43
44 std::vector<std::string>
45 get_reference_pdb_list(const std::string &dir_name) {
46
47 // stat "MAPVIEW_REF_STRUCTS", opendir
48 //
49 // match filenames, read them all.
50 //
51 //
52 std::vector<std::string> pdb_list;
53
54 std::string ref_str_dir_str(dir_name);
55
56 DIR *ref_struct_dir = opendir(ref_str_dir_str.c_str());
57
58 if (ref_struct_dir == NULL) {
59 std::cout << "An error occured on opendir" << std::endl;
60 } else {
61
62 std::cout << ref_str_dir_str << " successfully opened" << std::endl;
63
64 // loop until the end of the filelist (readdir returns NULL)
65 //
66 struct dirent *dir_ent;
67
68 while(1) {
69 dir_ent = readdir(ref_struct_dir);
70
71 if (dir_ent != NULL) {
72
73 std::string file_str(dir_ent->d_name);
74 if (matches_pdb_name(file_str)) {
75
76 // Construct a file in a unix directory - non-portable?
77 //
78 std::string s(dir_name);
79 s += "/";
80 s += file_str;
81
82 pdb_list.push_back(s);
83 }
84 } else {
85 break;
86 }
87 }
88 closedir(ref_struct_dir);
89 }
90 return pdb_list;
91 }
92
93 bool
94 matches_pdb_name(const std::string &file_str) {
95
96 short int match_flag = 0;
97
98 // We have to cast to (char *) here because it seems to me a bug in
99 // the library. The function strchr accepts const char *, but the
100 // function that it calls FirstOccurence (and similarly
101 // LastOccurence) are typed with char *. Crapness.
102 //
103 char *f_pt = strrchr( (char *) file_str.c_str(), '.');
104
105 if (f_pt == NULL) return 0;
106
107 if (! (strncmp(f_pt, ".pdb", 4))) {
108 match_flag = 1;
109 } else {
110
111 char *f_pt1 = strchr((char *) file_str.c_str(), '.');
112
113 if (! (strncmp(f_pt1, ".pdb.gz", 7))) {
114 match_flag = 1;
115
116 } else {
117
118 // was it a pdb CD entries thing?
119 // e.g. pdb1ge0.gz
120 //
121
122 if ( (! strncmp(f_pt, ".gz", 3)) &&
123 (! strncmp((char *) file_str.c_str(), "pdb", 3))) {
124 match_flag = 1;
125 }
126 }
127 }
128 return match_flag;
129 }
130
131 void
132 coot::strands_t::analyse_pdb_file(const std::string &filename) {
133
134 mmdb::Manager *mol = get_mol(filename);
135 if (mol) {
136 mmdb::Model *model_p = mol->GetModel(1);
137 int status = model_p->CalcSecStructure(1);
138 if (status == SSERC_Ok) {
139 std::cout << "INFO:: SSE status was OK\n";
140 strand_analysis(model_p, mol, filename);
141 } else {
142 std::cout << "INFO:: SSE status was bad\n" << status << "\n";
143 }
144 }
145 }
146
147 // Running over all of Top500:
148 // oxygen distance
149 // Strand stats: 4.5171 +/- 0.356533
150 // Helix stats: 3.56436 +/- 0.333756
151 //
152 void
153 distance_checks(mmdb::Model *model_p) {
154
155 int imod = 1;
156
157 mmdb::Chain *chain_p;
158 int nchains = model_p->GetNumberOfChains();
159 for (int ichain=0; ichain<nchains; ichain++) {
160 chain_p = model_p->GetChain(ichain);
161 int nres = chain_p->GetNumberOfResidues();
162 mmdb::PResidue residue_p_1;
163 mmdb::PResidue residue_p_2;
164 for (int ires=0; ires<(nres-1); ires++) {
165 residue_p_1 = chain_p->GetResidue(ires);
166 residue_p_2 = chain_p->GetResidue(ires+1);
167 int n_atoms_1 = residue_p_1->GetNumberOfAtoms();
168 int n_atoms_2 = residue_p_2->GetNumberOfAtoms();
169 if (n_atoms_1 && n_atoms_2) {
170 if (residue_p_1->GetSeqNum() == (residue_p_2->GetSeqNum()-1)) {
171 int sse_1 = SSE(residue_p_1);
172 int sse_2 = SSE(residue_p_2);
173 if (((sse_1 == SSE_Strand) && (sse_2 == SSE_Strand)) ||
174 ((sse_1 == SSE_Helix) && (sse_2 == SSE_Helix))) {
175 float d = oxygen_check(residue_p_1, residue_p_2);
176 if (d > 0) {
177 std::string ss("Helix ");
178 if (sse_1 == SSE_Strand)
179 ss = "Strand";
180 std::cout << ss << " " << d << " "
181 << residue_p_1->GetChainID()
182 << " "
183 << residue_p_1->GetSeqNum()
184 << std::endl;
185 }
186 }
187 }
188 }
189 }
190 }
191 }
192
193 // We need to pass the mol, because we do an atom selection and the
194 // mol gives us the handle and access to the Select() function
195 //
196 void
197 coot::strands_t::strand_analysis(mmdb::Model *model_p, mmdb::Manager *mol,
198 const std::string &filename) {
199
200 PCSheets sheets = model_p->GetSheets();
201 std::cout << "has " << sheets->nSheets << " sheets" << std::endl;
202 for (int isheet=0; isheet<sheets->nSheets; isheet++) {
203 CSheet *sheet = sheets->Sheet[isheet];
204 int nstrands = sheet->nStrands;
205 std::cout << " Sheet " << isheet << " has " << nstrands
206 << " strands " << std::endl;
207 for (int istrand=0; istrand<nstrands; istrand++) {
208 CStrand *strand = sheet->Strand[istrand];
209 if (strand) {
210 std::cout << " strand " << strand->strandNo << " from "
211 << strand->initChainID << " " << strand->initSeqNum
212 << " " << strand->initICode << " to "
213 << strand->endChainID << " " << strand->endSeqNum
214 << " " << strand->endICode << std::endl;
215
216 // Now do a residue selection for that strand. Once only
217 // and pass around the handle, from which we do a
218 // GetSelIndex().
219 //
220 int SelHnd = mol->NewSelection();
221 mol->Select(SelHnd, mmdb::STYPE_RESIDUE, 1,
222 strand->initChainID,
223 strand->initSeqNum, strand->initICode,
224 strand->endSeqNum, strand->endICode,
225 "*", "*",
226 "*", "*",
227 mmdb::SKEY_NEW);
228 std::pair<bool, clipper::RTop_orth> ori = orient_strand_on_z(SelHnd, mol);
229 if (ori.first)
230 apply_rtop_to_strand(SelHnd, mol, ori.second);
231
232 add_strand(filename, mol, strand, SelHnd);
233
234 // mol->DeleteSelection(SelHnd);
235 }
236 }
237 }
238
239 std::string::size_type islash = filename.find_last_of("/");
240 std::string::size_type idot = filename.find_last_of(".");
241 std::string new_pdb_name = filename.substr(islash + 1, idot - islash - 1) +
242 "-a.pdb";
243 // std::cout << "debug filename bits: "
244 // << filename << " "
245 // << filename.find_last_of("/") << " "
246 // << filename.find_last_of(".") << std::endl;
247 mol->WritePDBASCII((char *)new_pdb_name.c_str());
248 }
249
250 void
251 coot::strands_t::add_strand(const std::string &filename,
252 mmdb::Manager *mol,
253 CStrand *strand_p,
254 int SelectionHandle) {
255
256 int nSelResidues = 0;
257 mmdb::PPResidue SelResidues;
258 mol->GetSelIndex(SelectionHandle, SelResidues, nSelResidues);
259
260 coot::strand_info_t s;
261 s.length = nSelResidues;
262 s.strand = *strand_p;
263 std::string name = filename;
264 s.name = filename;
265 std::string::size_type islash = filename.find_last_of("/");
266 if (islash == std::string::npos) {
267 name = filename;
268 } else {
269 name = filename.substr(islash+1, filename.length());
270 }
271 std::string::size_type ipdb = name.rfind(".pdb");
272 if (ipdb == std::string::npos) {
273 // name == name;
274 } else {
275 name = name.substr(0,ipdb);
276 }
277 s.name = name;
278 s.mol = mol;
279 s.SelectionHandle = SelectionHandle;
280 strand_infos.push_back(s);
281
282 }
283
284
285 // return the bool == 1 on good rtop.
286 std::pair<bool, clipper::RTop_orth>
287 orient_strand_on_z(int SelHnd, mmdb::Manager *mol) {
288
289 clipper::RTop_orth rtop = clipper::RTop_orth::identity();
290 bool stat = 0;
291 int nSelResidues = 0;
292 mmdb::PPResidue SelResidues;
293 mol->GetSelIndex(SelHnd, SelResidues, nSelResidues);
294 // std::cout << " selected " << nSelResidues << " residues "
295 // << std::endl;
296 std::vector<clipper::Coord_orth> z_points = z_control_points(nSelResidues);
297 std::vector<clipper::Coord_orth> atom_vec;
298
299 for (int ires=0; ires<nSelResidues; ires++) {
300 mmdb::Residue *residue_p = SelResidues[ires];
301 int n_atoms;
302 mmdb::PPAtom atoms;
303 residue_p->GetAtomTable(atoms, n_atoms);
304 for (int iat=0; iat<n_atoms; iat++) {
305 mmdb::Atom *at = atoms[iat];
306 std::string atom_name(at->name);
307 if (atom_name == " N ") {
308 clipper::Coord_orth pt(at->x, at->y, at->z);
309 atom_vec.push_back(pt);
310 }
311 if (atom_name == " CA ") {
312 clipper::Coord_orth pt(at->x, at->y, at->z);
313 atom_vec.push_back(pt);
314 }
315 if (atom_name == " C ") {
316 clipper::Coord_orth pt(at->x, at->y, at->z);
317 atom_vec.push_back(pt);
318 }
319 }
320 }
321 if (atom_vec.size() != 3* nSelResidues) {
322 std::cout << "skipping this strange strand with " << atom_vec.size()
323 << " atoms (should be " << 3*nSelResidues << ") and "
324 << nSelResidues << " residues" << std::endl;
325 } else {
326 // find the rtop from this clipper method
327 rtop = clipper::RTop_orth(atom_vec, z_points);
328 stat = 1;
329 }
330 return std::pair<bool, clipper::RTop_orth> (stat, rtop);
331 }
332
333 // fiddle with mol
334 void apply_rtop_to_strand(int SelHnd, mmdb::Manager *mol,
335 const clipper::RTop_orth &rtop) {
336
337 int nSelResidues = 0;
338 mmdb::PPResidue SelResidues;
339 mol->GetSelIndex(SelHnd, SelResidues, nSelResidues);
340 for (int ires=0; ires<nSelResidues; ires++) {
341 mmdb::Residue *residue_p = SelResidues[ires];
342 int n_atoms;
343 mmdb::PPAtom atoms;
344 residue_p->GetAtomTable(atoms, n_atoms);
345 for (int iat=0; iat<n_atoms; iat++) {
346 mmdb::Atom *at = atoms[iat];
347 clipper::Coord_orth pt(at->x, at->y, at->z);
348 clipper::Coord_orth n = pt.transform(rtop);
349 at->x = n.x();
350 at->y = n.y();
351 at->z = n.z();
352 }
353 }
354 }
355
356
357 std::vector<clipper::Coord_orth> z_control_points(int nres) {
358
359 std::vector<clipper::Coord_orth> r;
360 double stl = 3.8 * 0.866; // the angle subtended on the z-axis of
361 // the CA-CA distance.
362 double curve_len = 0.2; // how much the strand curves per residues:
363 // used to set the control points.
364
365 double base = -double(nres-1)/2.0 * stl;
366 // double curve_base = double(nres-1)/2.0 * curve_len;
367
368 for (int ires=0; ires<nres; ires++) {
369 // Add control points for N CA and C
370 double d = double(ires) * stl + base;
371 // double cb = curve_base = double(ires)*0.2 + curve_base;
372 double cb = 0;
373 clipper::Coord_orth ca( cb, 0, d);
374 clipper::Coord_orth n( cb + 0.4, 0, d-1.1);
375 clipper::Coord_orth c( cb + 0.4, 0, d+1.1);
376 r.push_back(ca);
377 r.push_back(n);
378 r.push_back(c);
379 if (0) {
380 clipper::String s1("(to-generic-object-add-point 0 \"blue\" 5 ");
381 s1 += clipper::String(n.x());
382 s1 += " ";
383 s1 += clipper::String(n.y());
384 s1 += " ";
385 s1 += clipper::String(n.z());
386 s1 += ") ";
387 std::cout << s1 << std::endl;
388 clipper::String s2("(to-generic-object-add-point 0 \"green\" 5 ");
389 s2 += clipper::String(ca.x());
390 s2 += " ";
391 s2 += clipper::String(ca.y());
392 s2 += " ";
393 s2 += clipper::String(ca.z());
394 s2 += ") ";
395 std::cout << s2 << std::endl;
396 clipper::String s3("(to-generic-object-add-point 0 \"red\" 5 ");
397 s3 += clipper::String(c.x());
398 s3 += " ";
399 s3 += clipper::String(c.y());
400 s3 += " ";
401 s3 += clipper::String(c.z());
402 s3 += ") ";
403 std::cout << s3 << std::endl;
404 }
405 }
406 return r;
407 }
408
409
410 int SSE(mmdb::Residue *res) {
411 return res->SSE;
412 }
413
414 mmdb::Manager *get_mol(const std::string &filename) {
415
416 mmdb::Manager *MMDBManager = new mmdb::Manager();
417 int err = MMDBManager->ReadCoorFile((char *)filename.c_str());
418 if (err) {
419 std::cout << "Error reading " << filename << std::endl;
420 delete MMDBManager;
421 MMDBManager = 0;
422 } else {
423 std::cout << "Read OK: " << filename << std::endl;
424 }
425 return MMDBManager;
426 }
427
428
429
430 // return -1 on failure
431 //
432 float oxygen_check(mmdb::Residue *residue_p_1, mmdb::Residue *residue_p_2) {
433
434 int n_atoms_1 = residue_p_1->GetNumberOfAtoms();
435 int n_atoms_2 = residue_p_2->GetNumberOfAtoms();
436 mmdb::Atom *at, *at2;
437 float d = -1;
438 short int done_it = 0;
439
440 for (int iat=0; iat<n_atoms_1; iat++) {
441 at = residue_p_1->GetAtom(iat);
442 std::string atom_name (at->name);
443 if (atom_name == " O ") {
444 for (int iat2=0; iat2<n_atoms_2; iat2++) {
445 at2 = residue_p_2->GetAtom(iat2);
446 std::string atom_name_2 (at2->name);
447 if (atom_name_2 == " O ") {
448 std::string altconf1 = at->altLoc;
449 std::string altconf2 = at2->altLoc;
450 if ((altconf1 == "") && (altconf2 == "")) {
451 clipper::Coord_orth c1(at->x, at->y, at->z);
452 clipper::Coord_orth c2(at2->x, at2->y, at2->z);
453 d = clipper::Coord_orth::length(c1, c2);
454 done_it = 1;
455 break;
456 }
457 }
458 }
459 }
460 if (done_it)
461 break;
462 }
463 return d;
464 }
465
466 void
467 coot::strands_t::post_read_analysis(int this_length) const {
468
469 std::cout << "Captured " << strand_infos.size() << " strands" << std::endl;
470
471 std::vector<int> strand_lengths(20, 0);
472
473 for (unsigned int i=0; i<strand_infos.size(); i++) {
474 int l = strand_infos[i].length;
475 if (l >= 0)
476 if (l < 20)
477 strand_lengths[l]++;
478 }
479
480 for (unsigned int i=0; i<20; i++) {
481 std::cout << "strand_length: " << i << " " << strand_lengths[i] << std::endl;
482 }
483
484 int n_strands = strand_lengths[this_length];
485 std::cout << "There are " << n_strands << " strands of length " << this_length << std::endl;
486 std::vector<std::pair<std::string,std::vector<float> > > dist_arr(n_strands, std::pair<std::string, std::vector<float> > ("name", std::vector<float>(n_strands, 0.0)));
487 int iarr_index = 0;
488 for (unsigned int istrand=0; istrand<strand_infos.size(); istrand++) {
489 int l1 = strand_infos[istrand].length;
490 if (l1 == this_length) {
491 // strand_names[iarr_index] = strand_infos[istrand].name;
492 int jarr_index = 0;
493 for (unsigned int jstrand=0; jstrand<strand_infos.size(); jstrand++) {
494 int l2 = strand_infos[jstrand].length;
495 if (l2 == this_length) {
496 float v = residual_between_strands(istrand, jstrand, this_length);
497 // std::cout << "setting dist_arr[" << iarr_index << "][" << jarr_index
498 // << "]" << std::endl;
499 dist_arr[iarr_index].second[jarr_index] = v;
500 dist_arr[iarr_index].first = strand_infos[istrand].name;
501 jarr_index++;
502 }
503 }
504 iarr_index++;
505 }
506 }
507
508 std::vector<std::pair< std::string,std::vector<float> > > f_dist_arr = filter_bad_fits(dist_arr);
509
510 // istrand is indexing dist_arr now
511 //
512 if (0) {
513 for (int istrand=0; istrand<n_strands; istrand++) {
514 for (int jstrand=0; jstrand<n_strands; jstrand++) {
515 std::cout << istrand << " " << jstrand << " "
516 << dist_arr[istrand].second[jstrand] << std::endl;
517 }
518 }
519 }
520
521 if (0) {
522 std::cout << " " << n_strands << std::endl;
523 for (int istrand=0; istrand<n_strands; istrand++) {
524 // the front of the line
525 std::cout << "strand" << istrand << " ";
526 // all the row
527 std::cout.setf(std::ios::fixed);
528 std::cout.precision(7);
529 for (int jstrand=0; jstrand<n_strands; jstrand++) {
530 std::cout << dist_arr[istrand].second[jstrand] << " ";
531 }
532 std::cout << std::endl;
533 }
534 }
535
536 if (1) {
537 int f_n_strands = f_dist_arr.size();
538 printf(" %d\n", f_n_strands);
539 for (int istrand=0; istrand<f_n_strands; istrand++) {
540
541 // the front of the line
542 std::string fl = f_dist_arr[istrand].first;
543 if (fl.length() < 6)
544 fl += " ";
545 printf("%s ", fl.c_str());
546
547 // all the row
548 for (int jstrand=0; jstrand<n_strands; jstrand++) {
549 float v = f_dist_arr[istrand].second[jstrand];
550 if (v < 0.0)
551 printf("wierd! %f\n", v);
552 else
553 if (v > 2000)
554 printf("wierd! %f\n", v);
555
556 printf("%7.4f ", v);
557 }
558 printf("\n");
559 }
560 }
561
562 }
563
564
565 std::vector<std::pair<std::string, std::vector<float> > >
566 coot::strands_t::filter_bad_fits(const std::vector<std::pair<std::string, std::vector<float> > > &dist_arr) const {
567
568 std::vector<std::pair<std::string, std::vector<float> > > r = dist_arr;
569
570 // first get the sums of all the rows
571 std::vector<std::pair<float,float> > stats = get_stats(dist_arr);
572 float sum = 0.0;
573 float sum_sq = 0.0;
574 for (int i=0; i<stats.size(); i++) {
575 float v = stats[i].first;
576 sum += v;
577 sum_sq += v*v;
578 }
579 float mean_of_means = sum/float(stats.size());
580 float var_of_means = sum_sq/float(stats.size()) - mean_of_means*mean_of_means;
581 float std_dev = -1; // undefined
582 if (var_of_means >= 0)
583 std_dev = sqrt(var_of_means);
584 else
585 std::cout << "Ooops can't calculate std dev from variance: " << var_of_means << std::endl;
586 for (int i=0; i<stats.size(); i++) {
587 std::cout << "column " << i << " " << stats[i].first << "\n";
588 }
589
590 float threash = mean_of_means + 4*std_dev;
591 threash = 10.0;
592 std::cout << " mean= " << mean_of_means << " std_dev: " << std_dev
593 << " threash= " << threash << std::endl;
594 int bad_column = -1; // unset
595 for (int i=0; i<stats.size(); i++) {
596 if (stats[i].first > threash) {
597 std::cout << "column " << i << " is an outlier: v= " << stats[i].first
598 << " mean= " << mean_of_means << " std_dev: " << std_dev << std::endl;
599 threash = stats[i].first;
600 bad_column = i;
601 }
602 }
603
604 // recur if we cleaned out a column
605 if (bad_column > -1) {
606 return filter_bad_fits(clear_out_column(bad_column, dist_arr));
607 } else {
608 return r;
609 }
610 }
611
612 std::vector<std::pair<std::string, std::vector<float> > >
613 coot::strands_t::clear_out_column(int bad_col, const std::vector<std::pair<std::string, std::vector<float> > > &d) const {
614
615 std::vector<std::pair<std::string, std::vector<float> > > r1;
616 std::vector<std::pair<std::string, std::vector<float> > > r2;
617 for (unsigned int i=0; i<d.size(); i++) {
618 if (i != bad_col)
619 r1.push_back(d[i]);
620 else
621 std::cout << "DEBUG:: ommmited col " << bad_col << std::endl;
622 }
623
624 for (unsigned int i=0; i<r1.size(); i++) {
625 std::pair<std::string, std::vector<float> > p(r1[i].first, std::vector<float>(0));
626 for (unsigned int j=0; j<r1[i].second.size(); j++) {
627 if (j != bad_col) {
628 float v = r1[i].second[j];
629 if (v < 0.0) {
630 std::cout << "replacing neg weird value " << v << " at " << i << " " << j << " with 0"
631 << std::endl;
632 p.second.push_back(0.0);
633 } else {
634 if (v > 2000.0) {
635 std::cout << "replacing high weird value " << v << " at " << i << " " << j << " with 8"
636 << std::endl;
637 p.second.push_back(8.0);
638 } else {
639 p.second.push_back(v); // normal case
640 }
641 }
642 } else {
643 std::cout << "DEBUG:: omitted row " << bad_col << " with value "
644 << r1[i].second[j] << std::endl;
645 }
646 }
647 r2.push_back(p);
648 }
649 return r2;
650 }
651
652 // Return a vector of sums and standard deviations
653 std::vector<std::pair<float, float> >
654 coot::strands_t::get_stats(const std::vector<std::pair<std::string, std::vector<float> > > &dist_arr) const {
655
656 std::vector<std::pair<float, float> > sums(dist_arr.size(), std::pair<float, float>(0.0, 0.0));
657 for (unsigned int i=0; i<dist_arr.size(); i++) {
658 for (unsigned int j=0; j<dist_arr[i].second.size(); j++) {
659 float v = dist_arr[i].second[j];
660 sums[i].first += v;
661 sums[i].second += v*v;
662 }
663 }
664 for (unsigned int i=0; i<sums.size(); i++) {
665 sums[i].first /= float(sums.size());
666 sums[i].second /= float(sums.size()) - sums[i].first*sums[i].first;
667 }
668 return sums; // now mean and var
669 }
670
671
672 float
673 coot::strands_t::residual_between_strands(int istrand, int jstrand, int strand_length) const {
674
675 float r = -1; // bad value
676
677 // get first set of atoms
678 int nSelResidues1 = 0;
679 mmdb::PPResidue SelResidues1;
680 mmdb::Manager *mol1 = strand_infos[istrand].mol;
681 mol1->GetSelIndex(strand_infos[istrand].SelectionHandle, SelResidues1, nSelResidues1);
682 int nSelResidues2 = 0;
683 mmdb::PPResidue SelResidues2;
684 mmdb::Manager *mol2 = strand_infos[jstrand].mol;
685 mol2->GetSelIndex(strand_infos[jstrand].SelectionHandle, SelResidues2, nSelResidues2);
686 std::vector<clipper::Coord_orth> found_atoms_strand_1;
687 std::vector<clipper::Coord_orth> found_atoms_strand_2;
688
689 if (nSelResidues1 == nSelResidues2) {
690 // which they should do!
691 float sum_dist_sq = 0;
692 int n_res_with_atoms = 0;
693 for (int ires=0; ires<nSelResidues1; ires++) {
694
695 int n_atoms_1;
696 mmdb::PPAtom residue_atoms_1;
697 int n_atoms_2;
698 mmdb::PPAtom residue_atoms_2;
699
700 SelResidues1[ires]->GetAtomTable(residue_atoms_1, n_atoms_1);
701 SelResidues2[ires]->GetAtomTable(residue_atoms_2, n_atoms_2);
702
703 std::pair<mmdb::Atom *, mmdb::Atom *> o(0,0);
704 std::pair<mmdb::Atom *, mmdb::Atom *> c(0,0);
705 std::pair<mmdb::Atom *, mmdb::Atom *> ca(0,0);
706 std::pair<mmdb::Atom *, mmdb::Atom *> n(0,0);
707
708 for (int iatom1=0; iatom1<n_atoms_1; iatom1++) {
709 std::string at_name1 = residue_atoms_1[iatom1]->name;
710 if (at_name1 == " CA ") {
711 ca.first = residue_atoms_1[iatom1];
712 }
713 if (at_name1 == " O ") {
714 o.first = residue_atoms_1[iatom1];
715 }
716 if (at_name1 == " C ") {
717 c.first = residue_atoms_1[iatom1];
718 }
719 if (at_name1 == " N ") {
720 n.first = residue_atoms_1[iatom1];
721 }
722 }
723
724 for (int iatom2=0; iatom2<n_atoms_2; iatom2++) {
725 std::string at_name2 = residue_atoms_2[iatom2]->name;
726 if (at_name2 == " CA ") {
727 ca.second = residue_atoms_2[iatom2];
728 }
729 if (at_name2 == " O ") {
730 o.second = residue_atoms_2[iatom2];
731 }
732 if (at_name2 == " C ") {
733 c.second = residue_atoms_2[iatom2];
734 }
735 if (at_name2 == " N ") {
736 n.second = residue_atoms_2[iatom2];
737 }
738 }
739
740 // OK are all the atoms filled?
741 if (ca.first && ca.second) {
742 if (c.first && c.second) {
743 if (n.first && n.second) {
744 if (o.first && o.second) {
745
746 clipper::Coord_orth cat1;
747 clipper::Coord_orth cat2;
748
749 cat1 = clipper::Coord_orth(ca.first->x, ca.first->y, ca.first->z);
750 cat2 = clipper::Coord_orth(ca.second->x, ca.second->y, ca.second->z);
751 found_atoms_strand_1.push_back(cat1);
752 found_atoms_strand_2.push_back(cat2);
753
754 cat1 = clipper::Coord_orth(c.first->x, c.first->y, c.first->z);
755 cat2 = clipper::Coord_orth(c.second->x, c.second->y, c.second->z);
756 found_atoms_strand_1.push_back(cat1);
757 found_atoms_strand_2.push_back(cat2);
758
759 cat1 = clipper::Coord_orth(o.first->x, o.first->y, o.first->z);
760 cat2 = clipper::Coord_orth(o.second->x, o.second->y, o.second->z);
761 found_atoms_strand_1.push_back(cat1);
762 found_atoms_strand_2.push_back(cat2);
763
764 cat1 = clipper::Coord_orth(n.first->x, n.first->y, n.first->z);
765 cat2 = clipper::Coord_orth(n.second->x, n.second->y, n.second->z);
766 found_atoms_strand_1.push_back(cat1);
767 found_atoms_strand_2.push_back(cat2);
768
769
770
771
772 float x2 = ca.first->x - ca.second->x;
773 float y2 = ca.first->y - ca.second->y;
774 float z2 = ca.first->z - ca.second->z;
775 sum_dist_sq += x2*x2 + y2*y2 + z2*z2;
776 n_res_with_atoms++;
777 }
778 }
779 }
780 }
781 }
782 float var = -1;
783 if (n_res_with_atoms > 0) {
784 float var = sum_dist_sq/float(4*n_res_with_atoms);
785 r = var;
786 }
787 coot::stats_t s =
788 get_rtop_and_apply(found_atoms_strand_1, found_atoms_strand_2, SelResidues2, nSelResidues2);
789 r = s.mean;
790
791 } else {
792 std::cout << "ERROR:: Something bad in residual_between_strands " << istrand << " "
793 << jstrand << std::endl;
794 }
795 return r;
796 }
797
798
799 coot::stats_t
800 coot::get_rtop_and_apply(const std::vector<clipper::Coord_orth> &found_atoms_strand_1,
801 const std::vector<clipper::Coord_orth> &found_atoms_strand_2,
802 mmdb::PPResidue SelResidues2, int nSelResidues2) {
803 coot::stats_t s(-1,0);
804
805 clipper::RTop_orth matching_rtop(found_atoms_strand_2, found_atoms_strand_1);
806 float sum_d = 0.0;
807 float sum_d2 = 0.0;
808 float sum_do = 0.0;
809 float sum_d2o = 0.0;
810
811 if (found_atoms_strand_1.size() != found_atoms_strand_2.size()) {
812 std::cout << " Not match on number of atoms in get_rtop_and_apply()!\n";
813 } else {
814 if (found_atoms_strand_1.size() == 0) {
815 std::cout << "Empty coord vectors in get_rtop_and_apply()!\n";
816 } else {
817 for (unsigned int i=0; i<found_atoms_strand_1.size(); i++) {
818 float d = clipper::Coord_orth::length(found_atoms_strand_1[i], found_atoms_strand_2[i].transform(matching_rtop));
819 float dor = clipper::Coord_orth::length(found_atoms_strand_1[i],found_atoms_strand_2[i]);
820 sum_d += d;
821 sum_do += dor;
822 sum_d2 += d*d;
823 sum_d2o += dor*dor;
824 }
825 float mean = sum_d/float(found_atoms_strand_1.size());
826 float var = sum_d2/float(found_atoms_strand_1.size()) - mean*mean;
827 // std::cout << "Distances pre-match:"
828 // << sum_do/float(found_atoms_strand_1.size())
829 // << " post-match: " << mean << std::endl;
830 s = coot::stats_t(mean, var);
831 }
832 }
833 return s;
834 }