def call_file(filename): out = [] try: with get_fast5_file(filename, mode="r") as f5: ftype = check_file_type(f5) # single-read/multi-read for read in f5.get_reads(): read_id = read.read_id run_id = read.run_id.decode('utf-8') read_number = read.handle['Raw'].attrs[ 'read_number'] if ftype == 'multi-read' else read.status.read_info[ 0].read_number start_time = read.handle['Raw'].attrs[ 'start_time'] if ftype == 'multi-read' else read.status.read_info[ 0].start_time channel_number = read.handle[ read.global_key + 'channel_id'].attrs['channel_number'].decode('utf-8') sampling_rate = read.handle[ read.global_key + 'channel_id'].attrs['sampling_rate'] exp_start_time = read.handle[ read.global_key + 'tracking_id'].attrs['exp_start_time'].decode('utf-8') start_time = add_time_seconds(exp_start_time, start_time / sampling_rate) signal = read.get_raw_data() signal = rescale_signal(signal) basecall, qual = caller.call_raw_signal(signal) out.append((read_id, run_id, read_number, channel_number, start_time, basecall, qual)) except OSError: return [] return out
def test_correct_type(self): single_read_path = os.path.join(test_data, "single_reads", "read0.fast5") single_read_id = Fast5File(single_read_path).get_read_id() with get_fast5_file(single_read_path) as f5: self.assertEqual(type(f5), Fast5File) self.assertEqual(check_file_type(f5), SINGLE_READ) self.assertEqual(len(f5.get_read_ids()), 1) self.assertEqual(single_read_id, f5.get_read_ids()[0]) self.get_raw(f5) multi_read_path = os.path.join(test_data, "multi_read", "batch_0.fast5") with get_fast5_file(multi_read_path) as f5: self.assertEqual(type(f5), MultiFast5File) self.assertEqual(check_file_type(f5), MULTI_READ) self.assertTrue(len(f5.get_read_ids()) >= 1) self.get_raw(f5)
def check_file_type(myfile): fobj = fast5_interface.get_fast5_file(os.path.join(root, name)) if fast5_interface.check_file_type(fobj) == "multi-read": #convert file to single fast5 print("converting fast5 file****") multi_to_single_fast5.convert_multi_to_single(os.path.join(root, name), directory, "single")
def try_convert_read(input_file, output_handle): with Fast5File(input_file, 'r') as single_f5: file_type = check_file_type(single_f5) if file_type != SINGLE_READ: raise Fast5FileTypeError( "Could not convert Single->Multi for file type '{}' with path '{}'" "".format(file_type, input_file)) add_single_read_to_multi_fast5(output_handle, single_f5) return os.path.basename(input_file)
def convert_fast5_type(directory): #go through fast5 files and check if the files is multi or single fast5 file #we need a single fast5 file for root, dirs, files in os.walk(directory): for name in files: if name.endswith(".fast5"): fobj = fast5_interface.get_fast5_file(os.path.join(root, name)) if fast5_interface.check_file_type(fobj) == "multi-read": #convert file to single fast5 print("converting fast5 file****") multi_to_single_fast5.convert_multi_to_single( os.path.join(root, name), directory, "single")
def try_multi_to_single_conversion(input_file, output_folder, subfolder): output_files = [] with MultiFast5File(input_file, 'r') as multi_f5: file_type = check_file_type(multi_f5) if file_type != MULTI_READ: raise Fast5FileTypeError( "Could not convert Multi->Single for file type '{}' with path '{}'" "".format(file_type, input_file)) for read in multi_f5.get_reads(): try: output_file = os.path.join(output_folder, subfolder, "{}.fast5".format(read.read_id)) create_single_f5(output_file, read) output_files.append(os.path.basename(output_file)) except Exception as e: logger.error("{}\n\tFailed to copy read '{}' from {}" "".format(str(e), read.read_id, input_file), exc_info=exc_info) return output_files