Repository navigation
Expand file tree
/
Copy pathpopulate_headers.py
More file actions
130 lines (118 loc) · 4.45 KB
/
Copy pathpopulate_headers.py
File metadata and controls
130 lines (118 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 14 16:08:34 2020
@author: pma
"""
import csv
import sys
# define help menu
try:
metadata_file_path = sys.argv[1]
mass_files_folder_path = sys.argv[2]
mode = sys.argv[3]
print('Parsing file '
+ metadata_file_path
+ ' and appending metadata to mass raw files loacted in '
+ mass_files_folder_path
+ ' in '
+ mode
+ ' mode')
except:
print('Please fill metadata file path followed by mass_files_folder_path')
# This will contain the tsv data
data = {}
header = False # Was header read?
# Read the tsv file and store data in data
with open(metadata_file_path, 'r') as tsv_file:
tsv_reader = csv.reader(tsv_file, delimiter='\t', quotechar='"')
try:
for row in tsv_reader:
# Set the header if it wasn't yet
if header is False:
header = row
continue # Go to the next row
if row != []: # Take care of shitty windows format
data[row[header.index('SHORT_IK')]] = row
except csv.Error as e:
sys.exit('file {}, line {}: {}'.format(metadata_file_path,
tsv_reader.line_num,
e))
done = 0
skipped = 0
# Iterate over all files
for pos, row in data.items():
# This will contain the mgf data
content = ''
print(row[header.index('SHORT_IK')])
filename = row[header.index('SHORT_IK')]
if ".mgf" not in filename:
filename = "%s.mgf" % filename
# Create content
# To access a row of a specific name:
# row[header.index('NAMEOFTHEROW')]
# Careful, as the program will crash if the row doesn't
# exist (left as an exercise ;) )
content += "BEGIN IONS\n"
if mode == "negative":
content += "PEPMASS={}\n".format(row[header.index('deprotonated_emass')])
content += "CHARGE=1-\n"
else:
content += "PEPMASS={}\n".format(row[header.index('protonated_emass')])
content += "CHARGE=1+\n"
# MSLEVEL=xxx
# content += "SOURCE_INSTRUMENT={}-{}\n".format(
# row[header.index('INSTRUMENT')],
# row[header.index('IONSOURCE')])
content += "FILENAME={}\n".format(row[header.index('SHORT_IK')])
# content += "InChIKey={}\n".format(row[header.index('InChIKey')])
content += "MOLECULAR_FORMULA={}\n".format(row[header.index('MF')])
content += "SEQ=*..*\n"
# content += "NOTES={}:{}:{}:{}:{}:{}\n".format(
# row[header.index('PI')],
# row[header.index('DATACOLLECTOR')],
# "N/A", # Change that
# "N/A", # Change that
# row[header.index('ACQUISITION')],
# "N/A" # Change that
# )
if mode == "negative":
content += "IONMODE=NEGATIVE\n"
else:
content += "IONMODE=POSITIVE\n"
content += "EXACTMASS={}\n".format(row[header.index('EMW')])
content += "NAME={}\n".format(row[header.index('SHORT_IK')])
content += "SMILES={}\n".format(row[header.index('SMILES')])
content += "INCHI={}\n".format(row[header.index('InChI')])
# content += "Synonyms={}\n".format(row[header.index('Syn_list')])
# content += "LIB={}\n".format(row[header.index('LIB')])
# content += "INCHIAUX={}\n".format(row[header.index('INCHI_KEY')])
if mode == "negative":
content += "LIBRARYQUALITY=In-silico ESI-MS/MS [M-H]- Spectra PREDICTED BY CFM-ID 4.4.7\n"
else:
content += "LIBRARYQUALITY=In-silico ESI-MS/MS [M+H]+ Spectra PREDICTED BY CFM-ID 4.4.7\n"
# Change that
# ORGANISM=xxx
# SPECTRUMID=xxx
# ACTIVATION=xxx
# INSTRUMENT=xxx
# TITLE=xxx
# content += "SCANS={}\n".format(row[header.index('EXTRACTSCAN')])
content += "SCANS=1\n"
# Try to open the file
# Be careful given that the path below is relative be sure to check were you launch the script from
try:
with open(str(mass_files_folder_path) + '{}'.format(filename), 'r') as raw_file:
for line in raw_file:
content += line.strip() + "\n"
content += '\n'.join(list(raw_file))
content += "END IONS\n"
# Write the output file
with open(str(mass_files_folder_path) + '{}'.format(filename), 'w') as mgf_file:
mgf_file.write(content)
done += 1
except IOError:
print('File {} not found, skipped'.format(filename))
skipped += 1
continue
print('Treated {} files, skipped {}.'.format(done, skipped))