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); }
private void menuClick_FileOpen(object sender, RoutedEventArgs args) { bool bSuccessfullyLoaded = false; var dialog = new WinForms.OpenFileDialog(); dialog.Filter = "Personal Genome Explorer files|*.PersonalGenomeData|All files (*.*)|*.*"; if (dialog.ShowDialog() == WinForms.DialogResult.OK) { // If we had a file stream open for saving the current document, close it now to ensure that it doesn't prevent us from opening the user's new file. if (App.documentSaveStream != null) { App.documentSaveStream.Close(); App.documentSaveStream = null; } // Open the chosen file. using (var fileStream = dialog.OpenFile()) { int TryIndex = 0; while (true) { // Seek to the beginning of the file. fileStream.Seek(0, SeekOrigin.Begin); // Try a blank password before prompting the user. string password = ""; if (TryIndex > 0) { // Prompt the user for the genome file's password. var passwordWindow = new PasswordWindow(); passwordWindow.Owner = this; passwordWindow.ShowDialog(); password = passwordWindow.password; if (!passwordWindow.bOkPressed) { // The user cancelled the password dialog, abort the open. break; } } // Try to load the genome from the chosen file. GenomeLoadResult result = IndividualGenomeDatabase.Load(fileStream, password, ref App.document); // If there was an error, display the appropriate dialog. if (result == GenomeLoadResult.IncorrectPassword) { if (TryIndex > 0) { // If the user entered the wrong password, give them another chance to enter it. WinForms.MessageBox.Show("The file cannot be decrypted with that password.", "Incorrect Password"); } } else if (result == GenomeLoadResult.UnrecognizedFile) { WinForms.MessageBox.Show("The file doesn't appear to be a valid PersonalGenomeData file.", "Unrecognized file"); break; } else { bSuccessfullyLoaded = true; break; } ++TryIndex; } ; } } if (bSuccessfullyLoaded) { // Reanalyze after loading the data. InitAnalysis(); } }