Matrix Science Mascot Parser toolkit
 
Loading...
Searching...
No Matches
create_mgf.py

Create an mgf file from a Mascot results file.

1#!/usr/bin/python
2
17
18import msparser
19import sys
20import os.path
21
22def main() :
23 if len(sys.argv) < 2 :
24 usage()
25 return 1
26
27 createMGF(sys.argv[1])
28
29
30def createMGF(filename) :
31 resfile = msparser.ms_mascotresfilebase.createResfile(filename)
32
33 if not resfile.isValid() :
34 print("Cannot open results file %s : %s" % (filename, resfile.getLastErrorString()))
35 return
36
37 output_filename = filename + '.mgf'
38
39 if os.path.isfile(output_filename) :
40 print("%s already exists; will not overwrite" % output_filename)
41 return
42
43 try :
44 fh = open(output_filename, "w")
45
46 except IOError as err:
47 errno, strerror = err.args
48 print("Cannot open output_filename for writing: I/O error(%d): %s" % (
49 errno, strerror
50 ))
51 sys.exit(1)
52
53 for q in range(1, 1 + resfile.getNumQueries()) :
54 inp_query = msparser.ms_inputquery(resfile, q)
55
56 if inp_query.getNumberOfPeaks(1) == 0 :
57 # PMF - just the mass
58 fh.write("%f\n" % resfile.getObservedMass(q))
59 continue
60
61 fh.write("BEGIN_IONS\n");
62 fh.write("PEPMASS=%f\n" % resfile.getObservedMass(q))
63
64 if resfile.getObservedCharge(q) > 0 :
65 fh.write("CHARGE=%f+\n" % resfile.getObservedCharge(q))
66 else :
67 fh.write("CHARGE=Mr\n")
68
69 if inp_query.getStringTitle(True) :
70 fh.write("TITLE=%s\n" % inp_query.getStringTitle(True))
71
72 for i in range(1, 1 + inp_query.getNumberOfPeaks(1)) :
73 fh.write("%f %f\n" % (inp_query.getPeakMass(1, i), inp_query.getPeakIntensity(1, i)))
74
75 fh.write("END IONS\n")
76
77 fh.close()
78
79
80def usage() :
81 print("""
82Usage: create_mgf.py <results file>
83
84Given a mascot results file name, create an MGF file. The MGF file
85will be named <results file>.mgf in the same directory where <results file>
86is located.
87""")
88
89if __name__ == "__main__" :
90 sys.exit(main())
91