peteshadbolt.co.uk

image

 

import numpy as np
import h5py
from matplotlib import pyplot as plt
import array
import wave

PATH = "nanopore-data.fast5"
INTERP = 10

# Load nanopore data
sig = []
hdf = h5py.File(PATH, 'r')
c = list(hdf['Raw/Reads'].keys())
for col in hdf['Raw/Reads/'][c[0]]['Signal'][()]:
    sig.append(int(col))

# Interpolate
data = np.array(sig)
x2 = np.linspace(0, len(sig), len(sig)*INTERP)
data = np.interp(x2, np.arange(len(sig)), data)

# Normalize
data -= np.average(data)
data *= (2**14-1.)/np.amax(data)
plt.plot(data)
plt.show()

# Make wav file
data = array.array("h", np.int_(data)).tostring()
output_file = wave.open("output.wav", "w")
output_file.setparams((1, 2, 44100, 0, "NONE", "not compressed"))
output_file.writeframes(data)
output_file.close()