AbstractTissue.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2010
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 #include "AbstractTissue.hpp"
00030 #include "AbstractOdeBasedCellCycleModel.hpp"
00031 
00032 template<unsigned DIM>
00033 AbstractTissue<DIM>::AbstractTissue(std::vector<TissueCell>& rCells,
00034                                     const std::vector<unsigned> locationIndices)
00035     : mCells(rCells.begin(), rCells.end()),
00036       mTissueContainsMesh(false),
00037       mpMutationStateRegistry(CellMutationStateRegistry::Instance()->TakeOwnership())
00038 {
00039     // There must be at least one cell
00040     assert(mCells.size() > 0);
00041 
00042     // To avoid double-counting problems, clear the passed-in cells vector
00043     rCells.clear();
00044 
00045     if (!locationIndices.empty())
00046     {
00047         // There must be a one-one correspondence between cells and location indices
00048         if (mCells.size() != locationIndices.size())
00049         {
00050             EXCEPTION("There is not a one-one correspondence between cells and location indices");
00051         }
00052     }
00053 
00054     // Set up the map between location indices and cells
00055     std::list<TissueCell>::iterator it = mCells.begin();
00056     for (unsigned i=0; it != mCells.end(); ++it, ++i)
00057     {
00058         unsigned index = locationIndices.empty() ? i : locationIndices[i]; // assume that the ordering matches
00059         mLocationCellMap[index] = &(*it);
00060         mCellLocationMap[&(*it)] = index;
00061     }
00062 
00063     // Initialise cell counts to zero
00068     mCellProliferativeTypeCount = std::vector<unsigned>(NUM_CELL_PROLIFERATIVE_TYPES);
00069     for (unsigned i=0; i<mCellProliferativeTypeCount.size(); i++)
00070     {
00071         mCellProliferativeTypeCount[i] = 0;
00072     }
00073 
00074     mCellCyclePhaseCount = std::vector<unsigned>(NUM_CELL_CYCLE_PHASES);
00075     for (unsigned i=0; i<mCellCyclePhaseCount.size(); i++)
00076     {
00077         mCellCyclePhaseCount[i] = 0;
00078     }
00079 }
00080 
00081 template<unsigned DIM>
00082 void AbstractTissue<DIM>::InitialiseCells()
00083 {
00084     for (std::list<TissueCell>::iterator iter=mCells.begin(); iter!=mCells.end(); ++iter)
00085     {
00086         iter->InitialiseCellCycleModel();
00087     }
00088 }
00089 
00090 template<unsigned DIM>
00091 std::list<TissueCell>& AbstractTissue<DIM>::rGetCells()
00092 {
00093     return mCells;
00094 }
00095 
00096 template<unsigned DIM>
00097 bool AbstractTissue<DIM>::HasMesh()
00098 {
00099     return mTissueContainsMesh;
00100 }
00101 
00102 template<unsigned DIM>
00103 unsigned AbstractTissue<DIM>::GetNumRealCells()
00104 {
00105     unsigned counter = 0;
00106     for (typename AbstractTissue<DIM>::Iterator cell_iter=this->Begin(); cell_iter!=this->End(); ++cell_iter)
00107     {
00108         counter++;
00109     }
00110     return counter;
00111 }
00112 
00113 template<unsigned DIM>
00114 void AbstractTissue<DIM>::SetCellAncestorsToLocationIndices()
00115 {
00116     for (typename AbstractTissue<DIM>::Iterator cell_iter=this->Begin(); cell_iter!=this->End(); ++cell_iter)
00117     {
00118         cell_iter->SetAncestor(mCellLocationMap[&(*cell_iter)]);
00119     }
00120 }
00121 
00122 template<unsigned DIM>
00123 std::set<unsigned> AbstractTissue<DIM>::GetCellAncestors()
00124 {
00125     std::set<unsigned> remaining_ancestors;
00126     for (typename AbstractTissue<DIM>::Iterator cell_iter=this->Begin(); cell_iter!=this->End(); ++cell_iter)
00127     {
00128         remaining_ancestors.insert(cell_iter->GetAncestor());
00129     }
00130     return remaining_ancestors;
00131 }
00132 
00133 template<unsigned DIM>
00134 std::vector<unsigned> AbstractTissue<DIM>::GetCellMutationStateCount()
00135 {
00136     if (TissueConfig::Instance()->GetOutputCellMutationStates()==false)
00137     {
00138         EXCEPTION("Call TissueConfig::Instance()->SetOutputCellMutationStates(true) before using this function");
00139     }
00140 
00141     // An ordering must have been specified for cell mutation states
00142     SetDefaultMutationStateOrdering();
00143 
00144     const std::vector<boost::shared_ptr<AbstractCellMutationState> >& r_mutation_states
00145         = GetMutationRegistry()->rGetAllMutationStates();
00146 
00147     std::vector<unsigned> cell_mutation_state_count(r_mutation_states.size());
00148     for (unsigned i=0; i<r_mutation_states.size(); i++)
00149     {
00150         cell_mutation_state_count[i] = r_mutation_states[i]->GetCellCount();
00151     }
00152     return cell_mutation_state_count;
00153 }
00154 
00155 template<unsigned DIM>
00156 const std::vector<unsigned>& AbstractTissue<DIM>::rGetCellProliferativeTypeCount() const
00157 {
00158     if (TissueConfig::Instance()->GetOutputCellProliferativeTypes()==false)
00159     {
00160         EXCEPTION("Call TissueConfig::Instance()->SetOutputCellProliferativeTypes(true) before using this function");
00161     }
00162     return mCellProliferativeTypeCount;
00163 }
00164 
00165 template<unsigned DIM>
00166 const std::vector<unsigned>& AbstractTissue<DIM>::rGetCellCyclePhaseCount() const
00167 {
00168     if (TissueConfig::Instance()->GetOutputCellCyclePhases()==false)
00169     {
00170         EXCEPTION("Call TissueConfig::Instance()->SetOutputCellCyclePhases(true) before using this function");
00171     }
00172     return mCellCyclePhaseCount;
00173 }
00174 
00175 template<unsigned DIM>
00176 TissueCell& AbstractTissue<DIM>::rGetCellUsingLocationIndex(unsigned index)
00177 {
00178     // Get a pointer to the cell corresponding to this location index
00179     TissueCell* p_cell = mLocationCellMap[index];
00180 
00181     // Unless this pointer is null, return a reference to the cell
00182     if (p_cell)
00183     {
00184         return *p_cell;
00185     }
00186     else
00187     {
00188         EXCEPTION("Location index input argument does not correspond to a TissueCell");
00189     }
00190 }
00191 
00192 template<unsigned DIM>
00193 unsigned AbstractTissue<DIM>::GetLocationIndexUsingCell(TissueCell& rCell)
00194 {
00195     return mCellLocationMap[&rCell];
00196 }
00197 
00198 template<unsigned DIM>
00199 boost::shared_ptr<CellMutationStateRegistry> AbstractTissue<DIM>::GetMutationRegistry()
00200 {
00201     return mpMutationStateRegistry;
00202 }
00203 
00204 template<unsigned DIM>
00205 void AbstractTissue<DIM>::SetDefaultMutationStateOrdering()
00206 {
00207     boost::shared_ptr<CellMutationStateRegistry> p_registry = GetMutationRegistry();
00208     if (!p_registry->HasOrderingBeenSpecified())
00209     {
00210         std::vector<boost::shared_ptr<AbstractCellMutationState> > mutations;
00211         mutations.push_back(p_registry->Get<WildTypeCellMutationState>());
00212         mutations.push_back(p_registry->Get<LabelledCellMutationState>());
00213         mutations.push_back(p_registry->Get<ApcOneHitCellMutationState>());
00214         mutations.push_back(p_registry->Get<ApcTwoHitCellMutationState>());
00215         mutations.push_back(p_registry->Get<BetaCateninOneHitCellMutationState>());
00216         p_registry->SpecifyOrdering(mutations);
00217     }
00218 }
00219 
00221 //                             Output methods                               //
00223 
00224 
00225 template<unsigned DIM>
00226 void AbstractTissue<DIM>::CreateOutputFiles(const std::string& rDirectory, bool cleanOutputDirectory)
00227 {
00228     // Helper pointer
00229     TissueConfig* p_config = TissueConfig::Instance();
00230 
00231     OutputFileHandler output_file_handler(rDirectory, cleanOutputDirectory);
00232     mpVizNodesFile = output_file_handler.OpenOutputFile("results.viznodes");
00233     mpBoundaryNodesFile = output_file_handler.OpenOutputFile("results.vizboundarynodes");
00234     mpVizCellProliferativeTypesFile = output_file_handler.OpenOutputFile("results.vizcelltypes");
00235 
00236     if (p_config->GetOutputCellAncestors())
00237     {
00238         mpCellAncestorsFile = output_file_handler.OpenOutputFile("results.vizancestors");
00239     }
00240     if (p_config->GetOutputCellMutationStates())
00241     {
00242         mpCellMutationStatesFile = output_file_handler.OpenOutputFile("cellmutationstates.dat");
00243         *mpCellMutationStatesFile << "Time\t Healthy\t Labelled\t APC_1\t APC_2\t BETA_CAT \n";
00244     }
00245     if (p_config->GetOutputCellProliferativeTypes())
00246     {
00247         mpCellProliferativeTypesFile = output_file_handler.OpenOutputFile("celltypes.dat");
00248     }
00249     if (p_config->GetOutputCellVariables())
00250     {
00251         mpCellVariablesFile = output_file_handler.OpenOutputFile("cellvariables.dat");
00252     }
00253     if (p_config->GetOutputCellCyclePhases())
00254     {
00255         mpCellCyclePhasesFile = output_file_handler.OpenOutputFile("cellcyclephases.dat");
00256     }
00257     if (p_config->GetOutputCellAges())
00258     {
00259         mpCellAgesFile = output_file_handler.OpenOutputFile("cellages.dat");
00260     }
00261     if (p_config->GetOutputCellIdData())
00262     {
00263         mpCellIdFile = output_file_handler.OpenOutputFile("loggedcell.dat");
00264     }
00265 }
00266 
00267 template<unsigned DIM>
00268 void AbstractTissue<DIM>::CloseOutputFiles()
00269 {
00270     // Helper pointer
00271     TissueConfig* p_config = TissueConfig::Instance();
00272 
00273     mpVizNodesFile->close();
00274     mpBoundaryNodesFile->close();
00275     mpVizCellProliferativeTypesFile->close();
00276 
00277     if (p_config->GetOutputCellMutationStates())
00278     {
00279         mpCellMutationStatesFile->close();
00280     }
00281     if (p_config->GetOutputCellProliferativeTypes())
00282     {
00283         mpCellProliferativeTypesFile->close();
00284     }
00285     if (p_config->GetOutputCellVariables())
00286     {
00287         mpCellVariablesFile->close();
00288     }
00289     if (p_config->GetOutputCellCyclePhases())
00290     {
00291         mpCellCyclePhasesFile->close();
00292     }
00293     if (p_config->GetOutputCellAncestors())
00294     {
00295         mpCellAncestorsFile->close();
00296     }
00297     if (p_config->GetOutputCellAges())
00298     {
00299         mpCellAgesFile->close();
00300     }
00301     if (p_config->GetOutputCellIdData())
00302     {
00303         mpCellIdFile->close();
00304     }
00305 }
00306 
00307 template<unsigned DIM>
00308 void AbstractTissue<DIM>::GenerateCellResults(unsigned locationIndex,
00309                                               std::vector<unsigned>& rCellProliferativeTypeCounter,
00310                                               std::vector<unsigned>& rCellCyclePhaseCounter)
00311 {
00312     // Helper pointer
00313     TissueConfig* p_config = TissueConfig::Instance();
00314 
00315     unsigned colour = STEM_COLOUR;
00316 
00317     TissueCell* p_cell = mLocationCellMap[locationIndex];
00318 
00319     if (p_config->GetOutputCellCyclePhases())
00320     {
00321         // Update rCellCyclePhaseCounter
00322         switch (p_cell->GetCellCycleModel()->GetCurrentCellCyclePhase())
00323         {
00324             case G_ZERO_PHASE:
00325                 rCellCyclePhaseCounter[0]++;
00326                 break;
00327             case G_ONE_PHASE:
00328                 rCellCyclePhaseCounter[1]++;
00329                 break;
00330             case S_PHASE:
00331                 rCellCyclePhaseCounter[2]++;
00332                 break;
00333             case G_TWO_PHASE:
00334                 rCellCyclePhaseCounter[3]++;
00335                 break;
00336              case M_PHASE:
00337                 rCellCyclePhaseCounter[4]++;
00338                 break;
00339             default:
00340                 NEVER_REACHED;
00341         }
00342     }
00343 
00344     if (p_config->GetOutputCellAncestors())
00345     {
00346         // Set colour dependent on cell ancestor and write to file
00347         colour = p_cell->GetAncestor();
00348         if (colour == UNSIGNED_UNSET)
00349         {
00350             // Set the file to -1 to mark this case.
00351             colour = 1;
00352             *mpCellAncestorsFile << "-";
00353         }
00354         *mpCellAncestorsFile << colour << " ";
00355     }
00356 
00357     // Set colour dependent on cell type
00358     switch (p_cell->GetCellProliferativeType())
00359     {
00360         case STEM:
00361             colour = STEM_COLOUR;
00362             if (p_config->GetOutputCellProliferativeTypes())
00363             {
00364                 rCellProliferativeTypeCounter[0]++;
00365             }
00366             break;
00367         case TRANSIT:
00368             colour = TRANSIT_COLOUR;
00369             if (p_config->GetOutputCellProliferativeTypes())
00370             {
00371                 rCellProliferativeTypeCounter[1]++;
00372             }
00373             break;
00374         case DIFFERENTIATED:
00375             colour = DIFFERENTIATED_COLOUR;
00376             if (p_config->GetOutputCellProliferativeTypes())
00377             {
00378                 rCellProliferativeTypeCounter[2]++;
00379             }
00380             break;
00381         case APOPTOTIC:
00382             colour = APOPTOSIS_COLOUR;
00383             if (p_config->GetOutputCellProliferativeTypes())
00384             {
00385                 rCellProliferativeTypeCounter[3]++;
00386             }
00387             break;
00388         default:
00389             NEVER_REACHED;
00390     }
00391 
00392     if (p_config->GetOutputCellMutationStates())
00393     {
00394         // Set colour dependent on cell mutation state
00395         if (!p_cell->GetMutationState()->IsType<WildTypeCellMutationState>())
00396         {
00397             colour = p_cell->GetMutationState()->GetColour();
00398         }
00399     }
00400 
00401     if (p_cell->HasApoptosisBegun())
00402     {
00403         // For any type of cell set the colour to this if it is undergoing apoptosis.
00404         colour = APOPTOSIS_COLOUR;
00405     }
00406 
00407     // Write cell variable data to file if required
00408     if ( p_config->GetOutputCellVariables() && dynamic_cast<AbstractOdeBasedCellCycleModel*>(p_cell->GetCellCycleModel()) )
00409     {
00410         // Write location index corresponding to cell
00411         *mpCellVariablesFile << locationIndex << " ";
00412 
00413         // Write cell variables
00414         std::vector<double> proteins = (static_cast<AbstractOdeBasedCellCycleModel*>(p_cell->GetCellCycleModel()))->GetProteinConcentrations();
00415         for (unsigned i=0; i<proteins.size(); i++)
00416         {
00417             *mpCellVariablesFile << proteins[i] << " ";
00418         }
00419     }
00420 
00421     // Write cell age data to file if required
00422     if (p_config->GetOutputCellAges())
00423     {
00424         // Write location index corresponding to cell
00425         *mpCellAgesFile << locationIndex << " ";
00426 
00427         // Write cell location
00428         c_vector<double, DIM> cell_location = GetLocationOfCellCentre(*p_cell);
00429 
00430         for (unsigned i=0; i<DIM; i++)
00431         {
00432             *mpCellAgesFile << cell_location[i] << " ";
00433         }
00434 
00435         // Write cell age
00436         *mpCellAgesFile << p_cell->GetAge() << " ";
00437     }
00438 
00439     *mpVizCellProliferativeTypesFile << colour << " ";
00440 }
00441 
00442 template<unsigned DIM>
00443 void AbstractTissue<DIM>::WriteCellResultsToFiles(std::vector<unsigned>& rCellProliferativeTypeCounter,
00444                                                   std::vector<unsigned>& rCellCyclePhaseCounter)
00445 {
00446     // Helper pointer
00447     TissueConfig* p_config = TissueConfig::Instance();
00448 
00449     *mpVizCellProliferativeTypesFile << "\n";
00450 
00451     if (p_config->GetOutputCellAncestors())
00452     {
00453         *mpCellAncestorsFile << "\n";
00454     }
00455 
00456     // Write cell mutation state data to file if required
00457     if (p_config->GetOutputCellMutationStates())
00458     {
00459         // An ordering must be specified for cell mutation states
00460         SetDefaultMutationStateOrdering();
00461 
00462         const std::vector<boost::shared_ptr<AbstractCellMutationState> >& r_mutation_states
00463             = GetMutationRegistry()->rGetAllMutationStates();
00464 
00465         for (unsigned i=0; i<r_mutation_states.size(); i++)
00466         {
00467             *mpCellMutationStatesFile << r_mutation_states[i]->GetCellCount() << "\t";
00468         }
00469         *mpCellMutationStatesFile << "\n";
00470     }
00471 
00472     // Write cell type data to file if required
00473     if (p_config->GetOutputCellProliferativeTypes())
00474     {
00475         for (unsigned i=0; i<mCellProliferativeTypeCount.size(); i++)
00476         {
00477             mCellProliferativeTypeCount[i] = rCellProliferativeTypeCounter[i];
00478             *mpCellProliferativeTypesFile << rCellProliferativeTypeCounter[i] << "\t";
00479         }
00480         *mpCellProliferativeTypesFile << "\n";
00481     }
00482 
00483     if (p_config->GetOutputCellVariables())
00484     {
00485         *mpCellVariablesFile << "\n";
00486     }
00487 
00488     // Write cell cycle phase data to file if required
00489     if (p_config->GetOutputCellCyclePhases())
00490     {
00491         for (unsigned i=0; i<mCellCyclePhaseCount.size(); i++)
00492         {
00493             mCellCyclePhaseCount[i] = rCellCyclePhaseCounter[i];
00494             *mpCellCyclePhasesFile << rCellCyclePhaseCounter[i] << "\t";
00495         }
00496         *mpCellCyclePhasesFile << "\n";
00497     }
00498 
00499     // Write cell age data to file if required
00500     if (p_config->GetOutputCellAges())
00501     {
00502         *mpCellAgesFile << "\n";
00503     }
00504 }
00505 
00506 template<unsigned DIM>
00507 void AbstractTissue<DIM>::WriteTimeAndNodeResultsToFiles()
00508 {
00509     double time = SimulationTime::Instance()->GetTime();
00510 
00511     *mpVizNodesFile << time << "\t";
00512     *mpBoundaryNodesFile << time << "\t";
00513 
00514     // Write node data to file
00515     for (unsigned node_index=0; node_index<GetNumNodes(); node_index++)
00516     {
00517         // Hack that covers the case where the node is associated with a cell that has just been killed (#1129)
00518         bool node_corresponds_to_dead_cell = false;
00519         if (mLocationCellMap[node_index])
00520         {
00521             node_corresponds_to_dead_cell = mLocationCellMap[node_index]->IsDead();
00522         }
00523 
00524         // Write node data to file
00525         if ( !(GetNode(node_index)->IsDeleted()) && !node_corresponds_to_dead_cell)
00526         {
00527             const c_vector<double,DIM>& position = GetNode(node_index)->rGetLocation();
00528 
00529             for (unsigned i=0; i<DIM; i++)
00530             {
00531                 *mpVizNodesFile << position[i] << " ";
00532             }
00533             *mpBoundaryNodesFile << GetNode(node_index)->IsBoundaryNode() << " ";
00534         }
00535     }
00536     *mpVizNodesFile << "\n";
00537     *mpBoundaryNodesFile << "\n";
00538 }
00539 
00540 template<unsigned DIM>
00541 void AbstractTissue<DIM>::WriteResultsToFiles()
00542 {
00543     // Helper pointer
00544     TissueConfig* p_config = TissueConfig::Instance();
00545 
00546     WriteTimeAndNodeResultsToFiles();
00547 
00548     double time = SimulationTime::Instance()->GetTime();
00549 
00550     *mpVizCellProliferativeTypesFile << time << "\t";
00551 
00552     if (p_config->GetOutputCellAncestors())
00553     {
00554         *mpCellAncestorsFile << time << "\t";
00555     }
00556     if (p_config->GetOutputCellMutationStates())
00557     {
00558         *mpCellMutationStatesFile << time << "\t";
00559     }
00560     if (p_config->GetOutputCellProliferativeTypes())
00561     {
00562         *mpCellProliferativeTypesFile << time << "\t";
00563     }
00564     if (p_config->GetOutputCellVariables())
00565     {
00566         *mpCellVariablesFile << time << "\t";
00567     }
00568     if (p_config->GetOutputCellCyclePhases())
00569     {
00570         *mpCellCyclePhasesFile << time << "\t";
00571     }
00572     if (p_config->GetOutputCellAges())
00573     {
00574         *mpCellAgesFile << time << "\t";
00575     }
00576 
00577     GenerateCellResultsAndWriteToFiles();
00578 
00579     // Write logged cell data if required
00580     if (p_config->GetOutputCellIdData())
00581     {
00582         WriteCellIdDataToFile();
00583     }
00584 }
00585 
00586 template<unsigned DIM>
00587 void AbstractTissue<DIM>::WriteCellIdDataToFile()
00588 {
00589     // Write time to file
00590     *mpCellIdFile << SimulationTime::Instance()->GetTime();
00591 
00592     for (typename AbstractTissue<DIM>::Iterator cell_iter = Begin();
00593          cell_iter != End();
00594          ++cell_iter)
00595     {
00596         unsigned cell_id = cell_iter->GetCellId();
00597         unsigned location_index = mCellLocationMap[&(*cell_iter)];
00598         *mpCellIdFile << " " << cell_id << " " << location_index;
00599 
00600         c_vector<double, DIM> coords = GetLocationOfCellCentre(*cell_iter);
00601         for (unsigned i=0; i<DIM; i++)
00602         {
00603             *mpCellIdFile << " " << coords[i];
00604         }
00605     }
00606     *mpCellIdFile << "\n";
00607 }
00608 
00609 
00611 // Explicit instantiation
00613 
00614 template class AbstractTissue<1>;
00615 template class AbstractTissue<2>;
00616 template class AbstractTissue<3>;

Generated by  doxygen 1.6.2