AbstractTissue.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2009
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(const std::vector<TissueCell>& rCells,
00034                                     const std::vector<unsigned> locationIndices)
00035     : mCells(rCells.begin(), rCells.end()),
00036       mTissueContainsMesh(false)
00037 {
00038     // There must be at least one cell
00039     assert(mCells.size() > 0);
00040 
00041     if (!locationIndices.empty())
00042     {
00043         // There must be a one-one correspondence between cells and location indices
00044         if (mCells.size() != locationIndices.size())
00045         {
00046             std::stringstream ss;
00047             ss << "There is not a one-one correspondence between cells and location indices";
00048             EXCEPTION(ss.str());
00049         }
00050     }
00051 
00052     // Set up the map between location indices and cells
00053     std::list<TissueCell>::iterator it = mCells.begin();
00054     for (unsigned i=0; it != mCells.end(); ++it, ++i)
00055     {
00056         unsigned index = i;
00057         if (!locationIndices.empty())
00058         {
00059             // Assume that the ordering matches
00060             index = locationIndices[i];
00061         }
00062         mLocationCellMap[index] = &(*it);
00063         mCellLocationMap[&(*it)] = index;
00064     }
00065 
00066     // Initialise cell counts to zero
00067     for (unsigned i=0; i<NUM_CELL_MUTATION_STATES; i++)
00068     {
00069         mCellMutationStateCount[i] = 0;
00070     }
00071     for (unsigned i=0; i<NUM_CELL_TYPES; i++)
00072     {
00073         mCellTypeCount[i] = 0;
00074     }
00075     for (unsigned i=0; i<5; 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();
00085         iter != mCells.end();
00086         ++iter)
00087     {
00088         iter->InitialiseCellCycleModel();
00089     }
00090 }
00091 
00092 template<unsigned DIM>
00093 std::list<TissueCell>& AbstractTissue<DIM>::rGetCells()
00094 {
00095     return mCells;
00096 }
00097 
00098 template<unsigned DIM>
00099 bool AbstractTissue<DIM>::HasMesh()
00100 {
00101     return mTissueContainsMesh;
00102 }
00103 
00104 template<unsigned DIM>
00105 unsigned AbstractTissue<DIM>::GetNumRealCells()
00106 {
00107     unsigned counter = 0;
00108     for (typename AbstractTissue<DIM>::Iterator cell_iter=this->Begin(); cell_iter!=this->End(); ++cell_iter)
00109     {
00110         counter++;
00111     }
00112     return counter;
00113 }
00114 
00115 template<unsigned DIM>
00116 void AbstractTissue<DIM>::SetCellAncestorsToNodeIndices()
00117 {
00118     for (typename AbstractTissue<DIM>::Iterator cell_iter = this->Begin(); cell_iter!=this->End(); ++cell_iter)
00119     {
00120         cell_iter->SetAncestor( mCellLocationMap[&(*cell_iter)] );
00121     }
00122 }
00123 
00124 template<unsigned DIM>
00125 std::set<unsigned> AbstractTissue<DIM>::GetCellAncestors()
00126 {
00127     std::set<unsigned> remaining_ancestors;
00128     for (typename AbstractTissue<DIM>::Iterator cell_iter=this->Begin(); cell_iter!=this->End(); ++cell_iter)
00129     {
00130         remaining_ancestors.insert(cell_iter->GetAncestor());
00131     }
00132     return remaining_ancestors;
00133 }
00134 
00135 template<unsigned DIM>
00136 c_vector<unsigned, NUM_CELL_MUTATION_STATES> AbstractTissue<DIM>::GetCellMutationStateCount()
00137 {
00138     return mCellMutationStateCount;
00139 }
00140 
00141 template<unsigned DIM>
00142 c_vector<unsigned, NUM_CELL_TYPES> AbstractTissue<DIM>::GetCellTypeCount()
00143 {
00144     return mCellTypeCount;
00145 }
00146 
00147 template<unsigned DIM>
00148 c_vector<unsigned, 5> AbstractTissue<DIM>::GetCellCyclePhaseCount()
00149 {
00150     return mCellCyclePhaseCount;
00151 }
00152 
00153 template<unsigned DIM>
00154 bool AbstractTissue<DIM>::IsGhostNode(unsigned index)
00155 {
00156     return false;
00157 }
00158 
00159 template<unsigned DIM>
00160 TissueCell& AbstractTissue<DIM>::rGetCellUsingLocationIndex(unsigned index)
00161 {
00162     if (mLocationCellMap[index])
00163     {
00164         return *(mLocationCellMap[index]);
00165     }
00166     else
00167     {
00168         EXCEPTION("Location index input argument does not correspond to a TissueCell");
00169     } 
00170 }
00171 
00172 template<unsigned DIM>
00173 unsigned AbstractTissue<DIM>::GetLocationIndexUsingCell(TissueCell* pCell)
00174 {
00175     return mCellLocationMap[pCell];
00176 }
00177 
00178 
00180 //                             Output methods                               //
00182 
00183 template<unsigned DIM>
00184 void AbstractTissue<DIM>::WriteMeshToFile(const std::string &rArchiveDirectory, const std::string &rMeshFileName)
00185 {
00186 }
00187 
00188 template<unsigned DIM>
00189 void AbstractTissue<DIM>::CreateOutputFiles(const std::string &rDirectory,
00190                                             bool rCleanOutputDirectory,
00191                                             bool outputCellMutationStates,
00192                                             bool outputCellTypes,
00193                                             bool outputCellVariables,
00194                                             bool outputCellCyclePhases,
00195                                             bool outputCellAncestors)
00196 {
00197     OutputFileHandler output_file_handler(rDirectory, rCleanOutputDirectory);
00198     mpVizNodesFile = output_file_handler.OpenOutputFile("results.viznodes");
00199     mpVizCellTypesFile = output_file_handler.OpenOutputFile("results.vizcelltypes");
00200 
00201     if (outputCellAncestors)
00202     {
00203         mpCellAncestorsFile = output_file_handler.OpenOutputFile("results.vizancestors");
00204     }
00205     if (outputCellMutationStates)
00206     {
00207         mpCellMutationStatesFile = output_file_handler.OpenOutputFile("cellmutationstates.dat");
00208         *mpCellMutationStatesFile << "Time\t Healthy\t Labelled\t APC_1\t APC_2\t BETA_CAT \n";
00209     }
00210     if (outputCellTypes)
00211     {
00212         mpCellTypesFile = output_file_handler.OpenOutputFile("celltypes.dat");
00213     }
00214     if (outputCellVariables)
00215     {
00216         mpCellVariablesFile = output_file_handler.OpenOutputFile("cellvariables.dat");
00217     }
00218     if (outputCellCyclePhases)
00219     {
00220         mpCellCyclePhasesFile = output_file_handler.OpenOutputFile("cellcyclephases.dat");
00221     }
00222 }
00223 
00224 template<unsigned DIM>
00225 void AbstractTissue<DIM>::CloseOutputFiles(bool outputCellMutationStates,
00226                                            bool outputCellTypes,
00227                                            bool outputCellVariables,
00228                                            bool outputCellCyclePhases,
00229                                            bool outputCellAncestors)
00230 {
00231     mpVizNodesFile->close();
00232     mpVizCellTypesFile->close();
00233 
00234     if (outputCellMutationStates)
00235     {
00236         mpCellMutationStatesFile->close();
00237     }
00238     if (outputCellTypes)
00239     {
00240         mpCellTypesFile->close();
00241     }
00242     if (outputCellVariables)
00243     {
00244         mpCellVariablesFile->close();
00245     }
00246     if (outputCellCyclePhases)
00247     {
00248         mpCellCyclePhasesFile->close();
00249     }
00250     if (outputCellAncestors)
00251     {
00252         mpCellAncestorsFile->close();
00253     }
00254 }
00255 
00256 template<unsigned DIM>
00257 void AbstractTissue<DIM>::GenerateCellResults(unsigned locationIndex,
00258                                               bool outputCellMutationStates,
00259                                               bool outputCellTypes,
00260                                               bool outputCellVariables,
00261                                               bool outputCellCyclePhases,
00262                                               bool outputCellAncestors,
00263                                               std::vector<unsigned>& rCellTypeCounter,
00264                                               std::vector<unsigned>& rCellMutationStateCounter,
00265                                               std::vector<unsigned>& rCellCyclePhaseCounter)
00266 {
00267     unsigned colour = STEM_COLOUR;
00268     if (IsGhostNode(locationIndex) == true)
00269     {
00270         colour = INVISIBLE_COLOUR;
00271     }
00272     else
00273     {
00274         TissueCell* p_cell = mLocationCellMap[locationIndex];
00275 
00276         // Cell cycle phase
00277         if (outputCellCyclePhases)
00278         {
00279             switch (p_cell->GetCellCycleModel()->GetCurrentCellCyclePhase())
00280             {
00281                 case G_ZERO_PHASE:
00282                     rCellCyclePhaseCounter[0]++;
00283                     break;
00284                 case G_ONE_PHASE:
00285                     rCellCyclePhaseCounter[1]++;
00286                     break;
00287                 case S_PHASE:
00288                     rCellCyclePhaseCounter[2]++;
00289                     break;
00290                 case G_TWO_PHASE:
00291                     rCellCyclePhaseCounter[3]++;
00292                     break;
00293                  case M_PHASE:
00294                     rCellCyclePhaseCounter[4]++;
00295                     break;
00296                 default:
00297                     NEVER_REACHED;
00298             }
00299         }
00300 
00301         // Cell ancestors
00302         if (outputCellAncestors)
00303         {
00304             colour = p_cell->GetAncestor();
00305             if (colour == UNSIGNED_UNSET)
00306             {
00307                 // Set the file to -1 to mark this case.
00308                 colour = 1;
00309                 *mpCellAncestorsFile << "-";
00310             }
00311             *mpCellAncestorsFile << colour << " ";
00312         }
00313 
00314         // Set colour dependent on cell type
00315         switch (p_cell->GetCellType())
00316         {
00317             case STEM:
00318                 colour = STEM_COLOUR;
00319                 if (outputCellTypes)
00320                 {
00321                     rCellTypeCounter[0]++;
00322                 }
00323                 break;
00324             case TRANSIT:
00325                 colour = TRANSIT_COLOUR;
00326                 if (outputCellTypes)
00327                 {
00328                     rCellTypeCounter[1]++;
00329                 }
00330                 break;
00331             case DIFFERENTIATED:
00332                 colour = DIFFERENTIATED_COLOUR;
00333                 if (outputCellTypes)
00334                 {
00335                     rCellTypeCounter[2]++;
00336                 }
00337                 break;
00338             case APOPTOTIC:
00339                 colour = APOPTOSIS_COLOUR;
00340                 if (outputCellTypes)
00341                 {
00342                     rCellTypeCounter[3]++;
00343                 }
00344                 break;
00345             default:
00346                 NEVER_REACHED;
00347         }
00348 
00349         // Override colours for mutant or labelled cells
00350         CellMutationState mutation = p_cell->GetMutationState();
00351         switch (mutation)
00352         {
00353             case HEALTHY:
00354                 if (outputCellMutationStates)
00355                 {
00356                     rCellMutationStateCounter[0]++;
00357                 }
00358                 break;
00359             case APC_ONE_HIT:
00360                 colour = EARLY_CANCER_COLOUR;
00361                 if (outputCellMutationStates)
00362                 {
00363                     rCellMutationStateCounter[2]++;
00364                 }
00365                 break;
00366             case APC_TWO_HIT:
00367                 colour = LATE_CANCER_COLOUR;
00368                 if (outputCellMutationStates)
00369                 {
00370                     rCellMutationStateCounter[3]++;
00371                 }
00372                 break;
00373             case BETA_CATENIN_ONE_HIT:
00374                 colour = LATE_CANCER_COLOUR;
00375                 if (outputCellMutationStates)
00376                 {
00377                     rCellMutationStateCounter[4]++;
00378                 }
00379                 break;
00380             case LABELLED:
00381                 colour = LABELLED_COLOUR;
00382                 if (outputCellMutationStates)
00383                 {
00384                     rCellMutationStateCounter[1]++;
00385                 }
00386                 break;
00387             default:
00388                 NEVER_REACHED;
00389         }
00390 
00391         if (p_cell->HasApoptosisBegun())
00392         {
00393             // For any type of cell set the colour to this if it is undergoing apoptosis.
00394             colour = APOPTOSIS_COLOUR;
00395         }
00396 
00397         // Write cell variable data to file if required
00398         if ( outputCellVariables && dynamic_cast<AbstractOdeBasedCellCycleModel*>(p_cell->GetCellCycleModel()) )
00399         {
00400             std::vector<double> proteins = (static_cast<AbstractOdeBasedCellCycleModel*>(p_cell->GetCellCycleModel()))->GetProteinConcentrations();
00401 
00402             // Loop over cell positions
00404             //        outputs the location index (either node index or vertex element index)
00405             //        associated with the cell. This is so that this code works for vertex-
00406             //        as well as cell-centre-based tissues (see #827).
00407             for (unsigned i=0; i<DIM; i++)
00408             {
00409                 *mpCellVariablesFile << locationIndex << " ";
00410             }
00411             // Loop over cell variables
00412             for (unsigned i=0; i<proteins.size(); i++)
00413             {
00414                 *mpCellVariablesFile << proteins[i] << " ";
00415             }
00416         }
00417     }
00418     *mpVizCellTypesFile << colour << " ";
00419 }
00420 
00421 template<unsigned DIM>
00422 void AbstractTissue<DIM>::WriteCellResultsToFiles(bool outputCellMutationStates,
00423                                                   bool outputCellTypes,
00424                                                   bool outputCellVariables,
00425                                                   bool outputCellCyclePhases,
00426                                                   bool outputCellAncestors,
00427                                                   std::vector<unsigned>& rCellTypeCounter,
00428                                                   std::vector<unsigned>& rCellMutationStateCounter,
00429                                                   std::vector<unsigned>& rCellCyclePhaseCounter)
00430 {
00431     *mpVizCellTypesFile << "\n";
00432 
00433     if (outputCellAncestors)
00434     {
00435         *mpCellAncestorsFile << "\n";
00436     }
00437 
00438     // Write cell mutation state data to file if required
00439     if (outputCellMutationStates)
00440     {
00441         for (unsigned i=0; i<NUM_CELL_MUTATION_STATES; i++)
00442         {
00443             mCellMutationStateCount[i] = rCellMutationStateCounter[i];
00444             *mpCellMutationStatesFile << rCellMutationStateCounter[i] << "\t";
00445         }
00446         *mpCellMutationStatesFile << "\n";
00447     }
00448 
00449     // Write cell type data to file if required
00450     if (outputCellTypes)
00451     {
00452         for (unsigned i=0; i<NUM_CELL_TYPES; i++)
00453         {
00454             mCellTypeCount[i] = rCellTypeCounter[i];
00455             *mpCellTypesFile << rCellTypeCounter[i] << "\t";
00456         }
00457         *mpCellTypesFile << "\n";
00458     }
00459 
00460     if (outputCellVariables)
00461     {
00462         *mpCellVariablesFile << "\n";
00463     }
00464 
00465     // Write cell cycle phase data to file if required
00466     if (outputCellCyclePhases)
00467     {
00468         for (unsigned i=0; i<5; i++)
00469         {
00470             mCellCyclePhaseCount[i] = rCellCyclePhaseCounter[i];
00471             *mpCellCyclePhasesFile << rCellCyclePhaseCounter[i] << "\t";
00472         }
00473         *mpCellCyclePhasesFile << "\n";
00474     }
00475 }
00476 
00477 template<unsigned DIM>
00478 void AbstractTissue<DIM>::WriteTimeAndNodeResultsToFiles(bool outputCellMutationStates,
00479                                                          bool outputCellTypes,
00480                                                          bool outputCellVariables,
00481                                                          bool outputCellCyclePhases,
00482                                                          bool outputCellAncestors,
00483                                                          std::vector<unsigned>& rCellTypeCounter,
00484                                                          std::vector<unsigned>& rCellMutationStateCounter,
00485                                                          std::vector<unsigned>& rCellCyclePhaseCounter)
00486 {
00487     // Write current simulation time
00488     SimulationTime *p_simulation_time = SimulationTime::Instance();
00489     double time = p_simulation_time->GetTime();
00490 
00491     *mpVizNodesFile << time << "\t";
00492     *mpVizCellTypesFile << time << "\t";
00493 
00494     if (outputCellAncestors)
00495     {
00496         *mpCellAncestorsFile << time << "\t";
00497     }
00498     if (outputCellMutationStates)
00499     {
00500         *mpCellMutationStatesFile << time << "\t";
00501     }
00502     if (outputCellTypes)
00503     {
00504         *mpCellTypesFile << time << "\t";
00505     }
00506     if (outputCellVariables)
00507     {
00508         *mpCellVariablesFile << time << "\t";
00509     }
00510     if (outputCellCyclePhases)
00511     {
00512         *mpCellCyclePhasesFile << time << "\t";
00513     }
00514 
00515     // Set up cell type counter
00516     rCellTypeCounter.reserve(mCellTypeCount.size());
00517     for (unsigned i=0; i<NUM_CELL_TYPES; i++)
00518     {
00519         rCellTypeCounter[i] = 0;
00520     }
00521 
00522     // Set up cell mutation state counter
00523     rCellMutationStateCounter.reserve(mCellMutationStateCount.size());
00524     for (unsigned i=0; i<NUM_CELL_MUTATION_STATES; i++)
00525     {
00526         rCellMutationStateCounter[i] = 0;
00527     }
00528 
00529     // Set up cell cycle phase counter
00530     rCellCyclePhaseCounter.reserve(5);
00531     for (unsigned i=0; i<5; i++)
00532     {
00533         rCellCyclePhaseCounter[i] = 0;
00534     }
00535 
00536     // Write node data to file
00537     for (unsigned node_index=0; node_index<GetNumNodes(); node_index++)
00538     {
00539         // Write node data to file
00540         if ( !(GetNode(node_index)->IsDeleted()) )
00541         {
00542             const c_vector<double,DIM>& position = GetNode(node_index)->rGetLocation();
00543 
00544             for (unsigned i=0; i<DIM; i++)
00545             {
00546                 *mpVizNodesFile << position[i] << " ";
00547             }
00548         }
00549     }
00550     *mpVizNodesFile << "\n";
00551 }
00552 
00553 
00555 // Explicit instantiation
00557 
00558 template class AbstractTissue<1>;
00559 template class AbstractTissue<2>;
00560 template class AbstractTissue<3>;

Generated on Wed Mar 18 12:51:49 2009 for Chaste by  doxygen 1.5.5