/*************************************************************************** sandy_mod.cpp - description ------------------- begin : Sun Jan 25 22:22:42 2009 copyright : (C) 1999-2008 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 #define ALMTOSTRING(s) \ template<> \ inline string to_string(const s & ){ \ return "<" #s ">"; \ } namespace Almost { namespace NMR { typedef pair int_pair; struct Peak2DFlat { double w1; double w2; double Vol; vector f; vector s; vector W; vector Dinv6; void move(){ double ww = 0; for(int i=0;i n1; vector W1; vector n2; vector W2; vector > _12; vector > _13; vector > _14; int gmin; int gmax; vector > Upper; vector > Lower; vector > W; vector > D; vector > S; vector > N; vector > Ntmp; void init_w(){ W.clear(); W.resize(n1.size()); Upper.clear(); Upper.resize(n1.size()); for(int i=0;i & atm){ for(int i=0;i q(a1,a2); return find(_14.begin(),_14.end(),q)!=_14.end(); } bool is_13(int a1, int a2){ pair q(a1,a2); return find(_13.begin(),_13.end(),q)!=_13.end(); } bool is_12(int a1, int a2){ pair q(a1,a2); return find(_12.begin(),_12.end(),q)!=_12.end(); } bool has_12(){ return _12.size()>0;} bool has_13(){ return _13.size()>0;} bool has_14(){ return _14.size()>0;} int n_12(){ return _12.size();} int n_13(){ return _13.size();} int n_14(){ return _14.size();} bool is_assignment(int a1, int a2, bool both = true){ if(find(n1.begin(),n1.end(),a1)!=n1.end() && find(n2.begin(),n2.end(),a2)!=n2.end()) return true; if(both) if(find(n1.begin(),n1.end(),a2)!=n1.end() && find(n2.begin(),n2.end(),a1)!=n2.end()) return true; return false; } int find_first(int a){ vector::iterator iter = find(n1.begin(),n1.end(),a); if(iter==n1.end()) throw "Atom not found"; return iter-n1.begin(); } int find_second(int a){ vector::iterator iter = find(n2.begin(),n2.end(),a); if(iter==n2.end()) throw "Atom not found"; return iter-n2.begin(); } bool is_symmetric(int a1,int a2){ vector::iterator p1 = find(n1.begin(),n1.end(),a1); if(p1==n1.end()) throw "NA: atom 1 not found"; vector::iterator p2 = find(n2.begin(),n2.end(),a2); if(p2==n2.end()) throw "NA: atom 2 not found"; return S[p1-n1.begin()][p2-n2.begin()]; } void init_D(){ for(int i=0;i8) D[i][j] = 0; } } } double NA(int a1, int a2){ vector::iterator p1 = find(n1.begin(),n1.end(),a1); if(p1==n1.end()) throw "NA: atom 1 not found"; vector::iterator p2 = find(n2.begin(),n2.end(),a2); if(p2==n2.end()) throw "NA: atom 2 not found"; return N[p1-n1.begin()][p2-n2.begin()]; } double upper(int a1, int a2){ vector::iterator p1 = find(n1.begin(),n1.end(),a1); if(p1==n1.end()) throw "NA: atom 1 not found"; vector::iterator p2 = find(n2.begin(),n2.end(),a2); if(p2==n2.end()) throw "NA: atom 2 not found"; double d = Upper[p1-n1.begin()][p2-n2.begin()]; if(d<1) return 10000; return d; } double lower(int a1, int a2){ vector::iterator p1 = find(n1.begin(),n1.end(),a1); if(p1==n1.end()) throw "NA: atom 1 not found"; vector::iterator p2 = find(n2.begin(),n2.end(),a2); if(p2==n2.end()) throw "NA: atom 2 not found"; double d = Lower[p1-n1.begin()][p2-n2.begin()]; if(d<0.1) return -1; return d; } void normalize_na(){ for(int i=0;iUlm) // Ntmp[i][j] =0.0001; } } double q= 0; for(int i=0;i peak; vector seq; map cs; vector > mat_12; vector > mat_13; vector > mat_14; Protein p; map > > topos_rest; //Dist rest map, double> Upper; map, double> Lower; public: Sandy():p("BLT"){} void read_peak(const char * file, double U){ ifstream in(file); if(!in){ string error = string("\t>> FATAL ERROR: unable to open file: ")+file +string("\n"); cout<>id; ss>> p.w1; ss>> p.w2; ss>>id; ss>> c; ss>> p.Vol; ss>> p.Vol2; peak.push_back(p); } } void read_seq(const char * file){ ifstream in(file); if(!in){ string error = string("\t>> FATAL ERROR: unable to open file: ")+file +string("\n"); cout<>s; if(s.size()<3) continue; ss>>n; n="_"+n; if(s.size()==3) seq.push_back(s+n); else if(s.size()>3) seq.push_back(string(s.begin(),s.begin()+3)+n); else { cout<<"\t>> Error in reading sequence file: "<> Error in reading sequence"; } } } void read_shifts(const char * file){ ifstream in(file); if(!in){ string error = string("\t>> FATAL ERROR: unable to open file: ")+file +string("\n"); cout< lseq = p.sequence(Protein::LONG); while(in.good()){ char line[256]; in.getline(line,256); if(line[0]=='#'||in.gcount()<10) continue; stringstream ss(line); int id; double w; double err; string atm; int nres; ss>>id; ss>>w; ss>>err; ss>>atm; ss>>nres; --nres; cout<<"\t>> Chemical shift for atom "<> Atom not in protein\n"; } if(natm!=-1){ cs[natm] = w; cout<<"\t>> CS assignment "<::const_iterator iter = cs.begin(), end = cs.end(); while(iter!=end){ for(int i=0;isecond)first); peak[i].W1.push_back(iter->second); } if(abs(peak[i].w2-iter->second)first); peak[i].W2.push_back(iter->second); } } ++iter; } for(int i=0;ipeak[i].gmax) peak[i].gmax = q; if(q::iterator iter= peak.begin(), end = peak.end(); while(iter!=peak.end()){ if(iter->n1.size()==0||iter->n2.size()==0){ cout<<"Deleting peak "<Vol<(a1,a2)); } } } } for(int i=0;i(a1,a2)); } } } } for(int i=0;i(a1,a2)); } } } } // { // vector::iterator iter = peak.begin(); // while(iter!=peak.end()){ // if(iter->n1.size()*iter->n2.size()>16) // iter = peak.erase(iter); // else // ++iter; // } // } } void read_topos(const char * file){ ifstream in; in.open(file); if(!in){ string error = string("\t>> FATAL ERROR: unable to open file: ")+file +string("\n"); cout< iter(in),end; while(iter!=end){ int n = atoi(iter->c_str())-1; ++iter; ++iter; double phi = atof(iter->c_str()); ++iter; double psi = atof(iter->c_str()); ++iter; pair p(phi,psi); topos_rest[n].push_back(p); } } bool have_topos(int m, int M, map > > &topos){ for(int i=m;i<=M;i++) if(topos.find(i)== topos.end()) return false; return true; } void check_dist(BondRotation & bond_rotation, int m, int M, int a1, int a2, int f1, int f2, double & D_m, double &D_M){ vector > phi = p.phi_atoms(); vector > psi = p.psi_atoms(); Coor c; c.resize(p.atom_size()); for(int i=0;i::DIM*i] = p[i][0]; c.coor[Coor::DIM*i+1] = p[i][1]; c.coor[Coor::DIM*i+2] = p[i][2]; } double d_min = 100000; double d_max = -1; for(int q=0;q<(M-m+1)*1000;q++){ for(int i=m;i<=M;i++){ double phi_a = topos_rest[i][lrand48()%topos_rest[i].size()].first*M_PI/180.0 +(drand48()-0.5)*M_PI/180.0*25.0; double psi_a = topos_rest[i][lrand48()%topos_rest[i].size()].second*M_PI/180.0+(drand48()-0.5)*M_PI/180.0*25.0; if(phi[i].size()<4||psi[i].size()<4) continue; bond_rotation.set_dihedral(c, phi[i][0], phi[i][1], phi[i][2], phi[i][3], phi_a); bond_rotation.set_dihedral(c, psi[i][0], psi[i][1], psi[i][2], psi[i][3], psi_a); } double d = 0; for(int i=0;i<3;i++){ double dd = c.coor[Coor::DIM*a1+i] - c.coor[Coor::DIM*a2+i]; d +=dd*dd; } d = sqrt(d); if(dd_max) d_max = d; } // cout<<"Distance "< bond_rotation(p); int count = 0; for(int i=0;i> Challenging peak "<> Atom "<12){ continue;}; if(!have_topos(m,M,topos_rest)){ continue;} double D_Min = 0, D_Max = 0; // if(n1!="CA"&&n1!="CB"&&n1!="C") continue; // if(n2!="CA"&&n2!="CB"&&n2!="C") continue; int_pair ab(peak[i].n1[a],peak[i].n2[b]); int_pair ba(peak[i].n2[b],peak[i].n1[a]); if(Upper.find(ab)==Upper.end()||Upper[ab]<0.1){ check_dist(bond_rotation,m,M,peak[i].n1[a],peak[i].n2[b],f1,f2,D_Min,D_Max); peak[i].Lower[a][b] = D_Min; peak[i].Upper[a][b] = D_Max; cout<<"Real "<2||peak[i].n2.size()>2) continue; // if(peak[i].gmax>10||peak[i].gmin<2) continue; out<<">> Peak "<, double>::iterator iter = Upper.begin(),end = Upper.end(); ofstream out; out.open("upper.dist"); while(iter!=end){ if(iter->second!=0) out<first.first<<" "<first.second<<" "<second<<"\n"; ++iter; } } { map, double>::iterator iter = Lower.begin(),end = Lower.end(); ofstream out; out.open("lower.dist"); while(iter!=end){ if(iter->second!=0) out<first.first<<" "<first.second<<" "<second<<"\n"; ++iter; } } } void dump2(vector & P_W, double K){ ofstream out; out.open("dump.peaks"); for(int i=0;i2||peak[i].n2.size()>2) continue; // if(peak[i].gmax>10||peak[i].gmin<2) continue; out<<">> Peak "<1) Ip += pow(peak[i].D[a1][a2],-6.0)*K; } } out<<"\t "< iter(in),end; while(iter != end){ int_pair p; double d; p.first = atoi(iter->c_str()); ++iter; p.second = atoi(iter->c_str()); ++iter; d = atof(iter->c_str()); ++iter; int_pair q; q.first = p.second; q.second = p.first; // if(p.first==279&&p.second==255){ // cout<<"DEDDD "<0){ Upper[p] = d; Upper[q] = d; } } } { ifstream in; in.open("lower.dist"); istream_iterator iter(in),end; while(iter != end){ int_pair p; double d; p.first = atoi(iter->c_str()); ++iter; p.second = atoi(iter->c_str()); ++iter; d = atof(iter->c_str()); ++iter; int_pair q; q.first = p.second; q.second = p.first; if(d>0){ Lower[p] = d; Lower[q] = d; } } } //Ass dist to peaks; for(int i=0;i > ab; for(int i=0;i> Peak "<>Peak "< > ab; for(int i=0;i> Triangle "<::iterator iter = ab[bp].begin(),end = ab[bp].end(); Vab += peak[*iter].NA(bp.first,bp.second); ++iter; } if(ab.find(bb)!=ab.end()){ set::iterator iter = ab[bb].begin(),end = ab[bb].end(); Vab += peak[*iter].NA(bb.first,bb.second); ++iter; } Vab = max(1.0,Vab); if(ab.find(pp)!=ab.end()){ set::iterator iter = ab[pp].begin(),end = ab[pp].end(); while(iter!=end){ peak[i].Ntmp[a][b] += (Vab)*(peak[*iter].NA(peak[i].n2[b],mat_12[a1][q])); ++iter; } } if(ab.find(qp)!=ab.end()){ set::iterator iter = ab[qp].begin(),end = ab[qp].end(); while(iter!=end){ peak[i].Ntmp[a][b] += (Vab)*(peak[*iter].NA(mat_12[a1][q],peak[i].n2[b])); ++iter; } } } } } int a2 = peak[i].n2[b]; for(int q=0;q> Triangle "<::iterator iter = ab[bp].begin(),end = ab[bp].end(); Vab += peak[*iter].NA(bp.first,bp.second); ++iter; } if(ab.find(bb)!=ab.end()){ set::iterator iter = ab[bb].begin(),end = ab[bb].end(); Vab += peak[*iter].NA(bb.first,bb.second); ++iter; } Vab = max(1.0,Vab); if(ab.find(pp)!=ab.end()){ set::iterator iter = ab[pp].begin(),end = ab[pp].end(); while(iter!=end){ peak[i].Ntmp[a][b] += (Vab)*(peak[*iter].NA(pp.first,pp.second)); ++iter; } } if(ab.find(qp)!=ab.end()){ set::iterator iter = ab[qp].begin(),end = ab[qp].end(); while(iter!=end){ peak[i].Ntmp[a][b] += (Vab)*(peak[*iter].NA(qp.first,qp.second)); ++iter; } } } } } } } } } void na_angle(){ map > ab; for(int i=0;i> TriAngle "<::iterator iter = ab[bp].begin(),end = ab[bp].end(); Vab += peak[*iter].NA(bp.first,bp.second); ++iter; } if(ab.find(bb)!=ab.end()){ set::iterator iter = ab[bb].begin(),end = ab[bb].end(); Vab += peak[*iter].NA(bb.first,bb.second); ++iter; } Vab = max(1.0,Vab); if(ab.find(pp)!=ab.end()){ set::iterator iter = ab[pp].begin(),end = ab[pp].end(); while(iter!=end){ peak[i].Ntmp[a][b] += (Vab)*(peak[*iter].NA(pp.first,pp.second)); ++iter; } } if(ab.find(qp)!=ab.end()){ set::iterator iter = ab[qp].begin(),end = ab[qp].end(); while(iter!=end){ peak[i].Ntmp[a][b] += (Vab)*(peak[*iter].NA(qp.first,qp.second)); ++iter; } } } } } int a2 = peak[i].n2[b]; for(int q=0;q> TriAngle "<::iterator iter = ab[bp].begin(),end = ab[bp].end(); Vab += peak[*iter].NA(bp.first,bp.second); ++iter; } if(ab.find(bb)!=ab.end()){ set::iterator iter = ab[bb].begin(),end = ab[bb].end(); Vab += peak[*iter].NA(bb.first,bb.second); ++iter; } Vab = max(1.0,Vab); if(ab.find(pp)!=ab.end()){ set::iterator iter = ab[pp].begin(),end = ab[pp].end(); while(iter!=end){ peak[i].Ntmp[a][b] += (Vab)*(peak[*iter].NA(pp.first,pp.second)); ++iter; } } if(ab.find(qp)!=ab.end()){ set::iterator iter = ab[qp].begin(),end = ab[qp].end(); while(iter!=end){ peak[i].Ntmp[a][b] += (Vab)*(peak[*iter].NA(qp.first,qp.second)); ++iter; } } } } } } } } } void na_dihe(){ map > ab; for(int i=0;i> TriAngle "<::iterator iter = ab[bp].begin(),end = ab[bp].end(); Vab += peak[*iter].NA(bp.first,bp.second); ++iter; } if(ab.find(bb)!=ab.end()){ set::iterator iter = ab[bb].begin(),end = ab[bb].end(); Vab += peak[*iter].NA(bb.first,bb.second); ++iter; } Vab = max(1.0,Vab); if(ab.find(pp)!=ab.end()){ set::iterator iter = ab[pp].begin(),end = ab[pp].end(); while(iter!=end){ peak[i].Ntmp[a][b] += (Vab)*(peak[*iter].NA(pp.first,pp.second)); ++iter; } } if(ab.find(qp)!=ab.end()){ set::iterator iter = ab[qp].begin(),end = ab[qp].end(); while(iter!=end){ peak[i].Ntmp[a][b] += (Vab)*(peak[*iter].NA(qp.first,qp.second)); ++iter; } } } } } int a2 = peak[i].n2[b]; for(int q=0;q> TriAngle "<::iterator iter = ab[bp].begin(),end = ab[bp].end(); Vab += peak[*iter].NA(bp.first,bp.second); ++iter; } if(ab.find(bb)!=ab.end()){ set::iterator iter = ab[bb].begin(),end = ab[bb].end(); Vab += peak[*iter].NA(bb.first,bb.second); ++iter; } Vab = max(1.0,Vab); if(ab.find(pp)!=ab.end()){ set::iterator iter = ab[pp].begin(),end = ab[pp].end(); while(iter!=end){ peak[i].Ntmp[a][b] += (Vab)*(peak[*iter].NA(pp.first,pp.second)); ++iter; } } if(ab.find(qp)!=ab.end()){ set::iterator iter = ab[qp].begin(),end = ab[qp].end(); while(iter!=end){ peak[i].Ntmp[a][b] += (Vab)*(peak[*iter].NA(qp.first,qp.second)); ++iter; } } } } } } } } } double Vval(int a1, int a2, map > &ab){ double Res = 0; { int_pair pp(a1,a2); set::iterator iter = ab[pp].begin(), end = ab[pp].end(); while(iter!=end){ Res += peak[*iter].NA(pp.first,pp.second); ++iter; } } { int_pair pp(a2,a1); set::iterator iter = ab[pp].begin(), end = ab[pp].end(); while(iter!=end){ Res += peak[*iter].NA(pp.first,pp.second); ++iter; } } if(is_12(a1,a2)||is_13(a1,a2)||is_14(a1,a2)) Res = max(1.0,Res); return Res; } vector g_atoms(int a1, int a2, int fa1, int fa2, map > &ab){ vector g; for(int aa = max(0,fa1-1);aa atm = p.fragment_atoms(aa); for(int q=0;q atm = p.fragment_atoms(aa); for(int q=0;q > ab; for(int i=0;i >::iterator iter = ab.begin(), end = ab.end(); while(iter!=end){ // cout<<"Doing...."<first.first; int fa = p.find_fragment(a); int b = iter->first.second; int fb = p.find_fragment(b); //search g connected to a and b //g has to be in [fa-1, fa +1] or [fb-1,fb+1] vector g = g_atoms(a,b,fa,fb,ab); vector g2 = g_atoms(b,a,fb,fa,ab); // for(int gg=0;gg::iterator p_iter = iter->second.begin(), p_end = iter->second.end(); while(p_iter!=p_end){ int p_a = peak[*p_iter].find_first(a); int p_b = peak[*p_iter].find_second(b); peak[*p_iter].Ntmp[p_a][p_b] = Nab; ++p_iter; } } ++iter; // done } } void na_residue(){ ofstream out; out.open("na.res"); map > ab; for(int i=0;i CA; for(int aa = 0; aa atm1 = p.fragment_atoms(aa); for(int bb = 0; bb atm2 = p.fragment_atoms(bb); for(int a1 = 0; a1< atm1.size();a1++){ if(p[atm1[a1]].name()[0]!='C') continue; for(int a2 = 0;a2::iterator p_iter = ab[pp].begin(), p_end = ab[pp].end(); while(p_iter!=p_end){ int p_a = peak[*p_iter].find_first(atm1[a1]); int p_b = peak[*p_iter].find_second(atm2[a2]); NA += peak[*p_iter].Ntmp[p_a][p_b]; ++p_iter; ++counter; } // D += p.distance(atm1[a1],atm2[a2]); } } out< > &ab){ int_pair pp(a1,a2); int_pair qp(a2,a1); if(ab.find(pp)!=ab.end()&&ab.find(qp)!=ab.end()) return true; return false; } double bond_tol(){ double T = 0; for(int i = 0; i< mat_12.size(); i++){ int a1 = i; if(cs.find(a1) == cs.end()) continue; for(int j=0;jT&&D<0.5) T=D; cout<<"Tol Pair "<T&&D<0.5) T=D; cout<<"Tol Pair "< flat; for(int p = 0; p0&&pp.s.size()>0){ pp.w1 = peak[p].w1; pp.w2 = peak[p].w2; pp.Vol = peak[p].Vol; flat.push_back(pp); } } cout<<"Keeping "<> Calibrating....\n"; double Im = 0; double Rm = 0; double Dm = 0; int count = 0; for(int p=0;p7.5) continue; R6inv += pow(d,-6.0);///flat[p].f.size(); Dm += d; ++count; } Im += flat[p].Vol*R6inv; Rm += R6inv*R6inv; } K = Im/Rm; Dm = Dm/count; Dm /=2; cout<<">> K: "<1){ // cout<<"Opt peak "<> Peak "<::iterator iter = flat.begin(); while(iter!=flat.end()){ double R6inv = 0; for(int a=0;af.size();a++){ R6inv += pow(pro.distance(iter->f[a],iter->s[a]),-6.0); } double E = abs(iter->Vol - K*R6inv)/min(K*R6inv,iter->Vol); if(E>10){ // cout<<"ERASING!!!"<Vol<<" "<Vol - K*R6inv)/(K*R6inv)<1){ // out<<"Opt peak "<> Peak "< > s1_s; s1_s.resize(s1.peak.size()); for(int p = 0; p< s1.peak.size();++p){ for(int q = 0; q< s1.peak.size();q++){ if(p==q) continue; if(dist(s1.peak[p],s1.peak[q]) > s1_n; s1_n.resize(s1.peak.size()); vector > s1_n2; s1_n2.resize(s1.peak.size()); for(int p = 0; p< s1.peak.size();++p){ for(int q = 0; q< s2.peak.size();q++){ if(dist(s1.peak[p],s2.peak[q])> Neighbors "<::iterator iter = s1_n[i].begin(); iter!= s1_n[i].end(); ++iter){ cout<<(*iter)+1<<" "; int j = *iter; cout<(mod.self(),"sandy") .def_method("read_peak",&NMR::Sandy::read_peak) .def_method("read_seq",&NMR::Sandy::read_seq) .def_method("read_shifts",&NMR::Sandy::read_shifts) .def_method("mkprotein",&NMR::Sandy::mkprotein) .def_method("assign_peaks",&NMR::Sandy::assign_peaks) .def_method("read_topos",&NMR::Sandy::read_topos) .def_method("challange_peaks",&NMR::Sandy::challange_peaks) .def_method("dump",&NMR::Sandy::dump) .def_method("read_dist",&NMR::Sandy::read_dist) .def_method("dump_symmetric",&NMR::Sandy::dump_symmetric) .def_method("na_bond",&NMR::Sandy::na_bond) .def_method("na_angle",&NMR::Sandy::na_angle) .def_method("normalize_na",&NMR::Sandy::normalize_na) .def_method("na_dihe",&NMR::Sandy::na_dihe) .def_method("na",&NMR::Sandy::na) .def_method("na_residue",&NMR::Sandy::na_residue) .def_method("bond_tol",&NMR::Sandy::bond_tol) .def_method("angle_tol",&NMR::Sandy::angle_tol) .def_method("assign_str",&NMR::Sandy::assign_str) ; Class(mod.self(),"chemicalshift") .def_method("clear",&NMR::ChemicalShift::clear) .def_method("read",&NMR::ChemicalShift::read) .def_method("size",&NMR::ChemicalShift::size) ; mod .def_const("CHESHIRE",(int)NMR::ChemicalShift::CHESHIRE) .def_const("XEASY",(int)NMR::ChemicalShift::XEASY) ; mod .def_function("cluster","Clusters peaks from different spectra",NMR::cluster); } }