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

Generated on Tue Aug 4 16:10:21 2009 for Chaste by  doxygen 1.5.5