Jump to content

Combining strings in a genetics script


Go to solution Solved by Christian F.,

Recommended Posts

I am currently developing an online horse game where players can breed horses using real-to-life genetics. In the past, I have distributed the alleles in separate columns (allele 1 and allele two, for each locus), but this time I'm having to work with another developer's code which places the two alleles under one table column (so at the locus agouti, for example [which is a column], it displays two alleles in the form of AA). 

 

I've now come to having to code the breeding script, which combines the genetics for both the sire and the dam of the resulting foal. But, I've run into a problem - since I could in the past pull each half of the gene from each parent, and combine randomly to come up with genetically accurate offspring, I'm not to sure how to go about it with this new database structure. If all the genes had one letter alleles it would be fine - however, some genes have two, or even three characters in the allele code (For example, Rnr is one allele variety for the Roan gene and). How, then, can I combine the genes of the parents, explode them (like I normally would) into separate strings, then recombine them into one? I cannot clear the database and start over - there are paying members already training and showing their horses and it would be unfair to delete and start over.

 

Hopefully that's somewhat clear - I'm sorry if there's too much technical genetics jargon!

Link to comment
https://forums.phpfreaks.com/topic/275133-combining-strings-in-a-genetics-script/
Share on other sites

I haven't started the breeding script yet, but here's a screenshot of what the database tables look like with the genes. 

 

So, essentially what I'd need to do is determine a random combination for the offspring from the genes of the parents (parents being shown as one row).

 

fF7yIWC.png

I think you are just trying to explode the strings by character?

 

str_split will explode a string into an array. I think you can just reference a string as an array as well, although I haven't tried it in a while.

http://php.net/manual/en/function.str-split.php

I think you are just trying to explode the strings by character?

 

str_split will explode a string into an array. I think you can just reference a string as an array as well, although I haven't tried it in a while.

http://php.net/manual/en/function.str-split.php

 

That may just do the trick! I'll have to try it. Thankfully it allows me to set a parameter for the length of the split - meaning, I can split the locus no matter how many letters in each allele.

  • Solution

Assuming that all of the pairs are comprised of alleles which are at most 1 character different in length, and that the first allele is always the longest one, this should work:

// First we get the length of the string containing the locus.
$len = mb_strlen ($locus, 'utf-8');

// Then we divide it by two, and round up, to get the length of the first allele.
$len = ceil ($len/2);

// Then split the string into two parts.
$alleles = str_split ($locus, $len);
Edited by Christian F.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.