示例#1
0
文件: Program.cs 项目: rfellers/pwiz
        public void Build()
        {
            foreach (var methodTranList in MethodTrans)
            {
                Console.Error.WriteLine("MESSAGE: Exporting method {0}", Path.GetFileName(methodTranList.FinalMethod));
                if (string.IsNullOrEmpty(methodTranList.TransitionList))
                {
                    throw new IOException(string.Format("Failure creating method file {0}. The mass list is empty.", methodTranList.FinalMethod));
                }

                string outMeth = EnsureExtension(methodTranList.OutputMethod, ".meth");  // Thermo needs the extension to be .meth
                if (!Equals(outMeth, methodTranList.OutputMethod) && File.Exists(methodTranList.OutputMethod))
                {
                    File.Move(methodTranList.OutputMethod, outMeth);
                }

                try
                {
                    using (IMethodXMLContext mxc = MethodXMLFactory.CreateContext(InstrumentType, InstrumentVersion))
                        using (IMethodXML mx = mxc.Create())
                        {
                            mx.Open(TemplateMethod);
                            mx.EnableValidation(true);

                            ListItem[] listItems = ParseList(methodTranList.TransitionList).ToArray();
                            if (!listItems.Any())
                            {
                                throw new IOException("Empty mass list found.");
                            }

                            if (InstrumentType.Equals(InstrumentFusion))
                            {
                                mx.ApplyMethodModificationsFromXML(GetFusionModificationXml(listItems, outMeth));
                            }
                            else
                            {
                                mx.ImportMassListFromXML(GetTsqMassListXml(listItems, outMeth));
                            }
                            mx.SaveAs(outMeth);
                        }

                    if (!File.Exists(outMeth))
                    {
                        throw new IOException(string.Format("Failure creating method file {0}.", methodTranList.FinalMethod));
                    }
                }
                finally
                {
                    if (!Equals(outMeth, methodTranList.OutputMethod))
                    {
                        File.Move(outMeth, methodTranList.OutputMethod);
                    }
                }
            }
        }
示例#2
0
        public static string GetSummary(string methodFile, string instrumentModel, string version)
        {
            methodFile = Path.GetFullPath(methodFile);

            using (IMethodXMLContext mxc = CreateContext(instrumentModel, version))
                using (IMethodXML xmlMeth = mxc.Create())
                {
                    xmlMeth.Open(methodFile);
                    return(xmlMeth.GetMethodSummary());
                }
        }
示例#3
0
        /// <summary>
        /// Validates a .meth file for errors in parameters/settings
        /// </summary>
        /// <param name="methodFilePath">The method file path</param>
        /// <param name="model">The instrument model</param>
        /// <param name="version">The instrument version</param>
        /// <returns>A list of errors it detected, or an empty list if no errors are found</returns>
        public static List <string> Validate(string methodFilePath, string model = "", string version = "")
        {
            var validationErrors = new List <string>();

            methodFilePath = Path.GetFullPath(methodFilePath);
            using (IMethodXMLContext mxc = CreateContext(model, version))
                using (IMethodXML xmlMeth = mxc.Create())
                {
                    xmlMeth.Open(methodFilePath);
                    if (!xmlMeth.ValidateMethod())
                    {
                        validationErrors.AddRange(xmlMeth.GetLastValidationErrors());
                    }
                }
            return(validationErrors);
        }
示例#4
0
        /// <summary>
        /// Applies the modification XML file to the method template, to produce a modified method file
        /// </summary>
        /// <param name="methodTemplate">The method to base off of</param>
        /// <param name="methodModXML">The modifications to be applied to the template method</param>
        /// <param name="outputMethod">The file path for the generated method</param>
        /// <param name="model">The instrument model</param>
        /// <param name="version">The instrument version</param>
        /// <param name="enableValidation">Enable automatic validation on saving</param>
        public static void ModifyMethod(string methodTemplate, string methodModXML, string outputMethod = "", string model = "", string version = "", bool enableValidation = true)
        {
            if (string.IsNullOrEmpty(methodTemplate))
            {
                throw new ArgumentException("A method file path must be specified", "Method Template");
            }

            if (string.IsNullOrEmpty(methodModXML))
            {
                throw new ArgumentException("A method modification path must be specified", "Method Modification");
            }

            // Handle relative/absolute paths
            methodTemplate = Path.GetFullPath(methodTemplate);
            methodModXML   = Path.GetFullPath(methodModXML);

            // These files are required
            if (!File.Exists(methodTemplate))
            {
                throw new IOException("File Not Found: " + methodTemplate);
            }

            if (!File.Exists(methodModXML))
            {
                throw new IOException("File Not Found: " + methodModXML);
            }

            // Create output file path if not specified
            if (string.IsNullOrEmpty(outputMethod))
            {
                outputMethod = methodTemplate.Replace(".meth", "_modified.meth");
            }
            outputMethod = Path.GetFullPath(outputMethod);

            // Create output directory if doesn't exist
            if (!Directory.Exists(Path.GetDirectoryName(outputMethod)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(outputMethod));
            }

            // If the instrument model is not specified, get the default installed one. (might not be the best)
            if (string.IsNullOrEmpty(model))
            {
                model = MethodXMLFactory.GetInstalledServerModel();

                if (string.IsNullOrEmpty(model))
                {
                    var instruments = MethodXMLFactory.GetInstalledInstrumentModels();

                    if (instruments.Count > 1)
                    {
                        throw new Exception(string.Format("Unable to find default installed instrument, you have {0} instruments registered", instruments.Count));
                    }
                    else if (instruments.Count == 0)
                    {
                        throw new Exception("Unable to find any installed instruments!");
                    }
                    else
                    {
                        model = instruments[0];
                    }
                }
            }

            // Get the type of instrument based on its name
            InstrumentFamily instrumentFamily = GetInstrumentFamilyFromModel(model);

            // Get the type of instrument modification based on the xml file
            InstrumentFamily xmlInstrumentFamily = GetInstrumentFamilyFromXml(methodModXML);

            // These two need to be equivalent to correctly apply the modifications
            if (instrumentFamily != xmlInstrumentFamily)
            {
                throw new ArgumentException(string.Format("The specified xml ({0}) is not compatible with the instrument model ({1}, {2})", xmlInstrumentFamily, instrumentFamily, model));
            }

            using (IMethodXMLContext mxc = CreateContext(model, version))
                using (IMethodXML xmlMeth = mxc.Create())
                {
                    // Open the template method
                    xmlMeth.Open(methodTemplate);

                    // Set the validation flag
                    xmlMeth.EnableValidation(enableValidation);

                    // Call the correct modification method based on the instrument type
                    switch (instrumentFamily)
                    {
                    case InstrumentFamily.OrbitrapFusion:
                        xmlMeth.ApplyMethodModificationsFromXMLFile(methodModXML);
                        break;

                    case InstrumentFamily.TSQ:
                        xmlMeth.ImportMassListFromXMLFile(methodModXML);
                        break;

                    default:
                        throw new ArgumentException("Unsupported instrument model:" + model);
                    }

                    // Save the in memory method to the output file
                    xmlMeth.SaveAs(outputMethod);
                }
        }
        /// <include file='doc\VsCodeDomParser.uex' path='docs/doc[@for="VsCodeDomParser.OnMethodPopulateStatements"]/*' />
        /// <devdoc>
        ///     Populate the parameter statement list from a method.  Here is where we invoke the XmlProcessor
        /// </devdoc>
        private void OnMethodPopulateStatements(object sender, EventArgs e)
        {
            CodeMemberMethod codeMethod = (CodeMemberMethod)sender;
            CodeFunction     vsFunction = (CodeFunction)GetVsElementFromCodeObject(codeMethod);

            IMethodXML xmlMethod = null;

            try {
                xmlMethod = (IMethodXML)vsFunction;
            }
            catch {
            }

            if (xmlMethod == null)
            {
                Debug.Fail("Couldn't get IMethodXML for method '" + codeMethod.Name + "'");
                return;
            }

            string xmlCode = null;

            CodeDomLoader.StartMark();

            // get a hold of the XML if possible.
            //
            try {
                xmlMethod.GetXML(ref xmlCode);
            }
            catch (Exception ex) {
                System.Runtime.InteropServices.COMException cex = ex as System.Runtime.InteropServices.COMException;
                throw new Exception(SR.GetString(SR.FailedToParseMethod, vsFunction.Name, ex.Message));
            }
            finally {
                CodeDomLoader.EndMark("GetXML");
                CodeDomLoader.StartMark();
            }

            // pass it along to the processor.
            //
            XmlProcessor.ParseXml(xmlCode, codeMethod.Statements, this.FileName, codeMethod.Name);

            CodeDomLoader.EndMark("XML -> CodeDom Tree");


            // Add the statement count to our user data.  We use this as a quick way to tell if we need to
            // regenerate the contents of a method.
            //
            codeMethod.UserData[VsGenerateStatementsKey] = VsGenerateStatementsKey;

            // clear out the methods that we've generated before...
            //
            provider.ClearGeneratedMembers();

            // now apply any handlers for default events.
            //

            if (provider.IsVB)
            {
                CodeTypeDeclaration codeTypeDecl = (CodeTypeDeclaration)codeMethod.UserData[typeof(CodeTypeDeclaration)];

                if (codeTypeDecl == null)
                {
                    return;
                }

                Hashtable handlers = (Hashtable)codeTypeDecl.UserData[VbHandlesClausesKey];

                // save this so we can compare against it later to see
                // if we need to reload due to a handler change.
                //
                provider.FullParseHandlers = handlers;

                if (handlers == null || handlers.Count == 0)
                {
                    return;
                }
                GetDelegateHookupsFromHandlesClauses(handlers, codeTypeDecl, codeMethod.Statements);
            }
        }