I am working on a code developed by others, and when I run following code:
os.chdir('C:\\Project Document\\BioPharm\\Mass Spectrometer\\Converted data')
f = open('List.txt','r')
filelist = f.readlines()
f.close()
all_spectra_mem = defaultdict(list)
all_spectra_interpol = defaultdict(list)
total_currents = defaultdict(list)
# Loop over files in filelist
for filename in filelist:
filename = filename.strip()
dataname = filename.split('.')[0]
sys.stderr.write('= Processing file %s\n' %(filename))
# Read the mzML file
msrun = pymzml.run.Reader(filename)
# print('This is msrun: ',msrun)
# In memory copy of the spectra
spectra_mem = []
all_spectra_mem[dataname].append(spectra_mem)
spectra_mem = all_spectra_mem[dataname]
# List of ion currents
ion_I = []
# Loop over spectra
n = 0
x_ion = []
for spe in msrun:
n = n + 1
# print('This is the indicator')
#print(spe)
#sys.exit('Error Message !')
# Copy current spectrum to memory
spectra_mem.append(copy.deepcopy(spe))
# Get the ion current for normalization
if 'total ion current' in spe:
print('Yes')
ion_I.append(spe['total ion current'])
x_ion.append(n)
# Event detection
mad = robust.mad(ion_I)
median = numpy.median(ion_I)
mean = numpy.mean(ion_I)
std = numpy.std(ion_I)
events = defaultdict(list)
current_event = 0
n = -1
last = 0
for spe in spectra_mem:
n = n + 1
if 'total ion current' in spe:
ion = spe['total ion current']
if ion >= (median+10*mad):
if last < (median+10*mad):
current_event += 1
events[current_event].append([n,ion])
last = ion
sys.stderr.write(' # A total of %i events were detected:\n' %(len(events)))
n = - 1
for key in events:
event = events[key]
sp_list = []
for sp in event:
sp_list.append(sp[0])
n = n + 1
sys.stderr.write('\tEvent %i with spectra: %s\n' %(key,', '.join(str(x) for x in sp_list)))
# Loop over events to get the peak
global_peaks_x = []
global_peaks_y = []
for key in events:
strfil='%s_event_%i.mzML' %(dataname,key)
print('This is strfil: ', strfil)
print('This is type indicator: ', type(strfil))
#isinstance(strfil,str)
# Write representative spectrum to new file
run2 = pymzml.run.Writer(filename=str(strfil), run=msrun, overwrite=True)
new_spectra = []
event = events[key]
event_currents = []
best_I = 0
best_sp = -1
for ev in event:
e1 = ev[0]
I = ev[1]
if I > best_I:
best_I = I
best_sp = e1
global_peaks_x.append(best_sp)
global_peaks_y.append(best_I)
sys.stderr.write('\tProcessing event %s spectrum %i\n' %(key,best_sp))`enter code here`
e1 = best_sp
I = best_I
run2.addSpec(spectra_mem[e1])
run2.save()
I get this error
Traceback (most recent call last):
File "C:\Project Document\Biopharm\Mass Spectrometer\Converted data|REIMES_event_identification.py", line 99, in <module>
run2=pymzml.run.writer(filename=str(strfil), run=msrun, overwrite=True)
File "C:\Anaconda3\lib\pymzml\run.py",line 708, in_init_input_xml_xtring +=new+line
TypeError: Cannot convert 'Bytes' object to str implicitly
Press any key to continue...
Appreciate that if you would suggest how to correct this issue.