Cell.cpp

00001 /*
00002 
00003 Copyright (c) 2005-2015, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 #include "Cell.hpp"
00037 
00050 struct null_deleter
00051 {
00053     void operator()(void const *) const
00054     {
00055     }
00056 };
00057 
00058 Cell::Cell(boost::shared_ptr<AbstractCellProperty> pMutationState,
00059            AbstractCellCycleModel* pCellCycleModel,
00060            bool archiving,
00061            CellPropertyCollection cellPropertyCollection)
00062     : mCanDivide(false),
00063       mCellPropertyCollection(cellPropertyCollection),
00064       mpCellCycleModel(pCellCycleModel),
00065       mDeathTime(DBL_MAX), // This has to be initialised for archiving
00066       mStartOfApoptosisTime(DBL_MAX),
00067       mApoptosisTime(0.25), // cell takes 15 min to fully undergo apoptosis
00068       mUndergoingApoptosis(false),
00069       mIsDead(false),
00070       mIsLogged(false)
00071 {
00072     if (SimulationTime::Instance()->IsStartTimeSetUp()==false)
00073     {
00074         EXCEPTION("Cell is setting up a cell-cycle model but SimulationTime has not been set up");
00075     }
00076 
00077     if (pCellCycleModel == NULL)
00078     {
00079         EXCEPTION("Cell-cycle model is null");
00080     }
00081 
00082     mpCellCycleModel->SetCell(CellPtr(this, null_deleter()));
00083 
00084     if (!mCellPropertyCollection.HasPropertyType<CellId>())
00085     {
00086         // Set cell identifier this will be called all the time unless the constructor is called through archiving
00087         MAKE_PTR(CellId, p_cell_id);
00088         p_cell_id->AssignCellId();
00089         mCellPropertyCollection.AddProperty(p_cell_id);
00090     }
00091 
00092     if (!pMutationState->IsSubType<AbstractCellMutationState>())
00093     {
00094         EXCEPTION("Attempting to create cell with a cell mutation state that is not a subtype of AbstractCellMutationState");
00095     }
00096 
00097     if (!mCellPropertyCollection.HasProperty(pMutationState))
00098     {
00099         mCellPropertyCollection.AddProperty(pMutationState);
00100     }
00101 
00102     if (!mCellPropertyCollection.HasPropertyType<CellData>())
00103     {
00104         // Add empty cell data
00105         MAKE_PTR(CellData, p_cell_data);
00106         mCellPropertyCollection.AddProperty(p_cell_data);
00107     }
00108 
00109     /*
00110      * If a cell proliferative type was not passed in via the input
00111      * argument cellPropertyCollection (for example as in the case
00112      * of a daughter cell being created following division) then add
00113      * add a 'default' cell proliferative type to the cell property
00114      * collection. This ensures that the method GetCellProliferativeType()
00115      * always returns a valid proliferative type.
00116      */
00117     if (!mCellPropertyCollection.HasPropertyType<AbstractCellProliferativeType>())
00118     {
00119         mCellPropertyCollection.AddProperty(CellPropertyRegistry::Instance()->Get<DefaultCellProliferativeType>());
00120     }
00121 
00122     if (!archiving)
00123     {
00124         // Increment cell count for each cell property in mCellPropertyCollection
00125         for (CellPropertyCollection::Iterator property_iter = mCellPropertyCollection.Begin();
00126              property_iter != mCellPropertyCollection.End();
00127              ++property_iter)
00128         {
00129             (*property_iter)->IncrementCellCount();
00130         }
00131     }
00132 }
00133 
00134 Cell::~Cell()
00135 {
00136     if (!mIsDead)
00137     {
00138         Kill();
00139     }
00140     delete mpCellCycleModel;
00141 }
00142 
00143 void Cell::SetCellProliferativeType(boost::shared_ptr<AbstractCellProperty> pProliferativeType)
00144 {
00145     if (!pProliferativeType->IsSubType<AbstractCellProliferativeType>())
00146     {
00147         EXCEPTION("Attempting to give cell a cell proliferative type that is not a subtype of AbstractCellProliferativeType");
00148     }
00149 
00150     boost::shared_ptr<AbstractCellProliferativeType> p_old_proliferative_type = GetCellProliferativeType();
00151 
00152     p_old_proliferative_type->DecrementCellCount();
00153     mCellPropertyCollection.RemoveProperty(p_old_proliferative_type);
00154 
00155     AddCellProperty(pProliferativeType);
00156 }
00157 
00158 boost::shared_ptr<AbstractCellProliferativeType> Cell::GetCellProliferativeType()  const
00159 {
00160     CellPropertyCollection proliferative_type_collection = mCellPropertyCollection.GetPropertiesType<AbstractCellProliferativeType>();
00161 
00162     /*
00163      * Note: In its current form the code requires each cell to have exactly
00164      * one proliferative type. This is reflected in the assertion below. If a user
00165      * wishes to include cells with multiple proliferative types, each possible
00166      * combination must be created as a separate proliferative type class.
00167      */
00168     assert(proliferative_type_collection.GetSize() == 1);
00169 
00170     return boost::static_pointer_cast<AbstractCellProliferativeType>(proliferative_type_collection.GetProperty());
00171 }
00172 
00173 void Cell::SetCellCycleModel(AbstractCellCycleModel* pCellCycleModel)
00174 {
00175     if (mpCellCycleModel != pCellCycleModel)
00176     {
00177         delete mpCellCycleModel;
00178     }
00179     mpCellCycleModel = pCellCycleModel;
00180     mpCellCycleModel->SetCell(CellPtr(this, null_deleter()));
00181 }
00182 
00183 AbstractCellCycleModel* Cell::GetCellCycleModel() const
00184 {
00185     return mpCellCycleModel;
00186 }
00187 
00188 void Cell::InitialiseCellCycleModel()
00189 {
00190     mpCellCycleModel->Initialise();
00191 }
00192 
00193 double Cell::GetAge() const
00194 {
00195     return mpCellCycleModel->GetAge();
00196 }
00197 
00198 double Cell::GetBirthTime() const
00199 {
00200     return mpCellCycleModel->GetBirthTime();
00201 }
00202 
00203 void Cell::SetBirthTime(double birthTime)
00204 {
00205     mpCellCycleModel->SetBirthTime(birthTime);
00206 }
00207 
00208 void Cell::SetMutationState(boost::shared_ptr<AbstractCellProperty> pMutationState)
00209 {
00210     if (!pMutationState->IsSubType<AbstractCellMutationState>())
00211     {
00212         EXCEPTION("Attempting to give cell a cell mutation state that is not a subtype of AbstractCellMutationState");
00213     }
00214 
00215     boost::shared_ptr<AbstractCellMutationState> p_old_mutation_state = GetMutationState();
00216     p_old_mutation_state->DecrementCellCount();
00217     mCellPropertyCollection.RemoveProperty(p_old_mutation_state);
00218 
00219     AddCellProperty(pMutationState);
00220 }
00221 
00222 boost::shared_ptr<AbstractCellMutationState> Cell::GetMutationState() const
00223 {
00224     CellPropertyCollection mutation_state_collection = mCellPropertyCollection.GetPropertiesType<AbstractCellMutationState>();
00225 
00226     /*
00227      * Note: In its current form the code requires each cell to have exactly
00228      * one mutation state. This is reflected in the assertion below. If a user
00229      * wishes to include cells with multiple mutation states, each possible
00230      * combination must be created as a separate mutation state class.
00231      */
00232     assert(mutation_state_collection.GetSize() == 1);
00233 
00234     return boost::static_pointer_cast<AbstractCellMutationState>(mutation_state_collection.GetProperty());
00235 }
00236 
00237 boost::shared_ptr<CellData> Cell::GetCellData() const
00238 {
00239     CellPropertyCollection cell_data_collection = mCellPropertyCollection.GetPropertiesType<CellData>();
00240 
00241     /*
00242      * Note: In its current form the code requires each cell to have exactly
00243      * one CellData object. This is reflected in the assertion below.
00244      */
00245     assert(cell_data_collection.GetSize() <= 1);
00246 
00247     return boost::static_pointer_cast<CellData>(cell_data_collection.GetProperty());
00248 }
00249 
00250 CellPropertyCollection& Cell::rGetCellPropertyCollection()
00251 {
00252     return mCellPropertyCollection;
00253 }
00254 
00255 const CellPropertyCollection& Cell::rGetCellPropertyCollection() const
00256 {
00257     return mCellPropertyCollection;
00258 }
00259 
00260 void Cell::AddCellProperty(const boost::shared_ptr<AbstractCellProperty>& rProperty)
00261 {
00262     // Note: if the cell already has the specified property, no action is taken
00263     if (!mCellPropertyCollection.HasProperty(rProperty))
00264     {
00265         mCellPropertyCollection.AddProperty(rProperty);
00266         rProperty->IncrementCellCount();
00267     }
00268 }
00269 
00270 void Cell::SetLogged()
00271 {
00272     mIsLogged = true;
00273 }
00274 
00275 bool Cell::IsLogged()
00276 {
00277     return mIsLogged;
00278 }
00279 
00280 void Cell::StartApoptosis(bool setDeathTime)
00281 {
00282     assert(!IsDead());
00283 
00284     if (mUndergoingApoptosis)
00285     {
00286         EXCEPTION("StartApoptosis() called when already undergoing apoptosis");
00287     }
00288     mUndergoingApoptosis = true;
00289     mStartOfApoptosisTime = SimulationTime::Instance()->GetTime();
00290     if (setDeathTime)
00291     {
00292         mDeathTime = mStartOfApoptosisTime + mApoptosisTime;
00293     }
00294     else
00295     {
00296         mDeathTime = DBL_MAX;
00297     }
00298     AddCellProperty(mCellPropertyCollection.GetCellPropertyRegistry()->Get<ApoptoticCellProperty>());
00299 }
00300 
00301 bool Cell::HasApoptosisBegun() const
00302 {
00303     return mUndergoingApoptosis;
00304 }
00305 
00306 double Cell::GetStartOfApoptosisTime() const
00307 {
00308     return mStartOfApoptosisTime;
00309 }
00310 
00311 double Cell::GetApoptosisTime() const
00312 {
00313     return mApoptosisTime;
00314 }
00315 
00316 void Cell::SetApoptosisTime(double apoptosisTime)
00317 {
00318     assert(apoptosisTime > 0.0);
00319     mApoptosisTime = apoptosisTime;
00320 }
00321 
00322 double Cell::GetTimeUntilDeath() const
00323 {
00324     if (!mUndergoingApoptosis || mDeathTime==DBL_MAX)
00325     {
00326         EXCEPTION("Shouldn't be checking time until apoptosis as it isn't set");
00327     }
00328 
00329     return mDeathTime - SimulationTime::Instance()->GetTime();
00330 }
00331 
00332 bool Cell::IsDead()
00333 {
00334     if (mUndergoingApoptosis && !mIsDead)
00335     {
00336         double sloppy_death_time = mDeathTime - DBL_EPSILON * mApoptosisTime;
00337         if (SimulationTime::Instance()->GetTime() >= sloppy_death_time )
00338         {
00339             this->Kill();
00340         }
00341     }
00342     return mIsDead;
00343 }
00344 
00345 void Cell::Kill()
00346 {
00347     // Decrement cell count for each cell property in mCellPropertyCollection
00348     for (CellPropertyCollection::Iterator property_iter = mCellPropertyCollection.Begin();
00349          property_iter != mCellPropertyCollection.End();
00350          ++property_iter)
00351     {
00352         (*property_iter)->DecrementCellCount();
00353     }
00354     mIsDead = true;
00355 }
00356 
00357 void Cell::SetAncestor(boost::shared_ptr<AbstractCellProperty> pCellAncestor)
00358 {
00359     if (!pCellAncestor->IsSubType<CellAncestor>())
00360     {
00361         EXCEPTION("Attempting to give cell a cell ancestor which is not a CellAncestor");
00362     }
00363 
00364     // You can only set ancestors once
00365     CellPropertyCollection ancestor_collection = mCellPropertyCollection.GetPropertiesType<CellAncestor>();
00366     if (ancestor_collection.GetSize() == 0)
00367     {
00368         AddCellProperty(pCellAncestor);
00369     }
00370     else
00371     {
00372         // Overwrite the CellAncestor
00373         RemoveCellProperty<CellAncestor>();
00374         AddCellProperty(pCellAncestor);
00375     }
00376 }
00377 
00378 unsigned Cell::GetAncestor() const
00379 {
00380     CellPropertyCollection ancestor_collection = mCellPropertyCollection.GetPropertiesType<CellAncestor>();
00381 
00382     assert(ancestor_collection.GetSize() <= 1);
00383     if (ancestor_collection.GetSize() == 0)
00384     {
00385         return UNSIGNED_UNSET;
00386     }
00387 
00388     boost::shared_ptr<CellAncestor> p_ancestor = boost::static_pointer_cast<CellAncestor>(ancestor_collection.GetProperty());
00389 
00390     return p_ancestor->GetAncestor();
00391 }
00392 
00393 unsigned Cell::GetCellId() const
00394 {
00395     CellPropertyCollection cell_id_collection = mCellPropertyCollection.GetPropertiesType<CellId>();
00396 
00397     assert(cell_id_collection.GetSize() == 1);
00398 
00399     boost::shared_ptr<CellId> p_cell_id = boost::static_pointer_cast<CellId>(cell_id_collection.GetProperty());
00400 
00401     return p_cell_id->GetCellId();
00402 }
00403 
00404 bool Cell::ReadyToDivide()
00405 {
00406     assert(!IsDead());
00407     if (mUndergoingApoptosis || HasCellProperty<ApoptoticCellProperty>())
00408     {
00409         return false;
00410     }
00411 
00412     mCanDivide = mpCellCycleModel->ReadyToDivide();
00413 
00414     return mCanDivide;
00415 }
00416 
00417 CellPtr Cell::Divide()
00418 {
00419     // Check we're allowed to divide
00420     assert(!IsDead());
00421     assert(mCanDivide);
00422     mCanDivide = false;
00423 
00424     // Reset properties of parent cell
00425     mpCellCycleModel->ResetForDivision();
00426 
00427     // Create copy of cell property collection to modify for daughter cell
00428     CellPropertyCollection daughter_property_collection = mCellPropertyCollection;
00429 
00430     // Remove the CellId from the daughter cell, as a new one will be assigned in the constructor
00431     daughter_property_collection.RemoveProperty<CellId>();
00432 
00433     // Copy all cell data (note we create a new object not just copying the pointer)
00434     assert(daughter_property_collection.HasPropertyType<CellData>());
00435 
00436     // Get the existing copy of the cell data and remove it from the daughter cell
00437     boost::shared_ptr<CellData> p_cell_data = GetCellData();
00438     daughter_property_collection.RemoveProperty(p_cell_data);
00439 
00440     // Create a new cell data object using the copy constructor and add this to the daughter cell
00441     MAKE_PTR_ARGS(CellData, p_daughter_cell_data, (*p_cell_data));
00442     daughter_property_collection.AddProperty(p_daughter_cell_data);
00443 
00444     // Create daughter cell with modified cell property collection
00445     CellPtr p_new_cell(new Cell(GetMutationState(), mpCellCycleModel->CreateCellCycleModel(), false, daughter_property_collection));
00446 
00447     // Initialise properties of daughter cell
00448     p_new_cell->GetCellCycleModel()->InitialiseDaughterCell();
00449 
00450     // Set the daughter cell to inherit the apoptosis time of the parent cell
00451     p_new_cell->SetApoptosisTime(mApoptosisTime);
00452 
00453     return p_new_cell;
00454 }

Generated by  doxygen 1.6.2