WildAshe Posted March 2, 2013 Share Posted March 2, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/275133-combining-strings-in-a-genetics-script/ Share on other sites More sharing options...
AyKay47 Posted March 2, 2013 Share Posted March 2, 2013 How exactly is the data for the alleles being stored (e.g. are they separated by a space or some distinct character)? Quote Link to comment https://forums.phpfreaks.com/topic/275133-combining-strings-in-a-genetics-script/#findComment-1416034 Share on other sites More sharing options...
WildAshe Posted March 2, 2013 Author Share Posted March 2, 2013 There's no separation by space or character, sadly - if it was, it'd be easy enough to use an explode. :/ Quote Link to comment https://forums.phpfreaks.com/topic/275133-combining-strings-in-a-genetics-script/#findComment-1416039 Share on other sites More sharing options...
AyKay47 Posted March 2, 2013 Share Posted March 2, 2013 Showing us a working example would help a lot. Quote Link to comment https://forums.phpfreaks.com/topic/275133-combining-strings-in-a-genetics-script/#findComment-1416042 Share on other sites More sharing options...
WildAshe Posted March 2, 2013 Author Share Posted March 2, 2013 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). Quote Link to comment https://forums.phpfreaks.com/topic/275133-combining-strings-in-a-genetics-script/#findComment-1416052 Share on other sites More sharing options...
teynon Posted March 2, 2013 Share Posted March 2, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/275133-combining-strings-in-a-genetics-script/#findComment-1416053 Share on other sites More sharing options...
WildAshe Posted March 2, 2013 Author Share Posted March 2, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/275133-combining-strings-in-a-genetics-script/#findComment-1416054 Share on other sites More sharing options...
jcbones Posted March 2, 2013 Share Posted March 2, 2013 Yes, you can reference a string by array. $str = 'A+At'; echo $str[1]; // output: + Quote Link to comment https://forums.phpfreaks.com/topic/275133-combining-strings-in-a-genetics-script/#findComment-1416062 Share on other sites More sharing options...
Solution Christian F. Posted March 3, 2013 Solution Share Posted March 3, 2013 (edited) 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 March 3, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/275133-combining-strings-in-a-genetics-script/#findComment-1416105 Share on other sites More sharing options...
WildAshe Posted March 5, 2013 Author Share Posted March 5, 2013 Thanks guys! These are awesome solutions and I will work through the code to see what works best. Quote Link to comment https://forums.phpfreaks.com/topic/275133-combining-strings-in-a-genetics-script/#findComment-1416824 Share on other sites More sharing options...
Christian F. Posted March 6, 2013 Share Posted March 6, 2013 *A wild Ashe appears* You're welcome, glad we could help. Quote Link to comment https://forums.phpfreaks.com/topic/275133-combining-strings-in-a-genetics-script/#findComment-1416920 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.