-
Notifications
You must be signed in to change notification settings - Fork 100
/
PhysiCell_rules.cpp
2381 lines (1910 loc) · 70.6 KB
/
PhysiCell_rules.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
###############################################################################
# If you use PhysiCell in your project, please cite PhysiCell and the version #
# number, such as below: #
# #
# We implemented and solved the model using PhysiCell (Version x.y.z) [1]. #
# #
# [1] A Ghaffarizadeh, R Heiland, SH Friedman, SM Mumenthaler, and P Macklin, #
# PhysiCell: an Open Source Physics-Based Cell Simulator for Multicellu- #
# lar Systems, PLoS Comput. Biol. 14(2): e1005991, 2018 #
# DOI: 10.1371/journal.pcbi.1005991 #
# #
# See VERSION.txt or call get_PhysiCell_version() to get the current version #
# x.y.z. Call display_citations() to get detailed information on all cite-#
# able software used in your PhysiCell application. #
# #
# Because PhysiCell extensively uses BioFVM, we suggest you also cite BioFVM #
# as below: #
# #
# We implemented and solved the model using PhysiCell (Version x.y.z) [1], #
# with BioFVM [2] to solve the transport equations. #
# #
# [1] A Ghaffarizadeh, R Heiland, SH Friedman, SM Mumenthaler, and P Macklin, #
# PhysiCell: an Open Source Physics-Based Cell Simulator for Multicellu- #
# lar Systems, PLoS Comput. Biol. 14(2): e1005991, 2018 #
# DOI: 10.1371/journal.pcbi.1005991 #
# #
# [2] A Ghaffarizadeh, SH Friedman, and P Macklin, BioFVM: an efficient para- #
# llelized diffusive transport solver for 3-D biological simulations, #
# Bioinformatics 32(8): 1256-8, 2016. DOI: 10.1093/bioinformatics/btv730 #
# #
###############################################################################
# #
# BSD 3-Clause License (see https://opensource.org/licenses/BSD-3-Clause) #
# #
# Copyright (c) 2015-2024, Paul Macklin and the PhysiCell Project #
# All rights reserved. #
# #
# Redistribution and use in source and binary forms, with or without #
# modification, are permitted provided that the following conditions are met: #
# #
# 1. Redistributions of source code must retain the above copyright notice, #
# this list of conditions and the following disclaimer. #
# #
# 2. Redistributions in binary form must reproduce the above copyright #
# notice, this list of conditions and the following disclaimer in the #
# documentation and/or other materials provided with the distribution. #
# #
# 3. Neither the name of the copyright holder nor the names of its #
# contributors may be used to endorse or promote products derived from this #
# software without specific prior written permission. #
# #
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" #
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE #
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE #
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE #
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR #
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF #
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS #
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN #
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) #
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE #
# POSSIBILITY OF SUCH DAMAGE. #
# #
###############################################################################
*/
#include "./PhysiCell_rules.h"
namespace PhysiCell{
#ifndef __PhysiCell_rules_cpp__
#define __PhysiCell_rules_cpp__
#endif
Hypothesis_Rule::Hypothesis_Rule()
{
signals_map.clear();
behavior = "none";
base_value = 1.0;
max_value = 10.0;
min_value = 0.1;
signals.resize(0);
responses.resize(0);
half_maxes.resize(0);
hill_powers.resize(0);
applies_to_dead_cells.resize(0);
up_signals.resize(0);
up_half_maxes.resize(0);
up_hill_powers.resize(0);
up_applies_to_dead_cells.resize(0);
down_signals.resize(0);
down_half_maxes.resize(0);
down_hill_powers.resize(0);
down_applies_to_dead_cells.resize(0);
cell_type = "none";
pCell_Definition = NULL;
return;
}
std::string convert_bool_to_response( bool input )
{
if( input )
{ return "increases"; }
return "decreases";
}
/*
double multivariate_Hill_response_function( std::vector<double> signals, std::vector<double> half_maxes , std::vector<double> hill_powers )
{
double temp1 = 0.0;
double temp2 = 0.0;
double temp3 = 0.0;
// create the generalized (s^h), stored in temp1;
for( int j=0 ; j < signals.size(); j++ )
{
temp2 = signals[j]; // s
temp2 /= half_maxes[j]; // s/s_half
temp3 = pow( temp2 , hill_powers[j] ); // (s/s_half)^h
temp1 += temp3;
}
temp2 = temp1; // numerator (S^h)
temp1 += 1.0; // denominator (1+S^h)
temp2 /= temp1; // numerator/denominator = S^h / (1+S^h)
return temp2;
}
double multivariate_linear_response_function( std::vector<double> signals, std::vector<double> min_thresholds , std::vector<double> max_thresholds )
{
double output = 0.0;
for( int j=0 ; j < signals.size(); j++ )
{ output += linear_response_function( signals[j] , min_thresholds[j], max_thresholds[j] ); }
if( output > 1.0 )
{ return 1.0; }
return output;
}
*/
void Hypothesis_Rule::display( std::ostream& os )
{
os << "For cell type " << cell_type << ": " << std::endl;
for( int j=0; j < signals.size(); j++ )
{ os << signals[j] << " " << convert_bool_to_response( responses[j] ) << " " << behavior << std::endl; }
return;
}
void Hypothesis_Rule::reduced_display( std::ostream& os )
{
for( int j=0; j < signals.size(); j++ )
{ os << signals[j] << " " << convert_bool_to_response( responses[j] ) << " " << behavior << std::endl; }
return;
}
void Hypothesis_Rule::detailed_display( std::ostream& os )
{
// os << "For cell type " << cell_type << ": " << std::endl;
os << behavior << " is modulated from " << min_value << " to " << max_value << " with a base value of " << base_value << std::endl;
os << "--------------------------------------------------------" << std::endl;
for( int j=0; j < signals.size(); j++ )
{
os << "\t" << signals[j] << " " << convert_bool_to_response( responses[j] ) << " " << behavior
<< " with half-max " << half_maxes[j] << " and Hill power " << hill_powers[j] << ".";
if( applies_to_dead_cells[j] == true )
{ os << " Rule applies to dead cells."; }
std::cout << std::endl;
}
return;
}
void Hypothesis_Rule::English_detailed_display( std::ostream& os )
{
for( int j=0 ; j < signals.size(); j++ )
{
os << signals[j] << " ";
if( responses[j] == true )
{ os << "increases "; }
else
{ os << "decreases "; }
os << behavior << " from " << base_value << " towards " ;
if( responses[j] == true )
{ os << max_value; }
else
{ os << min_value; }
os << " with a Hill response, with half-max " << half_maxes[j] ;
os << " and Hill power " << hill_powers[j] << ".";
if( applies_to_dead_cells[j] == true )
{ os << " Rule applies to dead cells."; }
os << std::endl;
}
}
void Hypothesis_Rule::English_detailed_display_HTML( std::ostream& os )
{
for( int j=0 ; j < signals.size(); j++ )
{
os << "<li>" << signals[j] << " ";
if( responses[j] == true )
{ os << "increases "; }
else
{ os << "decreases "; }
os << behavior << " from " << base_value << " towards " ;
if( responses[j] == true )
{ os << max_value; }
else
{ os << min_value; }
os << " with a Hill response, with half-max " << half_maxes[j] ;
os << " and Hill power " << hill_powers[j] << ".";
if( applies_to_dead_cells[j] == true )
{ os << " Rule applies to dead cells."; }
os << "</li>" << std::endl;
}
}
void Hypothesis_Rule::English_display( std::ostream& os )
{
for( int j=0 ; j < signals.size(); j++ )
{
os << signals[j] << " ";
if( responses[j] == true )
{ os << "increases "; }
else
{ os << "decreases "; }
os << behavior << std::endl;
}
}
void Hypothesis_Rule::English_display_HTML( std::ostream& os )
{
for( int j=0 ; j < signals.size(); j++ )
{
os << "<li>" << signals[j] << " ";
if( responses[j] == true )
{ os << "increases "; }
else
{ os << "decreases "; }
os << behavior << "</li>" << std::endl;
}
}
void Hypothesis_Rule::add_signal( std::string signal , double half_max , double hill_power , std::string response )
{
// check: is this a valid signal? (is it in the dictionary?)
if( find_signal_index(signal) < 0 )
{
std::cout << "Error! Attempted to add signal " << signal << " which is not in the dictionary." << std::endl;
std::cout << "Either fix your model or add the missing signal to the simulation." << std::endl;
std::cout << "\t\tSee possible fixes at https://github.com/physicell-training/PhysiCell_common_errors\n\n";
exit(-1);
}
// check to see if the signal and response already there
int n = find_signal(signal);
bool bResponse = false; // true if up-regulate, false if down
if( response == "increase" || response == "increases" || response == "promotes" )
{ bResponse = true; }
// if so, then just warn and exit.
if( n > -1 && responses[n] == bResponse)
{
std::cout << "Error! Signal " << signal << " and Response " << response << " were already part of the rule." <<
std::endl;
std::cout << "\t\tSee possible fixes at https://github.com/physicell-training/PhysiCell_common_errors\n\n";
exit(-1);
}
// add the signal;
signals_map[signal] = signals_map.size();
signals.push_back( signal );
half_maxes.push_back( half_max );
hill_powers.push_back( hill_power );
responses.push_back( bResponse );
applies_to_dead_cells.push_back( false );
// separate into up and down for our convenience
if( bResponse == true )
{
up_signals.push_back( signal );
up_half_maxes.push_back( half_max );
up_hill_powers.push_back( hill_power );
up_applies_to_dead_cells.push_back( false );
}
else
{
down_signals.push_back( signal );
down_half_maxes.push_back( half_max );
down_hill_powers.push_back( hill_power );
down_applies_to_dead_cells.push_back( false );
}
return;
}
void Hypothesis_Rule::add_signal( std::string signal , std::string response )
{ return add_signal( signal, 0.5 , 3.0 , response ); }
double Hypothesis_Rule::evaluate( std::vector<double> signal_values , bool dead )
{
// create signals
std::vector<double> up_signal(0,0); // up_signals.size() , 0.0 );
std::vector<double> down_signal(0,0); // down_signals.size() , 0.0 );
bool apply_rule = false;
// need to modify to evaluate if cell is live or if the rule is allowed for dead cells
for( int j=0; j < signal_values.size(); j++ )
{
if( applies_to_dead_cells[j] == true || dead == false )
{
if( responses[j] )
{ up_signal.push_back( signal_values[j]); }
else
{ down_signal.push_back( signal_values[j]); }
apply_rule = true;
}
else
{
// new oin sep 7 , 2022
if( responses[j] )
{ up_signal.push_back( 0.0 ); }
else
{ down_signal.push_back( 0.0 ); }
}
}
// March 27, 2023
// if none of the rules apply, the return an absurdly low value
// to signal that the parameter value shoudl not be written
if( apply_rule == false )
{ return -9e99; }
// up-regulation part
double HU = multivariate_Hill_response_function(up_signal,up_half_maxes,up_hill_powers);
double U = base_value + (max_value-base_value)*HU;
// then the down-regulation part
double DU = multivariate_Hill_response_function(down_signal,down_half_maxes,down_hill_powers);
double output = U + (min_value-U)*DU;
return output;
}
double Hypothesis_Rule::evaluate( std::vector<double> signal_values )
{ return Hypothesis_Rule::evaluate( signal_values , true ); }
double Hypothesis_Rule::evaluate( Cell* pCell )
{
// construct signal vector
std::vector<double> signal_values( signals.size() , 0.0 );
for( int i=0; i < signals.size(); i++ )
{ signal_values[i] = get_single_signal( pCell , signals[i] ); }
// now, get live/dead value
bool dead = (bool) get_single_signal( pCell, "dead" );
double out = evaluate( signal_values , dead );
// new March 27, 2023
// if the rule was found to not apply, then just get the prior value
if( out < -9e90 )
{ out = get_single_behavior( pCell , this->behavior ); }
return out;
}
void Hypothesis_Rule::apply( Cell* pCell )
{
// evaluate the rule
double param = evaluate( pCell );
// apply it ot the appropriate behavior
set_single_behavior( pCell , behavior , param );
return;
}
void Hypothesis_Rule::sync_to_cell_definition( Cell_Definition* pCD )
{
if( pCD == NULL )
{ return; }
cell_type = pCD->name;
pCell_Definition = pCD;
// sync base behavior
base_value = get_single_base_behavior(pCD,behavior);
return;
}
void Hypothesis_Rule::sync_to_cell_definition( std::string cell_name )
{ return sync_to_cell_definition( find_cell_definition(cell_name) ); }
int Hypothesis_Rule::find_signal( std::string name )
{
auto search = signals_map.find(name);
if( search == signals_map.end() )
{ return -1; }
return search->second;
}
void Hypothesis_Rule::set_half_max( std::string name , double hm )
{
int n = find_signal( name );
if( n < 0 )
{ return; }
half_maxes[n] = hm;
if( responses[n] == true )
{
for( int m=0; m < up_signals.size(); m++ )
{
if( up_signals[m] == name )
{ up_half_maxes[m] = hm; }
}
}
else
{
for( int m=0; m < down_signals.size(); m++ )
{
if( down_signals[m] == name )
{ down_half_maxes[m] = hm; }
}
}
return;
}
void Hypothesis_Rule::set_hill_power( std::string name , double hp )
{
int n = find_signal( name );
if( n < 0 )
{ return; }
hill_powers[n] = hp;
if( responses[n] == true )
{
for( int m=0; m < up_signals.size(); m++ )
{
if( up_signals[m] == name )
{ up_hill_powers[m] = hp; }
}
}
else
{
for( int m=0; m < down_signals.size(); m++ )
{
if( down_signals[m] == name )
{ down_hill_powers[m] = hp; }
}
}
return;
}
void Hypothesis_Rule::set_response( std::string name , std::string response )
{
int n = find_signal( name );
if( n < 0 )
{ return; }
bool bResponse = false; // true if up-regulate, false if down
if( response == "increase" || response == "increases" || response == "promotes" )
{ bResponse = true; }
// this is already my response? if so exit
if( bResponse == responses[n] )
{ return; }
if( responses[n] == true )
{
// need to switch from up to down
// find current index
int ci = -1;
for( int m=0; m < up_signals.size(); m++ )
{
if( up_signals[m] == name )
{ ci = m; }
}
// swap last inot that position
up_half_maxes[ci] = up_half_maxes.back();
up_hill_powers[ci] = up_hill_powers.back();
up_signals[ci] = up_signals.back();
up_applies_to_dead_cells[ci] = up_applies_to_dead_cells.back();
// reduce size by one
up_half_maxes.pop_back();
up_hill_powers.pop_back();
up_signals.pop_back();
up_applies_to_dead_cells.pop_back();
// move to the other side
down_half_maxes.push_back( half_maxes[n] );
down_hill_powers.push_back( hill_powers[n]);
down_signals.push_back( signals[n] );
down_applies_to_dead_cells.push_back( applies_to_dead_cells[n] );
}
else
{
// need to switch from down to up
// find current index
int ci = -1;
for( int m=0; m < down_signals.size(); m++ )
{
if( down_signals[m] == name )
{ ci = m; }
}
// swap last inot that position
down_half_maxes[ci] = down_half_maxes.back();
down_hill_powers[ci] = down_hill_powers.back();
down_signals[ci] = down_signals.back();
down_applies_to_dead_cells[ci] = up_applies_to_dead_cells.back();
// reduce size by one
down_half_maxes.pop_back();
down_hill_powers.pop_back();
down_signals.pop_back();
down_applies_to_dead_cells.pop_back();
// move to the other side
up_half_maxes.push_back( half_maxes[n] );
up_hill_powers.push_back( hill_powers[n]);
up_signals.push_back( signals[n] );
up_applies_to_dead_cells.push_back( applies_to_dead_cells[n] );
}
responses[n] = bResponse;
return;
}
/* add this to the core library! */
/*
double get_single_base_behavior( Cell_Definition* pCD , std::string name )
{
static int m = microenvironment.number_of_densities();
static int n = cell_definition_indices_by_name.size();
int index = find_behavior_index(name);
if( index < 0 )
{
std::cout << "Warning: attempted to get behavior " << name << " with unknown index " << index << std::endl
<< " I'm ignoring it, but you should fix it!" << std::endl;
return 0.0;
}
// substrate-related behaviors
// first m entries are secretion
static int first_secretion_index = find_behavior_index( microenvironment.density_names[0] + " secretion" ); // 0;
if( index >= first_secretion_index && index < first_secretion_index + m )
{ return pCD->phenotype.secretion.secretion_rates[index-first_secretion_index]; }
// next m entries are secretion targets
static int first_secretion_target_index = find_behavior_index( microenvironment.density_names[0] + " secretion target" ); // m;
if( index >= first_secretion_target_index && index < first_secretion_target_index + m )
{ return pCD->phenotype.secretion.saturation_densities[index-first_secretion_target_index]; }
// next m entries are uptake rates
static int first_uptake_index = find_behavior_index( microenvironment.density_names[0] + " uptake" ); // 2*m;
if( index >= first_uptake_index && index < first_uptake_index + m )
{ return pCD->phenotype.secretion.uptake_rates[index-first_uptake_index]; }
// next m entries are net export rates
static int first_export_index = find_behavior_index( microenvironment.density_names[0] + " export" ); // 3*m;
if( index >= first_export_index && index < first_export_index + m )
{ return pCD->phenotype.secretion.net_export_rates[index-first_export_index]; }
// cycle entry (exit from phase 0) and exit from up to 5 more phases
static int first_cycle_index = find_behavior_index("exit from cycle phase 0" ); // 4*m;
int max_cycle_index = pCD->phenotype.cycle.model().phases.size();
if( max_cycle_index > 6 )
{
max_cycle_index = 6;
std::cout << "Warning: Standardized behaviors only support exit rate from the first 6 phases of a cell cycle!" << std::endl
<< " Ignoring any later phase exit rates." << std::endl;
}
if( index >= first_cycle_index && index < first_cycle_index + 6 )
{
int ind = index - first_cycle_index;
if( ind < max_cycle_index )
{ return pCD->phenotype.cycle.data.exit_rate( ind ); }
return 0.0;
}
static int apoptosis_index = pCD->phenotype.death.find_death_model_index( PhysiCell_constants::apoptosis_death_model );
static int necrosis_index = pCD->phenotype.death.find_death_model_index( PhysiCell_constants::necrosis_death_model );
static int apop_param_index = find_behavior_index( "apoptosis");
static int necr_param_index = find_behavior_index( "necrosis");
// apoptosis
if( index == apop_param_index )
{ return pCD->phenotype.death.rates[apoptosis_index]; }
// necrosis
if( index == necr_param_index )
{ return pCD->phenotype.death.rates[necrosis_index]; }
// migration speed
static int migr_spd_index = find_behavior_index( "migration speed");
if( index == migr_spd_index )
{ return pCD->phenotype.motility.migration_speed; }
// migration bias
static int migr_bias_index = find_behavior_index( "migration bias");
if( index == migr_bias_index )
{ return pCD->phenotype.motility.migration_bias; }
// migration persistence time
static int migr_pt_index = find_behavior_index( "migration persistence time");
if( index == migr_pt_index )
{ return pCD->phenotype.motility.persistence_time; }
// chemotactic sensitivities
static int first_chemotaxis_index = find_behavior_index( "chemotactic response to " + microenvironment.density_names[0] );
if( index >= first_chemotaxis_index && index < first_chemotaxis_index + m )
{ return pCD->phenotype.motility.chemotactic_sensitivities[index-first_chemotaxis_index]; }
// cell-cell adhesion
static int cca_index = find_behavior_index( "cell-cell adhesion" );
if( index == cca_index )
{ return pCD->phenotype.mechanics.cell_cell_adhesion_strength; }
// cell-cell "springs"
static int cca_spring_index = find_behavior_index( "cell-cell adhesion elastic constant" );
if( index == cca_spring_index )
{ return pCD->phenotype.mechanics.attachment_elastic_constant; }
// cell adhesion affinities
static int first_affinity_index = find_behavior_index("adhesive affinity to " + cell_definitions_by_type[0]->name );
if( index >= first_affinity_index && index < first_affinity_index + n )
{ return pCD->phenotype.mechanics.cell_adhesion_affinities[index-first_affinity_index]; }
// max relative maximum adhesion distance
static int max_adh_index = find_behavior_index("relative maximum adhesion distance" );
if( index == max_adh_index )
{ return pCD->phenotype.mechanics.relative_maximum_adhesion_distance; }
// cell-cell repulsion
static int ccr_index = find_behavior_index("cell-cell repulsion" );
if( index == ccr_index )
{ return pCD->phenotype.mechanics.cell_cell_repulsion_strength; }
// cell-BM adhesion
static int cba_index = find_behavior_index("cell-BM adhesion" );
if( index == cba_index )
{ return pCD->phenotype.mechanics.cell_BM_adhesion_strength; }
// cell-BM repulsion
static int cbr_index = find_behavior_index("cell-BM repulsion" );
if( index == cbr_index )
{ return pCD->phenotype.mechanics.cell_BM_repulsion_strength; }
// dead cell phagocytosis
static int dead_phag_index = find_behavior_index("phagocytose dead cell" );
if( index == dead_phag_index )
{ return pCD->phenotype.cell_interactions.dead_phagocytosis_rate; }
// phagocytosis of each live cell type
static int first_phagocytosis_index = find_behavior_index( "phagocytose " + cell_definitions_by_type[0]->name );
if( index >= first_phagocytosis_index && index < first_phagocytosis_index + n )
{ return pCD->phenotype.cell_interactions.live_phagocytosis_rates[index-first_phagocytosis_index]; }
// attack of each live cell type
static int first_attack_index = find_behavior_index( "attack " + cell_definitions_by_type[0]->name );
if( index >= first_attack_index && index < first_attack_index + n )
{ return pCD->phenotype.cell_interactions.attack_rates[index-first_attack_index]; }
// fusion
static int first_fusion_index = find_behavior_index( "fuse to " + cell_definitions_by_type[0]->name );
if( index >= first_fusion_index && index < first_fusion_index + n )
{ return pCD->phenotype.cell_interactions.fusion_rates[index-first_fusion_index]; }
// transformation
static int first_transformation_index = find_behavior_index( "transform to " + cell_definitions_by_type[0]->name );
if( index >= first_transformation_index && index < first_transformation_index + n )
{ return pCD->phenotype.cell_transformations.transformation_rates[index-first_transformation_index]; }
// custom behavior
static int first_custom_ind = find_behavior_index( "custom 0");
static int max_custom_ind = first_custom_ind + pCD->custom_data.variables.size();
if( first_custom_ind >= 0 && index >= first_custom_ind && index < max_custom_ind )
{ return pCD->custom_data.variables[index-first_custom_ind].value; }
return -1;
}
*/
Hypothesis_Ruleset::Hypothesis_Ruleset()
{
cell_type = "none";
pCell_Definition = NULL;
rules.resize(0);
rules_map.clear();
return;
}
void Hypothesis_Ruleset::display( std::ostream& os )
{
os << "Behavioral rules for cell type " << cell_type << ":" << std::endl;
os << "===================================================" << std::endl;
for( int i=0; i < rules.size() ; i++ )
{ rules[i]->reduced_display(os); }
os << std::endl;
return;
}
void Hypothesis_Ruleset::detailed_display( std::ostream& os )
{
os << "Behavioral rules for cell type " << cell_type << ":" << std::endl;
os << "===================================================" << std::endl;
for( int i=0; i < rules.size() ; i++ )
{ rules[i]->detailed_display(os); }
os << std::endl;
return;
}
void Hypothesis_Ruleset::sync_to_cell_definition( Cell_Definition* pCD )
{
pCell_Definition = pCD;
cell_type = pCD->name;
for( int i=0; i < rules.size(); i++ )
{ rules[i]->sync_to_cell_definition(pCD); }
return;
}
Hypothesis_Rule* Hypothesis_Ruleset::add_behavior( std::string behavior , double min_behavior, double max_behavior )
{
// check: is this a valid signal? (is it in the dictionary?)
if( find_behavior_index(behavior) < 0 )
{
std::cout << "Warning! Attempted to add behavior " << behavior << " which is not in the dictionary." << std::endl;
std::cout << "Either fix your model or add the missing behavior to the simulation." << std::endl;
std::cout << "\t\tSee possible fixes at https://github.com/physicell-training/PhysiCell_common_errors\n\n";
exit(-1);
}
// first, check. Is there already a ruleset?
auto search = rules_map.find( behavior );
// if not, add it
if( search == rules_map.end() )
{
Hypothesis_Rule *pHR = new Hypothesis_Rule;
pHR->behavior = behavior;
pHR->sync_to_cell_definition( pCell_Definition );
pHR->min_value = min_behavior;
pHR->max_value = max_behavior;
rules.push_back(pHR);
rules_map[ behavior ] = pHR;
return pHR;
}
// otherwise, edit it
Hypothesis_Rule* pHR = search->second;
/*
// March 28 2023 fix : let's not overwrite eixsting values
pHR->min_value = min_behavior;
pHR->max_value = max_behavior;
*/
return pHR;
}
Hypothesis_Rule* Hypothesis_Ruleset::add_behavior( std::string behavior )
{
double min_behavior = 9e99; // Min behaviour high value
double max_behavior = -9e99; // Max behaviour low value
return Hypothesis_Ruleset::add_behavior( behavior, min_behavior, max_behavior );
}
void Hypothesis_Ruleset::sync_to_cell_definition( std::string cell_name )
{ return sync_to_cell_definition( find_cell_definition(cell_name) ); }
Hypothesis_Rule* Hypothesis_Ruleset::find_behavior( std::string name )
{
auto search = rules_map.find( name);
if( search == rules_map.end() )
{
// std::cout << "Warning! Ruleset does not contain " << name << std::endl;
// std::cout << " Returning NULL." << std::endl;
return NULL;
}
return search->second;
}
Hypothesis_Rule& Hypothesis_Ruleset::operator[]( std::string name )
{
Hypothesis_Rule* pHR = find_behavior(name);
return *pHR;
}
void Hypothesis_Ruleset::apply( Cell* pCell )
{
for( int n=0; n < rules.size() ; n++ )
{ rules[n]->apply( pCell ); }
return;
}
std::unordered_map< Cell_Definition* , Hypothesis_Ruleset > hypothesis_rulesets;
void add_hypothesis_ruleset( Cell_Definition* pCD )
{
auto search = hypothesis_rulesets.find( pCD );
if( search == hypothesis_rulesets.end() )
{
Hypothesis_Ruleset HRS;
HRS.sync_to_cell_definition( pCD );
hypothesis_rulesets[pCD] = HRS;
}
return;
}
void intialize_hypothesis_rulesets( void )
{
hypothesis_rulesets.clear(); // empty();
for( int n; n < cell_definitions_by_index.size() ; n++ )
{
Cell_Definition* pCD = cell_definitions_by_index[n];
add_hypothesis_ruleset(pCD);
}
return;
}
Hypothesis_Ruleset& access_ruleset( Cell_Definition* pCD )
{ return hypothesis_rulesets[pCD]; }
Hypothesis_Ruleset* find_ruleset( Cell_Definition* pCD )
{ return &(hypothesis_rulesets[pCD]); }
void display_hypothesis_rulesets( std::ostream& os )
{
for( int n=0 ; n < cell_definitions_by_index.size() ; n++ )
{ hypothesis_rulesets[ cell_definitions_by_index[n] ].display( os ); }
return;
}
void detailed_display_hypothesis_rulesets( std::ostream& os )
{
for( int n=0 ; n < cell_definitions_by_index.size() ; n++ )
{ hypothesis_rulesets[ cell_definitions_by_index[n] ].detailed_display( os ); }
return;
}
void add_rule( std::string cell_type, std::string signal, std::string behavior , std::string response )
{
Cell_Definition* pCD = find_cell_definition(cell_type);
if( !pCD )
{
std::cout << "Warning: Attempted to add rule for " << cell_type
<< ", but no cell definition found for this type." << std::endl;
exit(-1);
}
Hypothesis_Ruleset* pHRS = find_ruleset( pCD );
if( !pHRS )
{
std::cout << "Warning: Attempted to add rule for " << cell_type
<< ", but no hypothesis ruleset found for this type." << std::endl;
exit(-1);
}
if( pHRS->find_behavior(behavior) )
{
if( (*pHRS)[behavior].behavior != behavior )
{ (*pHRS)[behavior].behavior = behavior; std::cout << "wha?" << std::endl; }
}
pHRS->add_behavior(behavior);
(*pHRS)[behavior].add_signal(signal,response);
return;
}
void add_rule( std::string cell_type, std::string signal, std::string behavior , std::string response , bool use_for_dead )
{
Cell_Definition* pCD = find_cell_definition(cell_type);
if( !pCD )
{
std::cout << "Warning: Attempted to add rule for " << cell_type
<< ", but no cell definition found for this type." << std::endl;
exit(-1);
}
Hypothesis_Ruleset* pHRS = find_ruleset( pCD );
if( !pHRS )
{
std::cout << "Warning: Attempted to add rule for " << cell_type
<< ", but no hypothesis ruleset found for this type." << std::endl;
exit(-1);
}
if( pHRS->find_behavior(behavior) )
{
if( (*pHRS)[behavior].behavior != behavior )
{ (*pHRS)[behavior].behavior = behavior; std::cout << "wha?" << std::endl; }
}
pHRS->add_behavior(behavior);
(*pHRS)[behavior].add_signal(signal,response);
// set dead flag
int n = (*pHRS)[behavior].find_signal(signal);
(*pHRS)[behavior].applies_to_dead_cells[n] = use_for_dead;
return;
}
void set_hypothesis_parameters(std::string cell_type, std::string signal, std::string behavior ,
double half_max, double hill_power )
{
Cell_Definition* pCD = find_cell_definition( cell_type );
if( !pCD )
{
std::cout << "Warning: Attempted to set parameters for "
<< behavior << " modulated by " << signal
<< " in " << cell_type
<< ", but no cell definition found for this type." << std::endl;
exit(-1);
}
if( find_ruleset(pCD) == NULL )
{
std::cout << "Warning: Attempted to set parameters for "
<< behavior << " modulated by " << signal
<< " in " << cell_type
<< ", but no behavior ruleset for this cell type." << std::endl;
exit(-1);
}
if( hypothesis_rulesets[pCD].find_behavior(behavior) == NULL )
{
std::cout << "Warning: Attempted to set parameters for "
<< behavior << " modulated by " << signal
<< " in " << cell_type
<< ", but the cell type has no rules for this behavior." << std::endl;
exit(-1);
// std::cout << "Error. No " << behavior << " rules for " << cell_type << ". Ignoring."<< std::endl; return;
}
if( hypothesis_rulesets[pCD][behavior].find_signal(signal) < 0 )
{
std::cout << "Warning: Attempted to set parameters for "
<< behavior << " modulated by " << signal
<< " in " << cell_type
<< ", but the cell type's behavior does not vary with this signal." << std::endl;
exit(-1);
}
hypothesis_rulesets[pCD][behavior].set_half_max(signal,half_max);
hypothesis_rulesets[pCD][behavior].set_hill_power(signal,hill_power);