public void Randomize(string population) { var random = new Random(); var newSNPToGenotypeMap = new Dictionary <string, SNPGenotype>(); foreach (var pair in SNPDatabaseManager.localDatabase.snpToInfoMap) { // Chose a random genotype, weighted according to the specified population frequency. var randomFraction = random.NextDouble(); foreach (var genotypeInfo in pair.Value.genotypes) { // Determine the frequency for this genotype in the specified population. var populationFrequency = 1.0 / (double)pair.Value.genotypes.Length; if (genotypeInfo.populationFrequencies.ContainsKey(population)) { populationFrequency = genotypeInfo.populationFrequencies[population]; } // Randomly decide whether to use this genotype. if (randomFraction > populationFrequency) { randomFraction -= populationFrequency; } else { var newValue = new SNPGenotype(); newValue.genotype = genotypeInfo.genotype; newValue.orientation = pair.Value.orientation; newSNPToGenotypeMap.Add(pair.Key, newValue); break; } } } snpToGenotypeMap = newSNPToGenotypeMap; }
public static SNPGenotype Read(BinaryReader reader) { var result = new SNPGenotype(); result.orientation = (Orientation)reader.ReadByte(); result.genotype = DiploidGenotype.Read(reader); return(result); }
public static SNPGenotype ReadLegacy(BinaryReader reader) { var result = new SNPGenotype(); var legacyGenotype = DiploidGenotype.Read(reader); var legacyOrientation = reader.ReadByte(); result.orientation = Orientation.Plus; result.genotype = DiploidGenotype.Read(reader); return(result); }
public void AddSNPValue(string id, SNPGenotype value) { // Always store the SNP value with the lower-case SNP ID. id = id.ToLowerInvariant(); if (snpToGenotypeMap.ContainsKey(id)) { // If there SNP value is already in the database, ensure this data doesn't conflict with it. SNPGenotype oldValue = snpToGenotypeMap[id]; Debug.Assert(oldValue.Equals(value)); } else { // Add the SNP value to the database. snpToGenotypeMap.Add(id, value); } }
public IndividualGenomeDatabase Read() { // Initialize the database. var database = new IndividualGenomeDatabase(); // Read the table of SNP genotypes. tableReader.Read( delegate(string[] columns) { if (columns.Length == 4) { // Parse a column of this format: rs# chromosome position genotype var snpId = columns[0]; var snpValue = new SNPGenotype(); snpValue.genotype = DNA.StringToDiploidGenotype(columns[3]); snpValue.orientation = Orientation.Plus; // Add the SNP genotype to the database. database.AddSNPValue(snpId, snpValue); } }); return(database); }
public IndividualGenomeDatabase Read() { // Initialize the database. var database = new IndividualGenomeDatabase(); // Read the table of SNP genotypes. tableReader.Read( delegate(string[] columns) { if (columns.Length == 6) { // Parse a column of this format: rs4477212,A/G,1,72017,+,AA var snpId = columns[0]; var snpValue = new SNPGenotype(); snpValue.genotype = DNA.StringToDiploidGenotype(columns[5]); snpValue.orientation = DNA.StringToOrientation(columns[4]); // Add the SNP genotype to the database. database.AddSNPValue(snpId, snpValue); } }); return(database); }
public static GenomeLoadResult Load(Stream stream, string password, ref IndividualGenomeDatabase outResult) { var reader = new BinaryReader(stream); // Read the file magic and version. char[] fileMagic = reader.ReadChars(referenceFileMagic.Length); // If the file doesn't have the expected magic header, abort and return an error. bool bFileHasMagic = Utilities.ArrayCompare(fileMagic, referenceFileMagic); bool bFileHasLegacyMagic = Utilities.ArrayCompare(fileMagic, referenceFileMagicLegacy); if (!bFileHasMagic && !bFileHasLegacyMagic) { return(GenomeLoadResult.UnrecognizedFile); } // The encryption system doesn't handle short passwords gracefully, so extend them to 16 characters with spaces. password = password.PadRight(16, ' '); // Read the saved password hash from the file, and compare it to the hash of the password we're trying to load with. // This can't be done by saving some magic header inside the encrypted part of the file, as the CryptoStream will crash when decrypting with the wrong password. byte[] passwordASCII = ASCIIEncoding.ASCII.GetBytes(password); byte[] passwordHash = (new SHA512Managed()).ComputeHash(passwordASCII); byte[] savedPasswordHash = reader.ReadBytes(passwordHash.Length); if (!Utilities.ArrayCompare(passwordHash, savedPasswordHash)) { return(GenomeLoadResult.IncorrectPassword); } // Read the initialization vector. byte[] initializationVector = reader.ReadBytes(16); // Create a decrypting stream to read the encrypted data from the file. reader = new BinaryReader( new CryptoStream( stream, Rijndael.Create().CreateDecryptor( ASCIIEncoding.ASCII.GetBytes(password), initializationVector), CryptoStreamMode.Read)); // Create the genome database that's about to be loaded. outResult = new IndividualGenomeDatabase(); outResult.password = password; // Read SNPs until the end of the file. while (stream.Position < stream.Length) { // Read a SNP ID and value pair, and add them to the database. var Key = reader.ReadString(); SNPGenotype Genotype; if (bFileHasLegacyMagic) { Genotype = SNPGenotype.ReadLegacy(reader); } else { Genotype = SNPGenotype.Read(reader); } outResult.AddSNPValue(Key, Genotype); } return(GenomeLoadResult.Success); }