Debian Codegraph @

ccp4mg-utils/mmut/mmut_hbond.cc

raw symbols SCIP
1 /*
2 mmut/mmut_hbond.cc: CCP4MG Molecular Graphics Program
3 Copyright (C) 2001-2008 University of York, CCLRC
4
5 This library is free software: you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License
7 version 3, modified in accordance with the provisions of the
8 license to address the requirements of UK law.
9
10 You should have received a copy of the modified GNU Lesser General
11 Public License along with this library. If not, copies may be
12 downloaded from http://www.ccp4.ac.uk/ccp4license.php
13
14 This program 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
17 GNU Lesser General Public License for more details.
18 */
19
20
21 /*
22
23 Find hydrogen bonds
24 Liz Potterton Oct02
25
26 */
27
28 #include <iostream>
29 #include <string.h>
30 #include <string>
31 #include <math.h>
32 #include <stdlib.h>
33 #include <sstream>
34 #include <iomanip>
35 #include <mman_base.h>
36 #include <mmdb_manager.h>
37 #include <mmut_manager.h>
38 #include <mman_manager.h>
39 #include <mmut_hbond.h>
40 #include <mmut_sbase.h>
41 #include <connect.h>
42 #include <vector>
43 #include <mgutil.h>
44 using namespace std;
45
46
47
48 //---------------------------------------------------------------------
49 CHBond::CHBond( PCMMUTManager molHndin, int selHndin,
50 PCMMUTManager molHndin2, int selHndin2) :
51 CMMANBase ( molHndin, selHndin, molHndin2, selHndin2 ) {
52 //-------------------------------------------------------------------
53 InitParams();
54 }
55
56 //---------------------------------------------------------------------
57 CHBond::CHBond( PCMMUTManager molHndin) : CMMANBase ( molHndin) {
58 //-------------------------------------------------------------------
59 InitParams();
60 }
61
62 //-------------------------------------------------------------------
63 CHBond::CHBond( PCMMUTManager molHndin, int selHndin) :
64 CMMANBase ( molHndin, selHndin) {
65 //-------------------------------------------------------------------
66 InitParams();
67 }
68
69 //-------------------------------------------------------------------
70 CHBond::~CHBond( ) {
71 //-------------------------------------------------------------------
72 }
73
74 //-------------------------------------------------------------------
75 void CHBond::InitParams() {
76 //-------------------------------------------------------------------
77 //params
78 min_D_A = 2.0;
79 max_D_A = 3.9;
80 max_H_A = 2.5;
81 min_DD_D_A = 90 * PI/180.0;
82 min_D_A_AA = 90 * PI/180.0;
83 min_H_A_AA = 90 * PI/180.0;
84 min_D_H_A = 90 * PI/180.0;
85
86 }
87 //-------------------------------------------------------------------
88 void CHBond::SetParams(int nv, double *value) {
89 //-------------------------------------------------------------------
90 //params
91
92 min_D_A = value[0];
93 max_D_A = value[1];
94 max_H_A = value[2];
95 min_DD_D_A = value[3] * PI/180.0;
96 min_D_A_AA = value[4] * PI/180.0;
97 min_H_A_AA = value[5] * PI/180.0;
98 min_D_H_A = value[6] * PI/180.0;
99
100 }
101
102 //-------------------------------------------------------------------
103 int CHBond::Calculate(bool separate_models) {
104 //-------------------------------------------------------------------
105 //Calculate for all NMR models - each needs separate analysis
106 if (separate_models ) {
107 for (int i = 1; i <= GetMolHnd(0)->GetNumberOfModels(); i++) {
108 Calculate0(i);
109 }
110 }
111 else
112 Calculate0();
113 return 0;
114 }
115
116 //-------------------------------------------------------------------
117 int CHBond::Calculate0(int model) {
118 //-------------------------------------------------------------------
119 mmdb::PPAtom donorAtoms = NULL;
120 mmdb::PPAtom acceptorAtoms = NULL;
121 mmdb::PPAtom overlapAtoms = NULL;
122 int nDonors,nAcceptors;
123 int udd,udd2;
124 int loop,nLoops = 1;
125 int uddD = -1, uddA = -1,selHndD = -1, selHndA = -1;
126 PCMMUTManager molHndD = 0,molHndA = 0;
127 int selHndDonors,selHndAcceptors,selHndHydrogens,selHndOverlap = -1;
128 mmdb::Contact *contacts = NULL;
129 int ncontacts;
130 int nOverlap = 0;
131
132 int RC,hbtype1,hbtype2,accept;
133 int i,n;
134 SAtomBond *donorBond = 0;
135 SAtomBond *acceptorBond = 0;
136 int nDBonds,nABonds,nH;
137 mmdb::realtype H_A, D_H_A;
138 int donor_angles_OK;
139
140 //std::ostringstream label;
141 //label.precision(3);
142
143 udd = LoadUDDHBType(molHnds[0]);
144 if ( udd <= 0 ) return 2;
145 //cout << "udd " << udd;
146
147 if (molHnds[1]) {
148 udd2 = LoadUDDHBType(molHnds[1]);
149 if ( udd2 <= 0 ) return 2;
150 }
151 else
152 udd2 = udd;
153
154 if ( selHnds[1] >= 0 || molHnds[1] )
155 nLoops = 2;
156
157
158 // For two sets of atoms - do the sets overlap?
159 if (nLoops == 2 && molHnds[0] == molHnds[1]) {
160 selHndOverlap = molHnds[0]->NewSelection();
161 molHnds[0]->Select ( selHndOverlap,mmdb::STYPE_ATOM,selHnds[0],mmdb::SKEY_NEW );
162 molHnds[0]->Select ( selHndOverlap,mmdb::STYPE_ATOM,selHnds[1],SKEY_AND );
163 molHnds[0]->GetSelIndex(selHndOverlap,overlapAtoms,nOverlap);
164 //cout << "nOverlap " << nOverlap << endl;
165 }
166
167 //cout << "nLoops " << nLoops << " udd,udd2 " << udd << " " << udd2 << endl;
168 for (loop=0;loop<nLoops;loop++) {
169
170 if (nLoops == 1) {
171 molHndD = molHnds[0];
172 molHndA = molHnds[0];
173 selHndA = selHnds[0];
174 selHndD = selHnds[0];
175 uddD = udd;
176 uddA = udd;
177 }
178 else if ( loop == 0 ) {
179 molHndD = molHnds[0];
180 selHndD = selHnds[0];
181 uddD = udd;
182 molHndA = molHnds[1];
183 selHndA = selHnds[1];
184 uddA = udd2;
185 }
186 else if ( loop == 1 ) {
187 molHndD = molHnds[1];
188 selHndD = selHnds[1];
189 uddD = udd2;
190 molHndA = molHnds[0];
191 selHndA = selHnds[0];
192 uddA = udd;
193 }
194
195 //cout << "molHndD " << molHndD << " selHndD " << selHndD << endl;
196 //cout << "molHndA " << molHndA << " selHndA " << selHndA << endl;
197
198 selHndDonors = molHndD->NewSelection();
199 molHndD->Select ( selHndDonors,mmdb::STYPE_ATOM,selHndD,mmdb::SKEY_NEW );
200 if (model > 0 && model <= molHndD->GetNumberOfModels() )
201 molHndD->Select (selHndDonors,mmdb::STYPE_ATOM,model,"*",mmdb::ANY_RES,
202 "*",mmdb::ANY_RES, "*","*","*","*","*",SKEY_AND);
203 molHndD->SelectUDD (selHndDonors,mmdb::STYPE_ATOM,uddD,
204 HBTYPE_DONOR,HBTYPE_BOTH,SKEY_AND);
205 molHndD->GetSelIndex(selHndDonors,donorAtoms,nDonors);
206
207 selHndAcceptors = molHndA->NewSelection();
208 molHndA->Select ( selHndAcceptors,mmdb::STYPE_ATOM,selHndA,mmdb::SKEY_NEW );
209 if (model > 0 && model <= molHndA->GetNumberOfModels() )
210 molHndA->Select (selHndAcceptors,mmdb::STYPE_ATOM,model,"*",mmdb::ANY_RES,
211 "*",mmdb::ANY_RES, "*","*","*","*","*",SKEY_AND);
212 molHndA->SelectUDD (selHndAcceptors,mmdb::STYPE_ATOM,uddA,
213 HBTYPE_BOTH,HBTYPE_ACCEPTOR,SKEY_AND);
214 molHndA->GetSelIndex(selHndAcceptors,acceptorAtoms,nAcceptors);
215
216
217 // And are there any actual real hydrogen atoms?
218 selHndHydrogens = molHndD->NewSelection();
219 molHndD->Select ( selHndHydrogens,mmdb::STYPE_ATOM,selHndD,mmdb::SKEY_NEW );
220 molHndD->SelectUDD (selHndHydrogens,mmdb::STYPE_ATOM,uddD,
221 HBTYPE_HYDROGEN,HBTYPE_HYDROGEN,SKEY_AND);
222
223 /*
224 cout << "nDonors " << nDonors << " ";
225 for (int ii=0; ii<nDonors; ii++) {
226 cout << donorAtoms[ii]->name << " "; }
227 cout << endl;
228
229 cout << "nAcceptors " << nAcceptors << " ";
230 for (int ii=0; ii<nAcceptors; ii++) {
231 cout << acceptorAtoms[ii]->name << " "; }
232 cout << endl;
233 */
234
235 //cout << " nDonors " << nDonors << " nAcceptors " << nAcceptors << endl;
236 if ( nDonors > 0 && nAcceptors > 0 ) {
237 // Find close contacts beteen donors and acceptors
238 mmdb::mat44 * TMatrix=0;
239
240 molHnds[0]->SeekContacts ( donorAtoms,nDonors,acceptorAtoms,nAcceptors,
241 min_D_A,max_D_A,0,contacts,ncontacts, 0,TMatrix, 0 , 0);
242
243 //cout << "ncontacts " << ncontacts << endl;
244 if ( !contacts || ncontacts <= 0 ) {
245 if(selHndAcceptors>=0)molHndA->DeleteSelection(selHndAcceptors);
246 if(selHndDonors>=0)molHndD->DeleteSelection(selHndDonors);
247 //std::cout << "Returning due to dodgy ncontacts\n";
248 }
249
250 // Loop over the contacts testing the geometry
251
252 for ( n = 0; n<ncontacts;n++) {
253
254 // cout << donorAtoms[contacts[n].id1]->name << " " << acceptorAtoms[contacts[n].id2]->name << endl;
255
256 if( molHnds[0]->doAltLocMatch( donorAtoms[contacts[n].id1],
257 acceptorAtoms[contacts[n].id2]) ) {
258 donorBond = 0;
259 donorAtoms[contacts[n].id1]->GetBonds(donorBond,nDBonds);
260 acceptorBond = 0;
261 acceptorAtoms[contacts[n].id2]->GetBonds(acceptorBond,nABonds);
262 nH = 0;
263 // Is donor bonded to any H atoms
264 for ( i = 0; i < nDBonds; i++ ) {
265 // If there is a hydrogen then test geometry
266 if (donorBond[i].atom->isInSelection(selHndHydrogens) ) {
267 nH=nH+1;
268 H_A = molHnds[0]->BondLength(donorBond[i].atom,
269 acceptorAtoms[contacts[n].id2]);
270 D_H_A = molHnds[0]->BondAngle(donorAtoms[contacts[n].id1],
271 donorBond[i].atom,
272 acceptorAtoms[contacts[n].id2]);
273 if ( H_A < max_H_A && D_H_A > min_D_H_A ) {
274 if ( acceptorBond <= 0 ||
275 molHnds[0]->BondAngle( donorBond[i].atom,
276 acceptorAtoms[contacts[n].id2],acceptorBond[0].atom )
277 > min_H_A_AA ) {
278 // If the hydrogen atom is part of the original atom selection
279 // then record the bond as being to the hydrogen
280 if (donorBond[i].atom->isInSelection(selHndD) )
281 if (loop==1)
282 hbonds.AddConnection(acceptorAtoms[contacts[n].id2],
283 donorBond[i].atom,FloatToString(H_A,"%.1f"));
284 else
285 hbonds.AddConnection(donorBond[i].atom,
286 acceptorAtoms[contacts[n].id2],FloatToString(H_A,"%.1f"));
287 else
288 if (loop==1)
289 hbonds.AddConnection(acceptorAtoms[contacts[n].id2],
290 donorAtoms[contacts[n].id1],FloatToString(contacts[n].dist,"%.1f"));
291 else
292 hbonds.AddConnection( donorAtoms[contacts[n].id1],
293 acceptorAtoms[contacts[n].id2],FloatToString(contacts[n].dist,"%.1f"));
294
295 }
296 }
297 }
298 }
299 if ( nH <= 0 ) {
300 // If there were no hydrogens check that bonds between atoms that
301 // have HBTYPE_BOTH are only recorded once
302 // and check donor-acceptor geometry
303 if ( acceptorAtoms[contacts[n].id2]->serNum > donorAtoms[contacts[n].id1]->serNum )
304 accept = 1;
305 else {
306 RC = acceptorAtoms[contacts[n].id2]->GetUDData(uddA,hbtype1);
307 RC = donorAtoms[contacts[n].id1]->GetUDData(uddD,hbtype2);
308 accept = hbtype1 -hbtype2 ;
309 }
310 if ( accept && nOverlap>0 && loop==1 &&
311 acceptorAtoms[contacts[n].id2]->isInSelection(selHndOverlap) &&
312 donorAtoms[contacts[n].id1]->isInSelection(selHndOverlap) ) {
313 //cout << "overlapped " << acceptorAtoms[contacts[n].id2]->name << " " << donorAtoms[contacts[n].id1]->name << endl;
314 accept = 0;
315 }
316
317 //cout << acceptorAtoms[contacts[n].id2]->serNum <<
318 // acceptorAtoms[contacts[n].id2]->name << " " << donorAtoms[contacts[n].id1]->serNum << donorAtoms[contacts[n].id1]->name << " " << accept << endl;
319 if ( accept != 0 &&
320 ( nABonds <= 0 ||
321 molHnds[0]->BondAngle (donorAtoms[contacts[n].id1] ,
322 acceptorAtoms[contacts[n].id2],
323 acceptorBond[0].atom ) > min_D_A_AA ) ) {
324
325 donor_angles_OK = 1;
326 for ( i = 0; i < nDBonds; i++ ) {
327 //cout << " " << molHnds[0]->AtomLabel_atom(donorBond[i].atom);
328 //cout << " " << molHnds[0]->AtomLabel_atom(donorAtoms[contacts[n].id1]);
329 //cout << " " << molHnds[0]->AtomLabel_atom(acceptorAtoms[contacts[n].id2]);
330 //cout << " " << molHnds[0]->BondAngle (donorBond[i].atom,
331 // donorAtoms[contacts[n].id1],
332 // acceptorAtoms[contacts[n].id2]) << endl;
333
334 if ( strcmp(donorBond[i].atom->element," H")!= 0 &&
335 molHnds[0]->BondAngle (donorBond[i].atom,
336 donorAtoms[contacts[n].id1] ,
337 acceptorAtoms[contacts[n].id2] ) < min_DD_D_A ) {
338 donor_angles_OK = 0;
339 break;
340 }
341 }
342 if ( donor_angles_OK > 0) {
343 if (loop==1)
344 hbonds.AddConnection(acceptorAtoms[contacts[n].id2],
345 donorAtoms[contacts[n].id1],FloatToString(contacts[n].dist,"%.1f"));
346 else
347 hbonds.AddConnection(donorAtoms[contacts[n].id1],
348 acceptorAtoms[contacts[n].id2],FloatToString(contacts[n].dist,"%.1f"));
349 }
350 }
351 }
352 }
353
354 } }
355
356 //cout << "selHndAcceptors " << selHndAcceptors << selHndDonors << endl;
357
358 if (contacts) {
359 delete [] contacts;
360 contacts = 0;
361 }
362 if(selHndAcceptors>=0)molHndA->DeleteSelection(selHndAcceptors);
363 if(selHndDonors>=0)molHndD->DeleteSelection(selHndDonors);
364 if(selHndHydrogens>=0)molHndD->DeleteSelection(selHndHydrogens);
365
366
367 } // End of the big loop
368
369
370 //cout << "End of Calculate0" << endl;
371 return 0;
372
373 }
374
375 //-----------------------------------------------------------------------
376 int CHBond::LoadUDDHBType(PCMMUTManager molH ) {
377 //-----------------------------------------------------------------------
378 int nchains = molH->GetNumberOfChains(1);
379 int nr0,nat0;
380 int udd;
381 //char *foo;
382
383 udd = molH->GetUDDHandle ( mmdb::UDR_ATOM,"hbtype" );
384 if (udd <= 0 ) {
385 udd = -1;
386 udd = molH->RegisterUDInteger ( mmdb::UDR_ATOM,"hbtype" );
387 if (udd <= 0 ) return udd;
388 }
389 //cout << "HBond udd " << udd << endl;
390 mmdb::PPAtom atomTable = NULL;
391 for (int im=1;im<=molH->GetNumberOfModels();im++) {
392 for (int ic=0;ic<nchains;ic++) {
393 nr0 = molH->GetNumberOfResidues(im,ic);
394 for (int i=0;i<nr0;i++) {
395 molH->GetAtomTable(im,ic,i,atomTable,nat0);
396 for (int j=0;j<nat0;j++) {
397 atomTable[j]->PutUDData(udd,dynamic_cast<PCMMANManager>(molH)->
398 GetAtomHBondType1(atomTable[j]) );
399 //cout << atomTable[j]->GetAtomID(foo) << " " <<
400 // dynamic_cast<PCMMANManager>(molH)->GetAtomHBondType1(
401 // atomTable[j]) << endl;
402 }
403 }
404 }
405 }
406 return udd;
407 }
408
409 //-----------------------------------------------------------------------
410 std::string CHBond::Print(bool geometry) {
411 //-----------------------------------------------------------------------
412
413 SAtomBond *donorBond = 0;
414 SAtomBond *acceptorBond = 0;
415 int nDBonds,nABonds;
416
417 std::ostringstream output;
418 std::string first,second,first_bonded,second_bonded;
419 float first_angle, second_angle, distance;
420 std::vector<mmdb::PAtom>::iterator i = hbonds.pAtom1.begin();
421 std::vector<mmdb::PAtom>::iterator j = hbonds.pAtom2.begin();
422 PCMMUTManager molHnd0,molHnd1;
423 output.setf(ios::fixed);
424 output.setf(ios::showpoint);
425 output.setf(ios::left,ios::adjustfield);
426 output.precision(2);
427
428 output << "Neighb set1 (N1) "
429 << "Hbond atom set1(B1) "
430 << "Hbond atom set2 (B2)"
431 << "Neighb set2 (N2) " << endl << " "
432 << "N1-B1-B2 angle "
433 << "B1-B2 distance "
434 << "B1-B2-N2 angle" << endl << endl;
435 molHnd0 = molHnds[0];
436 if (molHnds[1])
437 molHnd1 = molHnds[1];
438 else
439 molHnd1 = molHnds[0];
440
441 while(i!=hbonds.pAtom1.end()){
442
443 first = molHnd0->AtomLabel_atom(*i);
444 second = molHnd1->AtomLabel_atom(*j);
445
446 if ( geometry ) {
447 // Ignore the naming of donor/acceptor - the lists indexed
448 // by i and j are the atoms from the two separate selections
449 donorBond = 0;
450 acceptorBond = 0;
451 first_bonded = "";
452 first_angle = 0.0;
453 second_bonded = "";
454 second_angle = 0.0;
455 (*i)->GetBonds(donorBond,nDBonds);
456 if ( nDBonds > 0 ) {
457 first_bonded = molHnd0->AtomLabel_atom(donorBond[0].atom);
458 first_angle = molHnd0->BondAngle (donorBond[0].atom ,
459 *i,*j)*180.0/PI;
460 }
461
462 (*j)->GetBonds(acceptorBond,nABonds);
463 if ( nABonds > 0 ) {
464 second_bonded = molHnd1->AtomLabel_atom(acceptorBond[0].atom);
465 second_angle = molHnd1->BondAngle (*i,*j,
466 acceptorBond[0].atom )*180.0/PI;
467 }
468
469 distance = molHnd0->BondLength(*i,*j);
470
471 output << first_bonded << std::setw(20-first_bonded.length()) << " "
472 << first << std::setw(20-first.length()) << " "
473 << second << std::setw(20-second.length()) << " "
474 << second_bonded << endl << " "
475 << std::setw(20) << first_angle
476 << std::setw(20) << distance
477 << std::setw(20) << second_angle << endl;
478
479 }
480 else {
481 output << second << std::setw(20-second.length()) << " " << first << endl;
482 }
483 i++;
484 j++;
485 }
486 //cout << output.str();
487
488 return output.str();
489 }