/*************************************************************************** gsl_mod.cpp - description ------------------- begin : Thu Aug 26 22:37:34 2004 copyright : (C) 2002 by Cavalli Andrea email : cavalli@bioc.unizh.ch **************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include #include #include using namespace Almost; // class GSLVector { // gsl_vector * v_; // public: // GSLVector(int n){ // v_ = gsl_vector_alloc(n); // } // GSLVector(const GSLVector & v){ // v_ = gsl_vector_alloc(v.size()); // gsl_vector_memcpy(v_,v.v_); // } // ~GSLVector(){ // gsl_vector_free(v_); // } // gsl_vector * gsl_vector_p(){ return v_;} // size_t size() const { return v_->size;} // //members // double get(int i) const { // return gsl_vector_get(v_,i); // } // void set(int i, double d){ // gsl_vector_set(v_,i,d); // } // //init // void set_all(double d){ // gsl_vector_set_all(v_,d); // } // void set_zero(){ // gsl_vector_set_zero(v_); // } // void set_basis(int i){ // gsl_vector_set_basis(v_,i); // } // //read -write- // void print(){ // gsl_vector_fprintf(stdout,v_,"%g"); // } // void read(string file,int s){ // if(s!=size()) return; // FILE *f=fopen(file.c_str(),"r"); // gsl_vector_fscanf(f,v_); // fclose(f); // } // //views ?? // }; template<> inline string to_string(const GSLVector &v){ return ""; } // class GSLMatrix { // gsl_matrix * m_; // public: // GSLMatrix(int rows, int cols){ // m_ = gsl_matrix_alloc(rows,cols); // } // GSLMatrix(const GSLMatrix & m){ // m_ = gsl_matrix_alloc(m.rows(),m.cols()); // gsl_matrix_memcpy(m_,m.m_); // } // ~GSLMatrix(){ // gsl_matrix_free(m_); // } // gsl_matrix * gsl_matrix_p(){ return m_;} // int rows() const { return m_->size1;} // int cols() const { return m_->size2;} // double get(int i, int j) const { // return gsl_matrix_get(m_,i,j); // } // void set(int i, int j, double d){ // gsl_matrix_set(m_,i,j,d); // } // //init // void set_all(double d){ // gsl_matrix_set_all(m_,d); // } // void set_identity(){ // gsl_matrix_set_identity(m_); // } // void set_zero(){ // gsl_matrix_set_zero(m_); // } // void print(){ // gsl_matrix_fprintf(stdout,m_,"%g"); // } // void read(string file,int s1,int s2){ // if(s1!=rows()) return; // if(s2!=cols()) return; // FILE *f=fopen(file.c_str(),"r"); // gsl_matrix_fscanf(f,m_); // fclose(f); // } // void print_row(int i){ // for(int j=0;jsize1;j++){ // cout<size1;j++){ // cout< inline string to_string(const GSLMatrix &m){ return ""; } void eigenv(GSLMatrix m, GSLVector & v){ int rows = m.rows(); int cols = m.cols(); if(rows!=cols) throw "matrix is not square!"; if(v.size()!=rows) throw "not enougth space to store EW!"; //alloc workspace gsl_eigen_symm_workspace * ws = gsl_eigen_symm_alloc(rows); int status = gsl_eigen_symm(m.gsl_matrix_p(),v.gsl_vector_p(),ws); gsl_eigen_symm_free(ws); } void eigenvv(GSLMatrix m, GSLVector & v,GSLMatrix & vv){ int rows = m.rows(); int cols = m.cols(); if(rows!=cols) throw "matrix is not square!"; if(v.size()!=rows) throw "not enougth space to store EW!"; //alloc workspace gsl_eigen_symmv_workspace * ws = gsl_eigen_symmv_alloc(rows); int status = gsl_eigen_symmv(m.gsl_matrix_p(), v.gsl_vector_p(), vv.gsl_matrix_p(), ws); gsl_eigen_symmv_sort( v.gsl_vector_p(), vv.gsl_matrix_p(), GSL_EIGEN_SORT_VAL_DESC); gsl_eigen_symmv_free(ws); } GSLMatrix cont_map(const Protein &p, double cut_off){ int size = p.fragment_size(); cout<<"\t>> Protin contat map size "< tmp = p.fragment_atoms(i); vector heavy_atoms; for(int a = 0;a tmp2 = p.fragment_atoms(j); vector heavy_atoms2; for(int a = 0;a & coor, vector > &ha, double c2){ int size = pe.size(); GSLMatrix vv(size,size); GSLVector ev(size); //compute cm cm.set_zero(); for(int i=0;i::DIM*ha[i][ii]; for(int jj = 0;jj::DIM*ha[j][jj]; d2 = (coor.coor[a1]-coor.coor[a2])*(coor.coor[a1]-coor.coor[a2])+ (coor.coor[a1+1]-coor.coor[a2+1])*(coor.coor[a1+1]-coor.coor[a2+1])+ (coor.coor[a1+2]-coor.coor[a2+2])*(coor.coor[a1+2]-coor.coor[a2+2]); if(d2 & coor, vector > &ha, double c2){ int size = cm.rows(); double ene = 0; for(int i=0;i::DIM*ha[i][ii]; for(int jj = 0;jj::DIM*ha[j][jj]; d2 = (coor.coor[a1]-coor.coor[a2])*(coor.coor[a1]-coor.coor[a2])+ (coor.coor[a1+1]-coor.coor[a2+1])*(coor.coor[a1+1]-coor.coor[a2+1])+ (coor.coor[a1+2]-coor.coor[a2+2])*(coor.coor[a1+2]-coor.coor[a2+2]); if(d2c2){ ene +=abs(d2_min-c2); } } else{ if(d2_min> step "<> step "<> step "<> step "<> step "<> step "<> Cut on: " // <> Cut off: " // <> Move set definitions: " // +opts.move_set+"\n"; // out<<"\t>> Trajectory: " // +opts.trj+"\n"; // out<<"\t>> Restart: " // +opts.rst+"\n"; // out<<"\t>> Number of steps: " // <> Print frequency: " // <> Save frequency: " // <> Temperature: " // <> CM Cut off " // <> RAND Number of steps: " // <> RAND Temperature: " // <> Lambda: " // < opts; // opts.nbond_options.nblist_options.CUTON = options.cut_on; // opts.nbond_options.nblist_options.CUTOFF = options.cut_off; // SC19HSForceField ff(molecules,mdb,opts); // MoveSet move_set(molecules,options.move_set); // DummyData dd; // dd.nsteps = options.steps; // dd.nsave = options.nsave; // dd.save_fitted = 0; // int size = molecules.fragment_size(); // //step 1 // GSLMatrix cm(size,size); // //step 2 // CoorManagerOptions cm_opts; // cm_opts.trj = options.trj; // cm_opts.rst = options.rst; // Molecules start_m = molecules; // if(linearize){ // for(int i=0;i coor_manager(start_m,cm_opts); // coor_manager.write_header(dd); // //step 3 local data // vector > Ha; // Ha.resize(size); // { // for(int i=0;i frag_atoms = molecules.fragment_atoms(i); // for(int a = 0; a< frag_atoms.size();a++){ // if(molecules[frag_atoms[a]].name()[0]!='H'){ // Ha[i].push_back(frag_atoms[a]); // } // } // } // } // double c2 = options.cm_cut_off*options.cm_cut_off; // double ff_ene = ff.energy(coor_manager.atomCoor); // double const_ene = energy_cont_map(pe, // cm, // coor_manager.atomCoor, // Ha, // c2); // //do monte carlo // srand48(clock()); // double lambda = options.lambda; // double ene = lambda*ff_ene + const_ene; // cout<<"\t>> Initial energy: "<> Doing randomization\n"; // int accept = 0; // int reject = 0; // double current_ff_ene; // double current_const_ene; // double current_ene; // for(int i=0;i> step "<> step "<> Starting ......."<> step "<> step "<> step "<> Temperature set to: "< opts; // opts.nbond_options.nblist_options.CUTON = options.cut_on; // opts.nbond_options.nblist_options.CUTOFF = options.cut_off; // SC19HSForceField ff(molecules,mdb,opts); // MoveSet move_set(molecules,options.move_set); // DummyData dd; // dd.nsteps = options.steps; // dd.nsave = options.nsave; // dd.save_fitted = 0; // int size = molecules.fragment_size(); // //step 1 // GSLMatrix cm(size,size); // //step 2 // CoorManagerOptions cm_opts; // cm_opts.trj = options.trj; // cm_opts.rst = options.rst; // CoorManager coor_manager(molecules,cm_opts,true); // coor_manager.write_header(dd); // coor_manager.read_rst(rst_file,dd); // //step 3 local data // vector > Ha; // Ha.resize(size); // { // for(int i=0;i frag_atoms = molecules.fragment_atoms(i); // for(int a = 0; a< frag_atoms.size();a++){ // if(molecules[frag_atoms[a]].name()[0]!='H'){ // Ha[i].push_back(frag_atoms[a]); // } // } // } // } // double c2 = options.cm_cut_off*options.cm_cut_off; // double ff_ene = ff.energy(coor_manager.atomCoor); // double const_ene = energy_cont_map(pe, // cm, // coor_manager.atomCoor, // Ha, // c2); // //do monte carlo // srand48(clock()); // double lambda = options.lambda; // double ene = lambda*ff_ene + const_ene; // cout<<"\t>> Initial energy: "<> Doing randomization\n"; // int accept = 0; // int reject = 0; // double current_ff_ene; // double current_const_ene; // double current_ene; // for(int i=0;i> step "<> step "<> Starting ......."<> step "<> step "<> step "< & options, // bool linearize, // int rexfq){ // SC19HSFFOptions opts; // opts.nbond_options.nblist_options.CUTON = options[0].cut_on; // opts.nbond_options.nblist_options.CUTOFF = options[0].cut_off; // SC19HSForceField ff(molecules,mdb,opts); // MoveSet move_set(molecules,options[0].move_set); // DummyData dd; // dd.nsteps = options[0].steps; // dd.nsave = options[0].nsave; // dd.save_fitted = 0; // int size = molecules.fragment_size(); // //step 1 // GSLMatrix cm(size,size); // //step 2 // Molecules start_m = molecules; // if(linearize){ // for(int i=0;i* > coor_manager; // coor_manager.resize(options.size()); // for(int i=0;i cm_opts; // cm_opts.trj = options[i].trj; // cm_opts.rst = options[i].rst; // coor_manager[i] = new CoorManager(start_m,cm_opts); // coor_manager[i]->write_header(dd); // } // //step 3 local data // vector > Ha; // Ha.resize(size); // { // for(int i=0;i frag_atoms = molecules.fragment_atoms(i); // for(int a = 0; a< frag_atoms.size();a++){ // if(molecules[frag_atoms[a]].name()[0]!='H'){ // Ha[i].push_back(frag_atoms[a]); // } // } // } // } // double c2 = options[0].cm_cut_off*options[0].cm_cut_off; // vector ff_ene; ff_ene.resize(coor_manager.size()); // vector const_ene; const_ene.resize(coor_manager.size()); // for(int i=0;iatomCoor); // const_ene [i] = energy_cont_map(pe, // cm, // coor_manager[i]->atomCoor, // Ha, // c2); // } // //do monte carlo // srand48(clock()); // double lambda = options[0].lambda; // vector ene; ene.resize(coor_manager.size()); // //rex_size // int rex_size = coor_manager.size(); // for(int i = 0;i> Initial energy: "<> Doing randomization\n"; // vector accept; accept.resize(rex_size); // vector reject; reject.resize(rex_size); // vector current_ff_ene; current_ff_ene.resize(rex_size); // vector current_const_ene; current_const_ene.resize(rex_size); // vector current_ene; current_ene.resize(rex_size); // for(int i=0;iatomCoor); // current_ff_ene[rex] = ff.energy(coor_manager[rex]->atomCoor); // current_const_ene[rex] = energy_cont_map(pe, // cm, // coor_manager[rex]->atomCoor, // Ha, // c2); // current_ene[rex] = lambda*current_ff_ene[rex] + current_const_ene[rex]; // double r = drand48(); // double de = (current_ene[rex]-ene[rex])/options[rex].rand_temp; // //accept // if(raccept(); // ene[rex] = current_ene[rex]; // ff_ene[rex] = current_ff_ene[rex]; // const_ene[rex] = current_const_ene[rex]; // ++accept[rex]; // } // else{ // coor_manager[rex]->reject(); // ++reject[rex]; // } // if((i+1)%1000 == 0){ // cout<<"\t>> step "<> step "< accept; accept.resize(rex_size); // vector reject; reject.resize(rex_size); // vector current_ff_ene; current_ff_ene.resize(rex_size); // vector current_const_ene; current_const_ene.resize(rex_size); // vector current_ene; current_ene.resize(rex_size); // vector tmp; tmp.resize(rex_size); // for(int i=0;i> Starting ......."<atomCoor); // current_ff_ene[rex] = ff.energy(coor_manager[rex]->atomCoor); // current_const_ene[rex] = energy_cont_map(pe, // cm, // coor_manager[rex]->atomCoor, // Ha, // c2); // current_ene[rex] = lambda*current_ff_ene[rex] + current_const_ene[rex]; // double r = drand48(); // double de = (current_ene[rex]-ene[rex])/tmp[rex]; // //accept // if(raccept(); // ene[rex] = current_ene[rex]; // ff_ene[rex] = current_ff_ene[rex]; // const_ene[rex] = current_const_ene[rex]; // ++accept[rex]; // } // else{ // coor_manager[rex]->reject(); // ++reject[rex]; // } // if((i+1)%options[rex].nprint == 0){ // cout<<"\t>> step "<> step "<> step "<> step "<save_trj(dd); // coor_manager[rex]->write_rst(dd); // accept[rex] = reject[rex] = 0; // } // } // //exchange // if((i+1)%rexfq == 0){ // int q = (i+1)/rexfq; // if((q%2)==0){ // cout<<"\t>> Trying even ...\n"; // for(int k = 0;k+2<=rex_size;k=k+2){ // double p = (ene[k+1]-ene[k])*(1/tmp[k]-1/tmp[k+1]); // double r = drand48(); // if(r> Exchange: "<> Trying odd ...\n"; // for(int k = 1;k+2<=rex_size;k=k+2){ // double p = (ene[k+1]-ene[k])*(1/tmp[k]-1/tmp[k+1]); // double r = drand48(); // if(r> Exchange: "< opts; // opts.nbond_options.nblist_options.CUTON = options.cut_on; // opts.nbond_options.nblist_options.CUTOFF = options.cut_off; // SC19HSForceField ff(molecules,mdb,opts); // MoveSet move_set(molecules,options.move_set); // DummyData dd; // dd.nsteps = options.steps; // dd.nsave = options.nsave; // dd.save_fitted = 0; // int size = molecules.fragment_size(); // //step 2 // CoorManagerOptions cm_opts; // cm_opts.trj = options.trj; // cm_opts.rst = options.rst; // CoorManager coor_manager(molecules,cm_opts); // coor_manager.write_header(dd); // //step 3 local data // vector > Ha; // Ha.resize(size); // { // for(int i=0;i frag_atoms = molecules.fragment_atoms(i); // for(int a = 0; a< frag_atoms.size();a++){ // if(molecules[frag_atoms[a]].name()[0]!='H'){ // Ha[i].push_back(frag_atoms[a]); // } // } // } // } // double c2 = options.cm_cut_off*options.cm_cut_off; // double ff_ene = ff.energy(coor_manager.atomCoor); // double const_ene = energy_cont_map_gauss(cm, // coor_manager.atomCoor, // Ha, // c2); // //do monte carlo // srand48(clock()); // double lambda = options.lambda; // double ene = lambda*ff_ene + const_ene; // cout<<"\t>> Initial energy: "<> Doing randomization\n"; // int accept = 0; // int reject = 0; // double current_ff_ene; // double current_const_ene; // double current_ene; // ff_ene = ff.energy(coor_manager.atomCoor); // ene = lambda*ff_ene; // for(int i=0;i> step "<> step "<> Starting ......."<> step "<> step "<> step "<::DIM*i; // res[i].set_x(coor_manager.atomCoor.coor[pos]); // res[i].set_y(coor_manager.atomCoor.coor[pos+1]); // res[i].set_z(coor_manager.atomCoor.coor[pos+2]); // } // return res; // } //analysis // class gsl_energy { // GSLMatrix cm; // GSLVector pe; // GSLMatrix l_cm; // double c2; // double cutoff; // SC19HSForceField *ff; // vector > Ha; // public: // gsl_energy(const Molecules & molecules,const MDB & mdb, // const GSLMatrix & c_map, // double cmap_cutoff, double ff_cutoff): // cm(c_map.rows(),c_map.cols()),pe(c_map.rows()), // l_cm(c_map.rows(),c_map.cols()) // { // c2 = cmap_cutoff*cmap_cutoff; // cutoff = ff_cutoff; // { // int count = 0; // for(int i=0;i0) ++count; // } // } // cout<<"\t>> Contact map has "< frag_atoms = molecules.fragment_atoms(i); // for(int a = 0; a< frag_atoms.size();a++){ // if(molecules[frag_atoms[a]].name()[0]!='H'){ // Ha[i].push_back(frag_atoms[a]); // } // } // } // } // } // //ff // SC19HSFFOptions opts; // opts.nbond_options.nblist_options.CUTON = cutoff; // opts.nbond_options.nblist_options.CUTOFF = cutoff; // ff = new SC19HSForceField(molecules,mdb,opts); // } // ~gsl_energy(){ delete ff;} // double ff_energy(const Coor & coor){ // Coor l_coor; // l_coor.resize(coor.size()); // for(int i=0;i::DIM*i; // l_coor.coor[pos] = coor.coor[pos]; // l_coor.coor[pos+1] = coor.coor[pos+1]; // l_coor.coor[pos+2] = coor.coor[pos+2]; // } // return ff->energy(l_coor); // } // double const_energy(const Coor & coor){ // Coor l_coor; // l_coor.resize(coor.size()); // for(int i=0;i::DIM*i; // l_coor.coor[pos] = coor.coor[pos]; // l_coor.coor[pos+1] = coor.coor[pos+1]; // l_coor.coor[pos+2] = coor.coor[pos+2]; // } // return energy_cont_map(pe, // l_cm, // l_coor, // Ha, // c2); // } // double matches(const Coor & coor){ // Coor l_coor; // int size = cm.rows(); // l_coor.resize(coor.size()); // for(int i=0;i::DIM*i; // l_coor.coor[pos] = coor.coor[pos]; // l_coor.coor[pos+1] = coor.coor[pos+1]; // l_coor.coor[pos+2] = coor.coor[pos+2]; // } // l_cm.set_zero(); // for(int i=0;i::DIM*Ha[i][ii]; // for(int jj = 0;jj::DIM*Ha[j][jj]; // d2 = // (l_coor.coor[a1]-l_coor.coor[a2])*(l_coor.coor[a1]-l_coor.coor[a2])+ // (l_coor.coor[a1+1]-l_coor.coor[a2+1])*(l_coor.coor[a1+1]-l_coor.coor[a2+1])+ // (l_coor.coor[a1+2]-l_coor.coor[a2+2])*(l_coor.coor[a1+2]-l_coor.coor[a2+2]); // if(d2 & coor){ // Coor l_coor; // l_coor.resize(coor.size()); // for(int i=0;i::DIM*i; // l_coor.coor[pos] = coor.coor[pos]; // l_coor.coor[pos+1] = coor.coor[pos+1]; // l_coor.coor[pos+2] = coor.coor[pos+2]; // } // int size = cm.rows(); // l_cm.set_zero(); // // int count=0; // for(int i=0;i::DIM*Ha[i][ii]; // for(int jj = 0;jj::DIM*Ha[j][jj]; // d2 = // (l_coor.coor[a1]-l_coor.coor[a2])*(l_coor.coor[a1]-l_coor.coor[a2])+ // (l_coor.coor[a1+1]-l_coor.coor[a2+1])*(l_coor.coor[a1+1]-l_coor.coor[a2+1])+ // (l_coor.coor[a1+2]-l_coor.coor[a2+2])*(l_coor.coor[a1+2]-l_coor.coor[a2+2]); // if(d2> Current number of contacts "<> Current number of match "<0)&&(cm.get(i,j)==0))<0)&&(cm.get(i,j)==0)) ++m; // } // } // return m; // } // int false_neg(const Coor & coor){ // Coor l_coor; // l_coor.resize(coor.size()); // for(int i=0;i::DIM*i; // l_coor.coor[pos] = coor.coor[pos]; // l_coor.coor[pos+1] = coor.coor[pos+1]; // l_coor.coor[pos+2] = coor.coor[pos+2]; // } // l_cm.set_zero(); // int size = cm.rows(); // for(int i=0;i::DIM*Ha[i][ii]; // for(int jj = 0;jj::DIM*Ha[j][jj]; // d2 = // (l_coor.coor[a1]-l_coor.coor[a2])*(l_coor.coor[a1]-l_coor.coor[a2])+ // (l_coor.coor[a1+1]-l_coor.coor[a2+1])*(l_coor.coor[a1+1]-l_coor.coor[a2+1])+ // (l_coor.coor[a1+2]-l_coor.coor[a2+2])*(l_coor.coor[a1+2]-l_coor.coor[a2+2]); // if(d20)) ++m; // } // } // return m; // } // }; template<> inline string to_string(const Molecules & molecules){ return ""; } // inline string to_string(const gsl_energy &v){ // return ""; // } extern "C" { void init_gsl(){ //declarations here Module mod = Module("gsl"); Class >(mod.self(), "vector") .def_method("size",&GSLVector::size) .def_method("get",&GSLVector::get) .def_method("set",&GSLVector::set) .def_method("set_all",&GSLVector::set_all) .def_method("set_zero",&GSLVector::set_zero) .def_method("set_basis",&GSLVector::set_basis) .def_method("show",&GSLVector::print) .def_method("read",&GSLVector::read); Class >(mod.self(), "matrix") .def_method("rows",&GSLMatrix::rows) .def_method("cols",&GSLMatrix::cols) .def_method("get",&GSLMatrix::get) .def_method("set",&GSLMatrix::set) .def_method("set_all",&GSLMatrix::set_all) .def_method("set_zero",&GSLMatrix::set_zero) .def_method("set_identity",&GSLMatrix::set_identity) .def_method("show",&GSLMatrix::print) .def_method("show_row",&GSLMatrix::print_row) .def_method("show_col",&GSLMatrix::print_col) .def_method("read",&GSLMatrix::read);; // Class(mod.self(),"pe_options") // .def_attribute("cut_on",&PeOptions::cut_on) // .def_attribute("cut_off",&PeOptions::cut_off) // .def_attribute("steps",&PeOptions::steps) // .def_attribute("nprint",&PeOptions::nprint) // .def_attribute("nsave",&PeOptions::nsave) // .def_attribute("temp",&PeOptions::temp) // .def_attribute("seed",&PeOptions::seed) // .def_attribute("move_set",&PeOptions::move_set) // .def_attribute("trj",&PeOptions::trj) // .def_attribute("rst",&PeOptions::rst) // .def_attribute("cm_cut_off",&PeOptions::cm_cut_off) // .def_attribute("rand_temp",&PeOptions::rand_temp) // .def_attribute("rand_steps",&PeOptions::rand_steps) // .def_attribute("lambda",&PeOptions::lambda) // ; // Class >(mod.self(),"rex_pe_options") // .def_method("push_back",&vector::push_back) // .def_method("size",&vector::size); mod.def_function("eigenvv", "Computes eigenvalues of symmetric matrix", eigenvv); mod.def_function("cont_map", "Computes the contact map of a pretein ad stores the result ina gsl::matrix.", cont_map); // mod.def_function("mc_find", // "Well, i tries ...", // mc_find); // mod.def_function("mc_find_ev", // "Well, i tries ...", // mc_find_ev); // mod.def_function("mc_find_cont", // "Well, i tries ...", // mc_find_cont); // mod.def_function("mc_find_ff", // "Well, i tries ...", // mc_find_ff); // mod.def_function("mc_find_ff_cont", // "Well, i tries ...", // mc_find_ff_cont); // mod.def_function("mc_find_struct", // "Well, i tries ...", // mc_find_struct); // mod.def_function("rex_find_ff", // "Well, i tries ...", // rex_find_ff); //analysis // Class >(mod.self(),"energy") // .def_method("ff_energy",&gsl_energy::ff_energy) // .def_method("const_energy",&gsl_energy::const_energy) // .def_method("matches",&gsl_energy::matches) // .def_method("false_pos",&gsl_energy::false_pos) // .def_method("false_neg",&gsl_energy::false_neg); } }