Skip to content

Commit db44c9a

Browse files
committed
Simulations
1 parent 3064ccc commit db44c9a

File tree

3 files changed

+79
-17
lines changed

3 files changed

+79
-17
lines changed

src/simulation/celltree.cpp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77

88
#include "celltree.h"
9-
#include <fstream>
109

1110
CellTree::CellTree(double K,
1211
double migrationRate,
@@ -140,6 +139,7 @@ void CellTree::migrate()
140139
{
141140
assert(t == currentNrAnatomicalSites);
142141
Node v_t = _G.addNode();
142+
_anatomicalSiteMap[v_t] = t;
143143
_indexToVertexG.push_back(v_t);
144144
_G.addArc(_indexToVertexG[s], v_t);
145145

@@ -152,7 +152,7 @@ void CellTree::migrate()
152152
}
153153
}
154154

155-
void CellTree::simulate()
155+
bool CellTree::simulate()
156156
{
157157
std::uniform_real_distribution<> unif(0, 1);
158158

@@ -239,7 +239,13 @@ void CellTree::simulate()
239239
++_generation;
240240
}
241241

242-
constructCloneTree();
242+
if (_nrExtantCells > 0)
243+
{
244+
constructCloneTree();
245+
return true;
246+
}
247+
248+
return false;
243249
}
244250

245251
MigrationGraph CellTree::getMigrationGraph() const
@@ -357,7 +363,10 @@ void CellTree::constructCloneTree()
357363
for (ArcIt a(newT); a != lemon::INVALID; ++a)
358364
{
359365
int j = arcToCharacter[a];
360-
arcToMutationSet[a] = toOriginalColumns[j];
366+
if (j != -1)
367+
{
368+
arcToMutationSet[a] = toOriginalColumns[j];
369+
}
361370
}
362371

363372
char buf[1024];
@@ -371,13 +380,21 @@ void CellTree::constructCloneTree()
371380
if (v != newRoot)
372381
{
373382
label[v] = "";
374-
for (int j : toOriginalColumns[arcToCharacter[InArcIt(newT, v)]])
383+
if (arcToCharacter[InArcIt(newT, v)] != -1)
375384
{
376-
if (label[v] != "")
377-
label[v] += ";";
378-
snprintf(buf, 1024, "%d", j);
385+
for (int j : toOriginalColumns[arcToCharacter[InArcIt(newT, v)]])
386+
{
387+
if (label[v] != "")
388+
label[v] += ";";
389+
snprintf(buf, 1024, "%d", j);
390+
label[v] += buf;
391+
}
379392
}
380393
}
394+
else
395+
{
396+
label[v] = "GL";
397+
}
381398
if (OutArcIt(newT, v) == lemon::INVALID)
382399
{
383400
IntSet mutations;
@@ -401,6 +418,14 @@ void CellTree::constructCloneTree()
401418
snprintf(buf, 1024, "M%d", s);
402419
anatomicalSiteStr[v] = buf;
403420
}
421+
}
422+
}
423+
424+
for (NodeIt v(newT); v != lemon::INVALID; ++v)
425+
{
426+
if (OutArcIt(newT, v) == lemon::INVALID)
427+
{
428+
label[v] = label[newT.source(InArcIt(newT, v))];
404429
label[v] += "_" + anatomicalSiteStr[v];
405430
invMap[label[v]] = v;
406431
}

src/simulation/celltree.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class CellTree
3030
/// Destructor
3131
~CellTree();
3232

33-
/// Simulate a cell tree
34-
void simulate();
33+
/// Simulate a cell tree. Return true if simulation was successful and false otherwise.
34+
bool simulate();
3535

3636
/// Return migration graph
3737
MigrationGraph getMigrationGraph() const;

src/simulation/simulationmain.cpp

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "utils.h"
99
#include "simulation/celltree.h"
1010
#include <lemon/arg_parser.h>
11+
#include <fstream>
1112

1213
int main(int argc, char** argv)
1314
{
@@ -16,24 +17,60 @@ int main(int argc, char** argv)
1617
double mutFreqThreshold = 0.05;
1718
double migrationRate = 0.000001;
1819
double detectionProb = 7e-8;
20+
std::string filenameColorMap;
21+
std::string outputDirectory = ".";
1922

2023
lemon::ArgParser ap(argc, argv);
2124
ap.refOption("s", "Random number generator seed (default: 5)", seed, false)
25+
.refOption("c", "Color map file", filenameColorMap, true)
2226
.refOption("K", "Carrying capacity (default: 10e4)", K)
2327
.refOption("f", "Mutation frequency threshold (default: 0.05)", mutFreqThreshold)
2428
.refOption("m", "Migration rate (default: 0.000001)", migrationRate)
25-
.refOption("d", "Detection probability (default: 7e-8)", detectionProb);
29+
.refOption("d", "Detection probability (default: 7e-8)", detectionProb)
30+
.refOption("o", "Output directory (default: '.')", outputDirectory);
31+
ap.parse();
32+
33+
char buf[1024];
34+
35+
// Read color map
36+
StringToIntMap colorMap;
37+
std::ifstream inColorMap(filenameColorMap.c_str());
38+
if (!inColorMap.good())
39+
{
40+
std::cerr << "Could not open '" << filenameColorMap << "' for reading" << std::endl;
41+
return 1;
42+
}
43+
if (!BaseTree::readColorMap(inColorMap, colorMap))
44+
{
45+
return 1;
46+
}
2647

2748
g_rng = std::mt19937(seed);
28-
// for (int i = 0; i < 100; ++i)
49+
CellTree cellTree(K, migrationRate, detectionProb, mutFreqThreshold);
50+
if (cellTree.simulate())
2951
{
30-
CellTree cellTree(K, migrationRate, detectionProb, mutFreqThreshold);
31-
cellTree.simulate();
32-
3352
const NonBinaryCloneTree& T = cellTree.getCloneTree();
34-
StringToIntMap colorMap = T.generateColorMap();
53+
MigrationGraph G = cellTree.getMigrationGraph();
54+
55+
snprintf(buf, 1024, "%s/T_seed%d.dot", outputDirectory.c_str(), seed);
56+
std::ofstream outT_dot(buf);
57+
T.writeDOT(outT_dot, colorMap);
58+
outT_dot.close();
59+
60+
snprintf(buf, 1024, "%s/T_seed%d.tree", outputDirectory.c_str(), seed);
61+
std::ofstream outT_tree(buf);
62+
T.write(outT_tree);
63+
outT_tree.close();
64+
65+
snprintf(buf, 1024, "%s/G_seed%d.dot", outputDirectory.c_str(), seed);
66+
std::ofstream outG_dot(buf);
67+
G.writeDOT(outG_dot, colorMap);
68+
outG_dot.close();
3569

36-
T.writeDOT(std::cout, colorMap);
70+
snprintf(buf, 1024, "%s/G_seed%d.tree", outputDirectory.c_str(), seed);
71+
std::ofstream outG_tree(buf);
72+
G.write(outG_tree);
73+
outG_tree.close();
3774
}
3875

3976
return 0;

0 commit comments

Comments
 (0)