Beispiel #1
0
        public void Run(ProcessStartInfo psi, string stdin, IProgressMonitor progress, ref IProgressStatus status, TextWriter writer)
        {
            // Make sure required streams are redirected.
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError  = true;

            _messageLog.Clear();

            var proc = Process.Start(psi);

            if (proc == null)
            {
                throw new IOException(string.Format(@"Failure starting {0} command.", psi.FileName));
            }
            if (stdin != null)
            {
                try
                {
                    proc.StandardInput.Write(stdin);
                }
                finally
                {
                    proc.StandardInput.Close();
                }
            }

            var           reader      = new ProcessStreamReader(proc);
            StringBuilder sbError     = new StringBuilder();
            int           percentLast = 0;
            string        line;

            while ((line = reader.ReadLine(progress)) != null)
            {
                if (writer != null && !line.StartsWith(HideLinePrefix))
                {
                    writer.WriteLine(line);
                }

                if (progress == null || line.ToLowerInvariant().StartsWith(@"error"))
                {
                    sbError.AppendLine(line);
                }
                else // if (progress != null)
                {
                    if (progress.IsCanceled)
                    {
                        proc.Kill();
                        progress.UpdateProgress(status = status.Cancel());
                        return;
                    }

                    if (!string.IsNullOrEmpty(MessagePrefix) && line.StartsWith(MessagePrefix))
                    {
                        _messageLog.Add(line.Substring(MessagePrefix.Length));
                    }
                    else if (line.EndsWith(@"%"))
                    {
                        double   percent;
                        string[] parts       = line.Split(' ');
                        string   percentPart = parts[parts.Length - 1];
                        if (double.TryParse(percentPart.Substring(0, percentPart.Length - 1), out percent))
                        {
                            percentLast = (int)percent;
                            status      = status.ChangePercentComplete(percentLast);
                            if (percent >= 100 && status.SegmentCount > 0)
                            {
                                status = status.NextSegment();
                            }
                            progress.UpdateProgress(status);
                        }
                    }
                    else if (StatusPrefix == null || line.StartsWith(StatusPrefix))
                    {
                        // Remove prefix, if there is one.
                        if (StatusPrefix != null)
                        {
                            line = line.Substring(StatusPrefix.Length);
                        }

                        status = status.ChangeMessage(line);
                        progress.UpdateProgress(status);
                    }
                }
            }
            proc.WaitForExit();
            int exit = proc.ExitCode;

            if (exit != 0)
            {
                line = proc.StandardError.ReadLine();
                if (line != null)
                {
                    sbError.AppendLine(line);
                }
                if (sbError.Length == 0)
                {
                    sbError.AppendLine(@"Error occurred running process.");
                }
                string processPath = Path.GetDirectoryName(psi.FileName)?.Length == 0
                    ? Path.Combine(Environment.CurrentDirectory, psi.FileName)
                    : psi.FileName;
                // ReSharper disable LocalizableElement
                sbError.AppendFormat("\r\nCommand-line: {0} {1}\r\nWorking directory: {2}{3}", processPath,
                                     // ReSharper restore LocalizableElement
                                     string.Join(" ", proc.StartInfo.Arguments), psi.WorkingDirectory,
                                     stdin != null ? "\r\nStandard input:\r\n" + stdin : "");
                throw new IOException(sbError.ToString());
            }
            // Make to complete the status, if the process succeeded, but never
            // printed 100% to the console
            if (percentLast < 100)
            {
                status = status.ChangePercentComplete(100);
                if (status.SegmentCount > 0)
                {
                    status = status.NextSegment();
                }
                if (progress != null)
                {
                    progress.UpdateProgress(status);
                }
            }
        }
Beispiel #2
0
        private bool BuildLibraryOrThrow(IProgressMonitor progress, ref IProgressStatus progressStatus)
        {
            progressStatus = progressStatus.ChangeSegments(0, 5);
            var standardSpectra = new List <SpectrumMzInfo>();

            // First get predictions for iRT standards specified by the user which may or may not be in the document
            if (IrtStandard != null && !ReferenceEquals(IrtStandard, IrtStandard.EMPTY) && !ReferenceEquals(IrtStandard, IrtStandard.AUTO))
            {
                var standardPeptidesToAdd = ReadStandardPeptides(IrtStandard);
                if (standardPeptidesToAdd != null && standardPeptidesToAdd.Count > 0)
                {
                    // Get iRTs
                    var standardIRTMap = _rtModel.Predict(_prositClient, _document.Settings,
                                                          standardPeptidesToAdd.Select(p => (PrositRetentionTimeModel.PeptideDocNodeWrapper)p.NodePep).ToArray(),
                                                          CancellationToken.None);

                    // Get spectra
                    var standardMS = _intensityModel.PredictBatches(_prositClient, progress, ref progressStatus, _document.Settings,
                                                                    standardPeptidesToAdd.Select(p => p.WithNCE(_nce)).ToArray(),
                                                                    CancellationToken.None);

                    // Merge iRT and MS2 into SpecMzInfos
                    standardSpectra = standardMS.Spectra.Select(m => m.SpecMzInfo).ToList();
                    for (var i = 0; i < standardSpectra.Count; ++i)
                    {
                        if (standardIRTMap.TryGetValue(standardMS.Spectra[i].PeptidePrecursorNCE.NodePep, out var iRT))
                        {
                            standardSpectra[i].RetentionTime = iRT;
                        }
                    }
                }
            }

            progressStatus = progressStatus.NextSegment();
            // Predict fragment intensities
            PrositMS2Spectra ms = _intensityModel.PredictBatches(_prositClient, progress, ref progressStatus, _document.Settings,
                                                                 _peptides.Zip(_precursors,
                                                                               (pep, prec) =>
                                                                               new PrositIntensityModel.PeptidePrecursorNCE(pep, prec, IsotopeLabelType.light, _nce)).ToArray(),
                                                                 CancellationToken.None);

            progressStatus = progressStatus.NextSegment();

            var specMzInfo = ms.Spectra.Select(m => m.SpecMzInfo).ToList();

            // Predict iRTs for peptides
            var distinctModifiedSequences = new HashSet <string>();
            var distinctPeps = new List <PrositRetentionTimeModel.PeptideDocNodeWrapper>();

            foreach (var p in _peptides)
            {
                if (distinctModifiedSequences.Add(p.ModifiedSequence))
                {
                    distinctPeps.Add(new PrositRetentionTimeModel.PeptideDocNodeWrapper(p));
                }
            }
            var iRTMap = _rtModel.PredictBatches(_prositClient, progress, ref progressStatus, _document.Settings,
                                                 distinctPeps, CancellationToken.None);

            progressStatus = progressStatus.NextSegment();

            for (var i = 0; i < specMzInfo.Count; ++i)
            {
                if (iRTMap.TryGetValue(ms.Spectra[i].PeptidePrecursorNCE.NodePep, out var iRT))
                {
                    specMzInfo[i].RetentionTime = iRT;
                }
            }

            // Build library
            var librarySpectra = SpectrumMzInfo.RemoveDuplicateSpectra(standardSpectra.Concat(specMzInfo).ToList());

            // Delete if already exists, no merging with Prosit
            var libraryExists = File.Exists(LibrarySpec.FilePath);

            if (libraryExists)
            {
                var replace = _replaceLibrary();
                if (!replace)
                {
                    return(false);
                }
                FileEx.SafeDelete(LibrarySpec.FilePath);
            }

            if (!librarySpectra.Any())
            {
                return(true);
            }

            progressStatus = progressStatus.NextSegment().ChangeMessage(Resources.SkylineWindow_SaveDocument_Saving___);
            // Build the library
            using (var blibDb = BlibDb.CreateBlibDb(LibrarySpec.FilePath))
            {
                var docLibrarySpec = new BiblioSpecLiteSpec(LibrarySpec.Name, LibrarySpec.FilePath);
                BiblioSpecLiteLibrary docLibraryNew = null;
                var docLibrarySpec2 = docLibrarySpec;

                docLibraryNew =
                    blibDb.CreateLibraryFromSpectra(docLibrarySpec2, librarySpectra, LibrarySpec.Name, progress, ref progressStatus);
                if (docLibraryNew == null)
                {
                    return(false);
                }
            }

            return(true);
        }