Ejemplo n.º 1
0
        private static void Run(FlashLfqSettings settings)
        {
            try
            {
                settings.ValidateCommandLineSettings();
            }
            catch (Exception e)
            {
                if (!settings.Silent)
                {
                    Console.WriteLine("Error: " + e.Message);
                }
                return;
            }

            // check to see if experimental design file exists
            string assumedPathToExpDesign = Path.Combine(settings.SpectraFileRepository, "ExperimentalDesign.tsv");

            if ((settings.Normalize || settings.BayesianProteinQuant) && !File.Exists(assumedPathToExpDesign))
            {
                if (!settings.Silent)
                {
                    Console.WriteLine("Could not find experimental design file " +
                                      "(required for normalization and Bayesian statistical analysis): " + assumedPathToExpDesign);
                }
                return;
            }

            // set up spectra file info
            List <SpectraFileInfo> spectraFileInfos = new List <SpectraFileInfo>();
            List <string>          filePaths        = Directory.GetFiles(settings.SpectraFileRepository)
                                                      .Where(f => acceptedSpectrumFileFormats.Contains(Path.GetExtension(f).ToLowerInvariant())).ToList();

            // check for duplicate file names (agnostic of file extension)
            foreach (var fileName in filePaths.GroupBy(p => Path.GetFileNameWithoutExtension(p)))
            {
                if (fileName.Count() > 1)
                {
                    var types = fileName.Select(p => Path.GetFileNameWithoutExtension(p)).Distinct();

                    if (!settings.Silent)
                    {
                        Console.WriteLine("Multiple spectra files with the same name were detected (maybe " + string.Join(" and ", types) + "?). " +
                                          "Please remove or rename duplicate files from the spectra file directory.");
                    }
                    return;
                }
            }

            if (settings.PrintThermoLicenceViaCommandLine)
            {
                Console.WriteLine(ThermoRawFileReaderLicence.ThermoLicenceText);
                return;
            }

            // check thermo licence agreement
            if (filePaths.Select(v => Path.GetExtension(v).ToLowerInvariant()).Any(f => f == ".raw"))
            {
                var licenceAgreement = LicenceAgreementSettings.ReadLicenceSettings();

                if (!licenceAgreement.HasAcceptedThermoLicence)
                {
                    if (settings.AcceptThermoLicenceViaCommandLine)
                    {
                        if (!settings.ReadOnlyFileSystem)
                        {
                            licenceAgreement.AcceptLicenceAndWrite();
                        }
                    }
                    else
                    {
                        // decided to write this even if it's on silent mode...
                        Console.WriteLine(ThermoRawFileReaderLicence.ThermoLicenceText);
                        Console.WriteLine("\nIn order to search Thermo .raw files, you must agree to the above terms. Do you agree to the above terms? y/n\n");

                        string res = Console.ReadLine();

                        if (res.ToLowerInvariant() == "y")
                        {
                            try
                            {
                                if (!settings.ReadOnlyFileSystem)
                                {
                                    licenceAgreement.AcceptLicenceAndWrite();
                                }
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e.Message);
                            }
                        }
                        else
                        {
                            Console.WriteLine("Thermo licence has been declined. Exiting FlashLFQ. You can still search .mzML and .mgf files without agreeing to the Thermo licence.");
                            return;
                        }
                    }
                }
            }

            if (File.Exists(assumedPathToExpDesign))
            {
                var experimentalDesign = File.ReadAllLines(assumedPathToExpDesign)
                                         .ToDictionary(v => v.Split('\t')[0], v => v);

                foreach (var file in filePaths)
                {
                    string filename = Path.GetFileNameWithoutExtension(file);

                    var expDesignForThisFile = experimentalDesign[filename];
                    var split = expDesignForThisFile.Split('\t');

                    string condition = split[1];
                    int    biorep    = int.Parse(split[2]);
                    int    fraction  = int.Parse(split[3]);
                    int    techrep   = int.Parse(split[4]);

                    // experimental design info passed in here for each spectra file
                    spectraFileInfos.Add(new SpectraFileInfo(fullFilePathWithExtension: file,
                                                             condition: condition,
                                                             biorep: biorep - 1,
                                                             fraction: fraction - 1,
                                                             techrep: techrep - 1));
                }
            }
            else
            {
                for (int i = 0; i < filePaths.Count; i++)
                {
                    var file = filePaths[i];
                    spectraFileInfos.Add(new SpectraFileInfo(fullFilePathWithExtension: file,
                                                             condition: "Default",
                                                             biorep: i,
                                                             fraction: 0,
                                                             techrep: 0));
                }
            }

            // check the validity of the settings and experimental design
            try
            {
                settings.ValidateSettings(spectraFileInfos);
            }
            catch (Exception e)
            {
                if (!settings.Silent)
                {
                    Console.WriteLine("Error: " + e.Message);
                }
                return;
            }

            // set up IDs
            List <Identification> ids;

            try
            {
                ids = PsmReader.ReadPsms(settings.PsmIdentificationPath, settings.Silent, spectraFileInfos);
            }
            catch (Exception e)
            {
                Console.WriteLine("Problem reading PSMs: " + e.Message);
                return;
            }

            if (ids.Any())
            {
                if (!settings.Silent)
                {
                    Console.WriteLine("Setup is OK; read in " + ids.Count + " identifications; starting FlashLFQ engine");
                }

                // write FlashLFQ settings to a file
                if (!Directory.Exists(settings.OutputPath))
                {
                    Directory.CreateDirectory(settings.OutputPath);
                }
                Nett.Toml.WriteFile(settings, Path.Combine(settings.OutputPath, "FlashLfqSettings.toml"));

                // make engine with desired settings
                FlashLfqEngine  engine  = null;
                FlashLfqResults results = null;
                try
                {
                    engine = FlashLfqSettings.CreateEngineWithSettings(settings, ids);

                    // run
                    results = engine.Run();
                }
                catch (Exception ex)
                {
                    string errorReportPath = Directory.GetParent(filePaths.First()).FullName;
                    if (settings.OutputPath != null)
                    {
                        errorReportPath = settings.OutputPath;
                    }

                    if (!settings.Silent)
                    {
                        Console.WriteLine("FlashLFQ has crashed with the following error: " + ex.Message +
                                          ".\nError report written to " + errorReportPath);
                    }

                    OutputWriter.WriteErrorReport(ex, Directory.GetParent(filePaths.First()).FullName, settings.OutputPath);
                }

                // output
                if (results != null)
                {
                    try
                    {
                        OutputWriter.WriteOutput(settings.PsmIdentificationPath, results, settings.Silent, settings.OutputPath);
                    }
                    catch (Exception ex)
                    {
                        if (!settings.Silent)
                        {
                            Console.WriteLine("Could not write FlashLFQ output: " + ex.Message);
                        }
                    }
                }
            }
            else
            {
                if (!settings.Silent)
                {
                    Console.WriteLine("No peptide IDs for the specified spectra files were found! " +
                                      "Check to make sure the spectra file names match between the ID file and the spectra files");
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Runs the FlashLFQ engine with the user's defined spectra files, ID files, and FlashLFQ
        /// settings.
        /// </summary>
        private void RunFlashLfq()
        {
            // read IDs
            var ids = new List <Identification>();

            try
            {
                foreach (var identFile in idFiles)
                {
                    ids = ids.Concat(PsmReader.ReadPsms(identFile.FilePath, false, spectraFiles.Select(p => p.SpectraFileInfo).ToList())).ToList();
                }
            }
            catch (Exception e)
            {
                string errorReportPath = Directory.GetParent(spectraFiles.First().FilePath).FullName;
                if (outputFolderPath != null)
                {
                    errorReportPath = outputFolderPath;
                }

                try
                {
                    OutputWriter.WriteErrorReport(e, Directory.GetParent(spectraFiles.First().FilePath).FullName,
                                                  outputFolderPath);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show("FlashLFQ has crashed with the following error: " + e.Message +
                                    ".\nThe error report could not be written: " + ex2.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Hand);

                    return;
                }

                MessageBox.Show("FlashLFQ could not read the PSM file: " + e.Message +
                                ".\nError report written to " + errorReportPath, "Error", MessageBoxButton.OK, MessageBoxImage.Hand);

                return;
            }

            if (!ids.Any())
            {
                MessageBox.Show("No peptide IDs for the specified spectra files were found! " +
                                "Check to make sure the spectra file names match between the ID file and the spectra files",
                                "Error", MessageBoxButton.OK, MessageBoxImage.Hand);

                return;
            }

            // run FlashLFQ engine
            try
            {
                flashLfqEngine = FlashLfqSettings.CreateEngineWithSettings(settings, ids);

                results = flashLfqEngine.Run();
            }
            catch (Exception ex)
            {
                string errorReportPath = Directory.GetParent(spectraFiles.First().FilePath).FullName;

                if (outputFolderPath != null)
                {
                    errorReportPath = outputFolderPath;
                }

                try
                {
                    OutputWriter.WriteErrorReport(ex, Directory.GetParent(spectraFiles.First().FilePath).FullName,
                                                  outputFolderPath);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show("FlashLFQ has crashed with the following error: " + ex.Message +
                                    ".\nThe error report could not be written: " + ex2.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Hand);

                    return;
                }

                MessageBox.Show("FlashLFQ has crashed with the following error: " + ex.Message +
                                ".\nError report written to " + errorReportPath, "Error", MessageBoxButton.OK, MessageBoxImage.Hand);

                return;
            }

            // write output
            if (results != null)
            {
                try
                {
                    OutputWriter.WriteOutput(Directory.GetParent(spectraFiles.First().FilePath).FullName, results, flashLfqEngine.Silent,
                                             outputFolderPath);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Could not write FlashLFQ output: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Hand);

                    return;
                }
            }
        }
Ejemplo n.º 3
0
        ///
        /// The purpose of this unit test is to ensure that the settings passed by the user through the command-line or the GUI
        /// are passed propertly into the FlashLFQ engine.
        ///
        public static void TestSettingsPassing()
        {
            // make settings
            FlashLfqSettings settings = new FlashLfqSettings();

            // set the settings to non-default values
            var properties = settings.GetType().GetProperties();

            foreach (var property in properties)
            {
                Type type = property.PropertyType;

                if (type == typeof(string))
                {
                    property.SetValue(settings, "TEST_VALUE");
                }
                else if (type == typeof(bool) || type == typeof(bool?))
                {
                    if (property.GetValue(settings) == null || (bool)property.GetValue(settings) == false)
                    {
                        property.SetValue(settings, true);
                    }
                    else
                    {
                        property.SetValue(settings, false);
                    }
                }
                else if (type == typeof(double) || type == typeof(double?))
                {
                    property.SetValue(settings, double.MinValue);
                }
                else if (type == typeof(int) || type == typeof(int?))
                {
                    property.SetValue(settings, int.MinValue);
                }
                else
                {
                    Assert.IsTrue(false);
                }
            }

            settings.MaxThreads = 1;

            FlashLfqEngine e = FlashLfqSettings.CreateEngineWithSettings(settings, new List <Identification>());

            var engineProperties = e.GetType().GetFields();

            // check to make sure the settings got passed properly into the engine (the settings should have identical values)
            foreach (var property in properties)
            {
                string name = property.Name;

                // skip settings that don't exist in the FlashLFQ engine
                // these are usually just command-line options for i/o stuff, etc.
                if (name == "PsmIdentificationPath" ||
                    name == "SpectraFileRepository" ||
                    name == "OutputPath" ||
                    name == "ReadOnlyFileSystem" ||
                    name == "PrintThermoLicenceViaCommandLine" ||
                    name == "AcceptThermoLicenceViaCommandLine")
                {
                    continue;
                }

                var engineProperty = engineProperties.First(p => p.Name == name);

                var settingsValue = property.GetValue(settings);
                var engineValue   = engineProperty.GetValue(e);

                Assert.AreEqual(settingsValue, engineValue);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Runs the FlashLFQ engine with the user's defined spectra files, ID files, and FlashLFQ
        /// settings.
        /// </summary>
        private void RunFlashLfq()
        {
            // read IDs
            var ids = new List <Identification>();

            try
            {
                foreach (var identFile in idFiles)
                {
                    ids = ids.Concat(PsmReader.ReadPsms(identFile.FilePath, false, spectraFiles.Select(p => p.SpectraFileInfo).ToList())).ToList();
                }
            }
            catch (Exception e)
            {
                string errorReportPath = Directory.GetParent(spectraFiles.First().FilePath).FullName;
                if (outputFolderPath != null)
                {
                    errorReportPath = outputFolderPath;
                }

                try
                {
                    OutputWriter.WriteErrorReport(e, Directory.GetParent(spectraFiles.First().FilePath).FullName,
                                                  outputFolderPath);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show("FlashLFQ has crashed with the following error: " + e.Message +
                                    ".\nThe error report could not be written: " + ex2.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Hand);

                    return;
                }

                MessageBox.Show("FlashLFQ could not read the PSM file: " + e.Message +
                                ".\nError report written to " + errorReportPath, "Error", MessageBoxButton.OK, MessageBoxImage.Hand);

                return;
            }

            if (!ids.Any())
            {
                MessageBox.Show("No peptide IDs for the specified spectra files were found! " +
                                "Check to make sure the spectra file names match between the ID file and the spectra files",
                                "Error", MessageBoxButton.OK, MessageBoxImage.Hand);

                return;
            }

            if (ids.Any(p => p.Ms2RetentionTimeInMinutes > 500))
            {
                var res = MessageBox.Show("It seems that some of the retention times in the PSM file(s) are in seconds and not minutes; FlashLFQ requires the RT to be in minutes. " +
                                          "Continue with the FlashLFQ run? (only click yes if the RTs are actually in minutes)",
                                          "Error", MessageBoxButton.YesNo, MessageBoxImage.Hand);

                if (res == MessageBoxResult.No)
                {
                    return;
                }
            }

            // run FlashLFQ engine
            try
            {
                flashLfqEngine = FlashLfqSettings.CreateEngineWithSettings(settings, ids);

                results = flashLfqEngine.Run();
            }
            catch (Exception ex)
            {
                string errorReportPath = Directory.GetParent(spectraFiles.First().FilePath).FullName;

                if (outputFolderPath != null)
                {
                    errorReportPath = outputFolderPath;
                }

                try
                {
                    OutputWriter.WriteErrorReport(ex, Directory.GetParent(spectraFiles.First().FilePath).FullName,
                                                  outputFolderPath);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show("FlashLFQ has crashed with the following error: " + ex.Message +
                                    ".\nThe error report could not be written: " + ex2.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Hand);

                    return;
                }

                MessageBox.Show("FlashLFQ has crashed with the following error: " + ex.Message +
                                ".\nError report written to " + errorReportPath, "Error", MessageBoxButton.OK, MessageBoxImage.Hand);

                return;
            }

            // write output
            if (results != null)
            {
                try
                {
                    OutputWriter.WriteOutput(Directory.GetParent(spectraFiles.First().FilePath).FullName, results, flashLfqEngine.Silent,
                                             outputFolderPath);

                    MessageBox.Show("Run complete");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Could not write FlashLFQ output: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Hand);

                    return;
                }
            }
        }