1 // $Id: region.h,v 1.51 2011/03/07 06:08:49 bobgian Exp $ 2 3 /* 4 Copyright 2002 Peter Beerli, Mary Kuhner, Jon Yamato and Joseph Felsenstein 5 6 This software is distributed free of charge for non-commercial use 7 and is copyrighted. Of course, we do not guarantee that the software 8 works, and are not responsible for any damage you may cause or have. 9 */ 10 11 // Region--a storage class, containing data information that applies 12 // to the genetic info of that region (regional quick theta estimate, 13 // number of tips, region name). 14 // 15 // In addition, Region serves as the bridge between the data and the 16 // tree via Region::CreateTree(). CreateTree() copies the prototype 17 // tree and finishes tree construction (creates the tips and individuals, 18 // sets the site ranges) and enables data likelihood calculation (creates 19 // the data likelihood calculator). It returns an owning pointer to 20 // the newly created tree. 21 // 22 // Written by Jim Sloan, heavily revised by Jon Yamato 23 // 2002/01/03 changed Tipdata::data to a vector for generality--Mary Kuhner 24 // 2002/07/25 split out Locus class -- Mary Kuhner 25 26 #ifndef REGION_H 27 #define REGION_H 28 29 #include <cassert> // May be needed for inline definitions. 30 #include <string> 31 #include <vector> 32 33 #include "vectorx.h" 34 #include "constants.h" 35 #include "types.h" 36 #include "individual.h" // for IndVec typedef 37 #include "locus.h" // for TipData and for Locus member 38 #include "newick.h" // for UserTree member, m_usertree; 39 #include "forceparam.h" // for return type of GetScalars() 40 41 //------------------------------------------------------------------------------------ 42 43 class Tree; 44 class MapCollector; 45 class Region 46 { 47 private: 48 49 Region(const Region& src); // not defined 50 Region& operator=(const Region& src); // not defined 51 52 IndVec m_individuals; 53 vector<Locus> m_loci; 54 vector<Locus> m_movingloci; // either 'floating' or 'jumping'. 55 string m_regionname; 56 long m_id; // region number 57 UserTree* m_usertree; // we own this! 58 double m_effpopsize; 59 std::set<long> m_ploidies; // Phase 1 only. 60 bool m_snppanel; 61 62 // utility functions 63 bool MultiSampleIndividuals() const; 64 Individual& GetIndividual(long n); 65 bool ValidLocus(long locus) const; 66 bool ValidMovingLocus(long locus) const; 67 StringVec1d MakeTraitsXML(unsigned long nspaces) const; 68 StringVec1d MakePopXML(unsigned long nspaces) const; 69 70 public: 71 72 bool RecombinationCanBeEstimated() const; Region(std::string rname)73 Region(std::string rname) : m_regionname(rname), m_usertree(new NullUserTree()), 74 m_effpopsize(1.0) {}; ~Region()75 ~Region() { delete m_usertree; }; 76 77 // Add a new Locus 78 void AddLocus(bool movable=false, string name=""); 79 80 //Save ploidy (used for trait stuff) AddPloidy(long ploid)81 void AddPloidy(long ploid) {m_ploidies.insert(ploid);}; 82 83 // Access AddIndividual(const Individual & newind)84 void AddIndividual(const Individual& newind) {m_individuals.push_back(newind);}; SetID(long newid)85 void SetID(long newid) {m_id = newid; }; SetUserTree(UserTree * utree)86 void SetUserTree(UserTree* utree) {delete m_usertree; m_usertree = utree;}; SetSnpPanel(bool val)87 void SetSnpPanel(bool val) {m_snppanel = val;}; GetSnpPanel()88 bool GetSnpPanel() {return m_snppanel;}; 89 90 //Phase 1 to Phase 2 functions: SetEffectivePopSize(double newsize)91 void SetEffectivePopSize(double newsize) {m_effpopsize = newsize; }; 92 void InitializeRegionalMappositionsUsingGlobalMappositions(); 93 void SetupAndMoveAllLoci(); 94 void MakeAllMovableLociMove(); 95 96 //Phase 2 to Phase 1 function: 97 void RevertMovingLoci(); 98 99 // Forwarders to Locus class; used by xml.cpp 100 void SetNmarkers(long locus, long n); 101 void SetGlobalMapposition(long locus, long n); 102 void SetName(long locus, std::string name); 103 void SetOffset(long locus, long n); 104 void SetPositions(long locus, const LongVec1d& pos); 105 void SetPositions(long locus); // set to defaults 106 void SetNsites(long locus, long numsites); 107 void SetDataType(long locus, const DataType_ptr dtype); 108 void SetTipData(long locus, const TipData& td); 109 110 //Forwarder to individuals; used by parsetreetodata.cpp 111 void SetPhaseMarkers(long indnum, LongVec2d& phases); 112 113 // Potentially expensive Forces::QuickCalc() helper functions 114 LongVec1d CalcNVariableMarkers() const; // dim: by xpart 115 116 //Getter functions GetID()117 long GetID() const {return m_id;}; GetEffectivePopSize()118 double GetEffectivePopSize() const {return m_effpopsize;}; GetNIndividuals()119 long GetNIndividuals() const {return m_individuals.size();}; GetIndividual(long n)120 const Individual& GetIndividual(long n) const {return m_individuals[n];}; GetRegionName()121 string GetRegionName() const {return m_regionname;}; GetLocus(long locus)122 Locus& GetLocus(long locus) {assert(ValidLocus(locus)); 123 return m_loci[locus]; }; GetLocus(long locus)124 const Locus& GetLocus(long locus) const {assert(ValidLocus(locus)); 125 return m_loci[locus]; }; GetMovingLocus(long mloc)126 const Locus& GetMovingLocus(long mloc) const {assert(ValidMovingLocus(mloc)); 127 return m_movingloci[mloc];} GetIndividuals()128 const IndVec& GetIndividuals() const {return m_individuals;}; 129 bool HasLocus(string lname)const; 130 const Locus& GetLocus(string lname)const; 131 long GetLocusIndex(string lname) const; GetNloci()132 long GetNloci() const {return m_loci.size(); }; 133 long GetNumAllLoci()const; 134 long GetNumFixedLoci()const; GetNumMovingLoci()135 long GetNumMovingLoci() const {return m_movingloci.size();}; 136 long GetNumSites() const; 137 rangepair GetSiteSpan() const; 138 void MakeUserTree(Tree* treetips); HasUserTree()139 bool HasUserTree() const {return m_usertree->Exists();}; 140 StringVec1d ToXML(unsigned long nspaces) const; 141 DoubleVec1d GetMuRatios() const; GetPloidies()142 std::set<long> GetPloidies() const {return m_ploidies;}; 143 144 // Forwarders to Locus class GetNmarkers(long locus)145 long GetNmarkers(long locus) const {return m_loci[locus].GetNmarkers();}; GetMapposition(long locus)146 long GetMapposition(long locus) const {return m_loci[locus].GetRegionalMapposition();}; GetGlobalMapposition(long locus)147 long GetGlobalMapposition(long locus) const {return m_loci[locus].GetGlobalMapposition();}; GetOffset(long locus)148 long GetOffset(long locus) const {return m_loci[locus].GetOffset();}; GetUserMarkerLocations(long locus)149 LongVec1d GetUserMarkerLocations(long locus) const {return m_loci[locus].GetUserMarkerLocations();}; 150 long GetLocusNsites(long locus) const; 151 StringVec1d GetAllLociNames() const; 152 StringVec1d GetAllLociDataTypes() const; 153 StringVec1d GetAllLociMuRates() const; GetLocusDataModel(long locus)154 DataModel_ptr GetLocusDataModel(long locus) const {return m_loci[locus].GetDataModel(); }; GetNTips()155 long GetNTips() const {return m_loci[0].GetNTips();}; 156 long GetNXTips(long xpart) const; GetAllTipData(long locus)157 vector<TipData> GetAllTipData(long locus) const {return m_loci[locus].GetTipData();}; 158 DoubleVec1d CountNBases() const; 159 void AddUniqueNamesTo(std::set<string>& popnames) const; 160 161 // Factory function 162 Tree* CreateTree(); 163 164 // Genotypic-data support functions 165 bool CanHaplotype() const; 166 void PruneSamePhaseUnknownSites(IndVec& indivs) const; 167 bool AnyPhaseUnknownSites() const; 168 169 bool AnyMapping() const; 170 bool AnyJumpingAnalyses() const; 171 bool AnySimulatedLoci() const; 172 173 bool AnySNPDataWithDefaultLocations() const; 174 175 // Helper function for original use in DataPack::RemoveUneededPartitions 176 void RemovePartitionFromLoci(force_type); 177 void CopyTipDataForLocus(const string& lname); 178 179 // Helper function for ReportPage(DataPage::Show, echoing data) 180 StringVec2d GetMarkerDataWithLabels() const; // dim: locus X tip 181 182 // Validity checking 183 bool IsValidRegion(string & errorString) const; 184 // helper for parsetreetodata 185 bool IsDuplicateTipName(const string& newname) const; 186 187 //We save the results of mapping in the appropriate loci. 188 void SaveMappingInfo(MapCollector* mapcoll); 189 void SaveMappingInfo(std::vector<MapCollector*> mapcolls, 190 DoubleVec1d logweights); 191 void ReportMappingInfo(); 192 StringVec2d CreateAllDataModelReports() const; 193 194 //Write out mapping files 195 void WriteAnyMapping() const; 196 197 // These are power-user functions, generally used by our lab in writing 198 // simulations; they are not called in normal execution. They are only 199 // used when JSIM is defined: 200 201 // Write a Fluctuate format file. 202 void WriteFlucFile(const string& outfilename, bool onlyregion) const; 203 // Break up a multi-population data set into single-population ones 204 StringVec2d MakeByPopXML(unsigned long nspaces) const; 205 void WritePopulationXMLFiles() const; 206 void WriteToXMLFileUsing(std::ofstream& ofile, StringVec1d& region_contents) const; 207 void WriteToXMLFileUsing(std::ofstream& ofile, StringVec2d& region_contents, bool produceonepop) const; 208 // helpers for XML infile creation 209 210 private: 211 std::vector<PopulationXML> MakeVectorOfPopulationXML() const; 212 213 }; 214 215 #endif // REGION_H 216 217 //____________________________________________________________________________________ 218