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); }