public bool LookupProteinMetadata(ref IProgressStatus progressStatus) { using (var proteomeDb = ProteomeDb.OpenProteomeDb(_pathProteome, _isTemporary)) { _progressStatus = progressStatus.ChangeMessage( string.Format(Resources.BackgroundProteomeManager_LoadBackground_Resolving_protein_details_for__0__proteome, _nameProteome)); bool result = false; // Well formatted Uniprot headers don't require web access, so do an inital pass in hopes of finding those, then a second pass that requires web access for (var useWeb = 0; useWeb <= 1; useWeb++) { if (_progressStatus.IsCanceled) { break; } if (useWeb == 1 && !_manager.FastaImporter.HasWebAccess()) // Do we even have web access? { _progressStatus = _progressStatus.ChangeMessage(Resources.DigestHelper_LookupProteinMetadata_Unable_to_access_internet_to_resolve_protein_details_) .ChangeWarningMessage(Resources.DigestHelper_LookupProteinMetadata_Unable_to_access_internet_to_resolve_protein_details_).Cancel(); result = false; } else { bool done; result |= proteomeDb.LookupProteinMetadata(this, ref _progressStatus, _manager.FastaImporter, useWeb == 0, out done); // first pass, just parse descriptions, second pass use web. if (done) { break; } } } progressStatus = _progressStatus; return(result); } }
private void RefreshStatus() { if (File.Exists(textPath.Text)) { btnAddFastaFile.Enabled = true; ProteomeDb proteomeDb = null; int proteinCount = 0; try { using (var longWaitDlg = new LongWaitDlg { Text = Resources.BuildBackgroundProteomeDlg_RefreshStatus_Loading_Proteome_File, Message = string.Format( Resources.BuildBackgroundProteomeDlg_RefreshStatus_Loading_protein_information_from__0__, textPath.Text) }) { longWaitDlg.PerformWork(this, 1000, () => { proteomeDb = ProteomeDb.OpenProteomeDb(textPath.Text); if (proteomeDb != null) { proteinCount = proteomeDb.GetProteinCount(); // This can be a lengthy operation on a large protdb, do it within the longwait } }); } if (proteomeDb == null) { throw new Exception(); } tbxStatus.Text = string.Format( Resources.BuildBackgroundProteomeDlg_RefreshStatus_The_proteome_file_contains__0__proteins, proteinCount); } catch (Exception) { tbxStatus.Text = Resources.BuildBackgroundProteomeDlg_OkDialog_The_proteome_file_is_not_valid; btnAddFastaFile.Enabled = false; } finally { if (null != proteomeDb) { proteomeDb.Dispose(); } } textPath.ForeColor = Color.Black; } else { btnAddFastaFile.Enabled = false; tbxStatus.Text = Resources.BuildBackgroundProteomeDlg_RefreshStatus_Click_the_Open_button_to_choose_an_existing_proteome_file_or_click_the_Create_button_to_create_a_new_proteome_file; textPath.ForeColor = string.IsNullOrEmpty(textPath.Text) ? Color.Black : Color.Red; } }
private void RefreshStatus() { if (File.Exists(textPath.Text)) { btnAddFastaFile.Enabled = true; ProteomeDb proteomeDb = null; try { using (var longWaitDlg = new LongWaitDlg { Text = Resources.BuildBackgroundProteomeDlg_RefreshStatus_Loading_Proteome_File, Message = string.Format( Resources.BuildBackgroundProteomeDlg_RefreshStatus_Loading_protein_information_from__0__, textPath.Text) }) { longWaitDlg.PerformWork(this, 1000, () => proteomeDb = ProteomeDb.OpenProteomeDb(textPath.Text)); } if (proteomeDb == null) { throw new Exception(); } int proteinCount = proteomeDb.GetProteinCount(); var digestions = proteomeDb.ListDigestions(); tbxStatus.Text = string.Format( Resources.BuildBackgroundProteomeDlg_RefreshStatus_The_proteome_file_contains__0__proteins, proteinCount); if (proteinCount != 0 && digestions.Count > 0) { tbxStatus.Text = TextUtil.LineSeparate(tbxStatus.Text, Resources.BuildBackgroundProteomeDlg_RefreshStatus_The_proteome_has_already_been_digested); } } catch (Exception) { tbxStatus.Text = Resources.BuildBackgroundProteomeDlg_OkDialog_The_proteome_file_is_not_valid; btnAddFastaFile.Enabled = false; } finally { if (null != proteomeDb) { proteomeDb.Dispose(); } } } else { btnAddFastaFile.Enabled = false; tbxStatus.Text = Resources.BuildBackgroundProteomeDlg_RefreshStatus_Click_the_Open_button_to_choose_an_existing_proteome_file_or_click_the_Create_button_to_create_a_new_proteome_file; } }
public void OkDialog() { if (!_messageBoxHelper.ValidateNameTextBox(textName, out _name)) { return; } if (_backgroundProteomeSpec == null || _name != _backgroundProteomeSpec.Name) { foreach (BackgroundProteomeSpec backgroundProteomeSpec in _existing) { if (_name == backgroundProteomeSpec.Name) { _messageBoxHelper.ShowTextBoxError(textName, Resources.BuildBackgroundProteomeDlg_OkDialog_The_background_proteome__0__already_exists, _name); return; } } } if (string.IsNullOrEmpty(textPath.Text)) { _messageBoxHelper.ShowTextBoxError(textPath, Resources.BuildBackgroundProteomeDlg_OkDialog_You_must_specify_a_proteome_file); return; } try { if (textPath.Text != Path.GetFullPath(textPath.Text)) { _messageBoxHelper.ShowTextBoxError(textPath, Resources.BuildBackgroundProteomeDlg_OkDialog_Please_specify_a_full_path_to_the_proteome_file); return; } else if (!File.Exists(textPath.Text)) { _messageBoxHelper.ShowTextBoxError(textPath, string.Format(Resources.BuildBackgroundProteomeDlg_OkDialog_The_proteome_file__0__does_not_exist, textPath.Text)); return; } ProteomeDb.OpenProteomeDb(textPath.Text); } catch (Exception x) { // In case exception is thrown opening protdb string message = TextUtil.LineSeparate(Resources.BuildBackgroundProteomeDlg_OkDialog_The_proteome_file_is_not_valid, Resources.BuildBackgroundProteomeDlg_OkDialog_Choose_a_valid_proteome_file__or_click_the__Create__button_to_create_a_new_one_from_FASTA_files); MessageDlg.ShowWithException(this, message, x); return; } _databasePath = textPath.Text; _name = textName.Text; _backgroundProteomeSpec = new BackgroundProteomeSpec(_name, _databasePath); DialogResult = DialogResult.OK; Close(); }
public void AddFastaFile(string fastaFilePath) { String databasePath = textPath.Text; Settings.Default.FastaDirectory = Path.GetDirectoryName(fastaFilePath); int duplicateSequenceCount = 0; using (var longWaitDlg = new LongWaitDlg { ProgressValue = 0 }) { try { longWaitDlg.PerformWork(this, 0, progressMonitor => { ProteomeDb proteomeDb = File.Exists(databasePath) ? ProteomeDb.OpenProteomeDb(databasePath) : ProteomeDb.CreateProteomeDb(databasePath); using (proteomeDb) { using (var reader = File.OpenText(fastaFilePath)) { IProgressStatus status = new ProgressStatus(longWaitDlg.Message); proteomeDb.AddFastaFile(reader, progressMonitor, ref status, false, out duplicateSequenceCount); } } }); } catch (Exception x) { var message = TextUtil.LineSeparate(string.Format(Resources.BuildBackgroundProteomeDlg_AddFastaFile_An_error_occurred_attempting_to_add_the_FASTA_file__0__, fastaFilePath), x.Message); MessageDlg.ShowWithException(this, message, x); return; } } string path = Path.GetFileName(fastaFilePath); if (path != null) { listboxFasta.Items.Add(path); } RefreshStatus(); if (duplicateSequenceCount > 0) { MessageDlg.Show(this, string.Format(Resources.BuildBackgroundProteomeDlg_AddFastaFile_The_added_file_included__0__repeated_protein_sequences__Their_names_were_added_as_aliases_to_ensure_the_protein_list_contains_only_one_copy_of_each_sequence_, duplicateSequenceCount)); } }
public bool Digest(ref IProgressStatus progressStatus, bool delayDbIndexing) { using (var proteomeDb = ProteomeDb.OpenProteomeDb(_pathProteome, _isTemporary)) { var enzyme = _settings.Enzyme; _progressStatus = progressStatus.ChangeMessage( string.Format(Resources.DigestHelper_Digest_Digesting__0__proteome_with__1__, _nameProteome, enzyme.Name)); var digestion = proteomeDb.Digest(new ProteaseImpl(enzyme), ProteomeDb.PROTDB_MAX_MISSED_CLEAVAGES, this, ref _progressStatus, delayDbIndexing); progressStatus = _progressStatus; return(digestion != null); } }
// ReSharper disable RedundantAssignment public Digestion Digest(ref ProgressStatus progressStatus) // ReSharper restore RedundantAssignment { using (var proteomeDb = ProteomeDb.OpenProteomeDb(_pathProteome, _isTemporary)) { var enzyme = _document.Settings.PeptideSettings.Enzyme; _progressStatus = new ProgressStatus( string.Format(Resources.DigestHelper_Digest_Digesting__0__proteome_with__1__, _nameProteome, enzyme.Name)); var digestion = proteomeDb.Digest(new ProteaseImpl(enzyme), Progress); progressStatus = _progressStatus; return(digestion); } }
// ReSharper disable RedundantAssignment public bool LookupProteinMetadata(ref ProgressStatus progressStatus) // ReSharper restore RedundantAssignment { if (!_manager.FastaImporter.HasWebAccess()) // Do we even have web access? { return(false); // Return silently rather than flashing the progress bar } using (var proteomeDb = ProteomeDb.OpenProteomeDb(_pathProteome, _isTemporary)) { _progressStatus = new ProgressStatus( string.Format(Resources.BackgroundProteomeManager_LoadBackground_Resolving_protein_details_for__0__proteome, _nameProteome)); var result = proteomeDb.LookupProteinMetadata(Progress, _manager.FastaImporter, true); // true means be polite, don't try to resolve all in one go progressStatus = _progressStatus; return(result); } }
public void AddFastaFile(string fastaFilePath) { String databasePath = textPath.Text; Settings.Default.FastaDirectory = Path.GetDirectoryName(fastaFilePath); using (var longWaitDlg = new LongWaitDlg { ProgressValue = 0 }) { var progressMonitor = new ProgressMonitor(longWaitDlg); try { longWaitDlg.PerformWork(this, 0, () => { ProteomeDb proteomeDb = File.Exists(databasePath) ? ProteomeDb.OpenProteomeDb(databasePath) : ProteomeDb.CreateProteomeDb(databasePath); using (proteomeDb) { using (var reader = File.OpenText(fastaFilePath)) { proteomeDb.AddFastaFile(reader, progressMonitor.UpdateProgress); } } }); } catch (Exception x) { var message = TextUtil.LineSeparate(string.Format(Resources.BuildBackgroundProteomeDlg_AddFastaFile_An_error_occurred_attempting_to_add_the_FASTA_file__0__, fastaFilePath), x.Message); MessageDlg.ShowWithException(this, message, x); return; } } string path = Path.GetFileName(fastaFilePath); if (path != null) { listboxFasta.Items.Add(path); } RefreshStatus(); }
public void TestNewerProteomeDb() { using (var testFilesDir = new TestFilesDir(TestContext, ZIP_FILE)) { string protDbPath = testFilesDir.GetTestPath("testv9999.protdb"); // a version 9999(!) protdb file try { using (var db = ProteomeDb.OpenProteomeDb(protDbPath)) { using (var session = db.OpenSession()) session.Close(); Assert.Fail("should not be able to open a version 9999 protdb file."); // Not L10N } } // ReSharper disable once EmptyGeneralCatchClause catch { } } }
public ProteomeDb OpenProteomeDb(CancellationToken cancellationToken) { return(ProteomeDb.OpenProteomeDb(FilePath, cancellationToken)); }
public void DoTestOlderProteomeDb(TestContext testContext, bool doActualWebAccess) { using (var testFilesDir = new TestFilesDir(testContext, ZIP_FILE)) { string fastaPath = testFilesDir.GetTestPath("tiny.fasta"); string protDbPath = testFilesDir.GetTestPath("celegans_mini.protdb"); // a version 0 protdb file string blibPath = testFilesDir.GetTestPath("random.blib"); // a bibliospec file // What happens when you try to open a random file as a protdb file? AssertEx.ThrowsException <DbException>(() => ProteomeDb.OpenProteomeDb(fastaPath)); // What happens when you try to open a non-protdb database file as a protdb file? AssertEx.ThrowsException <FileLoadException>(() => ProteomeDb.OpenProteomeDb(blibPath)); using (ProteomeDb proteomeDb = ProteomeDb.OpenProteomeDb(protDbPath)) { Assert.IsTrue(proteomeDb.GetSchemaVersionMajor() == 0); // the initial db from our zipfile should be ancient Assert.IsTrue(proteomeDb.GetSchemaVersionMinor() == 0); // the initial db from our zipfile should be ancient Assert.AreEqual(9, proteomeDb.GetProteinCount()); var protein = proteomeDb.GetProteinByName("Y18D10A.20"); Assert.IsNotNull(protein); Assert.IsTrue(String.IsNullOrEmpty(protein.Accession)); // old db won't have this populated WebEnabledFastaImporter searcher = new WebEnabledFastaImporter(doActualWebAccess ? null :new WebEnabledFastaImporter.FakeWebSearchProvider()); bool searchComplete; IProgressStatus status = new ProgressStatus(string.Empty); Assert.IsTrue(proteomeDb.LookupProteinMetadata(new SilentProgressMonitor(), ref status, searcher, false, out searchComplete)); // add any missing protein metadata Assert.IsTrue(searchComplete); protein = proteomeDb.GetProteinByName("Y18D10A.20"); Assert.IsNotNull(protein); if (doActualWebAccess) // We can actually go to the web for metadata { Assert.AreEqual("Q9XW16", protein.Accession); } using (var reader = new StreamReader(fastaPath)) { proteomeDb.AddFastaFile(reader, new SilentProgressMonitor(), ref status, false); } // the act of writing should update to the current version Assert.AreEqual(ProteomeDb.SCHEMA_VERSION_MAJOR_CURRENT, proteomeDb.GetSchemaVersionMajor()); Assert.AreEqual(ProteomeDb.SCHEMA_VERSION_MINOR_CURRENT, proteomeDb.GetSchemaVersionMinor()); Assert.AreEqual(19, proteomeDb.GetProteinCount()); // check for propery processed protein metadata Assert.IsTrue(proteomeDb.LookupProteinMetadata(new SilentProgressMonitor(), ref status, searcher, false, out searchComplete)); Assert.IsTrue(searchComplete); protein = proteomeDb.GetProteinByName("IPI00000044"); Assert.IsNotNull(protein); Assert.AreEqual("P01127", protein.Accession); // We get this offline with our ipi->uniprot mapper if (doActualWebAccess) { Assert.AreEqual("PDGFB_HUMAN", protein.PreferredName); // But this we get only with web access } /* * // TODO(bspratt): fix "GetDigestion has no notion of a Db that has been added to, doesn't digest the new proteins and returns immediately (issue #304)" * Enzyme trypsin = EnzymeList.GetDefault(); * proteomeDb.Digest(trypsin, new SilentProgressMonitor()); * Digestion digestion = proteomeDb.GetDigestion(trypsin.Name); * var digestedProteins0 = digestion.GetProteinsWithSequencePrefix("EDGWVK", 100); * Assert.IsTrue(digestedProteins0.Count >= 1); * */ } } }
public ProteomeDb OpenProteomeDb() { return(ProteomeDb.OpenProteomeDb(DatabasePath)); }
/// <summary> /// Opens a proteome database (creates a session factory, etc.). The ProteomeDb /// returned by this method must be Dispose'd. /// </summary> public ProteomeDb OpenProteomeDb() { return(ProteomeDb.OpenProteomeDb(FilePath)); }
private BackgroundProteome Load(IDocumentContainer container, PeptideSettings settings, SrmDocument docCurrent, bool isBackgroundLoad) { // Only allow one background proteome to load at a time. This can // get tricky, if the user performs an undo and then a redo across // a change in background proteome. // Our only priority is accessing web services to add missing protein metadata. // There may also be a load initiation by the Peptide Settings dialog as foreground task, // it takes priority over the background task. lock (_lockLoadBackgroundProteome) { BackgroundProteome originalBackgroundProteome = settings.BackgroundProteome; BackgroundProteome validatedBackgroundProtome = originalBackgroundProteome.DatabaseValidated ? originalBackgroundProteome : new BackgroundProteome(originalBackgroundProteome.BackgroundProteomeSpec); if (IsNotLoadedExplained(settings, validatedBackgroundProtome, true) == null) { // protein metadata is resolved CompleteProcessing(container, validatedBackgroundProtome); Helpers.AssignIfEquals(ref validatedBackgroundProtome, originalBackgroundProteome); return(validatedBackgroundProtome); // No change needed } // we are here to resolve the protein metadata string name = originalBackgroundProteome.Name; IProgressStatus progressStatus = new ProgressStatus(string.Format(Resources.BackgroundProteomeManager_LoadBackground_Resolving_protein_details_for__0__proteome, name)); try { // The transaction commit for writing the digestion info can be very lengthy, avoid lock timeouts // by doing that work in a tempfile that no other thread knows aboout using (FileSaver fs = new FileSaver(originalBackgroundProteome.DatabasePath, StreamManager)) { File.Copy(originalBackgroundProteome.DatabasePath, fs.SafeName, true); var digestHelper = new DigestHelper(this, container, docCurrent, name, fs.SafeName, true); bool success = digestHelper.LookupProteinMetadata(ref progressStatus); if (digestHelper.IsCanceled || !success) { // Processing was canceled if (docCurrent != null) { EndProcessing(docCurrent); } UpdateProgress(progressStatus.Cancel()); return(null); } using (var proteomeDb = ProteomeDb.OpenProteomeDb(originalBackgroundProteome.DatabasePath)) { proteomeDb.DatabaseLock.AcquireWriterLock(int.MaxValue); // Wait for any existing readers to complete, prevent any new ones try { if (File.GetLastWriteTime(fs.RealName) <= File.GetLastWriteTime(fs.SafeName)) // Don't overwrite if foreground task has already updated { proteomeDb.CloseDbConnection(); // Get rid of any file handles if (!fs.Commit()) { if (docCurrent != null) { EndProcessing(docCurrent); } throw new IOException(string.Format(Resources.BackgroundProteomeManager_LoadBackground_Unable_to_rename_temporary_file_to__0__, fs.RealName)); } } } finally { proteomeDb.DatabaseLock.ReleaseWriterLock(); } } var updatedProteome = new BackgroundProteome(originalBackgroundProteome); using (var proteomeDb = originalBackgroundProteome.OpenProteomeDb()) { proteomeDb.AnalyzeDb(); // Now it's safe to start this potentially lengthy indexing operation } CompleteProcessing(container, updatedProteome); UpdateProgress(progressStatus.Complete()); return(updatedProteome); } } catch (Exception x) { var message = new StringBuilder(); message.AppendLine( string.Format(Resources.BackgroundProteomeManager_LoadBackground_Failed_updating_background_proteome__0__, name)); message.Append(x.Message); UpdateProgress(progressStatus.ChangeErrorException(new IOException(message.ToString(), x))); return(null); } } }
protected override bool LoadBackground(IDocumentContainer container, SrmDocument document, SrmDocument docCurrent) { // Only allow one background proteome to load at a time. This can // get tricky, if the user performs an undo and then a redo across // a change in background proteome. // Our first priority is doing the digestions, the second is accessing web // services to add missing protein metadata. lock (_lockLoadBackgroundProteome) { BackgroundProteome originalBackgroundProteome = GetBackgroundProteome(docCurrent); // Check to see whether the Digestion already exists but has not been queried yet. BackgroundProteome backgroundProteomeWithDigestions = new BackgroundProteome(originalBackgroundProteome, true); if (IsNotLoadedExplained(docCurrent, backgroundProteomeWithDigestions, true) == null) { // digest is ready, and protein metdata is resolved CompleteProcessing(container, backgroundProteomeWithDigestions); return(true); } // are we here to do the digest, or to resolve the protein metadata? bool getMetadata = (IsNotLoadedExplained(docCurrent, backgroundProteomeWithDigestions, false) == null) && backgroundProteomeWithDigestions.NeedsProteinMetadataSearch; string name = originalBackgroundProteome.Name; ProgressStatus progressStatus = new ProgressStatus(string.Format(getMetadata?Resources.BackgroundProteomeManager_LoadBackground_Resolving_protein_details_for__0__proteome:Resources.BackgroundProteomeManager_LoadBackground_Digesting__0__proteome, name)); try { using (FileSaver fs = new FileSaver(originalBackgroundProteome.DatabasePath, StreamManager)) { File.Copy(originalBackgroundProteome.DatabasePath, fs.SafeName, true); var digestHelper = new DigestHelper(this, container, docCurrent, name, fs.SafeName, true); bool success; if (getMetadata) { success = digestHelper.LookupProteinMetadata(ref progressStatus); } else { success = (digestHelper.Digest(ref progressStatus) != null); } if (!success) { // Processing was canceled EndProcessing(docCurrent); UpdateProgress(progressStatus.Cancel()); return(false); } using (var proteomeDb = ProteomeDb.OpenProteomeDb(originalBackgroundProteome.DatabasePath)) { proteomeDb.DatabaseLock.AcquireWriterLock(int.MaxValue); try { if (!fs.Commit()) { EndProcessing(docCurrent); throw new IOException( string.Format( Resources .BackgroundProteomeManager_LoadBackground_Unable_to_rename_temporary_file_to__0__, fs.RealName)); } } finally { proteomeDb.DatabaseLock.ReleaseWriterLock(); } } CompleteProcessing(container, new BackgroundProteome(originalBackgroundProteome, true)); UpdateProgress(progressStatus.Complete()); return(true); } } catch (Exception x) { var message = new StringBuilder(); message.AppendLine( string.Format(Resources.BackgroundProteomeManager_LoadBackground_Failed_updating_background_proteome__0__, name)); message.Append(x.Message); UpdateProgress(progressStatus.ChangeErrorException(new IOException(message.ToString(), x))); return(false); } } }