Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
GeneGenerator
//GeneGenerator 1.0 //Copyright (C) 2013 nickolas360 /* * How to use GeneGenerator: * * First, you must define all genes you want to use. * * The format is: * (Heterozygous Genotype)/Dominant Phenotype/Recessive Phenotype/Heterozygous Phenotype * The Heterozygous Phenotype is not necessary if it is the same as the Dominant Phenotype. * * Only input the heterozygous genotype at the beginning! The program will assume that the * first letter is the dominant allele, and the second letter is the recessive one. * * For example, to define a gene Tt, with TT and Tt being tall and tt being short, you would type: * Tt/Tall/Short * * To define a gene with codominance BY, with BB being blue, YY being yellow, and BY being spotted, type: * BY/Blue/Yellow/Spotted * (You could switch the order of B and Y, as there is no "dominant" gene) * * Example definitions for blood: * AO/A blood/O blood * BO/B blood/O blood * AB/A blood/B blood/AB blood * * Example definitions for colorblindness (sex-linked): * Cc/Normal vision/Colorblind <-- For female * CY/Normal vision/ <-- For male; include slash at the end (YY will never happen) * cY/Colorblind/ <-- For male; include slash at the end (YY will never happen) */ /* * Next, specify any parameters you would like. * * Type "sortcount" to sort numbers of genotypes and phenotypes by how many of each kind * exist, rather than organizing from most dominant to most recessive. * * Type "punnet" to generate a punnet square in addition to genotype and phenotype information. */ /* * Next, type "start" * * Input two genotype combinations. * These are just strings of genotypes. * For example, AaBbCc or TtBYrrMm. * * The two genotype combinations must be the same number of genotypes. */ /* * The information should be displayed. */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Rextester { public class Program { private static List<GenotypeInfo> definedGenotypes = new List<GenotypeInfo>(); private static string[,] rawResults; private static Dictionary<string, Tuple<int, int>> resultsCount = new Dictionary<string, Tuple<int, int>>(); private static Dictionary<string, Tuple<int, int>> resultsPhenotypeCount = new Dictionary<string, Tuple<int, int>>(); private static List<KeyValuePair<string, Tuple<int, int>>> sortedResultsCount; private static List<KeyValuePair<string, Tuple<int, int>>> sortedResultsPhenotypeCount; private static bool sortByCount = false; private static bool punnetSquare = false; public static void Main(string[] args) { //Get user input. string input = Console.ReadLine(); //If input is "start", stop acceping gene definitions. while (input != "START" && input != "Start" && input != "start") { if (input == "SORTCOUNT" || input == "Sortcount" || input == "sortCount" || input == "sortcount") { //If input is "sortcount", set the sortByCount flag. sortByCount = true; } else if (input == "PUNNET" || input == "Punnet" || input == "punnet") { //If input is "punnet", set the punnetSquare flag. punnetSquare = true; } else { //Otherwise, parse gene definition. string heterozygousGenotype; string dominantPhenotype; string recessivePhenotype; string heterozygousPhenotype; //Get heterozygous genotype (already entered). heterozygousGenotype = input.Substring(0, input.IndexOf('/')); input = input.Substring(input.IndexOf('/') + 1); //Get dominant phenotype. dominantPhenotype = input.Substring(0, input.IndexOf('/')); input = input.Substring(input.IndexOf('/') + 1); //If there is only one more phenotype definition, gene is normal dominant-recessive. //Otherwise, there is a incomplete/co-dominance phenotype. if (input.IndexOf('/') == -1) { recessivePhenotype = input; heterozygousPhenotype = dominantPhenotype; } else { recessivePhenotype = input.Substring(0, input.IndexOf('/')); input = input.Substring(input.IndexOf('/') + 1); heterozygousPhenotype = input; } //Generate dominant and recessive genotypes. string dominantGenotype = heterozygousGenotype[0].ToString() + heterozygousGenotype[0].ToString(); string recessiveGenotype = heterozygousGenotype[1].ToString() + heterozygousGenotype[1].ToString(); //Create instances of GenotypeInfo and add them to the list. definedGenotypes.Add(new GenotypeInfo(dominantGenotype, dominantPhenotype, 0)); definedGenotypes.Add(new GenotypeInfo(recessiveGenotype, recessivePhenotype, 2)); definedGenotypes.Add(new GenotypeInfo(heterozygousGenotype, heterozygousPhenotype, 1)); } //Read user input again. input = Console.ReadLine(); } //Get results. GetResults(); /*Console.ReadKey(true);*/ } public static void GetResults() { //Get genotype combinations. /*Console.WriteLine("Genotype combination 1:");*/ string genotypeCollection1 = Console.ReadLine(); /*Console.WriteLine("Genotype combination 2:");*/ string genotypeCollection2 = Console.ReadLine(); /*Console.Write("\n\n");*/ //Variables. int genotypeCount = genotypeCollection1.Length / 2; string[] genotypes1 = new string[genotypeCount]; string[] genotypes2 = new string[genotypeCount]; char[] firstAlleles1 = new char[genotypeCount]; char[] secondAlleles1 = new char[genotypeCount]; char[] firstAlleles2 = new char[genotypeCount]; char[] secondAlleles2 = new char[genotypeCount]; //Separate genotypes. for (int i = 0; i < genotypeCount; i++) { genotypes1[i] = genotypeCollection1.Substring(i * 2, 2); genotypes2[i] = genotypeCollection2.Substring(i * 2, 2); } //Separate alleles. for (int i = 0; i < genotypeCount; i++) { firstAlleles1[i] = genotypes1[i][0]; secondAlleles1[i] = genotypes1[i][1]; firstAlleles2[i] = genotypes2[i][0]; secondAlleles2[i] = genotypes2[i][1]; } //Get combinations. string[] combinationsTop = GetCombinations(new string[] { string.Empty }, firstAlleles1.Length - 1, firstAlleles1, secondAlleles1); string[] combinationsLeft = GetCombinations(new string[] { string.Empty }, firstAlleles2.Length - 1, firstAlleles2, secondAlleles2); //If punnet square was requested, a raw results array is needed. if (punnetSquare) { rawResults = new string[combinationsTop.Length, combinationsLeft.Length]; } //Loops for top row * left row (this multiplies like a punnet square) for (int i = 0; i < combinationsLeft.Length; i++) { for (int k = 0; k < combinationsTop.Length; k++) { string singleCrossGenotypes = string.Empty; string fullCrossGenotypes = string.Empty; string singleCrossPhenotypes = string.Empty; string fullCrossPhenotypes = string.Empty; int rank = 0; //Cross each gene and add it to the current full cross. for (int m = 0; m < genotypeCount; m++) { //Cross one gene. singleCrossGenotypes = combinationsTop[k][m].ToString() + combinationsLeft[i][m].ToString(); foreach (GenotypeInfo genotypeInfo in definedGenotypes) { //Locate the instance of GenotypeInfo the crossed genotype corresponds to. if (genotypeInfo.GenotypeMatches(singleCrossGenotypes)) { //Store values. singleCrossGenotypes = genotypeInfo.Genotype; singleCrossPhenotypes = genotypeInfo.Phenotype; //Generate rank. rank += (int)Math.Pow(3, genotypeCount - (m + 1)) * genotypeInfo.Dominance; } } //Add single-gene crosses to the multi-gene ones (and format phenotype strings). fullCrossGenotypes += singleCrossGenotypes; fullCrossPhenotypes += fullCrossPhenotypes == string.Empty ? singleCrossPhenotypes : ", " + singleCrossPhenotypes; } //If punnet square was requested, add genotype combination to the raw results array. if (punnetSquare) { rawResults[i, k] = fullCrossGenotypes; } //If the generated geno/phenotype combination already exists, and one to the count. //If not, create a new dictionary entry and set the count to one. if (resultsCount.ContainsKey(fullCrossGenotypes)) { resultsCount[fullCrossGenotypes] = Tuple.Create( resultsCount[fullCrossGenotypes].Item1 + 1, resultsCount[fullCrossGenotypes].Item2); } else { resultsCount.Add(fullCrossGenotypes, Tuple.Create(1, rank)); } if (resultsPhenotypeCount.ContainsKey(fullCrossPhenotypes)) { resultsPhenotypeCount[fullCrossPhenotypes] = Tuple.Create( resultsPhenotypeCount[fullCrossPhenotypes].Item1 + 1, resultsPhenotypeCount[fullCrossPhenotypes].Item2); } else { resultsPhenotypeCount.Add(fullCrossPhenotypes, Tuple.Create(1, rank)); } } } //Sort arrays by rank (AABBCC...aabbcc, and equivalent phenotypes). sortedResultsCount = resultsCount.ToList(); sortedResultsCount.Sort((firstPair, nextPair) => { return firstPair.Value.Item2.CompareTo(nextPair.Value.Item2); }); sortedResultsPhenotypeCount = resultsPhenotypeCount.ToList(); sortedResultsPhenotypeCount.Sort((firstPair, nextPair) => { return firstPair.Value.Item2.CompareTo(nextPair.Value.Item2); }); //Sort by the number of instances of each geno/phenotype, if requested. if (sortByCount) { sortedResultsCount.Sort((firstPair, nextPair) => { return firstPair.Value.Item1.CompareTo(nextPair.Value.Item1); }); sortedResultsPhenotypeCount.Sort((firstPair, nextPair) => { return firstPair.Value.Item1.CompareTo(nextPair.Value.Item1); }); } //Display information to the user. DisplayInformation(genotypeCount, combinationsTop, combinationsLeft); } public static string[] GetCombinations(string[] currentArray, int allelePosition, char[] firstAlleles, char[] secondAlleles) { if (allelePosition >= 0) { //Double the current array, and to half, and one allele, and to the other half, add the other. string[] newCombinations = new string[currentArray.Length * 2]; for (int i = 0; i < currentArray.Length; i++) { newCombinations[i] = firstAlleles[allelePosition] + currentArray[i]; newCombinations[i + currentArray.Length] = secondAlleles[allelePosition] + currentArray[i]; } //Move the allele position along one and run the function again. return GetCombinations(newCombinations, allelePosition - 1, firstAlleles, secondAlleles); } else { //If the end of the alleles has been reached, return the array. return currentArray; } } public static void DisplayInformation(int numberOfGenotypes, string[] top, string[] left) { //If requested, display a punnet square. if (punnetSquare) { Console.Write(new string(' ', numberOfGenotypes + 1)); foreach (string alleleCombination in top) { Console.Write(alleleCombination + new string(' ', numberOfGenotypes + 1)); } for (int i = 0; i < left.Length; i++) { Console.Write("\n" + left[i] + ' '); for (int k = 0; k < top.Length; k++) { Console.Write(rawResults[i, k] + ' '); } } Console.Write("\n\n"); } //Write out how many of each geno/phenotype combination there are. foreach (KeyValuePair<string, Tuple<int, int>> genotypePair in sortedResultsCount) { Console.WriteLine("-- {0} instance{1} of genotype {2}.", genotypePair.Value.Item1, genotypePair.Value.Item1 > 1 ? "s" : " ", genotypePair.Key); } Console.Write("\n"); foreach (KeyValuePair<string, Tuple<int, int>> phenotypePair in sortedResultsPhenotypeCount) { Console.WriteLine("-- {0} instance{1} of phenotype {2}.", phenotypePair.Value.Item1, phenotypePair.Value.Item1 > 1 ? "s" : " ", phenotypePair.Key); } } } public class GenotypeInfo { public string Genotype { get; set; } public string Phenotype { get; set; } public int Dominance { get; set; } public GenotypeInfo(string genotype, string phenotype, int dominance) { Genotype = genotype; Phenotype = phenotype; Dominance = dominance; } public bool GenotypeMatches(string genotype) { if (genotype == Genotype) { return true; } else if (genotype[1].ToString() + genotype[0].ToString() == Genotype) { return true; } else { return false; } } } }
run
|
edit
|
history
|
help
0
float to short cast overflow
HelloWorld
list copy test
hackton
Singly-linked IList<T> in C#
Problem_binary
Events
Covariance & Contravariance
Triangulo de pascal
Math