This repository has been archived by the owner on Apr 5, 2023. It is now read-only.
forked from mafreitas/biosaur_adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBiosaurAdapter.py
executable file
·264 lines (205 loc) · 6.25 KB
/
BiosaurAdapter.py
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#! /usr/bin/env python3
import argparse
import logging
import pandas as pd
from pyopenms import *
import time
import os
import biosaur_src.biosaur as bsr
def timing(f):
"""
Helper function for timing other functions
Parameters
----------
arg1 : function
Returns
-------
funciton
new function wrap with timer and logging
"""
def wrap(*args):
time1 = time.time()
ret = f(*args)
time2 = time.time()
logging.info('{:s} function took {:.3f} s'.format(f.__name__, (time2-time1)))
return ret
return wrap
def process_arg(args):
"""
Convert namespace args objet to dictionary.
Helper function. conversion of the args from namespace to dictionary
allows for easier passing and modification.
Parameters
----------
args :
args manespace object from argspars
Returns
-------
dict :
dictionary of arguments
"""
return vars(args)
@timing
def write_feature(args):
"""
Write feature file
Read command line arguments and use psims package to format and write mzml
Parameters
----------
args : args manespace object from argspars
Returns
-------
None
"""
logging.info("Reading features from Biosaur")
df = pd.read_csv(args["output_file"], sep=',')
#print(df)
#print(df.columns)
#Create and Populate FeatureMap
#print([ bytes(args["input"].encode()) ])
fm = FeatureMap()
fm.setPrimaryMSRunPath( [ bytes(args["input"].encode()) ] )
#for index, row in df.iterrows():
for row in df.itertuples():
# print(row)
feature = Feature()
rtStart = row.rtStart*60.0
rtEnd = row.rtEnd*60.0
rtApex = row.rtApex*60.0
hull = ConvexHull2D()
mzend = row.mz + row.nIsotopes / row.charge
hull.addPoint([rtStart,row.mz])
hull.addPoint([rtEnd,row.mz])
hull.addPoint([rtEnd,mzend])
hull.addPoint([rtStart,mzend])
feature.setMZ( float(row.mz ))
feature.setRT( float(rtApex ) )
feature.setCharge( int(row.charge) )
feature.setIntensity( float(row.intensityApex) )
feature.setOverallQuality( float( row.cos_corr_2 ) )
feature.setConvexHulls([hull])
feature.setMetaValue(b"biosaur_massCalib" , float(row.massCalib) )
feature.setMetaValue(b"biosaur_rtApex" , float(row.rtApex) )
feature.setMetaValue(b"biosaur_intensityApex" , float(row.intensityApex) )
feature.setMetaValue(b"biosaur_charge" , float(row.charge) )
feature.setMetaValue(b"biosaur_nIsotopes" , float(row.nIsotopes) )
feature.setMetaValue(b"biosaur_nScans" , float(row.nScans) )
feature.setMetaValue(b"biosaur_cos_corr_1" , float(row.cos_corr_1) )
feature.setMetaValue(b"biosaur_cos_corr_2" , float(row.cos_corr_2) )
feature.setMetaValue(b"biosaur_diff_for_output" , float(row.diff_for_output) )
feature.setMetaValue(b"biosaur_ion_mobility" , float(row.ion_mobility) )
feature.setMetaValue(b"biosaur_FAIMS" , float(row.FAIMS) )
fm.push_back(feature)
logging.info("writing featureXML: {}".format(args['output']))
# Write featureXML file
fm.setUniqueIds()
FeatureXMLFile().store(args['output'], fm)
logging.info("writing featureXML: {}".format(args['output']))
def main():
"""
Main Function
Parse arguments and start writing
Parameters
----------
None
Returns
-------
None
"""
# Argument Parser
parser = argparse.ArgumentParser(description="tdf2mzml")
parser.add_argument(
'-in',
'--input',
required=True,
help='Input File')
parser.add_argument(
'-out',
'--output',
help='Output File')
parser.add_argument(
'-pxfp',
'--pep_xml_file_path',
default='0',
help='Pepxml filepath')
parser.add_argument(
'-cm',
'--correlation_map',
help='Add correlation map to final table',
action='store_true')
parser.add_argument(
'-nm',
'--negative_mode',
help='Add negative mode option',
action='store_true')
parser.add_argument(
'-sb',
'--skip_biosaur',
help='write_featurexml',
action='store_true')
parser.add_argument(
'-ac',
'--mass_accuracy',
help='Mass accuracy',
type=float,
default=8)
parser.add_argument(
'-minc',
'--min_charge',
help='Minimum charge',
type=int,
default=1)
parser.add_argument(
'-maxc',
'--max_charge',
help='Maximum charge',
type=int,
default=6)
parser.add_argument(
'-minl',
'--min_length',
help='Minimum length',
type=int,
default=3)
parser.add_argument(
'-minlh',
'--min_length_hill',
help='Minimum length for hill',
type=int,
default=2)
parser.add_argument(
'-mini',
'--min_intensity',
type=float,
help='Minimum intensity',
default=1)
parser.add_argument(
'-hvf',
'--hill_valley_factor',
help='Hill Valley Factor',
type=float,
default=1.3)
parser.add_argument(
'-np',
'--number_of_processes',
help='Number of processes',
default=0)
parser.add_argument(
'--debug',
action='store_true',
help='Enable debugging output')
logging.basicConfig(level=logging.INFO)
args = process_arg(parser.parse_args())
# Create input arg for Biosaur
args["input_mzml_path"] = [ args["input"] ]
# Force the output for Biosaur to conform to the following
args["output_file"] = "{}.features.tsv".format(os.path.splitext(args["input"])[0])
# If not set, supply a defaute featureXML file name
if args["output"] == None:
args["output"] = "{}.featureXML".format(os.path.splitext(args["input"])[0])
# Fortunately Biosaur uses the logging package
if not args["skip_biosaur"]:
bsr.bio.process_files(args)
write_feature(args)
if __name__ == "__main__":
main()