public static List <SearchResult> ReadSearchResultsViaBiblioSpec(String filename, Func <int, bool> progressMonitor)
        {
            string tempFile = null;

            try
            {
                tempFile = Path.GetTempFileName();
                var blibBuild = new BlibBuild(tempFile, new[] { filename })
                {
                    CompressLevel = 0,
                };
                var      status = new ProgressStatus("");
                var      progressMonitorImpl = ProgressMonitorImpl.NewProgressMonitorImpl(status, progressMonitor);
                string[] ambiguousPeptides;
                blibBuild.BuildLibrary(LibraryBuildAction.Create, progressMonitorImpl, ref status, out ambiguousPeptides);
                return(ReadBiblioSpecDatabase(tempFile, progressMonitor));
            }
            finally
            {
                try
                {
                    if (tempFile != null)
                    {
                        File.Delete(tempFile);
                    }
                }
                catch (Exception exception)
                {
                    Trace.TraceWarning("Exception deleting temp file {0}", exception);
                }
            }
        }
Beispiel #2
0
        public bool BuildLibrary(IProgressMonitor progress)
        {
            _ambiguousMatches = null;
            IProgressStatus status = new ProgressStatus(Resources.BiblioSpecLiteBuilder_BuildLibrary_Preparing_to_build_library);

            progress.UpdateProgress(status);
            if (InputFiles.Any(f => f.EndsWith(EXT_PILOT)))
            {
                try
                {
                    InputFiles = VendorIssueHelper.ConvertPilotFiles(InputFiles, progress, status);
                    if (progress.IsCanceled)
                    {
                        return(false);
                    }
                }
                catch (Exception x)
                {
                    progress.UpdateProgress(status.ChangeErrorException(x));
                    return(false);
                }
            }

            string message = string.Format(Resources.BiblioSpecLiteBuilder_BuildLibrary_Building__0__library,
                                           Path.GetFileName(OutputPath));

            progress.UpdateProgress(status = status.ChangeMessage(message));
            string redundantLibrary = BiblioSpecLiteSpec.GetRedundantName(OutputPath);
            var    blibBuilder      = new BlibBuild(redundantLibrary, InputFiles, TargetSequences)
            {
                IncludeAmbiguousMatches = IncludeAmbiguousMatches,
                CutOffScore             = CutOffScore,
                Id = Id,
            };

            try
            {
                if (!blibBuilder.BuildLibrary(Action, progress, ref status, out _ambiguousMatches))
                {
                    return(false);
                }
            }
            catch (IOException x)
            {
                progress.UpdateProgress(status.ChangeErrorException(x));
                return(false);
            }
            catch (Exception x)
            {
                Console.WriteLine(x.Message);
                progress.UpdateProgress(status.ChangeErrorException(
                                            new Exception(string.Format(Resources.BiblioSpecLiteBuilder_BuildLibrary_Failed_trying_to_build_the_redundant_library__0__,
                                                                        redundantLibrary))));
                return(false);
            }
            var blibFilter = new BlibFilter();

            status = new ProgressStatus(message);
            progress.UpdateProgress(status);
            // Write the non-redundant library to a temporary file first
            try
            {
                using (var saver = new FileSaver(OutputPath))
                {
                    if (!blibFilter.Filter(redundantLibrary, saver.SafeName, progress, ref status))
                    {
                        return(false);
                    }

                    saver.Commit();
                }
            }
            catch (IOException x)
            {
                progress.UpdateProgress(status.ChangeErrorException(x));
                return(false);
            }
            catch
            {
                progress.UpdateProgress(status.ChangeErrorException(
                                            new Exception(string.Format(Resources.BiblioSpecLiteBuilder_BuildLibrary_Failed_trying_to_build_the_library__0__,
                                                                        OutputPath))));
                return(false);
            }
            finally
            {
                if (!KeepRedundant)
                {
                    FileEx.SafeDelete(redundantLibrary, true);
                }
            }

            return(true);
        }
        public bool BuildLibrary(IProgressMonitor progress)
        {
            _ambiguousMatches = null;
            IProgressStatus status = new ProgressStatus(Resources.BiblioSpecLiteBuilder_BuildLibrary_Preparing_to_build_library);

            progress.UpdateProgress(status);
            if (InputFiles.Any(f => f.EndsWith(EXT_PILOT)))
            {
                try
                {
                    InputFiles = VendorIssueHelper.ConvertPilotFiles(InputFiles, progress, status);
                    if (progress.IsCanceled)
                    {
                        return(false);
                    }
                }
                catch (Exception x)
                {
                    progress.UpdateProgress(status.ChangeErrorException(x));
                    return(false);
                }
            }

            string message = string.Format(Resources.BiblioSpecLiteBuilder_BuildLibrary_Building__0__library,
                                           Path.GetFileName(OutputPath));

            progress.UpdateProgress(status = status.ChangeMessage(message));
            string redundantLibrary = BiblioSpecLiteSpec.GetRedundantName(OutputPath);
            var    blibBuilder      = new BlibBuild(redundantLibrary, InputFiles, TargetSequences)
            {
                IncludeAmbiguousMatches = IncludeAmbiguousMatches,
                CutOffScore             = CutOffScore,
                Id = Id,
                PreferEmbeddedSpectra = PreferEmbeddedSpectra,
                DebugMode             = DebugMode,
            };

            bool retry = true;

            do
            {
                try
                {
                    if (!blibBuilder.BuildLibrary(Action, progress, ref status,
                                                  out _buildCommandArgs, out _buildOutput, out _ambiguousMatches))
                    {
                        return(false);
                    }

                    retry = false;
                }
                catch (IOException x)
                {
                    if (IsLibraryMissingExternalSpectraError(x, out string spectrumFilename, out string resultsFilepath))
                    {
                        // replace the relative path to the results file (e.g. msms.txt) with the absolute path
                        string fullResultsFilepath = InputFiles.SingleOrDefault(o => o.EndsWith(resultsFilepath)) ??
                                                     throw new InvalidDataException(@"no results filepath from BiblioSpec error message", x);

                        // TODO: this will break if BiblioSpec output is translated to other languages
                        string messageWithFullFilepath =
                            x.Message.Replace(@"search results file '" + resultsFilepath,
                                              @"search results file '" + fullResultsFilepath);

                        var response =
                            progress.UpdateProgress(
                                status.ChangeErrorException(new IOException(messageWithFullFilepath, x)));
                        if (response == UpdateProgressResponse.cancel)
                        {
                            return(false);
                        }
                        else if (response == UpdateProgressResponse.normal)
                        {
                            blibBuilder.PreferEmbeddedSpectra = true;
                        }
                        continue;
                    }

                    progress.UpdateProgress(status.ChangeErrorException(x));
                    return(false);
                }
                catch (Exception x)
                {
                    Console.WriteLine(x.Message);
                    progress.UpdateProgress(status.ChangeErrorException(
                                                new Exception(string.Format(Resources.BiblioSpecLiteBuilder_BuildLibrary_Failed_trying_to_build_the_redundant_library__0__,
                                                                            redundantLibrary))));
                    return(false);
                }
            } while (retry);

            var blibFilter = new BlibFilter();

            status = new ProgressStatus(message);
            progress.UpdateProgress(status);
            // Write the non-redundant library to a temporary file first
            try
            {
                using (var saver = new FileSaver(OutputPath))
                {
                    if (!blibFilter.Filter(redundantLibrary, saver.SafeName, progress, ref status))
                    {
                        return(false);
                    }

                    saver.Commit();
                }
            }
            catch (IOException x)
            {
                progress.UpdateProgress(status.ChangeErrorException(x));
                return(false);
            }
            catch
            {
                progress.UpdateProgress(status.ChangeErrorException(
                                            new Exception(string.Format(Resources.BiblioSpecLiteBuilder_BuildLibrary_Failed_trying_to_build_the_library__0__,
                                                                        OutputPath))));
                return(false);
            }
            finally
            {
                if (!KeepRedundant)
                {
                    FileEx.SafeDelete(redundantLibrary, true);
                }
            }

            return(true);
        }