Exemplo n.º 1
0
        public static void TestMultipleFilesRunner()
        {
            foreach (var modFile in Directory.GetFiles(@"Mods"))
            {
                GlobalVariables.AddMods(PtmListLoader.ReadModsFromFile(modFile));
            }

            CalibrationTask task1 = new CalibrationTask
            {
                CommonParameters = new CommonParameters
                                   (
                    digestionParams: new DigestionParams(maxMissedCleavages: 0, minPeptideLength: 1, initiatorMethionineBehavior: InitiatorMethionineBehavior.Retain),
                    listOfModsVariable: new List <(string, string)> {
                    ("Common Variable", "Oxidation of M")
                },
Exemplo n.º 2
0
        public static void TestMultipleFilesRunner()
        {
            #region Setup tasks

            foreach (var modFile in Directory.GetFiles(@"Mods"))
            {
                GlobalVariables.AddMods(PtmListLoader.ReadModsFromFile(modFile));
            }

            CalibrationTask task1 = new CalibrationTask
            {
                CommonParameters = new CommonParameters
                {
                    DigestionParams = new DigestionParams
                    {
                        MaxMissedCleavages          = 0,
                        MinPeptideLength            = null,
                        InitiatorMethionineBehavior = InitiatorMethionineBehavior.Retain
                    },
                    ListOfModsVariable = new List <(string, string)> {
                        ("Common Variable", "Oxidation of M")
                    },
Exemplo n.º 3
0
        private static void Main(string[] args)
        {
            Console.WriteLine("Welcome to MetaMorpheus");
            Console.WriteLine(GlobalVariables.MetaMorpheusVersion);

            var p = new FluentCommandLineParser <ApplicationArguments>();

            p.Setup(arg => arg.Tasks)
            .As('t', "tasks")
            .SetDefault(new List <string>())
            .WithDescription("Single-task TOMLs.");

            p.Setup(arg => arg.OutputFolder)
            .As('o', "outputFolder")
            .SetDefault(null)
            .WithDescription("Folder into which to place results.");

            p.Setup(arg => arg.MetaTasks)
            .As('m', "meta-task")
            .SetDefault(new List <string>())
            .WithDescription("Multi-task TOMLs.");

            p.Setup(arg => arg.Spectra)
            .As('s', "spectra")
            .Required()
            .WithDescription("Spectra to analyze.");

            p.Setup(arg => arg.Databases)
            .As('d', "databases")
            .Required()
            .WithDescription("Protein sequence databases (FASTA, XML).");

            p.SetupHelp("h", "help")
            .Callback(text => Console.WriteLine(text));

            var result = p.Parse(args);

            if (p.Object.MetaTasks != null && (p.Object.MetaTasks.Count != 0 || p.Object.Tasks.Count != 0))
            {
                if (!result.HasErrors)
                {
                    MetaMorpheusEngine.WarnHandler                 += WarnHandler;
                    MetaMorpheusEngine.OutProgressHandler          += MyEngine_outProgressHandler;
                    MetaMorpheusEngine.StartingSingleEngineHander  += MyEngine_startingSingleEngineHander;
                    MetaMorpheusEngine.FinishedSingleEngineHandler += MyEngine_finishedSingleEngineHandler;

                    MetaMorpheusTask.WarnHandler += WarnHandler;
                    MetaMorpheusTask.LogHandler  += LogHandler;
                    MetaMorpheusTask.StartingSingleTaskHander   += MyTaskEngine_startingSingleTaskHander;
                    MetaMorpheusTask.FinishedSingleTaskHandler  += MyTaskEngine_finishedSingleTaskHandler;
                    MetaMorpheusTask.FinishedWritingFileHandler += MyTaskEngine_finishedWritingFileHandler;

                    foreach (var db in p.Object.Databases)
                    {
                        if (!Path.GetExtension(db).Equals(".fasta"))
                        {
                            GlobalVariables.AddMods(UsefulProteomicsDatabases.ProteinDbLoader.GetPtmListFromProteinXml(db).OfType <Modification>(), true);

                            // print any error messages reading the mods to the console
                            foreach (var error in GlobalVariables.ErrorsReadingMods)
                            {
                                Console.WriteLine(error);
                            }
                            GlobalVariables.ErrorsReadingMods.Clear();
                        }
                    }

                    List <(string, MetaMorpheusTask)> taskList = new List <(string, MetaMorpheusTask)>();

                    for (int i = 0; i < p.Object.Tasks.Count; i++)
                    {
                        var filePath = p.Object.Tasks[i];

                        var uhum = Toml.ReadFile(filePath, MetaMorpheusTask.tomlConfig);

                        switch (uhum.Get <string>("TaskType"))
                        {
                        case "Search":
                            var ye1 = Toml.ReadFile <SearchTask>(filePath, MetaMorpheusTask.tomlConfig);
                            taskList.Add(("Task" + (i + 1) + "SearchTask", ye1));
                            break;

                        case "Calibrate":
                            var ye2 = Toml.ReadFile <CalibrationTask>(filePath, MetaMorpheusTask.tomlConfig);
                            taskList.Add(("Task" + (i + 1) + "CalibrationTask", ye2));
                            break;

                        case "Gptmd":
                            var ye3 = Toml.ReadFile <GptmdTask>(filePath, MetaMorpheusTask.tomlConfig);
                            taskList.Add(("Task" + (i + 1) + "GptmdTask", ye3));
                            break;

                        case "XLSearch":
                            var ye4 = Toml.ReadFile <XLSearchTask>(filePath, MetaMorpheusTask.tomlConfig);
                            taskList.Add(("Task" + (i + 1) + "XLSearchTask", ye4));
                            break;

                        default:
                            Console.WriteLine(uhum.Get <string>("TaskType") + " is not a known task type! Skipping.");
                            break;
                        }
                    }

                    for (int i = 0; i < p.Object.MetaTasks.Count; i++)
                    {
                        var filePath = p.Object.MetaTasks[i];
                        var uhum     = Toml.ReadFile(filePath, MetaMorpheusTask.tomlConfig);
                        switch (uhum.Get <string>("TaskType"))
                        {
                        case "Search":
                            Console.WriteLine("Search tasks are individual tasks. Please use -t for task instead of -m. Skipping.");
                            break;

                        case "Calibrate":
                            Console.WriteLine("Calibrate tasks are individual tasks. Please use -t for task instead of -m. Skipping.");
                            break;

                        case "Gptmd":
                            Console.WriteLine("Gptmd tasks are individual tasks. Please use -t for task instead of -m. Skipping.");
                            break;

                        case "XLSearch":
                            Console.WriteLine("XLSearch tasks are individual tasks. Please use -t for task instead of -m. Skipping.");
                            break;

                        default:
                            Console.WriteLine(uhum.Get <string>("TaskType") + " is not a known task type! Skipping.");
                            break;
                        }
                    }

                    List <string>    startingRawFilenameList   = p.Object.Spectra.Select(b => Path.GetFullPath(b)).ToList();
                    List <DbForTask> startingXmlDbFilenameList = p.Object.Databases.Select(b => new DbForTask(Path.GetFullPath(b), IsContaminant(b))).ToList();

                    string outputFolder = p.Object.OutputFolder;
                    if (outputFolder == null)
                    {
                        var pathOfFirstSpectraFile = Path.GetDirectoryName(startingRawFilenameList.First());
                        outputFolder = Path.Combine(pathOfFirstSpectraFile, @"$DATETIME");
                    }

                    EverythingRunnerEngine a = new EverythingRunnerEngine(taskList, startingRawFilenameList, startingXmlDbFilenameList, outputFolder);

                    try
                    {
                        a.Run();
                    }
                    catch (Exception e)
                    {
                        while (e.InnerException != null)
                        {
                            e = e.InnerException;
                        }
                        var message = "Run failed, Exception: " + e.Message;
                        Console.WriteLine(message);
                    }
                }
                else
                {
                    Console.WriteLine("Error Text:" + result.ErrorText);
                }
            }
            else
            {
                Console.WriteLine("Error Text: No toml file was specified. Use -t for tasks or -m for meta-tasks.");
            }
        }
Exemplo n.º 4
0
        private static int Run(CommandLineSettings settings)
        {
            int errorCode = 0;

            if (settings.Verbosity == CommandLineSettings.VerbosityType.minimal || settings.Verbosity == CommandLineSettings.VerbosityType.normal)
            {
                Console.WriteLine("Welcome to MetaMorpheus");
            }

            if (settings.CustomDataDirectory != null)
            {
                GlobalVariables.UserSpecifiedDataDir = settings.CustomDataDirectory;
            }

            GlobalVariables.SetUpGlobalVariables();

            if (settings.Verbosity == CommandLineSettings.VerbosityType.minimal || settings.Verbosity == CommandLineSettings.VerbosityType.normal)
            {
                Console.WriteLine(GlobalVariables.MetaMorpheusVersion);
            }

            try
            {
                settings.ValidateCommandLineSettings();
                CommandLineSettings = settings;
            }
            catch (Exception e)
            {
                if (settings.Verbosity == CommandLineSettings.VerbosityType.minimal || settings.Verbosity == CommandLineSettings.VerbosityType.normal)
                {
                    Console.WriteLine("MetaMorpheus encountered the following error:" + Environment.NewLine + e.Message);
                }
                errorCode = 2;

                return(errorCode);
            }

            if (settings.GenerateDefaultTomls)
            {
                if (settings.Verbosity == CommandLineSettings.VerbosityType.minimal || settings.Verbosity == CommandLineSettings.VerbosityType.normal)
                {
                    Console.WriteLine("Generating default tomls at location: " + settings.OutputFolder);
                }
                CommandLineSettings.GenerateDefaultTaskTomls(settings.OutputFolder);

                return(errorCode);
            }

            // set up microvignette
            if (settings.RunMicroVignette)
            {
                // set up the spectra file
                settings.Spectra.Clear();
                settings.Spectra.Add(Path.Combine(GlobalVariables.DataDir, @"Data", "SmallCalibratible_Yeast.mzML"));

                // set up the database
                settings.Databases.Clear();
                settings.Databases.Add(Path.Combine(GlobalVariables.DataDir, @"Data", "SmallYeast.fasta"));

                // set up the tasks (calibration, GPTMD, search)
                settings.Tasks.Clear();
                CommandLineSettings.GenerateDefaultTaskTomls(settings.OutputFolder);
                settings.Tasks.Add(Path.Combine(settings.OutputFolder, "CalibrationTask.toml"));
                settings.Tasks.Add(Path.Combine(settings.OutputFolder, "GptmdTask.toml"));
                settings.Tasks.Add(Path.Combine(settings.OutputFolder, "SearchTask.toml"));
            }

            MetaMorpheusEngine.WarnHandler                 += WarnHandler;
            MetaMorpheusEngine.OutProgressHandler          += MyEngine_outProgressHandler;
            MetaMorpheusEngine.StartingSingleEngineHander  += MyEngine_startingSingleEngineHander;
            MetaMorpheusEngine.FinishedSingleEngineHandler += MyEngine_finishedSingleEngineHandler;

            MetaMorpheusTask.WarnHandler += WarnHandler;
            MetaMorpheusTask.LogHandler  += LogHandler;
            MetaMorpheusTask.StartingSingleTaskHander   += MyTaskEngine_startingSingleTaskHander;
            MetaMorpheusTask.FinishedSingleTaskHandler  += MyTaskEngine_finishedSingleTaskHandler;
            MetaMorpheusTask.FinishedWritingFileHandler += MyTaskEngine_finishedWritingFileHandler;

            bool containsRawFiles = settings.Spectra.Select(v => Path.GetExtension(v).ToLowerInvariant()).Any(v => v == ".raw");

            if (containsRawFiles && !GlobalVariables.GlobalSettings.UserHasAgreedToThermoRawFileReaderLicence)
            {
                // write the Thermo RawFileReader licence agreement
                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().ToLowerInvariant();
                if (res == "y")
                {
                    var newGlobalSettings = new GlobalSettings
                    {
                        UserHasAgreedToThermoRawFileReaderLicence = true,
                        WriteExcelCompatibleTSVs = GlobalVariables.GlobalSettings.WriteExcelCompatibleTSVs
                    };

                    Toml.WriteFile <GlobalSettings>(newGlobalSettings, Path.Combine(GlobalVariables.DataDir, @"settings.toml"));
                    GlobalVariables.GlobalSettings = newGlobalSettings;
                }
                else
                {
                    Console.WriteLine("Thermo licence has been declined. Exiting MetaMorpheus. You can still search .mzML and .mgf files without agreeing to the Thermo licence.");
                    errorCode = 3;
                    return(errorCode);
                }
            }

            foreach (var db in settings.Databases)
            {
                if (!Path.GetExtension(db).Equals(".fasta"))
                {
                    GlobalVariables.AddMods(UsefulProteomicsDatabases.ProteinDbLoader.GetPtmListFromProteinXml(db).OfType <Modification>(), true);

                    // print any error messages reading the mods to the console
                    foreach (var error in GlobalVariables.ErrorsReadingMods)
                    {
                        if (settings.Verbosity == CommandLineSettings.VerbosityType.minimal || settings.Verbosity == CommandLineSettings.VerbosityType.normal)
                        {
                            Console.WriteLine(error);
                        }
                    }

                    GlobalVariables.ErrorsReadingMods.Clear();
                }
            }

            List <(string, MetaMorpheusTask)> taskList = new List <(string, MetaMorpheusTask)>();

            var tasks = settings.Tasks.ToList();

            for (int i = 0; i < tasks.Count; i++)
            {
                var filePath = tasks[i];

                var toml = Toml.ReadFile(filePath, MetaMorpheusTask.tomlConfig);

                switch (toml.Get <string>("TaskType"))
                {
                case "Search":
                    var searchTask = Toml.ReadFile <SearchTask>(filePath, MetaMorpheusTask.tomlConfig);
                    taskList.Add(("Task" + (i + 1) + "SearchTask", searchTask));
                    break;

                case "Calibrate":
                    var calibrationTask = Toml.ReadFile <CalibrationTask>(filePath, MetaMorpheusTask.tomlConfig);
                    taskList.Add(("Task" + (i + 1) + "CalibrationTask", calibrationTask));
                    break;

                case "Gptmd":
                    var GptmdTask = Toml.ReadFile <GptmdTask>(filePath, MetaMorpheusTask.tomlConfig);
                    taskList.Add(("Task" + (i + 1) + "GptmdTask", GptmdTask));
                    break;

                case "XLSearch":
                    var XlTask = Toml.ReadFile <XLSearchTask>(filePath, MetaMorpheusTask.tomlConfig);
                    taskList.Add(("Task" + (i + 1) + "XLSearchTask", XlTask));
                    break;

                case "GlycoSearch":
                    var GlycoTask = Toml.ReadFile <GlycoSearchTask>(filePath, MetaMorpheusTask.tomlConfig);
                    taskList.Add(("Task" + (i + 1) + "GlycoSearchTask", GlycoTask));
                    break;

                default:
                    if (settings.Verbosity == CommandLineSettings.VerbosityType.minimal || settings.Verbosity == CommandLineSettings.VerbosityType.normal)
                    {
                        Console.WriteLine(toml.Get <string>("TaskType") + " is not a known task type! Skipping.");
                    }
                    break;
                }
            }

            List <string>    startingRawFilenameList   = settings.Spectra.Select(b => Path.GetFullPath(b)).ToList();
            List <DbForTask> startingXmlDbFilenameList = settings.Databases.Select(b => new DbForTask(Path.GetFullPath(b), IsContaminant(b))).ToList();

            // check that experimental design is defined if normalization is enabled
            var searchTasks = taskList
                              .Where(p => p.Item2.TaskType == MyTask.Search)
                              .Select(p => (SearchTask)p.Item2);

            string pathToExperDesign = Directory.GetParent(startingRawFilenameList.First()).FullName;

            pathToExperDesign = Path.Combine(pathToExperDesign, GlobalVariables.ExperimentalDesignFileName);

            if (!File.Exists(pathToExperDesign))
            {
                if (searchTasks.Any(p => p.SearchParameters.Normalize))
                {
                    if (settings.Verbosity == CommandLineSettings.VerbosityType.minimal || settings.Verbosity == CommandLineSettings.VerbosityType.normal)
                    {
                        Console.WriteLine("Experimental design file was missing! This must be defined to do normalization. Download a template from https://github.com/smith-chem-wisc/MetaMorpheus/wiki/Experimental-Design");
                    }
                    return(5);
                }
            }
            else
            {
                ExperimentalDesign.ReadExperimentalDesign(pathToExperDesign, startingRawFilenameList, out var errors);

                if (errors.Any())
                {
                    if (searchTasks.Any(p => p.SearchParameters.Normalize))
                    {
                        if (settings.Verbosity == CommandLineSettings.VerbosityType.minimal || settings.Verbosity == CommandLineSettings.VerbosityType.normal)
                        {
                            foreach (var error in errors)
                            {
                                Console.WriteLine(error);
                            }
                        }
                        return(5);
                    }
                    else
                    {
                        if (settings.Verbosity == CommandLineSettings.VerbosityType.minimal || settings.Verbosity == CommandLineSettings.VerbosityType.normal)
                        {
                            Console.WriteLine("An experimental design file was found, but an error " +
                                              "occurred reading it. Do you wish to continue with an empty experimental design? (This will delete your experimental design file) y/n" +
                                              "\nThe error was: " + errors.First());

                            var result = Console.ReadLine();

                            if (result.ToLowerInvariant() == "y" || result.ToLowerInvariant() == "yes")
                            {
                                File.Delete(pathToExperDesign);
                            }
                            else
                            {
                                return(5);
                            }
                        }
                        else
                        {
                            // just continue on if verbosity is on "none"
                            File.Delete(pathToExperDesign);
                        }
                    }
                }
                else
                {
                    if (settings.Verbosity == CommandLineSettings.VerbosityType.minimal || settings.Verbosity == CommandLineSettings.VerbosityType.normal)
                    {
                        Console.WriteLine("Read ExperimentalDesign.tsv successfully");
                    }
                }
            }

            EverythingRunnerEngine a = new EverythingRunnerEngine(taskList, startingRawFilenameList, startingXmlDbFilenameList, settings.OutputFolder);

            try
            {
                a.Run();
            }
            catch (Exception e)
            {
                while (e.InnerException != null)
                {
                    e = e.InnerException;
                }

                var message = "Run failed, Exception: " + e.Message;

                if (settings.Verbosity == CommandLineSettings.VerbosityType.minimal || settings.Verbosity == CommandLineSettings.VerbosityType.normal)
                {
                    Console.WriteLine(message);
                }
                errorCode = 4;
            }

            return(errorCode);
        }
Exemplo n.º 5
0
        public static void TestPrunedDatabase()
        {
            //Create Search Task
            SearchTask task1 = new SearchTask
            {
                SearchParameters = new SearchParameters
                {
                    WritePrunedDatabase  = true,
                    SearchTarget         = true,
                    MassDiffAcceptorType = MassDiffAcceptorType.Exact,
                    ModsToWriteSelection = new Dictionary <string, int>
                    {
                        { "ConnorModType", 1 }
                    }
                },
                CommonParameters = new CommonParameters(digestionParams: new DigestionParams(minPeptideLength: 5))
            };

            //add task to task list
            List <(string, MetaMorpheusTask)> taskList = new List <(string, MetaMorpheusTask)>
            {
                ("task1", task1)
            };

            ModificationMotif.TryGetMotif("P", out ModificationMotif motif);

            var connorMod = new Modification(_originalId: "ConnorMod on P", _modificationType: "ConnorModType", _target: motif, _locationRestriction: "Anywhere.", _monoisotopicMass: 10);

            GlobalVariables.AddMods(new List <Modification>
            {
                connorMod
            }, false);

            //create modification lists
            List <Modification> variableModifications = GlobalVariables.AllModsKnown.OfType <Modification>()
                                                        .Where(b => task1.CommonParameters.ListOfModsVariable.Contains((b.ModificationType, b.IdWithMotif))).ToList();

            //add modification to Protein object
            var          dictHere  = new Dictionary <int, List <Modification> >();
            Modification modToAdd  = connorMod;
            Modification modToAdd2 = connorMod;

            dictHere.Add(1, new List <Modification> {
                modToAdd
            });
            dictHere.Add(3, new List <Modification> {
                modToAdd2
            });

            //protein Creation (One with mod and one without)
            Protein TestProteinWithMod = new Protein("PEPTID", "accession1", "organism", new List <Tuple <string, string> >(), dictHere);

            //First Write XML Database
            string xmlName = "okkk.xml";

            //Add Mod to list and write XML input database
            Dictionary <string, HashSet <Tuple <int, Modification> > > modList = new Dictionary <string, HashSet <Tuple <int, Modification> > >();
            var Hash = new HashSet <Tuple <int, Modification> >
            {
                new Tuple <int, Modification>(3, modToAdd)
            };

            modList.Add("test", Hash);
            ProteinDbWriter.WriteXmlDatabase(modList, new List <Protein> {
                TestProteinWithMod
            }, xmlName);

            //now write MZML file
            var protein = ProteinDbLoader.LoadProteinXML(xmlName, true,
                                                         DecoyType.Reverse, new List <Modification>(), false, new List <string>(), out Dictionary <string, Modification> ok);

            //Dictionary 'ok' contains unknown modifications. There are no unknown modifications in this test.
            Assert.AreEqual(0, ok.Count);
            //One protein is read from the .xml database and one decoy is created. Therefore, the list of proteins contains 2 entries.
            Assert.AreEqual(2, protein.Count);
            //The original database had two localized mods on the protein. Therefore. both protein and decoy should have two mods.
            Assert.AreEqual(2, protein[0].OneBasedPossibleLocalizedModifications.Count);
            List <int> foundResidueIndicies   = protein[0].OneBasedPossibleLocalizedModifications.Select(k => k.Key).ToList();
            List <int> expectedResidueIndices = new List <int>()
            {
                1, 3
            };

            Assert.That(foundResidueIndicies, Is.EquivalentTo(expectedResidueIndices));
            Assert.AreEqual(2, protein[1].OneBasedPossibleLocalizedModifications.Count);
            foundResidueIndicies   = protein[1].OneBasedPossibleLocalizedModifications.Select(k => k.Key).ToList();
            expectedResidueIndices = new List <int>()
            {
                4, 6
            };                                                 //originally modified residues are now at the end in the decoy
            Assert.That(foundResidueIndicies, Is.EquivalentTo(expectedResidueIndices));

            var thisOk = ok;                                                      //for debugging
            var commonParamsAtThisPoint = task1.CommonParameters.DigestionParams; //for debugging

            var digestedList = protein[0].Digest(task1.CommonParameters.DigestionParams, new List <Modification> {
            },
                                                 variableModifications).ToList();

            Assert.AreEqual(4, digestedList.Count);

            //Set Peptide with 1 mod at position 3
            PeptideWithSetModifications pepWithSetMods1 = digestedList[1];

            //Finally Write MZML file
            Assert.AreEqual("PEP[ConnorModType:ConnorMod on P]TID", pepWithSetMods1.FullSequence);//this might be base sequence
            MsDataFile myMsDataFile = new TestDataFile(new List <PeptideWithSetModifications> {
                pepWithSetMods1
            });
            string mzmlName = @"hello.mzML";

            IO.MzML.MzmlMethods.CreateAndWriteMyMzmlWithCalibratedSpectra(myMsDataFile, mzmlName, false);

            //run!
            string outputFolder = Path.Combine(TestContext.CurrentContext.TestDirectory, @"TestPrunedDatabase");
            var    engine       = new EverythingRunnerEngine(taskList, new List <string> {
                mzmlName
            },
                                                             new List <DbForTask> {
                new DbForTask(xmlName, false)
            }, outputFolder);

            engine.Run();

            string final = Path.Combine(MySetUpClass.outputFolder, "task1", "okkkpruned.xml");

            var proteins = ProteinDbLoader.LoadProteinXML(final, true, DecoyType.Reverse, new List <Modification>(), false, new List <string>(), out ok);

            //check length
            Assert.AreEqual(1, proteins[0].OneBasedPossibleLocalizedModifications.Count);
            //check location (key)
            Assert.AreEqual(true, proteins[0].OneBasedPossibleLocalizedModifications.ContainsKey(3));
            List <Modification> listOfMods = proteins[0].OneBasedPossibleLocalizedModifications[3];

            //check Type, count, ID
            Assert.AreEqual(listOfMods[0].ModificationType, "ConnorModType");
            Assert.AreEqual(listOfMods[0].IdWithMotif, "ConnorMod on P");
            Assert.AreEqual(listOfMods.Count, 1);
            Directory.Delete(outputFolder, true);
            File.Delete(xmlName);
            File.Delete(mzmlName);
        }
Exemplo n.º 6
0
        public static void TestUserModSelectionInPrunedDB()
        {
            List <(string, string)> listOfModsFixed = new List <(string, string)> {
                ("Common Fixed", "Carbamidomethyl of C"), ("Common Fixed", "Carbamidomethyl of U")
            };
            //Create Search Task
            SearchTask task5 = new SearchTask
            {
                SearchParameters = new SearchParameters
                {
                    WritePrunedDatabase  = true,
                    SearchTarget         = true,
                    MassDiffAcceptorType = MassDiffAcceptorType.Exact,
                },
                CommonParameters = new CommonParameters(listOfModsFixed: listOfModsFixed)
            };

            task5.SearchParameters.ModsToWriteSelection["Mod"]          = 0;
            task5.SearchParameters.ModsToWriteSelection["Common Fixed"] = 1;
            task5.SearchParameters.ModsToWriteSelection["Glycan"]       = 2;
            task5.SearchParameters.ModsToWriteSelection["missing"]      = 3;

            //add task 1 to task list
            List <(string, MetaMorpheusTask)> taskList = new List <(string, MetaMorpheusTask)> {
                ("task5", task5)
            };

            ModificationMotif.TryGetMotif("P", out ModificationMotif motif);
            ModificationMotif.TryGetMotif("E", out ModificationMotif motif2);

            var connorMod  = new Modification(_originalId: "ModToNotAppear", _modificationType: "Mod", _target: motif, _locationRestriction: "Anywhere.", _monoisotopicMass: 10);
            var connorMod2 = new Modification(_originalId: "Default(Mod in DB and Observed)", _modificationType: "Common Fixed", _target: motif, _locationRestriction: "Anywhere.", _monoisotopicMass: 10);
            var connorMod3 = new Modification(_originalId: "ModToAlwaysAppear", _modificationType: "Glycan", _target: motif, _locationRestriction: "Anywhere.", _monoisotopicMass: 10);
            var connorMod4 = new Modification(_originalId: "ModObservedNotinDB", _modificationType: "missing", _target: motif2, _locationRestriction: "Anywhere.", _monoisotopicMass: 5);

            GlobalVariables.AddMods(new List <Modification>
            {
                connorMod,
                connorMod2,
                connorMod3,
                connorMod4
            }, false);

            //create modification lists
            List <Modification> variableModifications = GlobalVariables.AllModsKnown.OfType <Modification>().Where(b => task5.CommonParameters.ListOfModsVariable.Contains
                                                                                                                       ((b.ModificationType, b.IdWithMotif))).ToList();
            List <Modification> fixedModifications = GlobalVariables.AllModsKnown.OfType <Modification>().Where(b => task5.CommonParameters.ListOfModsFixed.Contains
                                                                                                                    ((b.ModificationType, b.IdWithMotif))).ToList();

            //add modification to Protein object
            var          dictHere  = new Dictionary <int, List <Modification> >();
            Modification modToAdd  = connorMod;
            Modification modToAdd2 = connorMod2;
            Modification modToAdd3 = connorMod3;
            Modification modToAdd4 = connorMod4;

            //add Fixed modifcation so can test if mod that is observed and not in DB
            fixedModifications.Add(connorMod4);
            listOfModsFixed.Add((connorMod4.ModificationType, connorMod4.IdWithMotif));

            dictHere.Add(1, new List <Modification> {
                modToAdd
            });
            dictHere.Add(2, new List <Modification> {
                modToAdd2
            });                                                    //default
            dictHere.Add(3, new List <Modification> {
                modToAdd3
            });                                                    //Alway Appear

            var dictHere2 = new Dictionary <int, List <Modification> >
            {
                { 1, new List <Modification> {
                      modToAdd
                  } },
                { 2, new List <Modification> {
                      modToAdd2
                  } },                                       //default
                { 3, new List <Modification> {
                      modToAdd3
                  } },                                       //Alway Appear
                { 4, new List <Modification> {
                      modToAdd4
                  } }                                      //observed
            };

            //protein Creation (One with mod and one without)
            Protein TestProteinWithModForDB    = new Protein("PPPPPPPPPPE", "accession1", "organism", new List <Tuple <string, string> >(), dictHere);
            Protein TestProteinWithModObsevred = new Protein("PPPPPPPPPPE", "accession1", "organism", new List <Tuple <string, string> >(), dictHere2);

            //First Write XML Database
            string xmlName  = "selectedMods.xml";
            string xmlName2 = "selectedModsObvs.xml";

            //Add Mod to list and write XML input database
            Dictionary <string, HashSet <Tuple <int, Modification> > > modList = new Dictionary <string, HashSet <Tuple <int, Modification> > >();
            var Hash = new HashSet <Tuple <int, Modification> >
            {
                new Tuple <int, Modification>(1, modToAdd),
                new Tuple <int, Modification>(2, modToAdd2),
                new Tuple <int, Modification>(3, modToAdd3),
                new Tuple <int, Modification>(4, modToAdd4), //Observed Only
            };

            modList.Add("test", Hash);
            ProteinDbWriter.WriteXmlDatabase(modList, new List <Protein> {
                TestProteinWithModForDB
            }, xmlName);

            //Add Observed Only
            modList.Add("test2", Hash);
            ProteinDbWriter.WriteXmlDatabase(modList, new List <Protein> {
                TestProteinWithModObsevred
            }, xmlName2);

            //now create MZML data
            var protein      = ProteinDbLoader.LoadProteinXML(xmlName2, true, DecoyType.Reverse, new List <Modification>(), false, new List <string>(), out Dictionary <string, Modification> ok);
            var digestedList = protein[0].Digest(task5.CommonParameters.DigestionParams, fixedModifications, variableModifications).ToList();

            //Set Peptide with 1 mod at position 3
            PeptideWithSetModifications pepWithSetMods1 = digestedList[0];
            PeptideWithSetModifications pepWithSetMods2 = digestedList[1];
            PeptideWithSetModifications pepWithSetMods3 = digestedList[2];
            PeptideWithSetModifications pepWithSetMods4 = digestedList[3];
            PeptideWithSetModifications pepWithSetMods5 = digestedList[4];

            //CUSTOM PEP
            MsDataFile myMsDataFile = new TestDataFile(new List <PeptideWithSetModifications>
            {
                pepWithSetMods1, pepWithSetMods2, pepWithSetMods3, pepWithSetMods4, pepWithSetMods5
            });
            string mzmlName = @"newMzml.mzML";

            IO.MzML.MzmlMethods.CreateAndWriteMyMzmlWithCalibratedSpectra(myMsDataFile, mzmlName, false);

            //make sure this runs correctly
            //run!
            string outputFolder = Path.Combine(TestContext.CurrentContext.TestDirectory, @"TestUserModSelectionInPrunedDB");
            var    engine       = new EverythingRunnerEngine(taskList, new List <string> {
                mzmlName
            }, new List <DbForTask> {
                new DbForTask(xmlName, false)
            }, outputFolder);

            engine.Run();
            string final    = Path.Combine(MySetUpClass.outputFolder, "task5", "selectedModspruned.xml");
            var    proteins = ProteinDbLoader.LoadProteinXML(final, true, DecoyType.Reverse, new List <Modification>(), false, new List <string>(), out ok);
            var    Dlist    = proteins[0].GetVariantProteins().SelectMany(vp => vp.Digest(task5.CommonParameters.DigestionParams, fixedModifications, variableModifications)).ToList();

            Assert.AreEqual(Dlist[0].NumFixedMods, 1);

            //check length
            Assert.AreEqual(proteins[0].OneBasedPossibleLocalizedModifications.Count, 3);
            List <Modification> listOfLocalMods = new List <Modification>();

            listOfLocalMods.AddRange(proteins[0].OneBasedPossibleLocalizedModifications[2]);
            listOfLocalMods.AddRange(proteins[0].OneBasedPossibleLocalizedModifications[3]);
            listOfLocalMods.AddRange(proteins[0].OneBasedPossibleLocalizedModifications[11]);

            //check Type, count, ID
            Assert.AreEqual(listOfLocalMods[0].ModificationType, "Common Fixed");
            Assert.AreEqual(listOfLocalMods[2].ModificationType, "missing");
            Assert.IsFalse(listOfLocalMods.Contains(connorMod)); //make sure that mod set not to show up is not in mod list

            Assert.AreEqual(listOfLocalMods[0].IdWithMotif, "Default(Mod in DB and Observed) on P");
            Assert.AreEqual(listOfLocalMods[1].IdWithMotif, "ModToAlwaysAppear on P");
            //Makes sure Mod that was not in the DB but was observed is in pruned DB
            Assert.AreEqual(listOfLocalMods[2].IdWithMotif, "ModObservedNotinDB on E");
            Assert.AreEqual(listOfLocalMods.Count, 3);
            Directory.Delete(outputFolder, true);
            File.Delete(mzmlName);
            File.Delete(xmlName);
            File.Delete(xmlName2);
        }
Exemplo n.º 7
0
        public static void TestEverythingRunner()
        {
            foreach (var modFile in Directory.GetFiles(@"Mods"))
            {
                GlobalVariables.AddMods(PtmListLoader.ReadModsFromFile(modFile));
            }

            CalibrationTask task1 = new CalibrationTask
            {
                CommonParameters = new CommonParameters(digestionParams: new DigestionParams(maxMissedCleavages: 0, minPeptideLength: 1, initiatorMethionineBehavior: InitiatorMethionineBehavior.Retain)),

                CalibrationParameters = new CalibrationParameters
                {
                    WriteIntermediateFiles = true,
                    NumFragmentsNeededForEveryIdentification = 6,
                }
            };
            GptmdTask task2 = new GptmdTask
            {
                CommonParameters = new CommonParameters()
            };

            SearchTask task3 = new SearchTask
            {
                CommonParameters = new CommonParameters(),

                SearchParameters = new SearchParameters
                {
                    DoParsimony  = true,
                    SearchTarget = true,
                    SearchType   = SearchType.Modern
                }
            };

            SearchTask task4 = new SearchTask
            {
                CommonParameters = new CommonParameters(),

                SearchParameters = new SearchParameters
                {
                    SearchType = SearchType.Modern,
                }
            };
            List <(string, MetaMorpheusTask)> taskList = new List <(string, MetaMorpheusTask)> {
                ("task1", task1),
                ("task2", task2),
                ("task3", task3),
                ("task4", task4),
            };

            List <ModificationWithMass> variableModifications = GlobalVariables.AllModsKnown.OfType <ModificationWithMass>().Where(b => task1.CommonParameters.ListOfModsVariable.Contains((b.modificationType, b.id))).ToList();
            List <ModificationWithMass> fixedModifications    = GlobalVariables.AllModsKnown.OfType <ModificationWithMass>().Where(b => task1.CommonParameters.ListOfModsFixed.Contains((b.modificationType, b.id))).ToList();

            // Generate data for files
            Protein ParentProtein = new Protein("MPEPTIDEKANTHE", "accession1");

            var digestedList = ParentProtein.Digest(task1.CommonParameters.DigestionParams, fixedModifications, variableModifications).ToList();

            Assert.AreEqual(3, digestedList.Count);

            PeptideWithSetModifications pepWithSetMods1 = digestedList[0];

            PeptideWithSetModifications pepWithSetMods2 = digestedList[2];

            var dictHere = new Dictionary <int, List <Modification> >();

            ModificationMotif.TryGetMotif("E", out ModificationMotif motif);
            dictHere.Add(3, new List <Modification> {
                new ModificationWithMass("21", null, motif, TerminusLocalization.Any, 21.981943)
            });
            Protein ParentProteinToNotInclude = new Protein("MPEPTIDEK", "accession2", "organism", new List <Tuple <string, string> >(), dictHere);

            digestedList = ParentProteinToNotInclude.Digest(task1.CommonParameters.DigestionParams, fixedModifications, variableModifications).ToList();

            MsDataFile myMsDataFile = new TestDataFile(new List <PeptideWithSetModifications> {
                pepWithSetMods1, pepWithSetMods2, digestedList[1]
            });

            Protein proteinWithChain = new Protein("MAACNNNCAA", "accession3", "organism", new List <Tuple <string, string> >(), new Dictionary <int, List <Modification> >(), new List <ProteolysisProduct> {
                new ProteolysisProduct(4, 8, "chain")
            }, "name2", "fullname2");

            string mzmlName = @"ok.mzML";

            IO.MzML.MzmlMethods.CreateAndWriteMyMzmlWithCalibratedSpectra(myMsDataFile, mzmlName, false);
            string xmlName = "okk.xml";

            ProteinDbWriter.WriteXmlDatabase(new Dictionary <string, HashSet <Tuple <int, Modification> > >(), new List <Protein> {
                ParentProtein, proteinWithChain
            }, xmlName);

            // RUN!
            var engine = new EverythingRunnerEngine(taskList, new List <string> {
                mzmlName
            }, new List <DbForTask> {
                new DbForTask(xmlName, false)
            }, Environment.CurrentDirectory);

            engine.Run();
        }
        public void SaveCustomMod_Click(object sender, RoutedEventArgs e)
        {
            string        modsDirectory  = Path.Combine(GlobalVariables.DataDir, @"Mods");
            string        customModsPath = Path.Combine(modsDirectory, @"CustomModifications.txt");
            List <string> customModsText = new List <string>();

            if (!File.Exists(customModsPath))
            {
                customModsText.Add("Custom Modifications");
            }
            else
            {
                customModsText = File.ReadAllLines(customModsPath).ToList();
            }

            string           idText               = originalIdTextBox.Text;
            string           motifText            = motifTextBox.Text;
            string           chemicalFormulaText  = chemicalFormulaTextBox.Text;
            string           modMassText          = modMassTextBox.Text;
            string           neutralLossText      = neutralLossTextBox.Text;
            string           diagnosticIonText    = diagnosticIonTextBox.Text;
            string           modificationTypeText = modificationTypeTextBox.Text;
            string           locationRestriction  = locationRestrictions[locationRestrictionComboBox.Text];
            DissociationType disType              = GlobalVariables.AllSupportedDissociationTypes[dissociationTypeComboBox.Text];

            if (ErrorsDetected(idText, motifText, modMassText, chemicalFormulaText, neutralLossText, modificationTypeText, diagnosticIonText))
            {
                return;
            }

            // create custom mod
            Dictionary <DissociationType, List <double> > neutralLosses = null;

            if (!string.IsNullOrEmpty(neutralLossText))
            {
                neutralLosses = new Dictionary <DissociationType, List <double> >
                {
                    { disType, neutralLossText.Split(',').Select(p => double.Parse(p, CultureInfo.InvariantCulture)).ToList() }
                };
            }

            Dictionary <DissociationType, List <double> > diagnosticIons = null;

            if (!string.IsNullOrEmpty(diagnosticIonText))
            {
                diagnosticIons = new Dictionary <DissociationType, List <double> >()
                {
                    { disType, diagnosticIonText.Split(',').Select(p => double.Parse(p, CultureInfo.InvariantCulture).ToMass(1)).ToList() }
                };
            }

            ModificationMotif.TryGetMotif(motifText, out ModificationMotif finalMotif);

            ChemicalFormula chemicalFormula = null;

            if (!string.IsNullOrEmpty(chemicalFormulaText))
            {
                chemicalFormula = ChemicalFormula.ParseFormula(chemicalFormulaText);
            }

            double?modMass = null;

            if (!string.IsNullOrEmpty(modMassText))
            {
                modMass = double.Parse(modMassText, CultureInfo.InvariantCulture);
            }

            Modification modification = new Modification(
                _originalId: idText,
                _modificationType: modificationTypeText,
                _target: finalMotif,
                _locationRestriction: locationRestriction,
                _chemicalFormula: chemicalFormula,
                _monoisotopicMass: modMass,
                _neutralLosses: neutralLosses,
                _diagnosticIons: diagnosticIons);

            if (GlobalVariables.AllModsKnownDictionary.ContainsKey(modification.IdWithMotif))
            {
                MessageBox.Show("A modification already exists with the name: " + modification.IdWithMotif, "Error", MessageBoxButton.OK, MessageBoxImage.Hand);
                return;
            }

            // write custom mod to mods file

            // write/read temp file to make sure the mod is readable, then delete it
            string tempPath = Path.Combine(modsDirectory, @"temp.txt");

            try
            {
                List <string> temp = new List <string> {
                    modification.ToString(), @"//"
                };
                File.WriteAllLines(tempPath, temp);
                var parsedMods = UsefulProteomicsDatabases.PtmListLoader.ReadModsFromFile(tempPath, out var errors);

                if (parsedMods.Count() != 1)
                {
                    MessageBox.Show("Problem parsing custom mod: One mod was expected, a different number was generated", "Error", MessageBoxButton.OK, MessageBoxImage.Hand);
                    return;
                }

                if (errors.Any())
                {
                    string concatErrors = string.Join(Environment.NewLine, errors.Select(p => p.Item2));
                    MessageBox.Show("Problem(s) parsing custom mod: " + Environment.NewLine + concatErrors, "Error", MessageBoxButton.OK, MessageBoxImage.Hand);
                    return;
                }

                File.Delete(tempPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problem parsing custom mod: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Hand);
                File.Delete(tempPath);
                return;
            }

            // delete old custom mods file, write new one
            try
            {
                customModsText.Add(modification.ToString());
                customModsText.Add(@"//");
                File.Delete(customModsPath);
                File.WriteAllLines(customModsPath, customModsText);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problem saving custom mod to file: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Hand);
                return;
            }

            GlobalVariables.AddMods(new List <Modification> {
                modification
            }, false);

            DialogResult = true;
        }
Exemplo n.º 9
0
        private static void Main(string[] args)
        {
            Console.WriteLine("Welcome to MetaMorpheus");
            Console.WriteLine(GlobalVariables.MetaMorpheusVersion);

            var p = new FluentCommandLineParser <ApplicationArguments>();

            p.Setup(arg => arg.Tasks)
            .As('t', "tasks")
            .SetDefault(new List <string>());

            p.Setup(arg => arg.MetaTasks)
            .As('m', "meta-task")
            .SetDefault(new List <string>());

            p.Setup(arg => arg.Spectra)
            .As('s', "spectra")
            .Required();

            p.Setup(arg => arg.Databases)
            .As('d', "databases")
            .Required();

            var result = p.Parse(args);

            if (p.Object.MetaTasks.Count != 0 || p.Object.Tasks.Count != 0)
            {
                if (!result.HasErrors)
                {
                    MetaMorpheusEngine.WarnHandler                 += WarnHandler;
                    MetaMorpheusEngine.OutProgressHandler          += MyEngine_outProgressHandler;
                    MetaMorpheusEngine.StartingSingleEngineHander  += MyEngine_startingSingleEngineHander;
                    MetaMorpheusEngine.FinishedSingleEngineHandler += MyEngine_finishedSingleEngineHandler;

                    MetaMorpheusTask.WarnHandler += WarnHandler;
                    MetaMorpheusTask.LogHandler  += LogHandler;
                    MetaMorpheusTask.StartingSingleTaskHander   += MyTaskEngine_startingSingleTaskHander;
                    MetaMorpheusTask.FinishedSingleTaskHandler  += MyTaskEngine_finishedSingleTaskHandler;
                    MetaMorpheusTask.FinishedWritingFileHandler += MyTaskEngine_finishedWritingFileHandler;

                    foreach (var db in p.Object.Databases)
                    {
                        if (!Path.GetExtension(db).Equals(".fasta"))
                        {
                            GlobalVariables.AddMods(UsefulProteomicsDatabases.ProteinDbLoader.GetPtmListFromProteinXml(db).OfType <ModificationWithLocation>());
                        }
                    }

                    List <(string, MetaMorpheusTask)> taskList = new List <(string, MetaMorpheusTask)>();

                    for (int i = 0; i < p.Object.Tasks.Count; i++)
                    {
                        var filePath = p.Object.Tasks[i];

                        var uhum = Toml.ReadFile(filePath, MetaMorpheusTask.tomlConfig);

                        switch (uhum.Get <string>("TaskType"))
                        {
                        case "Search":
                            var ye1 = Toml.ReadFile <SearchTask>(filePath, MetaMorpheusTask.tomlConfig);
                            taskList.Add(("Task" + (i + 1) + "SearchTask", ye1));
                            break;

                        case "Calibrate":
                            var ye2 = Toml.ReadFile <CalibrationTask>(filePath, MetaMorpheusTask.tomlConfig);
                            taskList.Add(("Task" + (i + 1) + "CalibrationTask", ye2));
                            break;

                        case "Gptmd":
                            var ye3 = Toml.ReadFile <GptmdTask>(filePath, MetaMorpheusTask.tomlConfig);
                            taskList.Add(("Task" + (i + 1) + "GptmdTask", ye3));
                            break;

                        case "XLSearch":
                            var ye4 = Toml.ReadFile <XLSearchTask>(filePath, MetaMorpheusTask.tomlConfig);
                            taskList.Add(("Task" + (i + 1) + "XLSearchTask", ye4));
                            break;

                        case "Neo":
                            Console.WriteLine("Neo tasks are meta-tasks that rely on several other tasks. Please use -m for meta instead of -t. Skipping.");
                            break;

                        default:
                            Console.WriteLine(uhum.Get <string>("TaskType") + " is not a known task type! Skipping.");
                            break;
                        }
                    }

                    for (int i = 0; i < p.Object.MetaTasks.Count; i++)
                    {
                        var filePath = p.Object.MetaTasks[i];
                        var uhum     = Toml.ReadFile(filePath, MetaMorpheusTask.tomlConfig);
                        switch (uhum.Get <string>("TaskType"))
                        {
                        case "Search":
                            Console.WriteLine("Search tasks are individual tasks. Please use -t for task instead of -m. Skipping.");
                            break;

                        case "Calibrate":
                            Console.WriteLine("Calibrate tasks are individual tasks. Please use -t for task instead of -m. Skipping.");
                            break;

                        case "Gptmd":
                            Console.WriteLine("Gptmd tasks are individual tasks. Please use -t for task instead of -m. Skipping.");
                            break;

                        case "XLSearch":
                            Console.WriteLine("XLSearch tasks are individual tasks. Please use -t for task instead of -m. Skipping.");
                            break;

                        case "Neo":
                            var ye5 = Toml.ReadFile <NeoSearchTask>(filePath, MetaMorpheusTask.tomlConfig);
                            foreach (MetaMorpheusTask task in NeoLoadTomls.LoadTomls(ye5))
                            {
                                taskList.Add(("Task" + (taskList.Count + 1) + ye5.TaskType, ye5));
                            }
                            break;

                        default:
                            Console.WriteLine(uhum.Get <string>("TaskType") + " is not a known task type! Skipping.");
                            break;
                        }
                    }

                    List <string>    startingRawFilenameList   = p.Object.Spectra.Select(b => Path.GetFullPath(b)).ToList();
                    List <DbForTask> startingXmlDbFilenameList = p.Object.Databases.Select(b => new DbForTask(Path.GetFullPath(b), IsContaminant(b))).ToList();

                    var MatchingChars =
                        from len in Enumerable.Range(0, startingRawFilenameList.Min(s => s.Length)).Reverse()
                        let possibleMatch = startingRawFilenameList.First().Substring(0, len)
                                            where startingRawFilenameList.All(f => f.StartsWith(possibleMatch, StringComparison.Ordinal))
                                            select possibleMatch;

                    string outputFolder = Path.Combine(Path.GetDirectoryName(MatchingChars.First()), @"$DATETIME");

                    EverythingRunnerEngine a = new EverythingRunnerEngine(taskList, startingRawFilenameList, startingXmlDbFilenameList, outputFolder);

                    try
                    {
                        a.Run();
                    }
                    catch (Exception e)
                    {
                        while (e.InnerException != null)
                        {
                            e = e.InnerException;
                        }
                        var message = "Run failed, Exception: " + e.Message;
                        Console.WriteLine(message);
                    }
                }
                else
                {
                    Console.WriteLine("Error Text:" + result.ErrorText);
                }
            }
            else
            {
                Console.WriteLine("Error Text: No toml file was specified. Use -t for tasks or -m for meta-tasks.");
            }
        }
Exemplo n.º 10
0
        private static int Run(CommandLineSettings settings)
        {
            if (settings.Verbosity == CommandLineSettings.VerbosityType.minimal || settings.Verbosity == CommandLineSettings.VerbosityType.normal)
            {
                Console.WriteLine("Welcome to MetaMorpheus");
                Console.WriteLine(GlobalVariables.MetaMorpheusVersion);
            }

            int errorCode = 0;

            try
            {
                settings.ValidateCommandLineSettings();
                CommandLineSettings = settings;
            }
            catch (Exception e)
            {
                if (settings.Verbosity == CommandLineSettings.VerbosityType.minimal || settings.Verbosity == CommandLineSettings.VerbosityType.normal)
                {
                    Console.WriteLine("MetaMorpheus encountered the following error:" + Environment.NewLine + e.Message);
                }
                errorCode = 2;

                return(errorCode);
            }

            if (settings.GenerateDefaultTomls)
            {
                if (settings.Verbosity == CommandLineSettings.VerbosityType.minimal || settings.Verbosity == CommandLineSettings.VerbosityType.normal)
                {
                    Console.WriteLine("Generating default tomls at location: " + settings.OutputFolder);
                }
                CommandLineSettings.GenerateDefaultTaskTomls(settings.OutputFolder);

                return(errorCode);
            }

            // set up microvignette
            if (settings.RunMicroVignette)
            {
                // set up the spectra file
                settings.Spectra.Clear();
                settings.Spectra.Add(Path.Combine(GlobalVariables.DataDir, @"Data", "SmallCalibratible_Yeast.mzML"));

                // set up the database
                settings.Databases.Clear();
                settings.Databases.Add(Path.Combine(GlobalVariables.DataDir, @"Data", "SmallYeast.fasta"));

                // set up the tasks (calibration, GPTMD, search)
                settings.Tasks.Clear();
                CommandLineSettings.GenerateDefaultTaskTomls(settings.OutputFolder);
                settings.Tasks.Add(Path.Combine(settings.OutputFolder, "CalibrationTask.toml"));
                settings.Tasks.Add(Path.Combine(settings.OutputFolder, "GptmdTask.toml"));
                settings.Tasks.Add(Path.Combine(settings.OutputFolder, "SearchTask.toml"));
            }

            MetaMorpheusEngine.WarnHandler                 += WarnHandler;
            MetaMorpheusEngine.OutProgressHandler          += MyEngine_outProgressHandler;
            MetaMorpheusEngine.StartingSingleEngineHander  += MyEngine_startingSingleEngineHander;
            MetaMorpheusEngine.FinishedSingleEngineHandler += MyEngine_finishedSingleEngineHandler;

            MetaMorpheusTask.WarnHandler += WarnHandler;
            MetaMorpheusTask.LogHandler  += LogHandler;
            MetaMorpheusTask.StartingSingleTaskHander   += MyTaskEngine_startingSingleTaskHander;
            MetaMorpheusTask.FinishedSingleTaskHandler  += MyTaskEngine_finishedSingleTaskHandler;
            MetaMorpheusTask.FinishedWritingFileHandler += MyTaskEngine_finishedWritingFileHandler;

            bool containsRawFiles = settings.Spectra.Select(v => Path.GetExtension(v).ToLowerInvariant()).Any(v => v == ".raw");

            if (containsRawFiles && !GlobalVariables.GlobalSettings.UserHasAgreedToThermoRawFileReaderLicence)
            {
                // write the Thermo RawFileReader licence agreement
                Console.WriteLine(ThermoRawFileReader.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().ToLowerInvariant();
                if (res == "y")
                {
                    var newGlobalSettings = new GlobalSettings
                    {
                        UserHasAgreedToThermoRawFileReaderLicence = true,
                        WriteExcelCompatibleTSVs = GlobalVariables.GlobalSettings.WriteExcelCompatibleTSVs
                    };

                    Toml.WriteFile <GlobalSettings>(newGlobalSettings, Path.Combine(GlobalVariables.DataDir, @"settings.toml"));
                    GlobalVariables.GlobalSettings = newGlobalSettings;
                }
                else
                {
                    Console.WriteLine("Thermo licence has been declined. Exiting MetaMorpheus. You can still search .mzML and .mgf files without agreeing to the Thermo licence.");
                    errorCode = 3;
                    return(errorCode);
                }
            }

            foreach (var db in settings.Databases)
            {
                if (!Path.GetExtension(db).Equals(".fasta"))
                {
                    GlobalVariables.AddMods(UsefulProteomicsDatabases.ProteinDbLoader.GetPtmListFromProteinXml(db).OfType <Modification>(), true);

                    // print any error messages reading the mods to the console
                    foreach (var error in GlobalVariables.ErrorsReadingMods)
                    {
                        if (settings.Verbosity == CommandLineSettings.VerbosityType.minimal || settings.Verbosity == CommandLineSettings.VerbosityType.normal)
                        {
                            Console.WriteLine(error);
                        }
                    }

                    GlobalVariables.ErrorsReadingMods.Clear();
                }
            }

            List <(string, MetaMorpheusTask)> taskList = new List <(string, MetaMorpheusTask)>();

            var tasks = settings.Tasks.ToList();

            for (int i = 0; i < tasks.Count; i++)
            {
                var filePath = tasks[i];

                var toml = Toml.ReadFile(filePath, MetaMorpheusTask.tomlConfig);

                switch (toml.Get <string>("TaskType"))
                {
                case "Search":
                    var searchTask = Toml.ReadFile <SearchTask>(filePath, MetaMorpheusTask.tomlConfig);
                    taskList.Add(("Task" + (i + 1) + "SearchTask", searchTask));
                    break;

                case "Calibrate":
                    var calibrationTask = Toml.ReadFile <CalibrationTask>(filePath, MetaMorpheusTask.tomlConfig);
                    taskList.Add(("Task" + (i + 1) + "CalibrationTask", calibrationTask));
                    break;

                case "Gptmd":
                    var GptmdTask = Toml.ReadFile <GptmdTask>(filePath, MetaMorpheusTask.tomlConfig);
                    taskList.Add(("Task" + (i + 1) + "GptmdTask", GptmdTask));
                    break;

                case "XLSearch":
                    var XlTask = Toml.ReadFile <XLSearchTask>(filePath, MetaMorpheusTask.tomlConfig);
                    taskList.Add(("Task" + (i + 1) + "XLSearchTask", XlTask));
                    break;

                case "GlycoSearch":
                    var GlycoTask = Toml.ReadFile <GlycoSearchTask>(filePath, MetaMorpheusTask.tomlConfig);
                    taskList.Add(("Task" + (i + 1) + "GlycoSearchTask", GlycoTask));
                    break;

                default:
                    if (settings.Verbosity == CommandLineSettings.VerbosityType.minimal || settings.Verbosity == CommandLineSettings.VerbosityType.normal)
                    {
                        Console.WriteLine(toml.Get <string>("TaskType") + " is not a known task type! Skipping.");
                    }
                    break;
                }
            }

            List <string>    startingRawFilenameList   = settings.Spectra.Select(b => Path.GetFullPath(b)).ToList();
            List <DbForTask> startingXmlDbFilenameList = settings.Databases.Select(b => new DbForTask(Path.GetFullPath(b), IsContaminant(b))).ToList();

            EverythingRunnerEngine a = new EverythingRunnerEngine(taskList, startingRawFilenameList, startingXmlDbFilenameList, settings.OutputFolder);

            try
            {
                a.Run();
            }
            catch (Exception e)
            {
                while (e.InnerException != null)
                {
                    e = e.InnerException;
                }

                var message = "Run failed, Exception: " + e.Message;

                if (settings.Verbosity == CommandLineSettings.VerbosityType.minimal || settings.Verbosity == CommandLineSettings.VerbosityType.normal)
                {
                    Console.WriteLine(message);
                }
                errorCode = 4;
            }

            return(errorCode);
        }
Exemplo n.º 11
0
        public static void TestPrunedDatabase()
        {
            //Create Search Task
            SearchTask task1 = new SearchTask
            {
                SearchParameters = new SearchParameters
                {
                    WritePrunedDatabase  = true,
                    SearchTarget         = true,
                    MassDiffAcceptorType = MassDiffAcceptorType.Exact,
                    ModsToWriteSelection = new Dictionary <string, int>
                    {
                        { "ConnorModType", 1 }
                    }
                },
                CommonParameters = new CommonParameters(digestionParams: new DigestionParams(minPeptideLength: 5))
            };

            //add task to task list
            List <(string, MetaMorpheusTask)> taskList = new List <(string, MetaMorpheusTask)>
            {
                ("task1", task1)
            };

            ModificationMotif.TryGetMotif("P", out ModificationMotif motif);

            var connorMod = new ModificationWithMass("ConnorMod", "ConnorModType", motif, TerminusLocalization.Any, 10);

            GlobalVariables.AddMods(new List <ModificationWithLocation>
            {
                connorMod
            });

            //create modification lists
            List <ModificationWithMass> variableModifications = GlobalVariables.AllModsKnown.OfType <ModificationWithMass>().Where
                                                                    (b => task1.CommonParameters.ListOfModsVariable.Contains((b.modificationType, b.id))).ToList();

            //add modification to Protein object
            var dictHere = new Dictionary <int, List <Modification> >();
            ModificationWithMass modToAdd  = connorMod;
            ModificationWithMass modToAdd2 = connorMod;

            dictHere.Add(1, new List <Modification> {
                modToAdd
            });
            dictHere.Add(3, new List <Modification> {
                modToAdd2
            });

            //protein Creation (One with mod and one without)
            Protein TestProteinWithMod = new Protein("PEPTID", "accession1", "organism", new List <Tuple <string, string> >(), dictHere);

            //First Write XML Database
            string xmlName = "okkk.xml";

            //Add Mod to list and write XML input database
            Dictionary <string, HashSet <Tuple <int, Modification> > > modList = new Dictionary <string, HashSet <Tuple <int, Modification> > >();
            var Hash = new HashSet <Tuple <int, Modification> >
            {
                new Tuple <int, Modification>(3, modToAdd)
            };

            modList.Add("test", Hash);
            ProteinDbWriter.WriteXmlDatabase(modList, new List <Protein> {
                TestProteinWithMod
            }, xmlName);

            //now write MZML file
            var protein = ProteinDbLoader.LoadProteinXML(xmlName, true,
                                                         DecoyType.Reverse, new List <Modification>(), false, new List <string>(), out Dictionary <string, Modification> ok);
            var digestedList = protein[0].Digest(task1.CommonParameters.DigestionParams, new List <ModificationWithMass> {
            },
                                                 variableModifications).ToList();

            Assert.AreEqual(4, digestedList.Count);

            //Set Peptide with 1 mod at position 3
            PeptideWithSetModifications pepWithSetMods1 = digestedList[1];

            //Finally Write MZML file
            Assert.AreEqual("PEP[ConnorModType:ConnorMod]TID", pepWithSetMods1.Sequence);
            MsDataFile myMsDataFile = new TestDataFile(new List <PeptideWithSetModifications> {
                pepWithSetMods1
            });
            string mzmlName = @"hello.mzML";

            IO.MzML.MzmlMethods.CreateAndWriteMyMzmlWithCalibratedSpectra(myMsDataFile, mzmlName, false);

            //run!
            var engine = new EverythingRunnerEngine(taskList, new List <string> {
                mzmlName
            },
                                                    new List <DbForTask> {
                new DbForTask(xmlName, false)
            }, Environment.CurrentDirectory);

            engine.Run();

            string final = Path.Combine(MySetUpClass.outputFolder, "task1", "okkkpruned.xml");

            var proteins = ProteinDbLoader.LoadProteinXML(final, true, DecoyType.Reverse, new List <Modification>(), false, new List <string>(), out ok);

            //check length
            Assert.AreEqual(proteins[0].OneBasedPossibleLocalizedModifications.Count, 1);
            //check location (key)
            Assert.AreEqual(proteins[0].OneBasedPossibleLocalizedModifications.ContainsKey(3), true);
            List <Modification> listOfMods = proteins[0].OneBasedPossibleLocalizedModifications[3];

            //check Type, count, ID
            Assert.AreEqual(listOfMods[0].modificationType, "ConnorModType");
            Assert.AreEqual(listOfMods[0].id, "ConnorMod");
            Assert.AreEqual(listOfMods.Count, 1);
        }
Exemplo n.º 12
0
        private void AddAFile(string draggedFilePath)
        {
            // this line is NOT used because .xml.gz (extensions with two dots) mess up with Path.GetExtension
            //var theExtension = Path.GetExtension(draggedFilePath).ToLowerInvariant();

            // we need to get the filename before parsing out the extension because if we assume that everything after the dot
            // is the extension and there are dots in the file path (i.e. in a folder name), this will mess up
            var filename     = Path.GetFileName(draggedFilePath);
            var theExtension = filename.Substring(filename.IndexOf(".")).ToLowerInvariant();

            switch (theExtension)
            {
            case ".raw":
                var versionCheckerResult = MyFileManager.ValidateThermoMsFileReaderVersion();

                if (versionCheckerResult.Equals(MyFileManager.ThermoMsFileReaderVersionCheck.IncorrectVersion))
                {
                    GuiWarnHandler(null, new StringEventArgs("Warning! Thermo MSFileReader is not version 3.0 SP2; a crash may result from searching this .raw file", null));
                }
                else if (versionCheckerResult.Equals(MyFileManager.ThermoMsFileReaderVersionCheck.DllsNotFound))
                {
                    GuiWarnHandler(null, new StringEventArgs("Warning! Cannot find Thermo MSFileReader (v3.0 SP2 is preferred); a crash may result from searching this .raw file", null));
                }
                goto case ".mzml";

            case ".mzml":
                RawDataForDataGrid zz = new RawDataForDataGrid(draggedFilePath);
                if (!ExistRaw(rawDataObservableCollection, zz))
                {
                    rawDataObservableCollection.Add(zz);
                }
                UpdateFileSpecificParamsDisplayJustAdded(Path.ChangeExtension(draggedFilePath, ".toml"));
                UpdateOutputFolderTextbox();
                break;

            case ".mzml.gz":      // not implemented yet
            case ".fasta.gz":     // not implemented yet
                GuiWarnHandler(null, new StringEventArgs("Cannot read, try uncompressing: " + draggedFilePath, null));
                break;

            case ".xml":
            case ".xml.gz":
            case ".fasta":
            case ".fa":
                ProteinDbForDataGrid uu = new ProteinDbForDataGrid(draggedFilePath);

                if (!ExistDa(proteinDbObservableCollection, uu))
                {
                    proteinDbObservableCollection.Add(uu);
                    if (theExtension.Equals(".xml") || theExtension.Equals(".xml.gz"))
                    {
                        try
                        {
                            GlobalVariables.AddMods(UsefulProteomicsDatabases.ProteinDbLoader.GetPtmListFromProteinXml(draggedFilePath).OfType <ModificationWithLocation>());
                        }
                        catch (Exception ee)
                        {
                            MessageBox.Show(ee.ToString());
                            GuiWarnHandler(null, new StringEventArgs("Cannot read: " + draggedFilePath, null));
                            proteinDbObservableCollection.Remove(uu);
                        }
                    }
                }
                break;

            case ".toml":
                var tomlFile = Toml.ReadFile(draggedFilePath, MetaMorpheusTask.tomlConfig);
                if (tomlFile.Keys.Contains("PrecursorMassTolerance") && tomlFile.Keys.Contains("ProductMassTolerance") && tomlFile.Keys.Count == 2)
                {
                    // do nothing; it's a ppm suggested tolerance toml from calibration, this gets read in elsewhere
                }
                else
                {
                    try
                    {
                        switch (tomlFile.Get <string>("TaskType"))
                        {
                        case "Search":
                            var ye1 = Toml.ReadFile <SearchTask>(draggedFilePath, MetaMorpheusTask.tomlConfig);
                            AddTaskToCollection(ye1);
                            break;

                        case "Calibrate":
                            var ye2 = Toml.ReadFile <CalibrationTask>(draggedFilePath, MetaMorpheusTask.tomlConfig);
                            AddTaskToCollection(ye2);
                            break;

                        case "Gptmd":
                            var ye3 = Toml.ReadFile <GptmdTask>(draggedFilePath, MetaMorpheusTask.tomlConfig);
                            AddTaskToCollection(ye3);
                            break;

                        case "XLSearch":
                            var ye4 = Toml.ReadFile <XLSearchTask>(draggedFilePath, MetaMorpheusTask.tomlConfig);
                            AddTaskToCollection(ye4);
                            break;

                        case "Neo":
                            var ye5 = Toml.ReadFile <NeoSearchTask>(draggedFilePath, MetaMorpheusTask.tomlConfig);
                            foreach (MetaMorpheusTask task in NeoLoadTomls.LoadTomls(ye5))
                            {
                                AddTaskToCollection(task);
                            }
                            break;
                        }
                    }
                    catch (Exception e)
                    {
                        GuiWarnHandler(null, new StringEventArgs("Could not parse .toml: " + e.Message, null));
                    }
                }
                break;

            default:
                GuiWarnHandler(null, new StringEventArgs("Unrecognized file type: " + theExtension, null));
                break;
            }
        }