Exemplo 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");
                }
            }
        }
Exemplo n.º 2
0
        private void ParseSettings()
        {
            // check for ID/spectra files
            if (!spectraFiles.Any())
            {
                throw new Exception("You need to add at least one spectra file!");
            }
            if (!idFiles.Any())
            {
                throw new Exception("You need to add at least one identification file!");
            }

            // fold-change cutoff
            if (!double.TryParse(FoldChangeCutoffManualTextBox.Text, NumberStyles.Number, CultureInfo.InvariantCulture, out double foldChangeCutoff))
            {
                throw new Exception("The fold-change cutoff must be a decimal number");
            }

            settings.ProteinQuantFoldChangeCutoff = double.Parse(FoldChangeCutoffManualTextBox.Text, CultureInfo.InvariantCulture);

            // ppm tolerance
            if (double.TryParse(ppmToleranceTextBox.Text, NumberStyles.Number, CultureInfo.InvariantCulture, out double ppmTolerance))
            {
                settings.PpmTolerance = ppmTolerance;
            }
            else
            {
                throw new Exception("The PPM tolerance must be a decimal number");
            }

            settings.Normalize        = normalizeCheckbox.IsChecked.Value;
            settings.MatchBetweenRuns = mbrCheckbox.IsChecked.Value;
            settings.UseSharedPeptidesForProteinQuant = sharedPeptideCheckbox.IsChecked.Value;
            settings.BayesianProteinQuant             = bayesianCheckbox.IsChecked.Value;

            settings.Integrate                 = integrateCheckBox.IsChecked.Value;
            settings.IdSpecificChargeState     = precursorIdOnlyCheckbox.IsChecked.Value;
            settings.ProteinQuantBaseCondition = (string)ControlConditionComboBox.SelectedItem;
            settings.RequireMsmsIdInCondition  = requireMsmsIdInConditionCheckbox.IsChecked.Value;

            // isotope PPM tolerance
            if (double.TryParse(isotopePpmToleranceTextBox.Text, NumberStyles.Number, CultureInfo.InvariantCulture, out double isotopePpmTolerance))
            {
                settings.IsotopePpmTolerance = isotopePpmTolerance;
            }
            else
            {
                throw new Exception("The isotope PPM tolerance must be a decimal number");
            }

            // num isotopes required
            if (int.TryParse(numIsotopesRequiredTextBox.Text, NumberStyles.Integer, CultureInfo.InvariantCulture, out int numIsotopesRequired))
            {
                settings.NumIsotopesRequired = numIsotopesRequired;
            }
            else
            {
                throw new Exception("The number of isotopes required must be an integer");
            }

            // mcmc iterations
            if (int.TryParse(mcmcIterationsTextBox.Text, out int McmcSteps))
            {
                settings.McmcSteps = McmcSteps;
            }
            else
            {
                throw new Exception("The number of MCMC iterations must be an integer");
            }

            // random seed
            if (int.TryParse(mcmcRandomSeedTextBox.Text, NumberStyles.Integer, CultureInfo.InvariantCulture, out int randomSeed))
            {
                settings.RandomSeed = randomSeed;
            }
            else
            {
                throw new Exception("The random seed must be an integer");
            }

            // MBR time tolerance
            if (double.TryParse(mbrRtWindowTextBox.Text, NumberStyles.Number, CultureInfo.InvariantCulture, out double MbrRtWindow))
            {
                settings.MbrRtWindow = MbrRtWindow;
            }
            else
            {
                throw new Exception("The MBR time window must be a decimal number");
            }

            settings.ValidateSettings(spectraFiles.Select(p => p.SpectraFileInfo).ToList());
        }