Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
packstat.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
11 /// @brief
12 
13 
14 // libRosetta headers
15 //#include <basic/options/option.hh>
16 
17 #include <core/scoring/packstat/types.hh>
18 #include <core/scoring/packstat/SimplePDB_Atom.hh>
19 #include <core/scoring/packstat/SimplePDB.hh>
20 #include <core/scoring/packstat/AtomRadiusMap.hh>
21 #include <core/scoring/packstat/compute_sasa.hh>
22 
23 #include <protocols/analysis/PackStatMover.hh>
24 
25 #include <devel/init.hh>
26 #include <core/types.hh>
27 #include <basic/options/option.hh>
28 
29 
30 #include <basic/Tracer.hh>
31 
32 #include <utility/vector1.hh>
33 #include <utility/file/FileName.hh>
34 #include <utility/io/izstream.hh>
35 #include <utility/io/ozstream.hh>
36 
37 
38 #include <iostream>
39 #include <fstream>
40 #include <sstream>
41 #include <string>
42 
43 
44 // option key includes
45 
46 #include <basic/options/keys/out.OptionKeys.gen.hh>
47 #include <basic/options/keys/packstat.OptionKeys.gen.hh>
48 #include <basic/options/keys/in.OptionKeys.gen.hh>
49 
50 #include <ObjexxFCL/format.hh>
51 
52 #include <protocols/jd2/JobDistributor.hh>
53 
55 
56 using core::Real;
57 
58 static THREAD_LOCAL basic::Tracer TRps( "packstat" );
59 
60 using namespace core::scoring::packstat;
61 
62 inline std::string base_name(const std::string& str) {
63  size_t begin = 0;
64  size_t end = str.length();
65 
66  for ( size_t i=0; i<str.length(); ++i ) {
67  if ( str[i] == '/' ) begin = i+1;
68  }
69 
70  return str.substr(begin,end);
71 }
72 
73 std::string get_out_tag(std::string fname) {
74  std::string base = base_name(fname);
75  std::transform( base.begin(), base.end(), base.begin(), tolower );
76  if ( system( ("mkdir -p out/" + base.substr(1,2)).c_str() ) == -1 ) {
77  TRps.Error << "Unable to make directory!" << std::endl;
78  }
79  std::string OUT_TAG = "out/" + base.substr(1,2) + "/" + base;
80  return OUT_TAG;
81 }
82 
83 void output_packstat_pdb( std::string fname, utility::vector1<core::Real> const & res_scores ) {
84  using core::Size;
85  using namespace core::scoring::packstat;
86  using namespace std;
87  using namespace core;
88  using namespace basic::options;
89  using namespace ObjexxFCL::format;
90  using namespace numeric;
91  using namespace utility;
92 
93  bool surface_accessibility = option[ OptionKeys::packstat::surface_accessibility ]();
94  core::Real burial_radius = option[ OptionKeys::packstat::cavity_burial_probe_radius ]();
95 
96  string OUT_TAG = get_out_tag(fname);
97 
98  AtomRadiusMap arm;
99  SimplePDB pdb;
100  utility::io::izstream in(fname.c_str());
101  in >> pdb;
102 
103  Spheres spheres;
104  spheres = pdb.get_spheres(arm);
105  std::cerr << "spheres len: " << spheres.size() << std::endl;
106  vector1< xyzVector<PackstatReal> > centers( pdb.get_res_centers() );
107  std::cerr << "centers len: " << centers.size() << std::endl;
108 
109  SasaOptions opts;
110  opts.prune_max_iters = 999;
111  opts.prune_max_delta = 0;
112  opts.num_cav_ball_layers = 10;
113  opts.frac_cav_ball_required_exposed = 0.00;
114  opts.area_cav_ball_required_exposed = 0.00;
115  opts.surrounding_sasa_smoothing_window = 3;
116  opts.min_cav_ball_radius = option[ OptionKeys::packstat::min_cav_ball_radius ]();
117  opts.min_cluster_overlap = option[ OptionKeys::packstat::min_cluster_overlap ]();
118  opts.cluster_min_volume = option[ OptionKeys::packstat::cluster_min_volume ]();
119  for ( PackstatReal pr = 3.0; pr >= 0.4; pr -= 0.1 ) opts.probe_radii.push_back(pr);
120  opts.prune_cavity_burial_probe_radii.push_back( burial_radius );
121  if ( surface_accessibility ) {
122  for ( PackstatReal pr = burial_radius-0.1; pr >= 0.1; pr -= 0.1 ) {
123  opts.prune_cavity_burial_probe_radii.push_back(pr);
124  }
125  }
126 
127  //TRps << "compute MSAs" << std::endl;
128  SasaResultOP sr = compute_sasa( spheres, opts );
129 
130  ////////////////////////////////////////////////////////////////////////////////////////////////
131  CavBalls cavballs = sr->cavballs;
132  //TRps << "pruning hidden cav balls " << cavballs.size() << std::endl;
133  cavballs = prune_hidden_cavity_balls( cavballs, opts );
134 
135  //TRps << "pruning exposed cav balls " << cavballs.size() << std::endl;
136  cavballs = prune_cavity_balls( spheres, cavballs, opts );
137 
138  //TRps << "compute cav ball volumes " << cavballs.size() << std::endl;
139  compute_cav_ball_volumes( cavballs, opts );
140 
142  compute_cav_ball_clusters( cavballs, opts );
143 
144  ///////////////////////////////////////////////////////////////////////////////////////////////
145  //TRps << "writting stupid pdb to "+OUT_TAG+".pdb" << std::endl;
146  ostringstream out;
147  // if( pdb.atoms().size() != spheres.size() ) { // hydrogens!
148  // TRps << "WARNING: output_packstat_pdb: size of pdb.atoms() != size of spheres" << std::endl;
149  // TRps << " spheres: " << spheres.size() << ", pdb.atoms(): " << pdb.atoms().size() << std::endl;
150  // }
151  int prev_resnum = -12345;
152  int resnum = 0;
153  int res_atom_count = 999;
154  for ( int i = 1; i <= (int)pdb.atoms().size(); ++i ) {
155  SimplePDB_Atom & atom( pdb.atoms()[i] );
156  // std::cerr << "FOO " << resnum << " " << res_atom_count << " " << prev_resnum << " " << atom.resnum << std::endl;
157  if ( prev_resnum != atom.resnum ) {
158  if ( res_atom_count > 3 ) {
159  resnum++;
160  }
161  prev_resnum = atom.resnum;
162  res_atom_count = 0;
163  }
164  res_atom_count++;
165  core::Real bfac = 1.0;
166  if ( resnum > (int)res_scores.size() ) {
167  TRps << "ERROR: more residues than res scores " << resnum << " " << res_scores.size() << std::endl;
168  } else {
169  bfac = res_scores[resnum];
170  }
171  // std::cerr << "bfac " << resnum << " " << res_scores.size() << " " << res_scores[resnum] << std::endl;
172  out << atom.whole_line.substr(0,61) << F(4,2,bfac) << atom.whole_line.substr(65) << std::endl;
173  // out << LJ(6,atom.ATOM) + I( 5, ( atom.num ) ) + " "
174  // + LJ(4,atom.type) + " " + LJ(3,atom.res) + " " + atom.chain
175  // + I( 4, atom.resnum ) + " "
176  // + F( 8, 3, atom.x ) + F( 8, 3, atom.y ) + F( 8, 3, atom.z )
177  // + F( 6, 2, atom.occ ) + ' ' + F( 5, 2, atom.bfac ) << std::endl;
178  }
179  // for( size_t cb = 1; cb <= cavballs.size(); ++cb ) {
180  // // if( cavballs[cb].radius() > 0.9 )
181  // out << cavballs[cb].hetero_atom_line() << std::endl;
182  // }
183  // for( size_t cb = 1; cb <= sel_cbs.size(); ++cb) {
184  // out << sel_cbs[cb].hetero_atom_line(7) << std::endl;
185  // }
186 
187  std::cout << "Add Cavities to PDB:" << std::endl;
188  Size count = 1;
189  for ( Size i = 1; i <= clusters.size(); i++ ) {
190  // if( clusters[i].volume < 150.0 ) continue;
191  if ( clusters[i].surface_accessibility < option[ OptionKeys::packstat::min_surface_accessibility ]() ) continue;
192  std::cout << "Cavity: " << count
193  << " volume " << clusters[i].volume
194  << " surf " << clusters[i].surface_area
195  << " surf. acc. " << clusters[i].surface_accessibility
196  << std::endl;
197  for ( Size j = 1; j <= clusters[i].cavballs.size(); j++ ) {
198  // if( clusters[i].cavballs[j].radius() > 0.7 )
199  out << clusters[i].cavballs[j].hetero_atom_line( centers.size()+count, count, 0.0 ) << std::endl;
200  }
201  count++;
202  }
203 
204  // for( Size i = 1; i <= clusters.size(); i++ ) {
205  // numeric::xyzVector<core::Real> xyz_ = clusters[i].center;
206  // out << "HETATM" + I( 5, 1 ) + " V CTR Z"
207  // + I( 4, 1 ) + " "
208  // + F( 8, 3, xyz_.x() ) + F( 8, 3, xyz_.y() ) + F( 8, 3, xyz_.z() )
209  // + F( 6, 2, 0.0 ) + ' ' + F( 5, 2, 0.0 ) << std::endl;
210  // }
211 
212  utility::io::ozstream outz((OUT_TAG+".pdb").c_str());
213  outz << out.str();
214  outz.close();
215  out.clear();
216 
217 }
218 
219 void output_packstat( std::string fname ) {
220 
221  using core::Size;
222  using namespace core::scoring::packstat;
223  using namespace std;
224  using namespace core;
225  using namespace basic::options;
226  using namespace basic::options::OptionKeys;
227  using namespace ObjexxFCL::format;
228  using namespace numeric;
229  using namespace utility;
230 
231  core::Size oversample = option[ OptionKeys::packstat::oversample ]();
232  bool include_water = option[ OptionKeys::packstat::include_water ]();
233  bool residue_scores = option[ OptionKeys::packstat::residue_scores ]();
234  bool packstat_pdb = option[ OptionKeys::packstat::packstat_pdb ]();
235  bool raw_stats = option[ OptionKeys::packstat::raw_stats ]();
236 
237  AtomRadiusMap arm;
238  SimplePDB pdb;
239  utility::io::izstream in(fname.c_str());
240  in >> pdb;
241 
242  Spheres spheres;
243  //TRps << fname << std::endl;
244  spheres = pdb.get_spheres(arm);
245  // write_spheres_pdb(spheres,"spheres_from_pdb.pdb");
246  // std::sort( spheres.begin(), spheres.end(), OrderSphereOnX() );
247  // std::cerr << "spheres len: " << spheres.size() << std::endl;
248  vector1< xyzVector<PackstatReal> > centers( pdb.get_res_centers() );
249  // std::cerr << "centers len: " << centers.size() << std::endl;
250 
251  PosePackData pd;
252  pd.spheres = spheres;
253  pd.centers = centers;
254  core::Real packing_score = compute_packing_score( pd, oversample );
255 
256  TRps << "packing score: " << fname << " " << centers.size() << " " << spheres.size() << " " << packing_score;
257  if ( include_water ) {
258  TRps << " ( with " << pdb.num_water() << " waters )";
259  }
260  TRps << std::endl;
261 
262  utility::vector1<core::Real> res_scores; // needed if output res scores or pdb
263  if ( packstat_pdb || residue_scores ) {
264  res_scores = compute_residue_packing_scores( pd, oversample );
265  }
266 
267  if ( packstat_pdb ) {
268  output_packstat_pdb( fname, res_scores );
269  }
270 
271  if ( residue_scores ) {
272  for ( int i = 1; i <= (int)res_scores.size(); ++i ) {
273  TRps << "packing score: residue " << i << " " << res_scores[i] << std::endl;
274  }
275  }
276 
277  if ( raw_stats ) { // stupid duplicate code....
278 
279  assert( pd.spheres.size() > 0 );
280  assert( pd.centers.size() > 0 );
281 
282  SasaOptions opts;
283  opts.surrounding_sasa_smoothing_window = 1+2*oversample;
284  opts.num_surrounding_sasa_bins = 7;
285  for ( core::Size ipr = 1; ipr <= 31; ++ipr ) {
286  PackstatReal pr = 3.0 - ((double)(ipr-1))/10.0;
287  PackstatReal ostep = 0.1 / (oversample*2.0+1.0);
288  for ( core::Size i = 1; i <= oversample; ++i ) opts.probe_radii.push_back( pr + i*ostep );
289  opts.probe_radii.push_back( pr );
290  for ( core::Size i = 1; i <= oversample; ++i ) opts.probe_radii.push_back( pr - i*ostep );
291  }
292 
293  SasaResultOP result = compute_sasa( pd.spheres, opts );
294  for ( core::Size i = 1; i <= pd.centers.size(); ++i ) {
295  // std::cout << i << " ";
296  PackingScoreResDataCOP dat( compute_surrounding_sasa( pd.centers[i], pd.spheres, result, opts ) );
297  TRps << "RAW_STATS " << i << " ";
298  for ( Size i =1; i <= dat->nrad(); ++i ) {
299  for ( Size j =1; j <= dat->npr(); ++j ) {
300  TRps << dat->msa(i,j) << " ";
301  }
302  }
303  TRps << std::endl;
304 
305  } // end raw_stats
306 
307  }
308 
309 }
310 
311 int main (int argc, char *argv[]) {
312  try {
313 
314  devel::init( argc, argv );
315 
316  using namespace core::scoring::packstat;
317  using namespace basic::options;
318  using namespace basic::options::OptionKeys;
319  using namespace utility;
320 
321  // test_io();
322 
323  // test_sasa_dots();
324  if ( !option[ out::output ].user() ) {
325  option[ out::nooutput ].value( true );
326  }
327 
328  if ( option[ in::file::silent ].user() ) {
329 
330  protocols::analysis::PackStatMoverOP pack_mover;
331  //protocols::jobdist::universal_main(m);
332  protocols::jd2::JobDistributor::get_instance()->go(pack_mover);
333 
334  } else {
335 
336  if ( option[ in::file::s ].user() ) {
338  for ( size_t i = 1; i <= files.size(); ++i ) {
339  output_packstat( files[i] );
340  }
341  } else if ( option[ in::file::l ].user() ) {
342  vector1<file::FileName> files( option[ in::file::l ]() );
343  for ( size_t i = 1; i <= files.size(); ++i ) {
344  utility::io::izstream list( files[i] );
345  std::string fname;
346  while ( list >> fname ) {
347  // std::cerr << "'" << fname << "'" << std::endl;
348  output_packstat( fname );
349  }
350  }
351  }
352 
353  }
354 
355  } catch ( utility::excn::EXCN_Base const & e ) {
356  std::cout << "caught exception " << e.msg() << std::endl;
357  return -1;
358  }
359 
360  return 0;
361 
362 }
363 
364 
ocstream cerr(std::cerr)
Wrapper around std::cerr.
Definition: ocstream.hh:290
#define THREAD_LOCAL
virtual std::string const msg() const
Definition: EXCN_Base.hh:70
platform::Size Size
Definition: types.hh:42
void init(int argc, char *argv[])
Command line init() version.
Definition: init.cc:23
BooleanOptionKey const user("options:user")
Definition: OptionKeys.hh:40
utility::keys::lookup::end< KeyType > const end
string output
Definition: contacts.py:31
void output_packstat_pdb(std::string fname, utility::vector1< core::Real > const &res_scores)
Definition: packstat.cc:83
int main(int argc, char *argv[])
Definition: packstat.cc:311
common derived classes for thrown exceptions
static THREAD_LOCAL basic::Tracer TRps("packstat")
Tracer IO system.
izstream: Input file stream wrapper for uncompressed and compressed files
Definition: izstream.hh:44
Input file stream wrapper for uncompressed and compressed files.
TracerProxy Error
Definition: Tracer.hh:262
File name class supporting Windows and UN*X/Linux format names.
rule< Scanner, options_closure::context_t > options
Definition: Tag.cc:377
list resnum
if line_edit[13:14]=='P': #Nucleic acid? Skip.
Output file stream wrapper for uncompressed and compressed files.
void close()
Close the ofstream and reset the state.
Definition: ozstream.hh:430
double Real
Definition: types.hh:39
std::string F(int const w, int const d, float const &t)
Fixed Point Format: float.
Definition: format.cc:387
ocstream cout(std::cout)
Wrapper around std::cout.
Definition: ocstream.hh:287
vector1: std::vector with 1-based indexing
Class for handling user debug/warnings/errors. Use instance of this class instead of 'std::cout' for ...
Definition: Tracer.hh:134
utility::keys::lookup::begin< KeyType > const begin
std::string get_out_tag(std::string fname)
Definition: packstat.cc:73
std::string base_name(const std::string &str)
Definition: packstat.cc:62
ozstream: Output file stream wrapper for uncompressed and compressed files
Definition: ozstream.hh:53
Program options global and initialization function.
void output_packstat(std::string fname)
Definition: packstat.cc:219
platform::Size Size
Definition: random.fwd.hh:30
int const silent
Named verbosity levels.
rule< Scanner, option_closure::context_t > option
Definition: Tag.cc:378