From 1eb19eb0e31d484883434895bef65afc71954b0e Mon Sep 17 00:00:00 2001 From: Dariusz Murakowski Date: Fri, 17 May 2013 10:27:10 -0400 Subject: [PATCH] Special two-site + two-allele (two-locus + binary) Hamiltonian with all parameters passed as command line arguments. --- hamiltonian.cpp | 113 ++++++++++++++++++++++++++++++++++++ hamiltonian.h | 9 +++ population.cpp | 42 ++++++++++++++ population.h | 1 + wf.cpp | 173 +++++++++++++++++++++++++++++++++++++++----------------- wf.h | 10 ++++ 6 files changed, 295 insertions(+), 53 deletions(-) diff --git a/hamiltonian.cpp b/hamiltonian.cpp index 842504a..593d9b0 100644 --- a/hamiltonian.cpp +++ b/hamiltonian.cpp @@ -292,6 +292,119 @@ double EpitopeHamiltonian::get_energy(const std::set &mutated_site } +/*********************************************** + * 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"); + + 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 & +// Output population to file +// two-site two-allele + +void Population::write_two_site_population(FILE *output, const Hamiltonian &H, unsigned int generation) { + + fprintf(output,"%d",generation); + + //static const unsigned int v00_arr[] = {}; + static const unsigned int v10_arr[] = {0}; + static const unsigned int v01_arr[] = {1}; + static const unsigned int v11_arr[] = {0,1}; + + //std::set v00_set (v00_arr, v00_arr+sizeof(v00_arr)/sizeof(v00_arr[0])); + std::set v10_set (v10_arr, v10_arr+sizeof(v10_arr)/sizeof(v10_arr[0])); + std::set v01_set (v01_arr, v01_arr+sizeof(v01_arr)/sizeof(v01_arr[0])); + std::set v11_set (v11_arr, v11_arr+sizeof(v11_arr)/sizeof(v11_arr[0])); + + Virus v00_vir (H, mu);//, v00_set); + Virus v10_vir (H, mu, v10_set); + Virus v01_vir (H, mu, v01_set); + Virus v11_vir (H, mu, v11_set); + + fprintf(output,"\t%d",pop[v00_vir]); + fprintf(output,"\t%d",pop[v10_vir]); + fprintf(output,"\t%d",pop[v01_vir]); + fprintf(output,"\t%d",pop[v11_vir]); + fprintf(output,"\n"); + + fflush(output); + +} + + diff --git a/population.h b/population.h index 7e7edd7..9374b9a 100644 --- a/population.h +++ b/population.h @@ -28,6 +28,7 @@ public: void next_generation(const Hamiltonian &H, gsl_rng* r, bool useRelative, bool useVerbose); void write_population(std::string filepath); void write_population(FILE *output, unsigned int generation); + void write_two_site_population(FILE *output, const Hamiltonian &H, unsigned int generation); unsigned int compute_population_size(); void print_population_size(); void mutate_population(const Hamiltonian &H, gsl_rng* r); diff --git a/wf.cpp b/wf.cpp index c89e797..01e4362 100644 --- a/wf.cpp +++ b/wf.cpp @@ -38,8 +38,6 @@ static unsigned sim_random_seed() { void run(RunParameters &r) { - printf("\n"); - // Initialize RNG and set initial state, if importing from file static gsl_rng *rnd = gsl_rng_alloc(gsl_rng_taus2); //pointer to random number generator state @@ -50,74 +48,107 @@ void run(RunParameters &r) { r.setFiles(); FILE *popout=fopen(r.trajectoryOutfile.c_str(),"w"); // .dat - FILE *supout=fopen(r.supplementaryOutfile.c_str(),"w"); // .sum - - if (r.importState) importState(r); - - - // Run (w/ targeted epitope) + FILE *supout=r.useTwoSite ? NULL : fopen(r.supplementaryOutfile.c_str(),"w"); // .sum - if (r.useEpitope) { + if (r.importState) importState(r); // .st - for (unsigned int n=0;n esc_var; - unsigned int esc_num=P.escape_variant(H, esc_var); + P.next_generation(H, rnd, r.useRelative, r.useVerbose); + P.write_population(popout,i); + + if (r.runUntilEscape && P.escaped(H)) break; + + } + + std::set esc_var; + unsigned int esc_num=P.escape_variant(H, esc_var); + + fprintf(supout,"%d\t%d",i,esc_num); + for (std::set::iterator iter=esc_var.begin(); iter!=esc_var.end(); ++iter) fprintf(supout,"\t%d",*iter); + fprintf(supout,"\n"); + + fflush(supout); - fprintf(supout,"%d\t%d",i,esc_num); - for (std::set::iterator iter=esc_var.begin(); iter!=esc_var.end(); ++iter) fprintf(supout,"\t%d",*iter); - fprintf(supout,"\n"); + } - fflush(supout); - } - } - - - // Run (w/out targeted epitope) - - else { - - //for (unsigned int n=0;n tempSet; + if (s1) tempSet.insert(0); + if (s2) tempSet.insert(1); + r.initPop.push_back(tempSet); + +} + + void usage() { std::cout << @@ -199,8 +245,16 @@ void usage() " -e (int) (int) targeted epitope spans sites [e#1, ..., e#2], assuming all WT\n" " -r flag to use relative energy condition for survival probability\n" " -esc flag to run until escape observed (or max number of generations reached)\n" -" -v flag for verbose output\n"; - std::cout.flush(); +" -v flag for verbose output\n" +"\n" +" -2site flag for two-site two-allele system\n" +" -h1 (double) value of field at site 1\n" +" -h2 (double) value of field at site 2\n" +" -J12 (double) value of coupling between sites 1 and 2\n" +" (note the sign convention on these is P ~ exp(-H),\n" +" H = sum_i h_i s_i + sum_{i > initPop; // Initial population sequences std::vector initFrac; // Initial population fraction @@ -127,6 +132,11 @@ public: useEpitope=false; useRelative=false; useVerbose=false; + + useTwoSite = false; + h1 = 0.0; + h2 = 0.0; + J12 = 0.0; } void setFiles() { -- 2.7.4