Chaste Release::3.1
AbstractCellPopulation.hpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, 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 #ifndef ABSTRACTCELLPOPULATION_HPP_
00037 #define ABSTRACTCELLPOPULATION_HPP_
00038 
00039 #include "Cell.hpp"
00040 #include "OutputFileHandler.hpp"
00041 
00042 #include <list>
00043 #include <map>
00044 #include <vector>
00045 #include <boost/shared_ptr.hpp>
00046 
00047 #include "ChasteSerialization.hpp"
00048 #include "ClassIsAbstract.hpp"
00049 #include <boost/serialization/vector.hpp>
00050 #include <boost/serialization/list.hpp>
00051 #include <boost/serialization/map.hpp>
00052 #include <boost/serialization/shared_ptr.hpp>
00053 
00054 #include "Node.hpp"
00055 #include "CellPropertyRegistry.hpp"
00056 #include "Identifiable.hpp"
00057 
00058 // Needed here to avoid serialization errors (on Boost<1.37)
00059 #include "WildTypeCellMutationState.hpp"
00060 #include "ApcOneHitCellMutationState.hpp"
00061 #include "ApcTwoHitCellMutationState.hpp"
00062 #include "BetaCateninOneHitCellMutationState.hpp"
00063 #include "ApoptoticCellProperty.hpp"
00064 #include "CellLabel.hpp"
00065 #include "CellData.hpp"
00066 #include "AbstractMesh.hpp"
00067 
00074 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM=ELEMENT_DIM>
00075 class AbstractCellPopulation : public Identifiable
00076 {
00077 private:
00078 
00080     friend class boost::serialization::access;
00081 
00088     template<class Archive>
00089     void serialize(Archive & archive, const unsigned int version)
00090     {
00091         archive & mCells;
00092         archive & mLocationCellMap;
00093         archive & mCellLocationMap;
00094         archive & mCellProliferativeTypeCount;
00095         archive & mCellCyclePhaseCount;
00096         archive & mpCellPropertyRegistry;
00097         archive & mOutputCellIdData;
00098         archive & mOutputCellMutationStates;
00099         archive & mOutputCellAncestors;
00100         archive & mOutputCellProliferativeTypes;
00101         archive & mOutputCellVariables;
00102         archive & mOutputCellCyclePhases;
00103         archive & mOutputCellAges;
00104         archive & mOutputCellVolumes;
00105     }
00106 
00107 protected:
00108 
00110     std::map<unsigned, std::set<CellPtr> > mLocationCellMap;
00111 
00113     std::map<Cell*, unsigned> mCellLocationMap;
00114 
00116     AbstractMesh<ELEMENT_DIM, SPACE_DIM>& mrMesh;
00117 
00119     std::list<CellPtr> mCells;
00120 
00122     std::vector<unsigned> mCellProliferativeTypeCount;
00123 
00125     std::vector<unsigned> mCellCyclePhaseCount;
00126 
00128     c_vector<double, SPACE_DIM> mCentroid;
00129 
00131     out_stream mpVizNodesFile;
00132 
00134     out_stream mpVizCellProliferativeTypesFile;
00135 
00137     out_stream mpVizCellProliferativePhasesFile;
00138 
00140     out_stream mpCellMutationStatesFile;
00141 
00143     out_stream mpVizCellAncestorsFile;
00144 
00146     out_stream mpCellProliferativeTypesFile;
00147 
00149     out_stream mpCellCyclePhasesFile;
00150 
00152     out_stream mpCellVariablesFile;
00153 
00155     out_stream mpCellAgesFile;
00156 
00158     out_stream mpCellIdFile;
00159 
00161     out_stream mpCellVolumesFile;
00162 
00164     out_stream mpVizBoundaryNodesFile;
00165 
00167     std::string mDirPath;
00168 
00170     out_stream mpVtkMetaFile;
00171 
00173     boost::shared_ptr<CellPropertyRegistry> mpCellPropertyRegistry;
00174 
00176     bool mOutputCellIdData;
00177 
00179     bool mOutputCellMutationStates;
00180 
00182     bool mOutputCellAncestors;
00183 
00185     bool mOutputCellProliferativeTypes;
00186 
00188     bool mOutputCellVariables;
00189 
00191     bool mOutputCellCyclePhases;
00192 
00194     bool mOutputCellAges;
00195 
00197     bool mOutputCellVolumes;
00198 
00205     virtual void Validate()=0;
00206 
00213     virtual void WriteVtkResultsToFile()=0;
00214 
00220     AbstractCellPopulation(AbstractMesh<ELEMENT_DIM, SPACE_DIM>& rMesh);
00221 
00222 public:
00223 
00235     AbstractCellPopulation( AbstractMesh<ELEMENT_DIM, SPACE_DIM>& rMesh,
00236                             std::vector<CellPtr>& rCells,
00237                             const std::vector<unsigned> locationIndices=std::vector<unsigned>());
00238 
00242     virtual ~AbstractCellPopulation();
00243 
00247     void InitialiseCells();
00248 
00254     void SetDataOnAllCells(const std::string& dataName, double dataValue);
00255 
00259     AbstractMesh<ELEMENT_DIM, SPACE_DIM>& rGetMesh();
00260 
00264     std::list<CellPtr>& rGetCells();
00265 
00272     virtual unsigned GetNumNodes()=0;
00273 
00283     virtual c_vector<double, SPACE_DIM> GetLocationOfCellCentre(CellPtr pCell)=0;
00284 
00294     virtual Node<SPACE_DIM>* GetNode(unsigned index)=0;
00295 
00305     virtual void SetNode(unsigned nodeIndex, ChastePoint<SPACE_DIM>& rNewLocation)=0;
00306 
00317     virtual bool IsCellAssociatedWithADeletedLocation(CellPtr pCell)=0;
00318 
00334     virtual CellPtr AddCell(CellPtr pNewCell, const c_vector<double,SPACE_DIM>& rCellDivisionVector, CellPtr pParentCell=CellPtr())=0;
00335 
00336     class Iterator; // Forward declaration; see below
00337 
00346     virtual unsigned RemoveDeadCells()=0;
00347 
00354     virtual void Update(bool hasHadBirthsOrDeaths=true)=0;
00355 
00365     std::vector<unsigned> GetCellMutationStateCount();
00366 
00375      const std::vector<unsigned>& rGetCellProliferativeTypeCount() const;
00376 
00387     const std::vector<unsigned>& rGetCellCyclePhaseCount() const;
00388 
00392     unsigned GetNumRealCells();
00393 
00398     void SetCellAncestorsToLocationIndices();
00399 
00403     void WriteCellIdDataToFile();
00404 
00411     std::set<unsigned> GetCellAncestors();
00412 
00422     CellPtr GetCellUsingLocationIndex(unsigned index);
00423 
00433     std::set<CellPtr> GetCellsUsingLocationIndex(unsigned index);
00434 
00442     bool IsCellAttachedToLocationIndex(unsigned index);
00443 
00454     void SetCellUsingLocationIndex(unsigned index, CellPtr pCell);
00455 
00462     virtual void AddCellUsingLocationIndex(unsigned index, CellPtr pCell);
00463 
00470     virtual void RemoveCellUsingLocationIndex(unsigned index, CellPtr pCell);
00471 
00479     void MoveCellInLocationMap(CellPtr pCell, unsigned old_index, unsigned new_index);
00480 
00490     unsigned GetLocationIndexUsingCell(CellPtr pCell);
00491 
00495     boost::shared_ptr<CellPropertyRegistry> GetCellPropertyRegistry();
00496 
00501     void SetDefaultMutationStateOrdering();
00502 
00512     virtual double GetWidth(const unsigned& rDimension)=0;
00513 
00521     virtual double GetVolumeOfCell(CellPtr pCell)=0;
00522 
00532     virtual std::set<unsigned> GetNeighbouringNodeIndices(unsigned index)=0;
00533 
00537     c_vector<double, SPACE_DIM> GetCentroidOfCellPopulation();
00538 
00545     virtual void CreateOutputFiles(const std::string& rDirectory,
00546                                    bool cleanOutputDirectory);
00547 
00551     virtual void WriteResultsToFiles();
00552 
00556     virtual void WriteTimeAndNodeResultsToFiles();
00557 
00561     virtual void GenerateCellResultsAndWriteToFiles()=0;
00562 
00570     virtual void GenerateCellResults(CellPtr pCell,
00571                              std::vector<unsigned>& rCellProliferativeTypeCounter,
00572                              std::vector<unsigned>& rCellCyclePhaseCounter);
00573 
00579     virtual void WriteCellVolumeResultsToFile()=0;
00580 
00587     void WriteCellResultsToFiles(std::vector<unsigned>& rCellProliferativeTypeCounter,
00588                                  std::vector<unsigned>& rCellCyclePhaseCounter);
00589 
00593     virtual void CloseOutputFiles();
00594 
00600     void OutputCellPopulationInfo(out_stream& rParamsFile);
00601 
00610     virtual void OutputCellPopulationParameters(out_stream& rParamsFile)=0;
00611 
00615     bool GetOutputCellIdData();
00616 
00620     bool GetOutputCellMutationStates();
00621 
00625     bool GetOutputCellAncestors();
00626 
00630     bool GetOutputCellProliferativeTypes();
00631 
00635     bool GetOutputCellVariables();
00636 
00640     bool GetOutputCellCyclePhases();
00641 
00645     bool GetOutputCellAges();
00646 
00650     bool GetOutputCellVolumes();
00651 
00657     void SetOutputCellIdData(bool outputCellIdData);
00658 
00664     void SetOutputCellMutationStates(bool outputCellMutationStates);
00665 
00671     void SetOutputCellAncestors(bool outputCellAncestors);
00672 
00678     void SetOutputCellProliferativeTypes(bool outputCellProliferativeTypes);
00679 
00685     void SetOutputCellVariables(bool outputCellVariables);
00686 
00692     void SetOutputCellCyclePhases(bool outputCellCyclePhases);
00693 
00699     void SetOutputCellAges(bool outputCellAges);
00700 
00706     void SetOutputCellVolumes(bool outputCellVolumes);
00707 
00712     c_vector<double,SPACE_DIM> GetSizeOfCellPopulation();
00713 
00720     class Iterator
00721     {
00722     public:
00723 
00727         inline CellPtr operator*();
00728 
00733         inline CellPtr operator->();
00734 
00740         inline bool operator!=(const AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator& rOther);
00741 
00745         inline Iterator& operator++();
00746 
00753         Iterator(AbstractCellPopulation& rCellPopulation, std::list<CellPtr>::iterator cellIter);
00754 
00758         virtual ~Iterator()
00759         {}
00760 
00761     private:
00762 
00769         virtual inline bool IsRealCell();
00770 
00774         inline bool IsAtEnd();
00775 
00777         AbstractCellPopulation& mrCellPopulation;
00778 
00780         std::list<CellPtr>::iterator mCellIter;
00781     };
00782 
00786     Iterator Begin();
00787 
00791     Iterator End();
00792 
00793 };
00794 
00795 enum cell_colours
00796 {
00797     STEM_COLOUR, // 0
00798     TRANSIT_COLOUR, // 1
00799     DIFFERENTIATED_COLOUR, // 2
00800     EARLY_CANCER_COLOUR, // 3
00801     LATE_CANCER_COLOUR, // 4
00802     LABELLED_COLOUR, // 5
00803     APOPTOSIS_COLOUR, // 6
00804     INVISIBLE_COLOUR // visualizer treats '7' as invisible
00805 };
00806 
00807 TEMPLATED_CLASS_IS_ABSTRACT_1_UNSIGNED(AbstractCellPopulation)
00808 
00809 
00810 //         Iterator class implementation - most methods are inlined         //
00812 
00813 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00814 CellPtr AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator::operator*()
00815 {
00816     assert(!IsAtEnd());
00817     return *mCellIter;
00818 }
00819 
00820 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00821 CellPtr AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator::operator->()
00822 {
00823     assert(!IsAtEnd());
00824     return *mCellIter;
00825 }
00826 
00827 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00828 bool AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator::operator!=(const AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator& rOther)
00829 {
00830     return mCellIter != rOther.mCellIter;
00831 }
00832 
00833 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00834 typename AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator& AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator::operator++()
00835 {
00836     do
00837     {
00838         ++mCellIter;
00839     }
00840     while (!IsAtEnd() && !IsRealCell());
00841 
00842     return (*this);
00843 }
00844 
00845 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00846 bool AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator::IsRealCell()
00847 {
00848     return !( mrCellPopulation.IsCellAssociatedWithADeletedLocation(*mCellIter) || (*this)->IsDead() );
00849 }
00850 
00851 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00852 bool AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator::IsAtEnd()
00853 {
00854     return mCellIter == mrCellPopulation.rGetCells().end();
00855 }
00856 
00857 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00858 AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator::Iterator(AbstractCellPopulation& rCellPopulation, std::list<CellPtr>::iterator cellIter)
00859     : mrCellPopulation(rCellPopulation),
00860       mCellIter(cellIter)
00861 {
00862     // The cell population can now return empty if it only has ghost nodes
00863     if (mrCellPopulation.rGetCells().empty())
00864     {
00865         mCellIter = mrCellPopulation.rGetCells().end();
00866     }
00867     else
00868     {
00869         // Make sure we start at a real cell
00870         if (mCellIter == mrCellPopulation.rGetCells().begin() && !IsRealCell())
00871         {
00872             ++(*this);
00873         }
00874     }
00875 }
00876 
00877 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00878 typename AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Begin()
00879 {
00880     return Iterator(*this, this->mCells.begin());
00881 }
00882 
00883 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00884 typename AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::End()
00885 {
00886     return Iterator(*this, this->mCells.end());
00887 }
00888 
00889 
00890 #endif /*ABSTRACTCELLPOPULATION_HPP_*/