From: Dariusz Murakowski Date: Tue, 21 Apr 2015 18:37:46 +0000 (-0400) Subject: Clarify dependencies. Copy hamiltonian.{cpp,h} to ham_ss.{cpp,h}. Copy contents of... X-Git-Url: http://src.murakowski.org/?a=commitdiff_plain;h=c88116482385e97e2dd1a9050a4b597a949145b9;p=VirEvoDyn.git Clarify dependencies. Copy hamiltonian.{cpp,h} to ham_ss.{cpp,h}. Copy contents of virus.{cpp,h} into pop_ss.{cpp,h}. --- diff --git a/Makefile b/Makefile index 7557a2f..a96a8b0 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ EXECNAME_WF = wf SRCS_MC = mc.cpp hamiltonian.cpp population.cpp virus.cpp EXECNAME_MC = mc -SRCS_SS = ss.cpp hamiltonian.cpp population.cpp virus.cpp pop_ss.cpp reaction.cpp +SRCS_SS = ss.cpp ham_ss.cpp pop_ss.cpp reaction.cpp EXECNAME_SS = ss diff --git a/ham_ss.cpp b/ham_ss.cpp new file mode 100644 index 0000000..5a0d41d --- /dev/null +++ b/ham_ss.cpp @@ -0,0 +1,504 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "ham_ss.h" + + +//Constructor for loading couplings from John Barton's Ising Inversion code +//Code for this function derived from John Barton's code + +Hamiltonian::Hamiltonian(std::string &FILENAME) { + + this->bh=1.0; + this->bJ=1.0; + + FILE* input; + input = fopen(FILENAME.c_str(),"r"); // .j + if (input == NULL) { perror((std::string("ERROR: ") + FILENAME).c_str()); exit(1); } + + typedef std::vector Key; + std::map JIndex; + char o; + int site=0; + + fscanf(input,"%d",&size); + + while (fscanf(input,"%d",&site)==1) { + + Key inputKey(2,site); + while (fscanf(input,"%c",&o)==1) { + + if (o==';') break; + + else { + + int neighborSite=0; + double neighborJ=0; + fscanf(input,"%d %c %lf",&neighborSite,&o,&neighborJ); + + if (neighborSite==site) { + + inputKey[1]=site; + JIndex[inputKey]=neighborJ; + + } + else { + + inputKey[1]=neighborSite; + JIndex[inputKey]=neighborJ; + + } + //std::cout << site << " " << neighborSite << " " << neighborJ << "\n"; + + } + + } + + } + + fclose(input); + Key inputKey(2,0); + J.resize(size); + + for(int i=0;ibh=1.0; + this->bJ=1.0; + + FILE* input; + input = fopen(FILENAME.c_str(),"r"); // .j + if (input == NULL) { perror((std::string("ERROR: ") + FILENAME).c_str()); exit(1); } + + typedef std::vector Key; + std::map JIndex; + char o; + int site=0; + + fscanf(input,"%d",&size); + + while (fscanf(input,"%d",&site)==1) { + + Key inputKey(2,site); + while (fscanf(input,"%c",&o)==1) { + + if (o==';') break; + + else { + + int neighborSite=0; + double neighborJ=0; + fscanf(input,"%d %c %lf",&neighborSite,&o,&neighborJ); + + if (neighborSite==site) { + + inputKey[1]=site; + JIndex[inputKey]=neighborJ; + + } + else { + + inputKey[1]=neighborSite; + JIndex[inputKey]=neighborJ; + + } + + } + + } + + } + + fclose(input); + Key inputKey(2,0); + J.resize(size); + + for(int i=0;i &mutated_sites) const { + + double efield = 0.0; + double ecoupling = 0.0; + std::set::iterator iter1; + std::set::iterator iter2; + + for (iter1=mutated_sites.begin();iter1!=mutated_sites.end();++iter1) { + + efield -= J[*iter1][*iter1]; + + iter2 = iter1; + ++iter2; + + for (;iter2!=mutated_sites.end();iter2++) ecoupling -= J[*iter1][*iter2]; + + } + + return ((efield * bh) + (ecoupling * bJ)); + +} + + +// Set the targeted epitope + +void EpitopeHamiltonian::set_epitope(const std::vector &eWT, const std::vector &eMut, double d) { + + penalty.push_back(d); + epitopeWT.push_back(eWT); + epitopeMut.push_back(eMut); + +} + + +// Did virus escape from *any* immune pressure anywhere? +bool EpitopeHamiltonian::escaped(const Virus &v) { + return EpitopeHamiltonian::escaped(v.mutated_sites); +} + +// Did sequence escape from *any* immune pressure anywhere? +bool EpitopeHamiltonian::escaped(const std::set &mutated_sites) const { + for (unsigned ep=0; ep &eWT, const std::vector &eMut) { + + return EpitopeHamiltonian::escaped(v.mutated_sites, eWT, eMut); + +} + + +// Return true if the sequence has escaped from immune pressure at particular epitope + +bool EpitopeHamiltonian::escaped(const std::set &mutated_sites, const std::vector &eWT, const std::vector &eMut) const { + + bool escape=false; + + for (unsigned i=0;i &mutated_sites) const { + for (unsigned ep=0; ep &mutated_sites) const +{ + return get_energy(mutated_sites); +} + +// tried to have a forward declaration of explicit specializations, +// which "must occur before instantiation", +// but then linker complained about undefined reference +double ensure_get_energy_false_instantiated(const std::set &x) { EpitopeHamiltonian A((std::string&)""); return A.get_energy(x); } + +// Return the energy for a given set of mutated sites + +template +double EpitopeHamiltonian::get_energy(const std::set &mutated_sites) const +{ + + double efield = 0.0; + double ecoupling = 0.0; + std::set::iterator iter1; + std::set::iterator iter2; + + for (iter1=mutated_sites.begin();iter1!=mutated_sites.end();++iter1) { + + efield -= J[*iter1][*iter1]; + + iter2 = iter1; + ++iter2; + + for (;iter2!=mutated_sites.end();iter2++) ecoupling -= J[*iter1][*iter2]; + + } + + double energy = (efield * bh) + (ecoupling * bJ); + + if (include_epitope) { + + // Check for mismatch between targeted epitope and current sequence + + for (unsigned ep=0; ep +inline bool contains(const std::vector &vec, const T &value) +{ + return std::find(vec.begin(), vec.end(), value) != vec.end(); +} + +// helper function to test whether set contains a value +// based on http://stackoverflow.com/questions/6194797/what-is-a-c-container-with-a-contains-operation +template +inline bool contains(const std::set &container, const T &value) +{ + return container.find(value) != container.end(); +} + +void EpitopeHamiltonian::generate_allsites_set() +{ + this->epitopeAllSites = MutatedSiteSequence(); + for (unsigned ep=0; epnotEpitopeAllSites = MutatedSiteSequence(); + for (unsigned i = 0; i < static_cast(this->size); ++i) { + if (!contains(epitopeAllSites,i)) { + notEpitopeAllSites.insert(i); + } + } +} + + + +/*********************************************** + * my simple 2-site 2-allele system + ***********************************************/ + +// Return the energy for a given set of mutated sites + +TwoSiteHamiltonian::TwoSiteHamiltonian(double h1, double h2, double J12) { + + this->bh=1.0; + this->bJ=1.0; + + this->size = 2; + + J.resize(size); + + for(int i=0; ibh=1.0; + this->bJ=1.0; + + FILE* input; + input = fopen(FILENAME.c_str(),"r"); + if (input == NULL) { perror((std::string("ERROR: ") + FILENAME).c_str()); exit(1); } + + typedef std::vector Key; + std::map JIndex; + char o; + int site=0; + + fscanf(input,"%d",&size); + + while (fscanf(input,"%d",&site)==1) { + + Key inputKey(2,site); + while (fscanf(input,"%c",&o)==1) { + + if (o==';') break; + + else { + + int neighborSite=0; + double neighborJ=0; + fscanf(input,"%d %c %lf",&neighborSite,&o,&neighborJ); + + if (neighborSite==site) { + + inputKey[1]=site; + JIndex[inputKey]=neighborJ; + + } + else { + + inputKey[1]=neighborSite; + JIndex[inputKey]=neighborJ; + + } + //std::cout << site << " " << neighborSite << " " << neighborJ << "\n"; + + } + + } + + } + + fclose(input); + Key inputKey(2,0); + J.resize(size); + + for(int i=0;i +#include +#include + +#include "pop_ss.h" + + +typedef std::vector > AdjacencyList; +typedef std::vector > Coupling; + +typedef std::set MutatedSiteSequence; + + +class Virus; + +//template bool contains(const std::vector &vec, const T &value); +//template bool contains(const std::set &container, const T &value); + + +class Hamiltonian { + +public: + + int size; + double bh; // fields "inverse temperature" multiplier + double bJ; // couplings "inverse temperature" multiplier + Coupling J; + AdjacencyList Graph; + + Hamiltonian(std::string &FILENAME); + Hamiltonian() { } + virtual ~Hamiltonian() { } + + virtual bool escaped(const Virus &v) { return false; } + virtual bool escaped(const std::set &mutated_sites) const { return false; } + virtual bool escaped_all(const Virus &v) { return false; } + virtual bool escaped_all(const std::set &mutated_sites) const { return false; } + virtual double get_energy(const std::set &mutated_sites) const; + + void set_temp(double x) { bh=x; bJ=x; } + void set_temp(double x, double y) { bh=x; bJ=y; } + +}; + + +class EpitopeHamiltonian : public Hamiltonian { + +public: + + std::vector penalty; + std::vector > epitopeWT; + std::vector > epitopeMut; + + MutatedSiteSequence epitopeAllSites; // all site indices belonging to an epitope (WT or mut ant) + MutatedSiteSequence notEpitopeAllSites; // all sites not involved in an epitope + + EpitopeHamiltonian(std::string &FILENAME); + + void set_epitope(const std::vector &eWT, const std::vector &eMut, double d); + void generate_allsites_set(); + + bool escaped(const Virus &v); + bool escaped(const std::set &mutated_sites) const; + bool escaped(const Virus &v, const std::vector &eWT, const std::vector &eMut); + bool escaped(const std::set &mutated_sites, const std::vector &eWT, const std::vector &eMut) const; + + bool escaped_all(const Virus &v); + bool escaped_all(const std::set &mutated_sites) const; + + double get_energy(const std::set &mutated_sites) const; + template double get_energy(const std::set &mutated_sites) const; + +}; + + +class TwoSiteHamiltonian : public Hamiltonian { + +public: + + TwoSiteHamiltonian(std::string &FILENAME); + TwoSiteHamiltonian(double h1, double h2, double J12); + +}; + +#endif // HAM_SS_H diff --git a/pop_ss.cpp b/pop_ss.cpp index 94fa159..3d625d0 100644 --- a/pop_ss.cpp +++ b/pop_ss.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -8,6 +9,101 @@ #include "pop_ss.h" + +//Constuct a virus object of wildtype + +Virus::Virus(const Hamiltonian &H, double mu) { + + this->mutated_sites.clear(); + this->mu=mu; + L=H.size; + energy=0; + +} + + +//Construct a virus and compute its energy. + +Virus::Virus(const Hamiltonian &H, double mu, const std::set &mutated_sites) { + + this->mutated_sites=mutated_sites; + this->mu=mu; + L=H.size; + update_energy(H); + +} + + +//Print key numerical parameters of the object to the terminal for diagnostics. + +void Virus::print_parameters() { + + std::cout << "mu = " << mu << std::endl; + std::cout << "energy = " << energy << std::endl; + +} + + +//Mutate the entire virus. Takes a Hamiltonian object and a pointer +//to an instance of a gsl_rng random number generator + +void Virus::mutate(const Hamiltonian &H, gsl_rng* r) { + + unsigned int n=gsl_ran_binomial(r,mu,L); + + while (n<1) n=gsl_ran_binomial(r,mu,L); + + for (unsigned i=0;i +#include #include #include #include -#include "virus.h" +#include "ham_ss.h" + + +class Hamiltonian; + + +class Virus { + +public: + + double energy; + std::set mutated_sites; + + Virus(const Hamiltonian &H, double mu); + Virus(const Hamiltonian &H, double mu, const std::set &mutated_sites); + + void print_parameters(); + void mutate(const Hamiltonian &H, gsl_rng* r); + double survival() const; + double survival(double Eavg) const; + + friend class Population; + +//private: + + double mu; + unsigned int L; + + void update_energy(const Hamiltonian &H); + +}; + + +bool operator<(const Virus& lhs, const Virus& rhs); typedef std::map virus_map; diff --git a/ss.cpp b/ss.cpp index ba25e86..739a1d5 100644 --- a/ss.cpp +++ b/ss.cpp @@ -9,9 +9,9 @@ #include -#include "hamiltonian.h" +#include "ham_ss.h" #include "pop_ss.h" -#include "virus.h" +//#include "virus.h" #include "ss.h" #include "reaction.h" diff --git a/ss.h b/ss.h index 0b86e4e..ff80c08 100644 --- a/ss.h +++ b/ss.h @@ -1,5 +1,5 @@ -#ifndef WF_H -#define WF_H +#ifndef SS_H +#define SS_H #include #include