14 #include <core/types.hh>
16 #include <core/chemical/AA.hh>
18 #include <core/scoring/Energies.hh>
19 #include <core/scoring/ScoreFunction.hh>
20 #include <core/scoring/ScoreFunctionFactory.hh>
22 #include <core/pack/pack_rotamers.hh>
23 #include <core/pack/task/PackerTask.hh>
24 #include <core/pack/task/TaskFactory.hh>
27 #include <core/pose/Pose.hh>
33 #include <core/import_pose/import_pose.hh>
37 #include <core/pack/task/ResfileReader.hh>
60 #include <basic/options/keys/out.OptionKeys.gen.hh>
61 #include <basic/options/keys/ddg.OptionKeys.gen.hh>
63 #include <core/chemical/ResidueType.hh>
73 using namespace basic;
74 using namespace scoring;
75 using namespace ObjexxFCL;
78 typedef std::vector<Real>
ddGs;
85 S <<
"'" << name <<
"' : " << value <<
", ";
94 if ( value ) sv =
"True";
97 S <<
"'" << name <<
"' : " << sv <<
", ";
108 for (
Size i =0; i<scores_to_sum.size(); i++ ) {
109 sum+=scores_to_sum[i];
118 for (
Size i =1; i<=scores_to_average.size(); i++ ) {
119 sum+=scores_to_average[i];
121 return (sum/scores_to_average.size());
126 scoring::ScoreFunction &
s,
131 Size num_score_components = 0;
133 while ( it != (s.weights()).
end() ) {
136 num_score_components++;
141 two_d_e_arrays.
dimension(num_score_components,size_to_expect);
143 Size current_score_component=0;
148 current_score_component++;
150 two_d_e_arrays(j++,next_index)=(*i) *
151 ((p.energies()).total_energies())[ScoreType(current_score_component)];
162 for (
Size i =
Size(scores_to_average.
l1()); i <=
Size(scores_to_average.
u1()); i++ ) {
163 Real sum_score_component = 0;
164 for (
Size j =
Size(scores_to_average.
l2()); j <=
Size(scores_to_average.
u2()); j++ ) {
165 sum_score_component += scores_to_average(i,j);
168 sum_score_component/(scores_to_average.
u2()-scores_to_average.
l2()+1);
176 if ( sim.size()!=exp.size() ) {
177 TR <<
"size does not match" << std::endl;
181 Real xx=0, yy=0, xy=0,
x=0,
y=0;
183 for (
Size i=1; i<=sim.size(); i++ ) {
184 TR <<
"sim[i]: " << sim[i] << std::endl;
185 TR <<
"exp[i]: " << exp[i] << std::endl;
200 return xy/sqrt(xx*yy);
206 std::string
const sim_filename( ddg_out.c_str() );
207 std::string
const exp_filename(
"./2lzm_ddg_exp.txt" );
208 std::ifstream sim_data( sim_filename.c_str() );
209 std::ifstream exp_data( exp_filename.c_str() );
211 std::string sim_data_line,exp_data_line;
212 char tmp[100],
wt[100],
res[100], mut[100], ddg[100];
215 std::string pos_info;
218 if ( !exp_data.good() ) {
219 std::cout <<
"Unable to open exp data!" <<std::endl;
221 while (
getline(exp_data, exp_data_line) ) {
222 sscanf(exp_data_line.c_str(),
"%s %s %s %s %s", tmp,
wt,
res, mut, ddg);
223 pos_info = string(wt) + string(
" ") + string(res) + string(
" ") + string(mut);
224 mut_exp.push_back(pos_info);
225 ddg_exp.push_back(atof(ddg));
229 if ( !sim_data.good() ) {
230 std::cout <<
"Unable to open exp data!" <<std::endl;
233 while (
getline( sim_data,sim_data_line) ) {
234 sscanf(sim_data_line.c_str(),
"%s %s %s %s",
wt,
res, mut, ddg);
235 pos_info = string(
wt) + string(
" ") + string(res) + string(
" ") + string(mut);
236 mut_sim.push_back(pos_info);
237 ddg_sim.push_back(atof(ddg));
241 Real correlation_result;
244 std::string results_fname(
".results.log" );
245 std::ofstream staResult( results_fname.c_str() );
247 TR.
Error <<
"Can not open file " << results_fname;
249 char correlation_out[100];
250 sprintf(correlation_out,
"%0.2f",correlation_result);
251 staResult <<
"The correlation coefficient for monomer ddG protocol on the t4-lysozyme test set: " << correlation_out << std::endl;
254 std::string yaml_fname(
".results.yaml" );
255 std::ofstream yaml( yaml_fname.c_str() );
257 TR.
Error <<
"Can not open file " << yaml_fname;
260 writeYamlValue(yaml,
"CorrelationCoefficient", correlation_result);
261 writeYamlValue(yaml,
"_isTestPassed", correlation_result > 0.81 );
269 main(
int argc,
char * argv [] )
273 using namespace pose;
274 using namespace scoring;
275 using namespace conformation;
278 using namespace basic::options::OptionKeys;
295 std::string weight_file =
option[ OptionKeys::ddg::weight_file ]();
296 ScoreFunctionOP score_structure_scorefxn(ScoreFunctionFactory::create_score_function(weight_file));
298 Size num_iterations =
option[ OptionKeys::ddg::iterations ]();
301 bool debug_output =
option[ OptionKeys::ddg::debug_output ]();
302 if ( debug_output ) {
304 score_structure_scorefxn->weights() <<
"\n";
308 bool dump_pdbs =
option[ OptionKeys::ddg::dump_pdbs ]();
311 std::string ddg_out =
option[ OptionKeys::ddg::out ]();
312 std::ofstream ddg_output(ddg_out.c_str(), std::ios_base::app);
314 std::cout <<
"having trouble opening output file for dumping predicted ddgs"
315 << ddg_out << std::endl;
320 (*score_structure_scorefxn)(
pose);
327 pack::task::PackerTaskOP storage_task(pack::task::TaskFactory::create_packer_task(
pose));
329 storage_task->initialize_from_command_line();
330 pack::task::parse_resfile(
pose, *storage_task);
331 storage_task->or_include_current(
true);
335 std::ostringstream native_logfile;
336 native_logfile <<
"native.logfile";
341 std::ifstream fh(native_logfile.str().c_str(),std::ios::in);
343 bool dG_native_calculated=
false;
344 bool averaged_wt_scores_calculated=
false;
345 if ( fh.is_open() ) {
347 "native logfile exists! checking for dG of native"
351 std::string result=
"";
352 std::string averaged_score_components=
"";
354 while ( fh >> line ) {
357 if ( line.compare(
"dG_wildtype:") == 0 ) {
360 if ( !result.empty() ) {
361 if ( debug_output ) {
362 std::cout <<
"result string is not empty.\n";
364 dG_native_calculated=
true;
367 if ( line.compare(
"averaged_score_components:") == 0 ) {
368 while ( fh >> averaged_score_components ) {
370 std::istringstream convert_to_double(averaged_score_components);
372 convert_to_double >> e_component;
373 wt_averaged_score_components.push_back(e_component);
375 averaged_wt_scores_calculated=
true;
378 if ( dG_native_calculated ) {
379 if ( debug_output ) {
382 std::istringstream convert_to_double(result);
383 convert_to_double >> dG_wildtype;
384 if ( debug_output ) {
386 "final value saved to variable dG_wildtype is: "
387 << dG_wildtype << std::endl;
389 if ( dG_native_calculated && averaged_wt_scores_calculated ) {
398 if ( !dG_native_calculated ) {
400 if ( debug_output ) {
401 std::cout <<
"dG_wildtype hasn't been calculated yet! " <<
402 "starting to calculate dG for native structure\n";
406 std::ofstream record_trajectories;
407 record_trajectories.open(native_logfile.str().c_str());
414 pack::task::PackerTaskOP repack_native(pack::task::TaskFactory::create_packer_task(
pose));
415 repack_native->restrict_to_repacking();
416 for (
Size j =1; j<=
pose.total_residue(); j++ ) {
418 repack_native->nonconst_residue_task(j).or_ex1(
true);
419 repack_native->nonconst_residue_task(j).or_ex2(
true);
421 for (
Size i=1; i<=num_iterations; i++ ) {
425 std::ostringstream q;
428 Real start_score_dG_wildtype( (*score_structure_scorefxn)( temporary_pose ) );
429 if ( debug_output ) {
430 record_trajectories <<
"round: "
431 << q.str() <<
" score before repacking wildtype "
432 << start_score_dG_wildtype <<
" \n"
433 <<
"start packing pose round: " << q.str() << std::endl;
435 pack::pack_rotamers(temporary_pose,(*score_structure_scorefxn),repack_native);
436 if ( debug_output ) {
437 record_trajectories <<
"end packing pose round: " << q.str() << std::endl;
441 free_energy_wildtype[i]=(*score_structure_scorefxn)(temporary_pose);
442 Real final_score_dG_wildtype((*score_structure_scorefxn)(temporary_pose));
445 store_energies(wt_score_components, (*score_structure_scorefxn), temporary_pose,i,num_iterations);
449 if ( debug_output ) {
450 record_trajectories <<
"round: " << q.str() <<
451 " score after repacking wildtype " <<
452 final_score_dG_wildtype <<
' ' <<
453 temporary_pose.energies().total_energies().weighted_string_of( score_structure_scorefxn->weights() )
458 std::string out_path;
464 std::string dump_repacked_wt = out_path +
"repacked_wt_round_" + q.str() +
".pdb";
465 temporary_pose.dump_pdb(dump_repacked_wt);
469 dG_wildtype =
average(free_energy_wildtype);
475 Size score_component =0;
476 std::string header=
"";
478 i != score_structure_scorefxn->weights().end(); ++i ) {
481 header = header + name_from_score_type(ScoreType(score_component)) +
" ";
486 record_trajectories <<
"dG_wildtype: " << dG_wildtype << std::endl;
487 record_trajectories <<
"averaged_score_components: ";
489 record_trajectories << std::endl;
492 record_trajectories.close();
502 for (
Size i =1; i<=
pose.total_residue(); i++ ) {
504 if ( storage_task->design_residue(i) ) {
505 for ( ResidueLevelTask::ResidueTypeCOPListConstIter aa_iter(storage_task->residue_task(i).allowed_residue_types_begin()),
506 aa_end(storage_task->residue_task(i).allowed_residue_types_end());
507 aa_iter != aa_end; ++aa_iter ) {
509 if ( debug_output ) {
510 std::cout <<
" mutating residue " << i <<
" from " <<
511 core::chemical::name_from_aa(
pose.aa(i)) <<
" to amino acid: " <<
512 core::chemical::name_from_aa((*aa_iter)->aa()) << std::endl;
514 char mutant_aa = core::chemical::oneletter_code_from_aa((*aa_iter)->aa());
515 char wildtype_aa = core::chemical::oneletter_code_from_aa(
pose.aa(i));
516 std::string str_wildtype_aa;
517 str_wildtype_aa.assign(&wildtype_aa,1);
518 std::string str_mutant_aa;
519 str_mutant_aa.assign(&mutant_aa,1);
523 std::ofstream record_mutant_trajectories;
525 filename << wildtype_aa <<
"_" << i <<
"_" << mutant_aa <<
".logfile";
527 std::ifstream fh( filename.str().c_str(), std::ios::in );
528 if ( fh.is_open() ) {
529 std::cout <<
"[DEBUG] file exists. "<< filename.str() <<
" skipping this mutation" << std::endl;
532 std::cout <<
"[DEBUG] file does not exist. "<< filename.str() <<
". Creating logfile now.\n";
534 record_mutant_trajectories.open( filename.str().c_str() );
535 if ( debug_output ) {
536 record_mutant_trajectories <<
"beginning logfile" <<std::endl;
545 for (
Size k = 1; k <= num_iterations; k++ ) {
549 restrict_to_aa[(*aa_iter)->aa()]=
true;
553 pack::task::PackerTaskOP point_mutant_packer_task(pack::task::TaskFactory::create_packer_task(temporary_pose));
555 point_mutant[i]=
true;
558 Real start_score_dG_mutant( (*score_structure_scorefxn)( temporary_pose ) );
560 if ( debug_output ) {
561 record_mutant_trajectories <<
"round: " <<
562 k <<
" score before mutation " <<
563 start_score_dG_mutant <<
' ' << std::endl;
567 for (
Size j=1; j <=temporary_pose.total_residue(); j++ ) {
569 point_mutant_packer_task->nonconst_residue_task(j).restrict_to_repacking();
571 point_mutant_packer_task->nonconst_residue_task(j).or_ex1(
true);
572 point_mutant_packer_task->nonconst_residue_task(j).or_ex2(
true);
577 point_mutant_packer_task->nonconst_residue_task(i).restrict_absent_canonical_aas(restrict_to_aa);
578 point_mutant_packer_task->nonconst_residue_task(i).or_ex1(
true);
579 point_mutant_packer_task->nonconst_residue_task(i).or_ex2(
true);
581 if ( debug_output ) {
582 record_mutant_trajectories <<
583 "start packing pose round: " << k <<
587 pack::pack_rotamers(temporary_pose,(*score_structure_scorefxn),point_mutant_packer_task);
589 if ( debug_output ) {
590 record_mutant_trajectories <<
591 "end packing pose round: " << k <<
595 Real const final_score( (*score_structure_scorefxn)( temporary_pose ) );
597 store_energies(mutant_energy_components, (*score_structure_scorefxn),temporary_pose, k,num_iterations);
599 record_mutant_trajectories <<
600 "score after mutation: residue " << wildtype_aa <<
" "
601 << i <<
" " << mutant_aa <<
" "
602 << final_score <<
' ' <<
603 temporary_pose.energies().total_energies().weighted_string_of( score_structure_scorefxn->weights() ) << std::endl;
606 std::ostringstream q;
608 std::ostringstream o;
610 record_mutant_trajectories <<
"round " << q.str()
611 <<
" mutate " << wildtype_aa
612 <<
" " << o.str() <<
" " <<
613 mutant_aa <<
" " << (final_score) << std::endl;
615 free_energy_mutants[k]=final_score;
618 std::string out_path;
624 std::string output_pdb = out_path +
"mut_" +
625 str_wildtype_aa +
"_" + o.str() +
"_" +
626 str_mutant_aa +
"round_" + q.str() +
".pdb";
627 temporary_pose.dump_pdb(output_pdb);
631 std::ostringstream
resnum;
642 for (
Size k =1; k<= averaged_score_components.size(); k++ ) {
643 if ( debug_output ) {
644 std::cout <<
"mutant score component at index " << k
645 <<
" is: " << averaged_score_components[k]
646 <<
" " <<
"wt score component at index " << k
647 <<
" is: " << wt_averaged_score_components[k] << std::endl;
649 Real delta_e = averaged_score_components[k]-wt_averaged_score_components[k];
651 delta_delta_G.push_back(delta_e);
655 delta_delta_energy_components.push_back( delta_delta_G );
656 delta_delta_G_prefix.push_back(str_wildtype_aa +
" " + resnum.str() +
" " + str_mutant_aa);
657 std::ostringstream r;
659 record_mutant_trajectories <<
"mutate " << wildtype_aa <<
660 " " << r.str() <<
" " << mutant_aa <<
" wildtype_dG is: "
661 << dG_wildtype <<
" and mutant_dG is: "
662 << end_dG <<
" ddG is: " << (end_dG-dG_wildtype)
664 record_mutant_trajectories.close();
670 for (
Size c=1; c<=delta_delta_G_prefix.size(); c++ ) {
671 ddg_output << delta_delta_G_prefix[c] <<
" ";
672 ddGs ddg_score_components = delta_delta_energy_components[c];
673 ddg_output <<
F(9,3,
sum(ddg_score_components)) <<
" ";
674 ddg_output << std::endl;
677 std::string
const sim_filename( ddg_out.c_str() );
678 std::string
const exp_filename(
"2lzm_ddg_exp.txt" );
679 std::ifstream sim_data( sim_filename.c_str() );
680 std::ifstream exp_data( exp_filename.c_str() );
685 std::cout <<
"caught exception " << e.
msg() << std::endl;
int u2() const
Upper Index of Dimension 2.
void correlation(std::string ddg_out)
virtual std::string const msg() const
Size store_energies(ObjexxFCL::FArray2D< Real > &two_d_e_arrays, scoring::ScoreFunction &s, pose::Pose &p, Size next_index, Size size_to_expect)
void exit(std::string const &file, int const line, std::string const &message, int const status)
Exit with file + line + message + optional status.
utility::keys::KeyLookup< KeyType >::const_iterator const_iterator
Key collection iterators.
vector0: std::vector with assert-checked bounds
static THREAD_LOCAL basic::Tracer TR("apps.pilot.yiliu.ddg")
Real average(utility::vector1< Real > &scores_to_average)
std::istream & getline(std::istream &stream, Fstring &s)
Get Line from Stream.
void init(int argc, char *argv[])
Command line init() version.
int main(int argc, char *argv[])
utility::keys::lookup::end< KeyType > const end
int l2() const
Lower Index of Dimension 2.
FArray2D & dimension(IR const &I1_a, IR const &I2_a)
Dimension by IndexRanges.
Random number generator system.
Tracer & T(std::string const &channel, TracerPriority priority)
T is special function for assign tracer property on the static object.
common derived classes for thrown exceptions
std::string start_file()
kind of like the old -s – just one file
Real sum(ddGs &scores_to_sum)
Tracer & Error(TracerPriority priority=t_error)
Predefined Error tracer.
Real correlation_coefficient(utility::vector1< Real > sim, utility::vector1< Real > exp)
File name class supporting Windows and UN*X/Linux format names.
rule< Scanner, options_closure::context_t > options
Tracer & Warning(TracerPriority priority=t_warning)
Predefined Warning tracer.
list resnum
if line_edit[13:14]=='P': #Nucleic acid? Skip.
int u1() const
Upper Index of Dimension 1.
ocstream cout(std::cout)
Wrapper around std::cout.
BooleanOptionKey const exit("options:exit")
vector1: std::vector with 1-based indexing
Class for handling user debug/warnings/errors. Use instance of this class instead of 'std::cout' for ...
utility::keys::lookup::begin< KeyType > const begin
std::ostream & writeYamlValue(std::ostream &S, std::string name, core::Real value)
rule< Scanner, string_closure::context_t > name
Program options global and initialization function.
int l1() const
Lower Index of Dimension 1.
Size average_score_components(ObjexxFCL::FArray2D< Real > &scores_to_average, utility::vector1< Real > &averaged_scores)
Fast (x,y,z)-coordinate numeric vector.
StringOptionKey wt("revert_app:wt")
rule< Scanner, option_closure::context_t > option
FArray2D: Fortran-Compatible 2D Array.