1 // $Id: gc_region.cpp,v 1.18 2011/03/08 19:22:00 bobgian Exp $
2 
3 /*
4   Copyright 2002  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 #include "gc_errhandling.h"
12 #include "gc_locus.h"
13 #include "gc_region.h"
14 #include "gc_strings.h"
15 #include "gc_strings_region.h"
16 #include "wx/log.h"
17 
GCMapPosition()18 GCMapPosition::GCMapPosition()
19     :
20     m_hasPosition(false),
21     m_position(0)
22 {
23 }
24 
~GCMapPosition()25 GCMapPosition::~GCMapPosition()
26 {
27 }
28 
29 long
GetPosition() const30 GCMapPosition::GetPosition() const
31 {
32     if(!m_hasPosition)
33     {
34         gc_implementation_error e(gcerr::regionNoPositionToGet.c_str());
35         throw e;
36     }
37     return m_position;
38 }
39 
40 bool
HasPosition() const41 GCMapPosition::HasPosition() const
42 {
43     return m_hasPosition;
44 }
45 
46 void
SetPosition(long position)47 GCMapPosition::SetPosition(long position)
48 {
49     m_hasPosition = true;
50     m_position = position;
51 }
52 
53 void
UnsetPosition()54 GCMapPosition::UnsetPosition()
55 {
56     m_hasPosition = false;
57 }
58 
59 wxString
AsString() const60 GCMapPosition::AsString() const
61 {
62     if(HasPosition())
63     {
64         return wxString::Format(gcstr_region::mapPosition,GetPosition());
65     }
66     return gcstr::mapPositionUnset;
67 }
68 
69 void
DebugDump(wxString prefix) const70 GCMapPosition::DebugDump(wxString prefix) const
71 {
72     wxLogDebug("%s%s",prefix.c_str(),AsString().c_str());   // EWDUMPOK
73 }
74 
GCLocusInfoMap()75 GCLocusInfoMap::GCLocusInfoMap()
76 {
77 }
78 
~GCLocusInfoMap()79 GCLocusInfoMap::~GCLocusInfoMap()
80 {
81 }
82 
83 wxString
AsString() const84 GCLocusInfoMap::AsString() const
85 {
86     wxString retString = "";
87     for(const_iterator i = begin(); i != end(); i++)
88     {
89         size_t locusId = (*i).first;
90         GCMapPosition mapPosition = (*i).second;
91         retString += wxString::Format(gcstr_region::locusMapPosition,(int)locusId,mapPosition.AsString().c_str());
92     }
93     return retString;
94 }
95 
GCTraitInfoSet()96 GCTraitInfoSet::GCTraitInfoSet()
97 {
98 }
99 
~GCTraitInfoSet()100 GCTraitInfoSet::~GCTraitInfoSet()
101 {
102 }
103 
104 wxString
AsString() const105 GCTraitInfoSet::AsString() const
106 {
107     wxString retString = "";
108     for(const_iterator i = begin(); i != end(); i++)
109     {
110         retString += wxString::Format(gcstr_region::traitIndexListMember,(int)(*i));
111     }
112     return retString;
113 }
114 
gcRegion()115 gcRegion::gcRegion()
116     :
117     m_name(wxString::Format(gcstr_region::internalName,(long)m_objId)),
118     m_blessed(false),
119     m_hasEffectivePopulationSize(false),
120     m_effectivePopulationSize(0.0)
121 {
122 }
123 
~gcRegion()124 gcRegion::~gcRegion()
125 {
126 }
127 
128 void
AddLocus(gcLocus & locus)129 gcRegion::AddLocus(gcLocus & locus)
130 {
131     if(m_loci.find(locus.GetId()) != m_loci.end())
132     {
133         wxString msg = wxString::Format(gcerr::duplicateLocusInRegion,
134                                         locus.GetName().c_str(),
135                                         GetName().c_str());
136         gc_implementation_error e(msg.c_str());
137         throw e;
138     }
139 
140     m_loci[locus.GetId()] = GCMapPosition();
141 }
142 
143 void
AddLocus(gcLocus & locus,long mapPosition)144 gcRegion::AddLocus(gcLocus & locus, long mapPosition)
145 {
146     AddLocus(locus);
147     m_loci[locus.GetId()].SetPosition(mapPosition);
148 }
149 
150 void
AddTraitId(size_t traitId)151 gcRegion::AddTraitId(size_t traitId)
152 {
153     if(m_traits.find(traitId) != m_traits.end())
154     {
155         wxString msg = wxString::Format(gcerr::regionTraitAlreadyAdded,
156                                         (int)traitId);
157         gc_implementation_error e(msg.c_str());
158         throw e;
159     }
160     m_traits.insert(traitId);
161 }
162 
163 void
RemoveLocusId(size_t locusId)164 gcRegion::RemoveLocusId(size_t locusId)
165 {
166     GCLocusInfoMap::iterator iter = m_loci.find(locusId);
167     if(iter == m_loci.end())
168     {
169         wxString msg = wxString::Format(gcerr::regionNoSuchLocus,
170                                         (int)locusId,
171                                         (int)GetId());
172         gc_implementation_error e(msg.c_str());
173         throw e;
174     }
175     m_loci.erase(iter);
176 }
177 
178 void
RemoveTraitId(size_t traitId)179 gcRegion::RemoveTraitId(size_t traitId)
180 {
181     GCTraitInfoSet::iterator iter = m_traits.find(traitId);
182     if(iter == m_traits.end())
183     {
184         wxString msg = wxString::Format(gcerr::regionNoSuchTrait,
185                                         (int)traitId,
186                                         (int)GetId());
187         gc_implementation_error e(msg.c_str());
188         throw e;
189     }
190     m_traits.erase(iter);
191 }
192 
193 bool
GetBlessed() const194 gcRegion::GetBlessed() const
195 {
196     return m_blessed;
197 }
198 
199 void
SetBlessed(bool blessed)200 gcRegion::SetBlessed(bool blessed)
201 {
202     m_blessed = blessed;
203 }
204 
205 wxString
GetName() const206 gcRegion::GetName() const
207 {
208     return m_name;
209 }
210 
211 void
SetName(wxString newName)212 gcRegion::SetName(wxString newName)
213 {
214     m_name = newName;
215 }
216 
217 bool
HasEffectivePopulationSize() const218 gcRegion::HasEffectivePopulationSize() const
219 {
220     return m_hasEffectivePopulationSize;
221 }
222 
223 double
GetEffectivePopulationSize() const224 gcRegion::GetEffectivePopulationSize() const
225 {
226     if(!(HasEffectivePopulationSize()))
227     {
228         wxString msg = wxString::Format(gcerr::regionNoEffectivePopSize,
229                                         (int)GetId());
230         gc_implementation_error e(msg.c_str());
231         throw e;
232     }
233     return m_effectivePopulationSize;
234 }
235 
236 void
SetEffectivePopulationSize(double effectivePopulationSize)237 gcRegion::SetEffectivePopulationSize(double effectivePopulationSize)
238 {
239     if( ! (effectivePopulationSize > 0))
240     {
241         wxString msg = wxString::Format(gcerr::badEffectivePopSize,
242                                         GetName().c_str(),
243                                         effectivePopulationSize);
244         gc_data_error e(msg.c_str());
245         throw e;
246     }
247 
248     m_hasEffectivePopulationSize = true;
249     m_effectivePopulationSize = effectivePopulationSize;
250 }
251 
252 const GCLocusInfoMap &
GetLocusInfoMap() const253 gcRegion::GetLocusInfoMap() const
254 {
255     return m_loci;
256 }
257 
258 size_t
GetLocusCount() const259 gcRegion::GetLocusCount() const
260 {
261     return m_loci.size();
262 }
263 
264 const GCTraitInfoSet &
GetTraitInfoSet() const265 gcRegion::GetTraitInfoSet() const
266 {
267     return m_traits;
268 }
269 
270 void
DebugDump(wxString prefix) const271 gcRegion::DebugDump(wxString prefix) const
272 {
273     wxLogDebug("%sregion \"%s\", (id %ld)",  // EWDUMPOK
274                prefix.c_str(),
275                GetName().c_str(),
276                (long)GetId());
277 
278     if(HasEffectivePopulationSize())
279     {
280         wxLogDebug("%seffecive population size %f", // EWDUMPOK
281                    (prefix+gcstr::indent).c_str(),
282                    GetEffectivePopulationSize());
283     }
284 
285     wxLogDebug("%sloci:%s", // EWDUMPOK
286                (prefix+gcstr::indent).c_str(),
287                m_loci.AsString().c_str());
288 
289     wxLogDebug("%straits:%s",   // EWDUMPOK
290                (prefix+gcstr::indent).c_str(),
291                m_traits.AsString().c_str());
292 }
293 
294 bool
CanMergeWith(const gcRegion & regionRef) const295 gcRegion::CanMergeWith(const gcRegion & regionRef) const
296 {
297     if(HasEffectivePopulationSize() && regionRef.HasEffectivePopulationSize())
298     {
299         if(GetEffectivePopulationSize() != regionRef.GetEffectivePopulationSize())
300         {
301             return false;
302         }
303     }
304 
305     return true;
306 }
307 
308 //____________________________________________________________________________________
309