Пример #1
0
        private void ShareMinimal(ZipFile zip)
        {
            TemporaryDirectory tempDir = null;

            try
            {
                var docOriginal = Document;
                if (Document.Settings.HasBackgroundProteome)
                {
                    // Remove any background proteome reference
                    Document = Document.ChangeSettings(Document.Settings.ChangePeptideSettings(
                                                           set => set.ChangeBackgroundProteome(BackgroundProteome.NONE)));
                }
                if (Document.Settings.HasRTCalcPersisted)
                {
                    // Minimize any persistable retention time calculator
                    tempDir = new TemporaryDirectory();
                    string tempDbPath = Document.Settings.PeptideSettings.Prediction.RetentionTime
                                        .Calculator.PersistMinimized(tempDir.DirPath, Document);
                    if (tempDbPath != null)
                    {
                        zip.AddFile(tempDbPath, string.Empty);
                    }
                }
                if (Document.Settings.HasOptimizationLibraryPersisted)
                {
                    if (tempDir == null)
                    {
                        tempDir = new TemporaryDirectory();
                    }
                    string tempDbPath = Document.Settings.TransitionSettings.Prediction.OptimizedLibrary.PersistMinimized(
                        tempDir.DirPath, Document);
                    if (tempDbPath != null)
                    {
                        zip.AddFile(tempDbPath, string.Empty);
                    }
                }
                if (Document.Settings.HasIonMobilityLibraryPersisted)
                {
                    // Minimize any persistable drift time predictor
                    if (tempDir == null)
                    {
                        tempDir = new TemporaryDirectory();
                    }
                    string tempDbPath = Document.Settings.PeptideSettings.Prediction.DriftTimePredictor
                                        .IonMobilityLibrary.PersistMinimized(tempDir.DirPath, Document);
                    if (tempDbPath != null)
                    {
                        zip.AddFile(tempDbPath, string.Empty);
                    }
                }
                if (Document.Settings.HasLibraries)
                {
                    // Minimize all libraries in a temporary directory, and add them
                    if (tempDir == null)
                    {
                        tempDir = new TemporaryDirectory();
                    }
                    Document = BlibDb.MinimizeLibraries(Document, tempDir.DirPath,
                                                        Path.GetFileNameWithoutExtension(DocumentPath),
                                                        ProgressMonitor);
                    if (ProgressMonitor != null && ProgressMonitor.IsCanceled)
                    {
                        return;
                    }

                    foreach (var librarySpec in Document.Settings.PeptideSettings.Libraries.LibrarySpecs)
                    {
                        var tempLibPath = Path.Combine(tempDir.DirPath, Path.GetFileName(librarySpec.FilePath) ?? string.Empty);
                        zip.AddFile(tempLibPath, string.Empty);

                        // If there is a .redundant.blib file that corresponds to a .blib file
                        // in the temp temporary directory, add that as well
                        IncludeRedundantBlib(librarySpec, zip, tempLibPath);
                    }
                }

                ShareDataAndView(zip);
                if (ReferenceEquals(docOriginal, Document))
                {
                    zip.AddFile(DocumentPath, string.Empty);
                }
                else
                {
                    // If minimizing changed the document, then serialize and archive the new document
                    if (tempDir == null)
                    {
                        tempDir = new TemporaryDirectory();
                    }
                    string fileName    = Path.GetFileName(DocumentPath) ?? string.Empty;
                    string tempDocPath = Path.Combine(tempDir.DirPath, fileName);
                    using (var writer = new XmlWriterWithProgress(tempDocPath, fileName, Encoding.UTF8,
                                                                  Document.MoleculeTransitionCount, ProgressMonitor)
                    {
                        Formatting = Formatting.Indented
                    })
                    {
                        XmlSerializer ser = new XmlSerializer(typeof(SrmDocument));
                        ser.Serialize(writer, Document);
                        zip.AddFile(tempDocPath, string.Empty);
                    }
                }
                Save(zip);
            }
            finally
            {
                if (tempDir != null)
                {
                    try
                    {
                        tempDir.Dispose();
                    }
                    catch (IOException x)
                    {
                        var message = TextUtil.LineSeparate(string.Format(Resources.SrmDocumentSharing_ShareMinimal_Failure_removing_temporary_directory__0__,
                                                                          tempDir.DirPath),
                                                            x.Message);
                        throw new IOException(message);
                    }
                }
            }
        }
Пример #2
0
        public static void SaveDocument(SrmDocument doc, string outFile, TextWriter outText)
        {
            // Make sure the containing directory is created
            string dirPath = Path.GetDirectoryName(outFile);
            if (dirPath != null)
                Directory.CreateDirectory(dirPath);

            var progressMonitor = new CommandProgressMonitor(outText, new ProgressStatus(string.Empty));
            using (var saver = new FileSaver(outFile))
            {
                saver.CheckException();

                using (var writer = new XmlWriterWithProgress(saver.SafeName, outFile, Encoding.UTF8, doc.MoleculeTransitionCount, progressMonitor))
                {
                    XmlSerializer ser = new XmlSerializer(typeof(SrmDocument));
                    ser.Serialize(writer, doc);

                    writer.Flush();
                    writer.Close();
                    saver.Commit();

                    var settings = doc.Settings;
                    if (settings.HasResults)
                    {
                        if (settings.MeasuredResults.IsLoaded)
                        {
                            FileStreamManager fsm = FileStreamManager.Default;
                            settings.MeasuredResults.OptimizeCache(outFile, fsm);

                            //don't worry about updating the document with the results of optimization
                            //as is done in SkylineFiles
                        }
                    }
                    else
                    {
                        string cachePath = ChromatogramCache.FinalPathForName(outFile, null);
                        FileEx.SafeDelete(cachePath, true);
                    }
                }
            }
        }
Пример #3
0
        public bool SaveDocument(String fileName, bool includingCacheFile = true)
        {
            if (string.IsNullOrEmpty(DocumentUI.Settings.DataSettings.DocumentGuid) ||
                !Equals(DocumentFilePath, fileName))
            {
                SrmDocument docOriginal;
                SrmDocument docNew;
                do
                {
                    docOriginal = Document;
                    docNew =
                        docOriginal.ChangeSettings(
                            docOriginal.Settings.ChangeDataSettings(
                                docOriginal.Settings.DataSettings.ChangeDocumentGuid()));
                } while (!SetDocument(docNew, docOriginal));
            }

            SrmDocument document = Document;

            try
            {
                using (var saver = new FileSaver(fileName))
                {
                    saver.CheckException();

                    using (var longWaitDlg = new LongWaitDlg(this)
                        {
                            Text = Resources.SkylineWindow_SaveDocument_Saving___,
                            Message = Path.GetFileName(fileName)
                        })
                    {
                        longWaitDlg.PerformWork(this, 800, progressMonitor =>
                        {
                            using (var writer = new XmlWriterWithProgress(saver.SafeName, fileName, Encoding.UTF8,
                                document.MoleculeTransitionCount, progressMonitor)
                            {
                                Formatting = Formatting.Indented
                            })
                            {
                                XmlSerializer ser = new XmlSerializer(typeof(SrmDocument));
                                ser.Serialize(writer, document);

                                writer.Flush();
                                writer.Close();

                                // If the user has chosen "Save As", and the document has a
                                // document specific spectral library, copy this library to
                                // the new name.
                                if (!Equals(DocumentFilePath, fileName))
                                    SaveDocumentLibraryAs(fileName);

                                saver.Commit();
                            }
                        });

                        // Sometimes this catches a cancellation that doesn't throw an OperationCanceledException.
                        if (longWaitDlg.IsCanceled)
                            return false;
                    }
                }
            }
            catch (OperationCanceledException)
            {
                return false;
            }
            catch (Exception ex)
            {
                var message = TextUtil.LineSeparate(string.Format(Resources.SkylineWindow_SaveDocument_Failed_writing_to__0__, fileName), ex.Message);
                MessageBox.Show(message);
                return false;
            }

            DocumentFilePath = fileName;
            _savedVersion = document.UserRevisionIndex;
            SetActiveFile(fileName);

            // Make sure settings lists contain correct values for this document.
            document.Settings.UpdateLists(DocumentFilePath);

            try
            {
                SaveLayout(fileName);

                // CONSIDER: Is this really optional?
                if (includingCacheFile)
                {
                    using (var longWaitDlg = new LongWaitDlg(this)
                    {
                        Text = Resources.SkylineWindow_SaveDocument_Optimizing_data_file___,
                        Message = Path.GetFileName(fileName)
                    })
                    {
                        longWaitDlg.PerformWork(this, 800, () =>
                            OptimizeCache(fileName, longWaitDlg));
                    }
                }
            }

            // We allow silent failures because it is OK for the cache to remain unoptimized
            // or the layout to not be saved.  These aren't critical as long as the document
            // was saved correctly.
            catch (UnauthorizedAccessException) {}
            catch (IOException) {}
            catch (OperationCanceledException) {}
            catch (TargetInvocationException) {}

            return true;
        }