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); } } }
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; } } }
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 first priority is doing the digestions, the second 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; // Check to see whether the Digestion already exists but has not been queried yet. BackgroundProteome backgroundProteomeWithDigestions = new BackgroundProteome(originalBackgroundProteome, true); if (IsNotLoadedExplained(settings, backgroundProteomeWithDigestions, true) == null) { // digest is ready, and protein metdata is resolved CompleteProcessing(container, backgroundProteomeWithDigestions); return(originalBackgroundProteome); // No change needed } // are we here to do the digest, or to resolve the protein metadata? var needsProteinMetadataSearch = backgroundProteomeWithDigestions.NeedsProteinMetadataSearch; bool getMetadata = (IsNotLoadedExplained(settings, backgroundProteomeWithDigestions, false) == null) && needsProteinMetadataSearch; string name = originalBackgroundProteome.Name; IProgressStatus progressStatus = new ProgressStatus(string.Format(getMetadata?Resources.BackgroundProteomeManager_LoadBackground_Resolving_protein_details_for__0__proteome:Resources.BackgroundProteomeManager_LoadBackground_Digesting__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, settings, name, fs.SafeName, true); bool success; if (getMetadata) { success = digestHelper.LookupProteinMetadata(ref progressStatus); } else { success = digestHelper.Digest(ref progressStatus, true); // Delay indexing until we copy the file back } 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 proteomeWithDigestions = new BackgroundProteome(originalBackgroundProteome, true); using (var proteomeDb = proteomeWithDigestions.OpenProteomeDb()) { proteomeDb.AnalyzeDb(); // Now it's safe to start this potentially lengthy indexing operation } CompleteProcessing(container, proteomeWithDigestions); UpdateProgress(progressStatus.Complete()); return(proteomeWithDigestions); } } 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); } } }