Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batch sample import service #897

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 172 additions & 0 deletions app/services/samples/batch_file_import_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
# frozen_string_literal: true

require 'roo'

module Samples
# Service used to batch create samples via a file
class BatchFileImportService < BaseService # rubocop:disable Metrics/ClassLength
SampleFileImportError = Class.new(StandardError)

def initialize(namespace, user = nil, blob_id = nil, params = {})
super(user, params)
@namespace = namespace
@file = ActiveStorage::Blob.find(blob_id)
@sample_name_column = params[:sample_name_column]
@project_puid_column = params[:project_puid_column]
@sample_description_column = params[:sample_description_column]
@spreadsheet = nil
@headers = nil
@temp_import_file = Tempfile.new
end

def execute
authorize! @namespace, to: :update_sample_metadata?

validate_sample_name_column

validate_file

perform_file_import
rescue Samples::BatchFileImportService::SampleFileImportError => e
@namespace.errors.add(:base, e.message)
{}
end

private

def validate_sample_name_column
return unless @sample_name_column.nil?

raise SampleFileImportError,
I18n.t('services.samples.metadata.import_file.empty_sample_id_column') # TODO: refactor out
end

def validate_file_extension
file_extension = File.extname(@file.filename.to_s).downcase

return file_extension if %w[.csv .tsv .xls .xlsx].include?(file_extension)

raise SampleFileImportError,
I18n.t('services.samples.metadata.import_file.invalid_file_extension') # TODO: refactor out
end

def validate_file_headers
duplicate_headers = @headers.find_all { |header| @headers.count(header) > 1 }.uniq
unless duplicate_headers.empty?
raise SampleFileImportError,
I18n.t('services.sammple.batch_import.duplicate_column_names')
end

unless @headers.include?(@sample_name_column)
raise SampleFileImportError,
I18n.t('services.samples.batch_import.missing_header',
header_title: @sample_name_column, header_desc: 'sample name')
end

unless @headers.include?(@project_puid_column) # rubocop:disable Style/GuardClause
raise SampleFileImportError,
I18n.t('services.samples.batch_import.missing_header',
header_title: @project_puid_column, header_desc: 'project puid')
end
end

def validate_file_rows
# Should have at least 2 rows
first_row = @spreadsheet.row(2)
return unless first_row.compact.empty?

raise SampleFileImportError,
I18n.t('services.samples.batch_import.missing_data_row')
end

def validate_file
if @file.nil?
raise SampleFileImportError,
I18n.t('services.samples.batch_import.empty_file')
end

extension = validate_file_extension
download_batch_import_file(extension)

@headers = @spreadsheet.row(1).compact
validate_file_headers

validate_file_rows
end

def download_batch_import_file(extension)
begin
@temp_import_file.binmode
@file.download do |chunk|
@temp_import_file.write(chunk)
end
ensure
@temp_import_file.close
end
@spreadsheet = if extension.eql? '.tsv'
Roo::CSV.new(@temp_import_file, extension:, csv_options: { col_sep: "\t" })
else
Roo::Spreadsheet.open(@temp_import_file.path, extension:)
end
end

def perform_file_import # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
response = {}
parse_settings = @headers.zip(@headers).to_h

@spreadsheet.each_with_index(parse_settings) do |data, index|
next unless index.positive?

# TODO: handle metadata

sample_name = data[@sample_name_column]
project_puid = data[@project_puid_column]
description = data[@sample_description_column]

if sample_name.nil? || project_puid.nil?
response["index #{index}"] = {
path: ['sample'],
message: I18n.t('services.samples.batch_import.missing_field', index: index)
}
next
end

project = Namespaces::ProjectNamespace.find_by(puid: project_puid)&.project
unless project
response[sample_name] = {
path: ['project'],
message: I18n.t('services.samples.batch_import.project_puid_not_found', project_puid: project_puid)
}
next
end

response[sample_name] = process_sample_row(sample_name, project, description)
cleanup_files
response
end
response
end

def cleanup_files
# delete the blob and temporary file as we no longer require them
@file.purge
@temp_import_file.unlink
end

def process_sample_row(name, project, description)
sample_params = { name:, description: }
sample = Samples::CreateService.new(current_user, project, sample_params).execute

if sample.persisted?
sample
else
sample.errors.map do |error|
{
path: ['sample', error.attribute.to_s.camelize(:lower)],
message: error.message
}
end
end
end
end
end
8 changes: 8 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1675,6 +1675,14 @@ en:
namespace_project_exists: Project with the same name or path already exists in the target namespace.
project_in_namespace: Project in namespace
samples:
batch_import:
project_puid_not_found: 'Project PUID %{project_puid} could not be found'
missing_field: 'Row with index %{index} is missing required fields'
missing_header: 'File is missing %{header_title} header for %{header_desc}'
missing_header_project: 'File is missing %{header_expected} header for project puid'
duplicate_column_names: The file has duplicate column header names. Please remove the columns with duplicate header names and retry uploading.
missing_data_row: The file does not have any data rows. Please add some rows of data to the file and retry uploading.
empty_file: The file cannot be empty.
clone:
empty_new_project_id: Destination project was not selected.
empty_sample_ids: The sample ids are empty.
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/files/valid_batch_sample_import.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sample_name,project_puid,description
my new sample,INXT_PRJ_AZCEZ6YMW3,my description
my new sample 2,INXT_PRJ_AZCEZ6YMW3,my description 2
Loading