Skip to content

Commit 5891bba

Browse files
committed
add phenotypeForTrait()
1 parent 6c1a4f9 commit 5891bba

8 files changed

Lines changed: 89 additions & 16 deletions

File tree

QtSLiM/help/SLiMHelpClasses.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,8 @@
468468
<p class="p6">This method replaces the deprecated method <span class="s1">uniqueMutationsOfType()</span>, while providing additional useful options.<span class="Apple-converted-space">  </span>It is particularly useful for efficient, vectorized assessment of the homozygous versus heterozygous state of the mutations contained by an individual, which is otherwise difficult to assess efficiently.</p>
469469
<p class="p5">– (float)offsetForTrait([Nio&lt;Trait&gt; trait = NULL])</p>
470470
<p class="p6">Returns the individual offset(s) for the trait(s) specified by <span class="s1">trait</span>.<span class="Apple-converted-space">  </span>The traits can be specified as <span class="s1">integer</span> indices of traits in the species, or directly as <span class="s1">Trait</span> objects; <span class="s1">NULL</span> represents all of the traits in the species, in the order in which they were defined.<span class="Apple-converted-space">  </span>Offsets for a given target individual will be returned consecutively in the order in which the traits are specified by <span class="s1">trait</span>.</p>
471+
<p class="p5">– (float)phenotypeForTrait([Nio&lt;Trait&gt; trait = NULL])</p>
472+
<p class="p6">Returns the individual phenotype(s) for the trait(s) specified by <span class="s1">trait</span>.<span class="Apple-converted-space">  </span>The traits can be specified as <span class="s1">integer</span> indices of traits in the species, or directly as <span class="s1">Trait</span> objects; <span class="s1">NULL</span> represents all of the traits in the species, in the order in which they were defined.<span class="Apple-converted-space">  </span>Phenotypes for a given target individual will be returned consecutively in the order in which the traits are specified by <span class="s1">trait</span>.</p>
471473
<p class="p5">+ (void)outputIndividuals([Ns$ filePath = NULL], [logical$ append = F], [Niso&lt;Chromosome&gt;$ chromosome = NULL], [logical$ spatialPositions = T], [logical$ ages = T], [logical$ ancestralNucleotides = F], [logical$ pedigreeIDs = F], [logical$ objectTags = F])</p>
472474
<p class="p6">Output the state of the target vector of individuals in SLiM's own format.<span class="Apple-converted-space">  </span>If the optional parameter <span class="s1">filePath</span> is <span class="s1">NULL</span> (the default), output will be sent to Eidos’s output stream.<span class="Apple-converted-space">  </span>Otherwise, output will be sent to the filesystem path specified by <span class="s1">filePath</span>, overwriting that file if <span class="s1">append</span> if <span class="s1">F</span>, or appending to the end of it if <span class="s1">append</span> is <span class="s1">T</span>.<span class="Apple-converted-space">  </span>This method is quite similar to the <span class="s1">Species</span> method <span class="s1">outputFull()</span>, but (1) it can produce output for any vector of individuals, not always for the entire population; (2) it does not support output in a binary format; (3) it can produce output regarding the genetics for all chromosomes or for just one focal chromosome; and (4) there is no corresponding read method, as r<span class="s1">eadFromPopulationFile()</span> can read the data saved by <span class="s1">outputFull()</span>.</p>
473475
<p class="p6">The <span class="s1">chromosome</span> parameter specifies a focal chromosome for which the genetics of the target individuals will be output.<span class="Apple-converted-space">  </span>If <span class="s1">chromosome</span> is <span class="s1">NULL</span>, all chromosomes will be output; otherwise, <span class="s1">chromosome</span> may specify the focal chromosome with an <span class="s1">integer</span> chromosome id, a <span class="s1">string</span> chromosome symbol, or a <span class="s1">Chromosome</span> object.</p>

SLiMgui/SLiMHelpClasses.rtf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3813,6 +3813,22 @@ This method replaces the deprecated method
38133813
\f4\fs20 .\
38143814
\pard\pardeftab720\li720\fi-446\ri720\sb180\sa60\partightenfactor0
38153815

3816+
\f3\fs18 \cf2 \'96\'a0(float)phenotypeForTrait([Nio<Trait>\'a0trait\'a0=\'a0NULL])\
3817+
\pard\pardeftab397\li547\ri720\sb60\sa60\partightenfactor0
3818+
3819+
\f4\fs20 \cf2 Returns the individual phenotype(s) for the trait(s) specified by
3820+
\f3\fs18 trait
3821+
\f4\fs20 . The traits can be specified as
3822+
\f3\fs18 integer
3823+
\f4\fs20 indices of traits in the species, or directly as
3824+
\f3\fs18 Trait
3825+
\f4\fs20 objects;
3826+
\f3\fs18 NULL
3827+
\f4\fs20 represents all of the traits in the species, in the order in which they were defined. Phenotypes for a given target individual will be returned consecutively in the order in which the traits are specified by
3828+
\f3\fs18 trait
3829+
\f4\fs20 .\
3830+
\pard\pardeftab720\li720\fi-446\ri720\sb180\sa60\partightenfactor0
3831+
38163832
\f3\fs18 \cf2 +\'a0(void)outputIndividuals([Ns$\'a0filePath\'a0=\'a0NULL], [logical$\'a0append\'a0=\'a0F], [Niso<Chromosome>$\'a0chromosome\'a0=\'a0NULL], [logical$\'a0spatialPositions\'a0=\'a0T], [logical$\'a0ages\'a0=\'a0T], [logical$\'a0ancestralNucleotides\'a0=\'a0F], [logical$\'a0pedigreeIDs\'a0=\'a0F], [logical$\'a0objectTags\'a0=\'a0F])\
38173833
\pard\pardeftab397\li547\ri720\sb60\sa60\partightenfactor0
38183834

VERSIONS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ multitrait branch:
6666
remove Mutation method setSelectionCoeff(), autofixing to setEffectForTrait(NULL, )
6767
rename the selectionCoeff property to effect, for both Mutation and Substitution; it changes from float$ to float, and now returns all trait effects; and SLiMgui autofixes this change
6868
remove the old C++ selection_coeff_ and dominance_coeff_ ivars in Mutation and Substitution, and begin the transition over to the new MutationTraitInfo struct
69+
add Individual method -(float)phenotypeForTrait([Nio<Trait> traits = NULL]) to get trait values
6970

7071

7172
version 5.1 (Eidos version 4.1):

core/individual.cpp

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ void Individual::_InitializePerTraitInformation(void)
145145
#endif
146146

147147
trait_info_ = &trait_info_0_;
148-
trait_info_0_.value_ = 0.0;
148+
trait_info_0_.phenotype_ = 0.0;
149149
trait_info_0_.offset_ = traits[0]->DrawIndividualOffset();
150150
}
151151
else if (trait_count == 0)
@@ -174,11 +174,11 @@ void Individual::_InitializePerTraitInformation(void)
174174
#endif
175175

176176
if (!trait_info_)
177-
trait_info_ = static_cast<SLiM_PerTraitInfo *>(malloc(trait_count * sizeof(SLiM_PerTraitInfo)));
177+
trait_info_ = static_cast<IndividualTraitInfo *>(malloc(trait_count * sizeof(IndividualTraitInfo)));
178178

179179
for (int trait_index = 0; trait_index < trait_count; ++trait_index)
180180
{
181-
trait_info_[trait_index].value_ = 0.0;
181+
trait_info_[trait_index].phenotype_ = 0.0;
182182
trait_info_[trait_index].offset_ = traits[trait_index]->DrawIndividualOffset();
183183
}
184184
}
@@ -1761,7 +1761,7 @@ EidosValue_SP Individual::GetProperty(EidosGlobalStringID p_property_id)
17611761

17621762
if (trait)
17631763
{
1764-
return EidosValue_SP(new (gEidosValuePool->AllocateChunk()) EidosValue_Float(trait_info_[trait->Index()].value_));
1764+
return EidosValue_SP(new (gEidosValuePool->AllocateChunk()) EidosValue_Float(trait_info_[trait->Index()].phenotype_));
17651765
}
17661766

17671767
return super::GetProperty(p_property_id);
@@ -2515,7 +2515,7 @@ EidosValue *Individual::GetProperty_Accelerated_TRAIT_VALUE(EidosGlobalStringID
25152515
{
25162516
const Individual *value = individuals_buffer[value_index];
25172517

2518-
float_result->set_float_no_check(value->trait_info_[trait_index].value_, value_index);
2518+
float_result->set_float_no_check(value->trait_info_[trait_index].phenotype_, value_index);
25192519
}
25202520
}
25212521
else
@@ -2527,7 +2527,7 @@ EidosValue *Individual::GetProperty_Accelerated_TRAIT_VALUE(EidosGlobalStringID
25272527
Trait *trait = value->subpopulation_->species_.TraitFromStringID(p_property_id);
25282528
int64_t trait_index = trait->Index();
25292529

2530-
float_result->set_float_no_check(value->trait_info_[trait_index].value_, value_index);
2530+
float_result->set_float_no_check(value->trait_info_[trait_index].phenotype_, value_index);
25312531
}
25322532
}
25332533

@@ -2659,7 +2659,7 @@ void Individual::SetProperty(EidosGlobalStringID p_property_id, const EidosValue
26592659

26602660
if (trait) // ACCELERATED
26612661
{
2662-
trait_info_[trait->Index()].value_ = (slim_effect_t)p_value.FloatAtIndex_NOCAST(0, nullptr);
2662+
trait_info_[trait->Index()].phenotype_ = (slim_effect_t)p_value.FloatAtIndex_NOCAST(0, nullptr);
26632663
return;
26642664
}
26652665

@@ -3087,7 +3087,7 @@ void Individual::SetProperty_Accelerated_TRAIT_VALUE(EidosGlobalStringID p_prope
30873087
{
30883088
const Individual *value = individuals_buffer[value_index];
30893089

3090-
value->trait_info_[trait_index].value_ = source_value;
3090+
value->trait_info_[trait_index].phenotype_ = source_value;
30913091
}
30923092
}
30933093
else
@@ -3096,7 +3096,7 @@ void Individual::SetProperty_Accelerated_TRAIT_VALUE(EidosGlobalStringID p_prope
30963096
{
30973097
const Individual *value = individuals_buffer[value_index];
30983098

3099-
value->trait_info_[trait_index].value_ = (slim_effect_t)source_data[value_index];
3099+
value->trait_info_[trait_index].phenotype_ = (slim_effect_t)source_data[value_index];
31003100
}
31013101
}
31023102
}
@@ -3113,7 +3113,7 @@ void Individual::SetProperty_Accelerated_TRAIT_VALUE(EidosGlobalStringID p_prope
31133113
Trait *trait = value->subpopulation_->species_.TraitFromStringID(p_property_id);
31143114
int64_t trait_index = trait->Index();
31153115

3116-
value->trait_info_[trait_index].value_ = source_value;
3116+
value->trait_info_[trait_index].phenotype_ = source_value;
31173117
}
31183118
}
31193119
else
@@ -3124,7 +3124,7 @@ void Individual::SetProperty_Accelerated_TRAIT_VALUE(EidosGlobalStringID p_prope
31243124
Trait *trait = value->subpopulation_->species_.TraitFromStringID(p_property_id);
31253125
int64_t trait_index = trait->Index();
31263126

3127-
value->trait_info_[trait_index].value_ = (slim_effect_t)source_data[value_index];
3127+
value->trait_info_[trait_index].phenotype_ = (slim_effect_t)source_data[value_index];
31283128
}
31293129
}
31303130
}
@@ -3138,6 +3138,7 @@ EidosValue_SP Individual::ExecuteInstanceMethod(EidosGlobalStringID p_method_id,
31383138
//case gID_countOfMutationsOfType: return ExecuteMethod_Accelerated_countOfMutationsOfType(p_method_id, p_arguments, p_interpreter);
31393139
case gID_haplosomesForChromosomes: return ExecuteMethod_haplosomesForChromosomes(p_method_id, p_arguments, p_interpreter);
31403140
case gID_offsetForTrait: return ExecuteMethod_offsetForTrait(p_method_id, p_arguments, p_interpreter);
3141+
case gID_phenotypeForTrait: return ExecuteMethod_phenotypeForTrait(p_method_id, p_arguments, p_interpreter);
31413142
case gID_relatedness: return ExecuteMethod_relatedness(p_method_id, p_arguments, p_interpreter);
31423143
case gID_sharedParentCount: return ExecuteMethod_sharedParentCount(p_method_id, p_arguments, p_interpreter);
31433144
//case gID_sumOfMutationsOfType: return ExecuteMethod_Accelerated_sumOfMutationsOfType(p_method_id, p_arguments, p_interpreter);
@@ -3353,6 +3354,40 @@ EidosValue_SP Individual::ExecuteMethod_offsetForTrait(EidosGlobalStringID p_met
33533354
}
33543355
}
33553356

3357+
// ********************* - (float)phenotypeForTrait([Nio<Trait> trait = NULL])
3358+
//
3359+
EidosValue_SP Individual::ExecuteMethod_phenotypeForTrait(EidosGlobalStringID p_method_id, const std::vector<EidosValue_SP> &p_arguments, EidosInterpreter &p_interpreter)
3360+
{
3361+
#pragma unused (p_method_id, p_interpreter)
3362+
EidosValue *trait_value = p_arguments[0].get();
3363+
3364+
// get the trait indices, with bounds-checking
3365+
Species &species = subpopulation_->species_;
3366+
std::vector<int64_t> trait_indices;
3367+
species.GetTraitIndicesFromEidosValue(trait_indices, trait_value, "phenotypeForTrait");
3368+
3369+
if (trait_indices.size() == 1)
3370+
{
3371+
int64_t trait_index = trait_indices[0];
3372+
slim_effect_t phenotype = trait_info_[trait_index].phenotype_;
3373+
3374+
return EidosValue_SP(new (gEidosValuePool->AllocateChunk()) EidosValue_Float(phenotype));
3375+
}
3376+
else
3377+
{
3378+
EidosValue_Float *float_result = (new (gEidosValuePool->AllocateChunk()) EidosValue_Float())->reserve(trait_indices.size());
3379+
3380+
for (int64_t trait_index : trait_indices)
3381+
{
3382+
slim_effect_t phenotype = trait_info_[trait_index].phenotype_;
3383+
3384+
float_result->push_float_no_check(phenotype);
3385+
}
3386+
3387+
return EidosValue_SP(float_result);
3388+
}
3389+
}
3390+
33563391
// ********************* - (float)relatedness(object<Individual> individuals, [Niso<Chromosome>$ chromosome = NULL])
33573392
//
33583393
EidosValue_SP Individual::ExecuteMethod_relatedness(EidosGlobalStringID p_method_id, const std::vector<EidosValue_SP> &p_arguments, EidosInterpreter &p_interpreter)
@@ -4209,6 +4244,7 @@ const std::vector<EidosMethodSignature_CSP> *Individual_Class::Methods(void) con
42094244
methods->emplace_back((EidosInstanceMethodSignature *)(new EidosInstanceMethodSignature(gStr_relatedness, kEidosValueMaskFloat))->AddObject("individuals", gSLiM_Individual_Class)->AddArgWithDefault(kEidosValueMaskNULL | kEidosValueMaskInt | kEidosValueMaskString | kEidosValueMaskObject | kEidosValueMaskOptional | kEidosValueMaskSingleton, "chromosome", gSLiM_Chromosome_Class, gStaticEidosValueNULL));
42104245
methods->emplace_back((EidosInstanceMethodSignature *)(new EidosInstanceMethodSignature(gStr_haplosomesForChromosomes, kEidosValueMaskObject, gSLiM_Haplosome_Class))->AddArgWithDefault(kEidosValueMaskNULL | kEidosValueMaskInt | kEidosValueMaskString | kEidosValueMaskObject | kEidosValueMaskOptional, "chromosomes", gSLiM_Chromosome_Class, gStaticEidosValueNULL)->AddInt_OSN("index", gStaticEidosValueNULL)->AddLogical_OS("includeNulls", gStaticEidosValue_LogicalT));
42114246
methods->emplace_back((EidosInstanceMethodSignature *)(new EidosInstanceMethodSignature(gStr_offsetForTrait, kEidosValueMaskFloat))->AddIntObject_ON("trait", gSLiM_Trait_Class, gStaticEidosValueNULL));
4247+
methods->emplace_back((EidosInstanceMethodSignature *)(new EidosInstanceMethodSignature(gStr_phenotypeForTrait, kEidosValueMaskFloat))->AddIntObject_ON("trait", gSLiM_Trait_Class, gStaticEidosValueNULL));
42124248
methods->emplace_back((EidosClassMethodSignature *)(new EidosClassMethodSignature(gStr_setOffsetForTrait, kEidosValueMaskVOID))->AddIntObject_ON("trait", gSLiM_Trait_Class, gStaticEidosValueNULL)->AddNumeric_ON("offset", gStaticEidosValueNULL));
42134249
methods->emplace_back((EidosInstanceMethodSignature *)(new EidosInstanceMethodSignature(gStr_sharedParentCount, kEidosValueMaskInt))->AddObject("individuals", gSLiM_Individual_Class));
42144250
methods->emplace_back(((EidosInstanceMethodSignature *)(new EidosInstanceMethodSignature(gStr_sumOfMutationsOfType, kEidosValueMaskFloat | kEidosValueMaskSingleton))->AddIntObject_S("mutType", gSLiM_MutationType_Class))->DeclareAcceleratedImp(Individual::ExecuteMethod_Accelerated_sumOfMutationsOfType));

core/individual.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ inline slim_pedigreeid_t SLiM_GetNextPedigreeID_Block(int p_block_size)
6565

6666
// This struct contains all information for a single trait in a single individual. In a multitrait
6767
// model, each individual has a pointer to a buffer of these records, providing per-trait information.
68-
typedef struct _SLiM_PerTraitInfo
68+
typedef struct _IndividualTraitInfo
6969
{
70-
slim_effect_t value_; // the phenotypic value for a trait
70+
slim_effect_t phenotype_; // the phenotypic value for a trait
7171
slim_effect_t offset_; // the individual offset combined in to produce a trait value
72-
} SLiM_PerTraitInfo;
72+
} IndividualTraitInfo;
7373

7474
class Individual : public EidosDictionaryUnretained
7575
{
@@ -161,8 +161,8 @@ class Individual : public EidosDictionaryUnretained
161161
// Per-trait information: trait offsets, trait values. If the species has 0 traits, the pointer is
162162
// nullptr; if 1 trait, it points to trait_info_0_ for memory locality and to avoid mallocs; if 2+
163163
// trait, it points to an OWNED malloced buffer.
164-
SLiM_PerTraitInfo trait_info_0_;
165-
SLiM_PerTraitInfo *trait_info_;
164+
IndividualTraitInfo trait_info_0_;
165+
IndividualTraitInfo *trait_info_;
166166

167167
// Continuous space ivars. These are effectively free tag values of type float, unless they are used by interactions.
168168
double spatial_x_, spatial_y_, spatial_z_;
@@ -340,6 +340,7 @@ class Individual : public EidosDictionaryUnretained
340340
static EidosValue_SP ExecuteMethod_Accelerated_countOfMutationsOfType(EidosObject **p_values, size_t p_values_size, EidosGlobalStringID p_method_id, const std::vector<EidosValue_SP> &p_arguments, EidosInterpreter &p_interpreter);
341341
EidosValue_SP ExecuteMethod_haplosomesForChromosomes(EidosGlobalStringID p_method_id, const std::vector<EidosValue_SP> &p_arguments, EidosInterpreter &p_interpreter);
342342
EidosValue_SP ExecuteMethod_offsetForTrait(EidosGlobalStringID p_method_id, const std::vector<EidosValue_SP> &p_arguments, EidosInterpreter &p_interpreter);
343+
EidosValue_SP ExecuteMethod_phenotypeForTrait(EidosGlobalStringID p_method_id, const std::vector<EidosValue_SP> &p_arguments, EidosInterpreter &p_interpreter);
343344
EidosValue_SP ExecuteMethod_relatedness(EidosGlobalStringID p_method_id, const std::vector<EidosValue_SP> &p_arguments, EidosInterpreter &p_interpreter);
344345
EidosValue_SP ExecuteMethod_sharedParentCount(EidosGlobalStringID p_method_id, const std::vector<EidosValue_SP> &p_arguments, EidosInterpreter &p_interpreter);
345346
static EidosValue_SP ExecuteMethod_Accelerated_sumOfMutationsOfType(EidosObject **p_values, size_t p_values_size, EidosGlobalStringID p_method_id, const std::vector<EidosValue_SP> &p_arguments, EidosInterpreter &p_interpreter);

core/slim_globals.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,7 @@ const std::string &gStr_positionsOfMutationsOfType = EidosRegisteredString("posi
13581358
const std::string &gStr_containsMarkerMutation = EidosRegisteredString("containsMarkerMutation", gID_containsMarkerMutation);
13591359
const std::string &gStr_haplosomesForChromosomes = EidosRegisteredString("haplosomesForChromosomes", gID_haplosomesForChromosomes);
13601360
const std::string &gStr_offsetForTrait = EidosRegisteredString("offsetForTrait", gID_offsetForTrait);
1361+
const std::string &gStr_phenotypeForTrait = EidosRegisteredString("phenotypeForTrait", gID_phenotypeForTrait);
13611362
const std::string &gStr_setOffsetForTrait = EidosRegisteredString("setOffsetForTrait", gID_setOffsetForTrait);
13621363
const std::string &gStr_relatedness = EidosRegisteredString("relatedness", gID_relatedness);
13631364
const std::string &gStr_sharedParentCount = EidosRegisteredString("sharedParentCount", gID_sharedParentCount);

core/slim_globals.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,7 @@ extern const std::string &gStr_positionsOfMutationsOfType;
948948
extern const std::string &gStr_containsMarkerMutation;
949949
extern const std::string &gStr_haplosomesForChromosomes;
950950
extern const std::string &gStr_offsetForTrait;
951+
extern const std::string &gStr_phenotypeForTrait;
951952
extern const std::string &gStr_setOffsetForTrait;
952953
extern const std::string &gStr_relatedness;
953954
extern const std::string &gStr_sharedParentCount;
@@ -1426,6 +1427,7 @@ enum _SLiMGlobalStringID : int {
14261427
gID_containsMarkerMutation,
14271428
gID_haplosomesForChromosomes,
14281429
gID_offsetForTrait,
1430+
gID_phenotypeForTrait,
14291431
gID_setOffsetForTrait,
14301432
gID_relatedness,
14311433
gID_sharedParentCount,

0 commit comments

Comments
 (0)