Matrix Science header

create_mgf.cs

Create an mgf file from a Mascot results file.

/*
##############################################################################
# file: create_mgf.cs                                                        #
# 'msparser' toolkit example code                                            #
##############################################################################
# COPYRIGHT NOTICE                                                           #
# Copyright 1998-2015 Matrix Science Limited  All Rights Reserved.           #
#                                                                            #
##############################################################################
#    $Source: parser/examples/test_csharp/create_mgf.cs $                                #
#     $Author: villek@matrixscience.com $                                                    #
#       $Date: 2018-07-30 16:23:53 +0100 $                                         #
#   $Revision: 1b450440f9c97e1e41d0fc6016a27d68951d4532 | MSPARSER_REL_2_8_1-0-gea32989045 $                                                         #
# $NoKeywords::                                                            $ #
##############################################################################
*/

using System;
using System.IO;
using matrix_science.msparser;

namespace MsParserExamples
{
    public class create_mgf
    {
        public static void Main(string[] argv)
        {
            if (argv.Length < 1)
            {
                usage();
                return;
            }
            createMGF(argv[0]);
        }

        private static void createMGF(string filename)
        {
            ms_mascotresfile resfile = new ms_mascotresfile(filename);
            if (!resfile.isValid())
            {
                Console.WriteLine("Cannot open results file {0}: {1}", filename, resfile.getLastErrorString());
                return;
            }            

            string outputFilename = filename + ".mgf";
            // check if the mgf file already exists - do not overwrite if it does
            if (File.Exists(outputFilename))
            {
                Console.WriteLine("{0} already exists; will not overwrite", outputFilename);
                return;
            }

            // create the file
            

            using (TextWriter tw = new StreamWriter(outputFilename))
            {
                for (int q = 1; q <= resfile.getNumQueries(); q++)
                {
                    ms_inputquery inpQuery = new ms_inputquery(resfile, q);
                    if (inpQuery.getNumberOfPeaks(1) == 0)
                    {
                        // PMF - just the mass
                        tw.WriteLine(resfile.getObservedMass(q));
                        continue;
                    }

                    tw.WriteLine("BEGIN IONS");
                    tw.WriteLine("PEPMASS={0}", resfile.getObservedMass(q));

                    if (Math.Abs(resfile.getObservedCharge(q)) > 0)
                    {
                        tw.WriteLine("CHARGE={0}", resfile.getObservedCharge(q));
                    }
                    else
                    {
                        tw.WriteLine("CHARGE=Mr");
                    }

                    if (inpQuery.getStringTitle(true).Length > 0)
                    {
                        tw.WriteLine("TITLE={0}", inpQuery.getStringTitle(true));
                    }

                    for (int i = 1; i <= inpQuery.getNumberOfPeaks(1); i++)
                    {
                        tw.WriteLine("{0} {1}", inpQuery.getPeakMass(1, i), inpQuery.getPeakIntensity(1, i));
                    }

                    tw.WriteLine("END IONS");
                }
            }

        }

        private static void usage()
        {
            Console.WriteLine(@"Usage: create_mgf <results file>.

Given a mascot results file name, create an MGF file.  The MGF file
will be named <results file>.mgf in the same directory where <results file>
is located.");
        }
    }
}

Copyright © 2022 Matrix Science Ltd.  All Rights Reserved. Generated on Thu Mar 31 2022 01:12:29