Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NcontactsCalculator.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file protocols/toolbox/PoseMetricCalculators/NcontactsCalculator.cc
11 /// @brief calculate #atom contacts divided by pose.total_residue() in a given pose
12 /// @detailed If two atom, each of which belongs to different residues, are within a
13 /// given distance, condist_( default 6.0A ), the two atoms are defined as contact pair.
14 /// The residues the atoms belong to are close ( isep_residue_ = 4 ) with each other along sequence,
15 /// the contact pair is ignored.
16 /// @author Nobuyasu Koga ( nobuyasu@uw.edu )
17 
18 // Unit Headers
20 
21 //package headers
26 
27 // Project Headers
28 #include <basic/MetricValue.hh>
31 #include <core/pose/Pose.hh>
32 #include <basic/Tracer.hh>
33 
34 // Utility headers
35 #include <utility/vector1.hh>
36 #include <utility/string_util.hh>
37 
38 //// C++ headers
39 #include <cmath>
40 
42 #include <utility/options/BooleanVectorOption.hh>
43 
44 
45 static basic::Tracer tr("protocols.fldsgn.NcontactsCalculator");
46 
47 namespace protocols {
48 namespace fldsgn {
49 
50 class MyAtom {
51 public:
52  typedef core::Size Size;
53  MyAtom(){
54  for ( Size j=1; j<= 50 ; ++j ) {
55  atom_hydrophobic_.push_back(false);
56  }
57  atom_hydrophobic_[ 3 ] = true; // CH1
58  atom_hydrophobic_[ 4 ] = true; // CH2
59  atom_hydrophobic_[ 5 ] = true; // CH3
60  atom_hydrophobic_[ 6 ] = true; // aroC
61  atom_hydrophobic_[ 19 ] = true; // CAbb
62  }
63  bool is_hydrophobic( Size const index ){
64  return atom_hydrophobic_[ index ];
65  }
66  private:
68 }; // MyAtom
69 
70 /// @brief default constructor
72  condist_( 6.0 ),
73  isep_residue_( 4 ),
74  ignore_loops_( false ),
75  ignore_same_sselement_( false ),
76  ignore_same_sheet_( false ),
77  use_only_calpha_( false )
78 {}
79 
80 /// @brief default constructor
82  Real const cdist,
83  Size const sep ):
84  condist_( cdist ),
85  isep_residue_( sep ),
86  ignore_loops_( false ),
87  ignore_same_sselement_( false ),
88  ignore_same_sheet_( false ),
89  use_only_calpha_( false )
90 {}
91 
92 /// @brief copy constructor
94  Super(),
95  condist_( rval.condist_ ),
96  isep_residue_( rval.isep_residue_ ),
97  ignore_loops_( rval.ignore_loops_ ),
98  ignore_same_sselement_( rval.ignore_same_sselement_ ),
99  ignore_same_sheet_( rval.ignore_same_sheet_ ),
100  use_only_calpha_( rval.use_only_calpha_ )
101 {}
102 
103 /// @brief destructor
105 
106 /// @brief
107 void
109  String const & key,
110  MetricValueBase * valptr
111 ) const
112 {
113  using namespace core;
114  if ( key == "all_heavy_atm" ) {
115  basic::check_cast( valptr, &nc_allatm_, "total_nlcontacts expects to return a Real" );
116  (static_cast<basic::MetricValue<Real> *>(valptr))->set( nc_allatm_ );
117 
118  } else if (key == "sidechain_heavy_apolar_atm" ) {
119  basic::check_cast( valptr, &nc_hpatm_, "special_region_nlcontacts expects to return a Real" );
120  (static_cast<basic::MetricValue<Real> *>(valptr))->set( nc_hpatm_ );
121 
122  } else if ( key == "sidechain_heavy_atm_apolar_aa" ) {
123  basic::check_cast( valptr, &nc_hpres_, "special_region2_nlcontacts expects to return a Real" );
124  (static_cast<basic::MetricValue<Real> *>(valptr))->set( nc_hpres_ );
125 
126  } else if ( key == "ss_entropy" ) {
127  basic::check_cast( valptr, &ss_entrpy_, "special_region1_intra_nlcontacts expects to return a Real" );
128  (static_cast<basic::MetricValue<Real> *>(valptr))->set( ss_entrpy_ );
129 
130  } else {
131  tr << "NcontactsCalculator cannot compute the requested metric " << key << std::endl;
132  utility_exit();
133  }
134 
135 } //lookup
136 
137 
138 // @brief
140 NcontactsCalculator::print( String const & key ) const
141 {
142  String result;
143 
144  // #atom-contacts among all heavy atoms
145  if ( key == "all_heavy_atm" ) {
146  result = utility::to_string( nc_allatm_ );
147  // #atom-contacts among hydrophobic heavy atoms
148  } else if ( key == "sidechain_heavy_apolar_atm" ) {
149  result = utility::to_string( nc_hpatm_ );
150  //#atom-contacts among heavy atoms of sidechains of hydrophobic residues
151  } else if ( key == "sidechain_heavy_atm_apolar_aa" ) {
152  result = utility::to_string( nc_hpres_ );
153  }else if ( key == "ss_entrpy" ) {
154  result = utility::to_string( ss_entrpy_ );
155  }else{
156  basic::Error() << "NcontactsCalculator cannot compute metric " << key << std::endl;
157  }
158  return result;
159 
160 } // apply
161 
162 /// @brief recomute ncontacts
163 void
165 {
170 
171  // intialize
172  Size nres( pose.total_residue() );
173  Real condist2( numeric::square(condist_) );
174  Size nc_allatm( 0 );
175  Size nc_hpatm( 0 );
176  Size nc_hpres( 0 );
177  topology::SS_Info2_OP ssinfo = new topology::SS_Info2( pose.secstruct() );
178 
179  StrandPairingSetOP spairset = new StrandPairingSet( protocols::fldsgn::topology::calc_strand_pairing_set( pose, ssinfo ) );
180  SheetSetOP sheet_set = new SheetSet( ssinfo, spairset );
181  // std::cout << *sheet_set;
182 
183  Size max_ssele = ssinfo->ss_element_id( nres );
184  utility::vector1< utility::vector1< Size > > ncon_sselements( max_ssele, (utility::vector1< Size >(max_ssele, 1)));
185  utility::vector1< utility::vector1< bool > > calc_sselements( max_ssele, (utility::vector1< bool >(max_ssele, false)));
186 
187  // calc number of contacts
188  MyAtom myatom;
189  for ( Size iaa=1; iaa<=nres-isep_residue_; ++iaa ) {
190 
191  // skip calc if ss element is loop
192  if( ignore_loops_ && ssinfo->secstruct( iaa ) == 'L' ) continue;
193 
194  Size iaa_ssele( ssinfo->ss_element_id( iaa ) );
195 
196  for ( Size jaa=iaa+isep_residue_; jaa<=nres; ++jaa ) {
197 
198  // skip calc if ss element is loop
199  if( ignore_loops_ && ssinfo->secstruct( jaa ) == 'L' ) continue;
200 
201  Size jaa_ssele( ssinfo->ss_element_id( jaa ) );
202 
203  // skip calc pair if residues of pair belong to same ss elements
204  if( ignore_same_sselement_ && iaa_ssele == jaa_ssele ) continue;
205 
206  // skip calc pair if residues of pair belong to same ss elements
207  if( ignore_same_sheet_ &&
208  ssinfo->secstruct( iaa ) == 'E' && ssinfo->secstruct( jaa ) == 'E' &&
209  sheet_set->which_sheet( ssinfo->strand_id( iaa ) ) ==
210  sheet_set->which_sheet( ssinfo->strand_id( jaa ) ) ) continue;
211 
212  Residue const & ires( pose.residue( iaa ) );
213  for ( Size iatm=1, iatm_end=ires.natoms(); iatm<=iatm_end; ++iatm ) {
214 
215  if( ires.atom_type( int(iatm) ).is_heavyatom() ){
216  Residue const & jres( pose.residue( jaa ) );
217 
218  for ( Size jatm=1, jatm_end=jres.natoms(); jatm<=jatm_end; ++jatm ) {
219 
220  if( jres.atom_type(int(jatm)).is_heavyatom() ) {
221 
222  Atom const & iatom( ires.atom( iatm ) );
223  Atom const & jatom( jres.atom( jatm ) );
224  Size const iadex ( ires.atom_type_index( int(iatm) ) );
225  Size const jadex ( jres.atom_type_index( int(jatm) ) );
226 
227  if( (use_only_calpha_ &&
228  ires.atom_type( iatm ).name() != "CAbb") || jres.atom_type( jatm ).name() != "CAbb" ) continue;
229 
230  calc_sselements[ iaa_ssele ][ jaa_ssele ] = true;
231 
232  Real const dsq( iatom.xyz().distance_squared( jatom.xyz() ));
233 
234  if( dsq <= condist2 ){
235  nc_allatm ++;
236 
237  if( myatom.is_hydrophobic(iadex) && myatom.is_hydrophobic(jadex) ){
238  nc_hpatm ++;
239 
240  ncon_sselements[iaa_ssele][jaa_ssele] ++ ;
241 
242  if( !ires.atom_is_backbone( int(iatm) ) && !jres.atom_is_backbone( int(jatm) ) &&
243  !ires.type().is_polar() && !jres.type().is_polar() ) {
244  nc_hpres ++;
245  }
246 
247  //std::cout << iaa << " " << jaa << " " << iatm << " " << jatm << " "
248  // << ires.name() << " " << jres.name() << " "
249  // << ssinfo->secstruct( iaa ) << " " << ssinfo->secstruct( jaa ) << " "
250  // << ires.atom_type( iatm ).name() << " " << jres.atom_type( jatm ).name() << " "
251  // << sheet_set->which_sheet( ssinfo->strand_id( iaa ) ) << " "
252  // << sheet_set->which_sheet( ssinfo->strand_id( jaa ) ) << " "
253  // << dsq << std::endl;
254 
255  }
256  } // dsq
257 
258  } //fi jatm is_hydrogen ?
259  } // loop for jatm
260  } // fi iatm is_hydrogen ?
261  } // loop for iatm
262 
263  } // loop for jaa
264  }// loop for iaa
265 
266  // finalize
267  Size tot( 0 );
268  for ( Size i=1 ;i<=max_ssele; ++i ) {
269  for ( Size j=1 ;j<=max_ssele; ++j ) {
270  if ( calc_sselements[ i ][ j ] ) {
271  tot += ncon_sselements[i][j];
272  }
273  }
274  }
275  ss_entrpy_ = 0.0;
276  for ( Size i=1 ;i<=max_ssele; ++i ) {
277  for ( Size j=1 ;j<=max_ssele; ++j ) {
278  if ( calc_sselements[ i ][ j ] ) {
279  Real prob = Real(ncon_sselements[i][j])/Real(tot);
280  ss_entrpy_ += -prob*( std::log( prob )/std::log( 2.0 ) );
281  }
282  }
283  }
284  nc_allatm_ = Real(nc_allatm)/Real(nres);
285  nc_hpatm_ = Real(nc_hpatm)/Real(nres);
286  nc_hpres_ = Real(nc_hpres)/Real(nres);
287 
288 } // recompute
289 
290 } // fldsgn
291 } // protocols