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: /vol/cvsroot/parser/examples/test_perl/create_mgf.pl,v $ #
#     $Author: villek $ #
#       $Date: 2010/08/31 08:48:07 $ #
#   $Revision: 1.5 $ #
# $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 © 2016 Matrix Science Ltd.  All Rights Reserved. Generated on Fri Jun 2 2017 01:44:50