Matrix Science header

create_mgf.pl

Create an mgf file from a Mascot results file.

#!/usr/local/bin/perl
##############################################################################
# file: create_mgf.pl                                                        #
# 'msparser' toolkit                                                         #
# Test harness / example code                                                #
##############################################################################
# COPYRIGHT NOTICE                                                           #
# Copyright 1998-2010 Matrix Science Limited  All Rights Reserved.           #
#                                                                            #
##############################################################################
#     $Source: parser/examples/test_perl/create_mgf.pl $ #
#     $Author: villek@matrixscience.com $ #
#       $Date: 2018-07-30 16:23:53 +0100 $ #
#   $Revision: 1b450440f9c97e1e41d0fc6016a27d68951d4532 | MSPARSER_REL_2_8_1-0-gea32989045 $ #
# $NoKeywords::                                                            $ #
##############################################################################
use strict;
##############################################################################

use msparser;

if (!defined($ARGV[0])) { 
  usage();
  exit(1);
}

createMGF($ARGV[0]);


sub createMGF {
    my ($filename) = @_;
    
    my $resfile = new msparser::ms_mascotresfile($filename);

    if (!$resfile->isValid) {
        print STDERR "Cannot open results file ", $filename, ": ";
        print STDERR $resfile->getLastErrorString(), "\n";
        return;
    }

    my $output_filename = $filename.'.mgf';

    if (-e $output_filename) {
        print STDERR "$output_filename already exists; will not overwrite\n";
        return;
    }

    open(my $fh, '>', $output_filename) 
        or die "Cannot open $output_filename for writing: $!";

    for my $q (1 .. $resfile->getNumQueries) {
        my $inp_query = new msparser::ms_inputquery($resfile, $q);

        if ($inp_query->getNumberOfPeaks(1) == 0) {
            # PMF - just the mass
            print $fh $resfile->getObservedMass($q), "\n";
            next;
        }

        print $fh "BEGIN_IONS\n";
        print $fh "PEPMASS=", $resfile->getObservedMass($q), "\n";

        if ($resfile->getObservedCharge($q) > 0) {
            print $fh "CHARGE=", $resfile->getObservedCharge($q), "+\n";
        } else {
            print $fh "CHARGE=Mr\n";
        }

        if ($inp_query->getStringTitle(1)) {
            print $fh "TITLE=", $inp_query->getStringTitle(1), "\n";
        }
        
        for my $i (1 .. $inp_query->getNumberOfPeaks(1)) {
            print $fh $inp_query->getPeakMass(1, $i), " ", $inp_query->getPeakIntensity(1, $i), "\n";
        }

        print $fh "END IONS\n\n";
    }
}

sub usage {
    print <<EOF;
Usage: create_mgf.pl <results file>

Given an 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.
EOF
}


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