public void ProteinListTest() { // Too few columns const char sep = TextUtil.SEPARATOR_TSV; AssertEx.ThrowsException <LineColNumberedIoException>(() => FastaImporter.ToFasta("PROT1\nPROT2\nPROT2", sep)); // Invalid sequence string invalidSeq = PROTEINLIST_CLIPBOARD_TEXT.Replace("MDAL", "***"); AssertEx.ThrowsException <LineColNumberedIoException>(() => FastaImporter.ToFasta(invalidSeq, sep)); string fastaText = FastaImporter.ToFasta(PROTEINLIST_CLIPBOARD_TEXT, sep); var reader = new StringReader(fastaText); int countProt = 0; string line; while ((line = reader.ReadLine()) != null) { if (line.StartsWith(">")) { countProt++; } else { Assert.IsTrue(FastaSequence.IsExSequence(line.Trim('*'))); } } Assert.AreEqual(3, countProt); }
public static SrmDocument ImportFasta(SrmDocument document, string fastaPath, IProgressMonitor monitor, IdentityPath to, out IdentityPath firstAdded, out IdentityPath nextAdd, out List <PeptideGroupDocNode> peptideGroupsNew) { var importer = new FastaImporter(document, false); using (TextReader reader = File.OpenText(fastaPath)) { peptideGroupsNew = importer.Import(reader, monitor, Helpers.CountLinesInFile(fastaPath)).ToList(); document = document.AddPeptideGroups(peptideGroupsNew, false, to, out firstAdded, out nextAdd); } return(document); }
public static SrmDocument ImportFasta(SrmDocument document, string fastaPath, IProgressMonitor monitor, IdentityPath to, out IdentityPath firstAdded, out IdentityPath nextAdd, out int emptyProteinCount) { var importer = new FastaImporter(document, false); using (TextReader reader = File.OpenText(fastaPath)) { document = document.AddPeptideGroups(importer.Import(reader, monitor, Helpers.CountLinesInFile(fastaPath)), false, null, out firstAdded, out nextAdd); } emptyProteinCount = importer.EmptyPeptideGroupCount; return(document); }
public static OptimizationDb ConvertFromOldFormat(string path, IProgressMonitor loadMonitor, ProgressStatus status, SrmDocument document) { // Try to open assuming old format (Id, PeptideModSeq, Charge, Mz, Value, Type) var precursors = new Dictionary<string, HashSet<int>>(); // PeptideModSeq -> charges var optimizations = new List<Tuple<DbOptimization, double>>(); // DbOptimization, product m/z int maxCharge = 1; using (SQLiteConnection connection = new SQLiteConnection("Data Source = " + path)) // Not L10N using (SQLiteCommand command = new SQLiteCommand(connection)) { connection.Open(); command.CommandText = "SELECT PeptideModSeq, Charge, Mz, Value, Type FROM OptimizationLibrary"; // Not L10N using (SQLiteDataReader reader = command.ExecuteReader()) { while (reader.Read()) { var type = (OptimizationType)reader["Type"]; // Not L10N var modifiedSequence = reader["PeptideModSeq"].ToString(); // Not L10N var charge = (int)reader["Charge"]; // Not L10N var productMz = (double)reader["Mz"]; // Not L10N var value = (double)reader["Value"]; // Not L10N optimizations.Add(new Tuple<DbOptimization, double>(new DbOptimization(type, modifiedSequence, charge, string.Empty, -1, value), productMz)); if (!precursors.ContainsKey(modifiedSequence)) { precursors[modifiedSequence] = new HashSet<int>(); } precursors[modifiedSequence].Add(charge); if (charge > maxCharge) { maxCharge = charge; } } } } var peptideList = (from precursor in precursors from charge in precursor.Value select string.Format("{0}{1}", precursor.Key, Transition.GetChargeIndicator(charge)) // Not L10N ).ToList(); var newDoc = new SrmDocument(document != null ? document.Settings : SrmSettingsList.GetDefault()); newDoc = newDoc.ChangeSettings(newDoc.Settings .ChangePeptideLibraries(libs => libs.ChangePick(PeptidePick.filter)) .ChangeTransitionFilter(filter => filter.ChangeFragmentRangeFirstName("ion 1") // Not L10N .ChangeFragmentRangeLastName("last ion") // Not L10N .ChangeProductCharges(Enumerable.Range(1, maxCharge).ToList()) .ChangeIonTypes(new []{ IonType.y, IonType.b })) .ChangeTransitionLibraries(libs => libs.ChangePick(TransitionLibraryPick.none)) ); var matcher = new ModificationMatcher { FormatProvider = NumberFormatInfo.InvariantInfo }; matcher.CreateMatches(newDoc.Settings, peptideList, Settings.Default.StaticModList, Settings.Default.HeavyModList); FastaImporter importer = new FastaImporter(newDoc, matcher); string text = string.Format(">>{0}\r\n{1}", newDoc.GetPeptideGroupId(true), TextUtil.LineSeparate(peptideList)); // Not L10N PeptideGroupDocNode imported = importer.Import(new StringReader(text), null, Helpers.CountLinesInString(text)).First(); int optimizationsUpdated = 0; foreach (PeptideDocNode nodePep in imported.Children) { string sequence = newDoc.Settings.GetSourceTextId(nodePep); foreach (var nodeGroup in nodePep.TransitionGroups) { int charge = nodeGroup.PrecursorCharge; foreach (var nodeTran in nodeGroup.Transitions) { double productMz = nodeTran.Mz; foreach (var optimization in optimizations.Where(opt => string.IsNullOrEmpty(opt.Item1.FragmentIon) && opt.Item1.ProductCharge == -1 && opt.Item1.PeptideModSeq == sequence && opt.Item1.Charge == charge && Math.Abs(opt.Item2 - productMz) < 0.00001)) { optimization.Item1.FragmentIon = nodeTran.FragmentIonName; optimization.Item1.ProductCharge = nodeTran.Transition.Charge; ++optimizationsUpdated; } } } } if (optimizations.Count > optimizationsUpdated) { throw new OptimizationsOpeningException(string.Format(Resources.OptimizationDb_ConvertFromOldFormat_Failed_to_convert__0__optimizations_to_new_format_, optimizations.Count - optimizationsUpdated)); } using (var fs = new FileSaver(path)) { OptimizationDb db = CreateOptimizationDb(fs.SafeName); db.UpdateOptimizations(optimizations.Select(opt => opt.Item1).ToArray(), new DbOptimization[0]); fs.Commit(); if (loadMonitor != null) loadMonitor.UpdateProgress(status.ChangePercentComplete(100)); return GetOptimizationDb(fs.RealName, null, null); } }
public static OptimizationDb ConvertFromOldFormat(string path, IProgressMonitor loadMonitor, ProgressStatus status, SrmDocument document) { // Try to open assuming old format (Id, PeptideModSeq, Charge, Mz, Value, Type) var precursors = new Dictionary <Target, HashSet <int> >(); // PeptideModSeq -> charges var optimizations = new List <Tuple <DbOptimization, double> >(); // DbOptimization, product m/z int maxCharge = 1; using (SQLiteConnection connection = new SQLiteConnection(@"Data Source = " + path)) using (SQLiteCommand command = new SQLiteCommand(connection)) { connection.Open(); command.CommandText = @"SELECT PeptideModSeq, Charge, Mz, Value, Type FROM OptimizationLibrary"; using (SQLiteDataReader reader = command.ExecuteReader()) { while (reader.Read()) { var type = (OptimizationType)reader[@"Type"]; var modifiedSequence = new Target(reader[@"PeptideModSeq"].ToString()); var charge = (int)reader[@"Charge"]; var productMz = (double)reader[@"Mz"]; var value = (double)reader[@"Value"]; optimizations.Add(new Tuple <DbOptimization, double>(new DbOptimization(type, modifiedSequence, Adduct.FromChargeProtonated(charge), string.Empty, Adduct.EMPTY, value), productMz)); if (!precursors.ContainsKey(modifiedSequence)) { precursors[modifiedSequence] = new HashSet <int>(); } precursors[modifiedSequence].Add(charge); if (charge > maxCharge) { maxCharge = charge; } } } } var peptideList = (from precursor in precursors from charge in precursor.Value select string.Format(@"{0}{1}", precursor.Key, Transition.GetChargeIndicator(Adduct.FromChargeProtonated(charge))) ).ToList(); var newDoc = new SrmDocument(document != null ? document.Settings : SrmSettingsList.GetDefault()); newDoc = newDoc.ChangeSettings(newDoc.Settings .ChangePeptideLibraries(libs => libs.ChangePick(PeptidePick.filter)) .ChangeTransitionFilter(filter => filter.ChangeFragmentRangeFirstName(@"ion 1") .ChangeFragmentRangeLastName(@"last ion") .ChangePeptideProductCharges(Enumerable.Range(1, maxCharge).Select(Adduct.FromChargeProtonated).ToList()) // TODO(bspratt) negative charge peptides .ChangePeptideIonTypes(new [] { IonType.y, IonType.b })) // TODO(bspratt) generalize to molecules? .ChangeTransitionLibraries(libs => libs.ChangePick(TransitionLibraryPick.none)) ); var matcher = new ModificationMatcher(); matcher.CreateMatches(newDoc.Settings, peptideList, Settings.Default.StaticModList, Settings.Default.HeavyModList); FastaImporter importer = new FastaImporter(newDoc, matcher); // ReSharper disable LocalizableElement string text = string.Format(">>{0}\r\n{1}", newDoc.GetPeptideGroupId(true), TextUtil.LineSeparate(peptideList)); // ReSharper restore LocalizableElement PeptideGroupDocNode imported = importer.Import(new StringReader(text), null, Helpers.CountLinesInString(text)).First(); int optimizationsUpdated = 0; foreach (PeptideDocNode nodePep in imported.Children) { foreach (var nodeGroup in nodePep.TransitionGroups) { var charge = nodeGroup.PrecursorAdduct; var libKeyToMatch = newDoc.Settings.GetSourceTarget(nodePep).GetLibKey(charge).LibraryKey; foreach (var nodeTran in nodeGroup.Transitions) { double productMz = nodeTran.Mz; foreach (var optimization in optimizations.Where(opt => string.IsNullOrEmpty(opt.Item1.FragmentIon) && opt.Item1.ProductAdduct.IsEmpty && Math.Abs(opt.Item2 - productMz) < 0.00001)) { var optLibKey = optimization.Item1.Target.GetLibKey(optimization.Item1.Adduct).LibraryKey; if (!LibKeyIndex.KeysMatch(optLibKey, libKeyToMatch)) { continue; } optimization.Item1.FragmentIon = nodeTran.FragmentIonName; optimization.Item1.ProductAdduct = nodeTran.Transition.Adduct; ++optimizationsUpdated; } } } } if (optimizations.Count > optimizationsUpdated) { throw new OptimizationsOpeningException(string.Format(Resources.OptimizationDb_ConvertFromOldFormat_Failed_to_convert__0__optimizations_to_new_format_, optimizations.Count - optimizationsUpdated)); } using (var fs = new FileSaver(path)) { OptimizationDb db = CreateOptimizationDb(fs.SafeName); db.UpdateOptimizations(optimizations.Select(opt => opt.Item1).ToArray(), new DbOptimization[0]); fs.Commit(); if (loadMonitor != null) { loadMonitor.UpdateProgress(status.ChangePercentComplete(100)); } return(GetOptimizationDb(fs.RealName, null, null)); } }
protected override void DoTest() { OpenDocument("Rat_plasma.sky"); RunUI(SkylineWindow.ShowAuditLog); var auditLogForm = WaitForOpenForm <AuditLogForm>(); Assert.IsFalse(SkylineWindow.Document.Settings.DataSettings.AuditLogging); RunUI(() => auditLogForm.EnableAuditLogging(true)); // Test that initial hash is correct var expectedHash = BlockHash.SafeToBase64(new byte[] { 0xFE, 0x0F, 0x0C, 0x54, 0xA0, 0x77, 0xE5, 0x8F, 0x77, 0xDC, 0x8B, 0xEE, 0x44, 0xB6, 0x65, 0x6D, 0x98, 0x31, 0xAA, 0x35 }); var actualHash = GetDocumentHash(); Assert.AreEqual(expectedHash, actualHash); // Test that the hash is the same as if the document was simply read and hashed // The document is really small (<20KB) so it's fine to read it all into memory var bytes = File.ReadAllBytes(SkylineWindow.DocumentFilePath); var hash = Hash(bytes); Assert.AreEqual(expectedHash, hash); // Make sure that there's an entry describing 1) number of nodes and 2) settings changes from default settings Assert.AreEqual(1, SkylineWindow.Document.AuditLog.AuditLogEntries.Count); if (!RecordNewestEntry()) { RunUI(() => { LOG_ENTRY_MESSAGES[0].AssertEquals(SkylineWindow.Document.AuditLog.AuditLogEntries); }); } // Modify and save the document so that the audit log gets saved ChangeSettings(settings => settings.ChangePeptideFilter(filter => filter.ChangeExcludeNTermAAs(3))); // Change from 2 to 3 RunUI(() => SkylineWindow.SaveDocument()); RecordNewestEntry(); Assert.IsTrue(File.Exists(SrmDocument.GetAuditLogPath(SkylineWindow.DocumentFilePath)), "Audit log does not exist after saving document"); AssertEx.ValidateAuditLogAgainstSchema(File.ReadAllText(SrmDocument.GetAuditLogPath(SkylineWindow.DocumentFilePath))); // Also validate an old document AssertEx.ValidateAuditLogAgainstSchema(File.ReadAllText(TestFilesDir.GetTestPath("old_rat_plasma.skyl"))); // Modify document outside of skyline var docPath = SkylineWindow.DocumentFilePath; RunUI(() => SkylineWindow.NewDocument(true)); // Audit logging should be enabled since the previous settings will be used Assert.IsTrue(SkylineWindow.Document.Settings.DataSettings.AuditLogging); // Perform the following action manually: // Settings > Peptide Settings -- Filter > Exclude N-Terminal AA's changed from "3" to "4" var text = File.ReadAllText(docPath); var match = Regex.Match(text, "<peptide_filter .*start=\"3\""); Assert.IsTrue(match.Success); Assert.IsFalse(match.NextMatch().Success); var sb = new StringBuilder(text); sb[match.Index + match.Length - 2] = '4'; File.WriteAllText(docPath, sb.ToString()); var oldRef = SkylineWindow.Document; // Add an entry describing this change OpenDocumentNoWait("Rat_plasma.sky"); var alert = WaitForOpenForm <AlertDlg>(); OkDialog(alert, alert.ClickYes); var logForm = WaitForOpenForm <DocumentChangeLogEntryDlg>(); RunUI(() => { logForm.LogMessage = "Changed Exlude N-Terminal AA's from 3 to 4 manually"; }); OkDialog(logForm, logForm.OkDialog); // Wait until document gets switched, otherwise WaitForDocumentLoaded will think that the document is already loaded // since we're not waiting for the document to open (OpenDocumentNoWait) WaitForCondition(() => !ReferenceEquals(oldRef, SkylineWindow.Document)); Assert.AreNotEqual(expectedHash, SkylineWindow.Document.DocumentHash); // If this wasn't the case the dialogs would never show up, but check anyways WaitForDocumentLoaded(); RunUI(() => SkylineWindow.SaveDocument()); // Save so that hash in audit log matches document now // Now audit logging should be enabled Assert.IsTrue(SkylineWindow.Document.Settings.DataSettings.AuditLogging); // Make sure the entry was actually added Assert.AreEqual(3, SkylineWindow.Document.AuditLog.AuditLogEntries.Count); // Check that this entry got saved and read correctly, we don't record here if (!IsRecordMode) { RunUI(() => { LOG_ENTRY_MESSAGES[1].AssertEquals(SkylineWindow.Document.AuditLog.AuditLogEntries.Parent); }); } if (!RecordNewestEntry()) { RunUI(() => { LOG_ENTRY_MESSAGES[2].AssertEquals(SkylineWindow.Document.AuditLog.AuditLogEntries); }); } var fasta = FastaImporter.ToFasta(PROTEINLIST_CLIPBOARD_TEXT, TextUtil.SEPARATOR_TSV); RunDlg <EmptyProteinsDlg>(() => SkylineWindow.Paste(fasta), dlg => dlg.KeepEmptyProteins()); if (!RecordNewestEntry()) { RunUI(() => { LOG_ENTRY_MESSAGES[3].AssertEquals(SkylineWindow.Document.AuditLog.AuditLogEntries); }); } RunUI(SkylineWindow.ShowAuditLog); var auditLogForm1 = WaitForOpenForm <AuditLogForm>(); RunUI(() => auditLogForm1.ChooseView(AuditLogStrings.AuditLogForm_MakeAuditLogForm_Undo_Redo)); AuditLogUtil.WaitForAuditLogForm(auditLogForm1); // Show extra info for this entry RunDlg <AuditLogExtraInfoForm>(() => { Assert.AreEqual(4, auditLogForm1.DataGridView.RowCount); var fastaRow = auditLogForm1.DataGridView.Rows[0]; var undoRedoCol = auditLogForm1.FindColumn(nameof(AuditLogRow.UndoRedoMessage)); Assert.IsNotNull(undoRedoCol); var auditLogRowText = fastaRow.Cells[undoRedoCol.Index].Value as AuditLogRow.AuditLogRowText; Assert.IsNotNull(auditLogRowText); var col = auditLogForm1.DataGridView.Columns[undoRedoCol.Index] as AuditLogColumn; Assert.IsNotNull(col); Assert.IsTrue(col.ShouldDisplay(auditLogRowText, (int)AuditLogColumn.ImageIndex.extra_info)); col.Click(auditLogRowText, (int)AuditLogColumn.ImageIndex.extra_info); }, form => { var entry = SkylineWindow.DocumentUI.AuditLog.AuditLogEntries; Assert.AreEqual(form.Message, entry.UndoRedo.ToString()); Assert.AreEqual(form.ExtraInfo, entry.ExtraInfo); form.OkDialog(); }); // Disable audit logging, this should warn the user RunDlg <AlertDlg>(() => { auditLogForm1.EnableAuditLogging(false); }, alertDlg => { alertDlg.ClickYes(); }); AuditLogUtil.WaitForAuditLogForm(auditLogForm1); Assert.AreEqual(0, SkylineWindow.Document.AuditLog.AuditLogEntries.Count); Assert.IsFalse(SkylineWindow.Document.Settings.DataSettings.AuditLogging); // Re-open document without saving RunUI(() => SkylineWindow.NewDocument(true)); OpenDocument("Rat_plasma.sky"); RunUI(SkylineWindow.ShowAuditLog); var auditLogForm2 = WaitForOpenForm <AuditLogForm>(); RunUI(() => auditLogForm2.ChooseView(AuditLogStrings.AuditLogForm_MakeAuditLogForm_Undo_Redo)); AuditLogUtil.WaitForAuditLogForm(auditLogForm2); RunUI(() => { // Audit logging shold be back on and the entries should still be there Assert.IsTrue(SkylineWindow.DocumentUI.Settings.DataSettings.AuditLogging); Assert.AreEqual(3, SkylineWindow.DocumentUI.AuditLog.AuditLogEntries.Count); }); // Disable again, this time save RunDlg <AlertDlg>(() => { auditLogForm2.EnableAuditLogging(false); }, alertDlg => { alertDlg.ClickYes(); }); RunUI(() => SkylineWindow.SaveDocument()); // audit log should be gone Assert.IsFalse(File.Exists(SrmDocument.GetAuditLogPath(SkylineWindow.DocumentFilePath))); }