Skip to content

Commit

Permalink
Merge pull request #1365 from sanger/y24-096-add-uuid-to-aliquot
Browse files Browse the repository at this point in the history
refactor: add uuid field to aliquot
  • Loading branch information
seenanair authored Jul 24, 2024
2 parents 06faafb + c150abb commit 290447a
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 8 deletions.
4 changes: 2 additions & 2 deletions app/exchanges/volume_tracking/message_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class MessageBuilder < Message::Message
# "messageUuid"=>"0a62ee15-bbf6-46f0-ba95-01d42622d076",
# "recordedAt"=>Mon, 15 Jul 2024 15:16:54.867713000 UTC +00:00,
# "volume"=>1.5, "concentration"=>10.0, "insertSize"=>100, "aliquotType"=>"primary",
# "aliquotId"=>"", "sourceType"=>"library", "sourceBarcode"=>"TRAC-2-35805",
# "aliquotUuid"=>"", "sourceType"=>"library", "sourceBarcode"=>"TRAC-2-35805",
# "sampleName"=>"Sample1", "usedByBarcode"=>"TRAC-2-35806", "usedByType"=>"pool"}}

def publish_data # rubocop:disable Metrics/MethodLength
Expand All @@ -20,7 +20,7 @@ def publish_data # rubocop:disable Metrics/MethodLength

aliquot = object
data = { source_type: '', source_barcode: '', sample_name: '',
used_by_type: 'nil', used_by_barcode: '', aliquot_id: aliquot.id.to_s || '' }
used_by_type: 'nil', used_by_barcode: '', aliquot_uuid: aliquot.uuid || '' }

case aliquot.source_type
when 'Pacbio::Library'
Expand Down
1 change: 1 addition & 0 deletions app/models/aliquot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# An aliquot can be used to track volumes and concentrations of samples
class Aliquot < ApplicationRecord
include SampleSheet::Aliquot
include Uuidable

enum state: { created: 0, used: 1 }
enum aliquot_type: { primary: 0, derived: 1 }
Expand Down
4 changes: 2 additions & 2 deletions config/pipelines/pacbio.yml
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,9 @@ default: &default
aliquotType:
type: :model
value: aliquot_type
aliquotId:
aliquotUuid:
type: :self
value: publish_data&.aliquot_id
value: publish_data&.aliquot_uuid
sourceType:
type: :self
value: publish_data&.source_type
Expand Down
17 changes: 17 additions & 0 deletions lib/tasks/update_aliquots_with_uuids.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

namespace :aliquots do
# A rake task to be run to add UUIDs to all aliquots that do not have one
desc 'Update aliquots with UUID'

task add_missing_uuids: :environment do
puts '-> Updating aliquots with UUID'

ActiveRecord::Base.transaction do
# If an aliquot does not have a UUID set it
Aliquot.where(uuid: nil).find_each do |aliquot|
aliquot.update!(uuid: SecureRandom.uuid)
end
end
end
end
6 changes: 3 additions & 3 deletions spec/exchanges/volume_tracking/message_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
sample_name: pacbio_library.sample_name,
used_by_type: 'nil',
used_by_barcode: '',
aliquot_id: aliquot.id.to_s || ''
aliquot_uuid: aliquot.uuid
})
end
end
Expand All @@ -37,7 +37,7 @@
sample_name: pacbio_library.sample_name,
used_by_type: 'pool',
used_by_barcode: pacbio_pool.tube.barcode,
aliquot_id: aliquot.id.to_s || ''
aliquot_uuid: aliquot.uuid
})
end
end
Expand All @@ -53,7 +53,7 @@
sample_name: pacbio_library.sample_name,
used_by_type: 'well',
used_by_barcode: "#{pacbio_well.plate.sequencing_kit_box_barcode}:#{pacbio_well.plate.plate_number}:#{pacbio_well.position}",
aliquot_id: aliquot.id.to_s || ''
aliquot_uuid: aliquot.uuid
})
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/volume_tracking_avro_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"subject": "create-aliquot-in-mlwh",
"version": 1,
"id": 14,
"schema": "{\"type\":\"record\",\"name\":\"CreateAliquotInMLWH\",\"namespace\":\"uk.ac.sanger.psd\",\"doc\":\"Create an Aliquot in MLWH coming from Traction.\",\"fields\":[{\"name\":\"messageUuid\",\"type\":{\"type\":\"string\",\"logicalType\":\"uuid\"},\"doc\":\"Unique message ID.\"},{\"name\":\"messageCreateDateUtc\",\"type\":{\"type\":\"long\",\"logicalType\":\"timestamp-millis\"},\"doc\":\"Date (UTC) that the message was created.\"},{\"name\":\"limsId\",\"type\":\"string\",\"doc\":\"The LIMS system that the aliquot was created in\"},{\"name\":\"aliquotId\",\"type\":{\"type\":\"string\",\"logicalType\":\"uuid\"},\"doc\":\"The UUID of the aliquot in the LIMS system\"},{\"name\":\"aliquotType\",\"type\":{\"type\":\"enum\",\"name\":\"allowedAliquotType\",\"symbols\":[\"primary\",\"derived\"]},\"doc\":\"The type of the aliquot\"},{\"name\":\"sourceType\",\"type\":{\"type\":\"enum\",\"name\":\"allowedSourceType\",\"symbols\":[\"well\",\"sample\",\"library\",\"pool\"]},\"doc\":\"The type of the source of the aliquot\"},{\"name\":\"sourceBarcode\",\"type\":\"string\",\"doc\":\"The barcode of the source of the aliquot\"},{\"name\":\"sampleName\",\"type\":\"string\",\"doc\":\"The name of the sample that the aliquot was created from\"},{\"name\":\"usedByType\",\"type\":{\"type\":\"enum\",\"name\":\"allowedUsedByType\",\"symbols\":[\"library\",\"pool\",\"run\",\"none\"],\"default\":\"none\"},\"doc\":\"The type of the entity that the aliquot is used by\"},{\"name\":\"usedByBarcode\",\"type\":\"string\",\"doc\":\"The barcode of the entity that the aliquot is used by\"},{\"name\":\"volume\",\"type\":\"float\",\"doc\":\"The volume of the aliquot (uL)\"},{\"name\":\"concentration\",\"type\":[\"null\",\"float\"],\"doc\":\"The concentration of the aliquot (ng/ul)\",\"default\":null},{\"name\":\"insertSize\",\"type\":[\"null\",\"int\"],\"doc\":\"The insert size for the aliquot\",\"default\":null},{\"name\":\"recordedAt\",\"type\":{\"type\":\"long\",\"logicalType\":\"timestamp-millis\"},\"doc\":\"The date and time that the aliquot was recorded\"}]}"
"schema": "{\"type\":\"record\",\"name\":\"CreateAliquotInMLWH\",\"namespace\":\"uk.ac.sanger.psd\",\"doc\":\"Create an Aliquot in MLWH coming from Traction.\",\"fields\":[{\"name\":\"messageUuid\",\"type\":{\"type\":\"string\",\"logicalType\":\"uuid\"},\"doc\":\"Unique message ID.\"},{\"name\":\"messageCreateDateUtc\",\"type\":{\"type\":\"long\",\"logicalType\":\"timestamp-millis\"},\"doc\":\"Date (UTC) that the message was created.\"},{\"name\":\"limsId\",\"type\":\"string\",\"doc\":\"The LIMS system that the aliquot was created in\"},{\"name\":\"aliquotUuid\",\"type\":{\"type\":\"string\",\"logicalType\":\"uuid\"},\"doc\":\"The UUID of the aliquot in the LIMS system\"},{\"name\":\"aliquotType\",\"type\":{\"type\":\"enum\",\"name\":\"allowedAliquotType\",\"symbols\":[\"primary\",\"derived\"]},\"doc\":\"The type of the aliquot\"},{\"name\":\"sourceType\",\"type\":{\"type\":\"enum\",\"name\":\"allowedSourceType\",\"symbols\":[\"well\",\"sample\",\"library\",\"pool\"]},\"doc\":\"The type of the source of the aliquot\"},{\"name\":\"sourceBarcode\",\"type\":\"string\",\"doc\":\"The barcode of the source of the aliquot\"},{\"name\":\"sampleName\",\"type\":\"string\",\"doc\":\"The name of the sample that the aliquot was created from\"},{\"name\":\"usedByType\",\"type\":{\"type\":\"enum\",\"name\":\"allowedUsedByType\",\"symbols\":[\"library\",\"pool\",\"run\",\"none\"],\"default\":\"none\"},\"doc\":\"The type of the entity that the aliquot is used by\"},{\"name\":\"usedByBarcode\",\"type\":\"string\",\"doc\":\"The barcode of the entity that the aliquot is used by\"},{\"name\":\"volume\",\"type\":\"float\",\"doc\":\"The volume of the aliquot (uL)\"},{\"name\":\"concentration\",\"type\":[\"null\",\"float\"],\"doc\":\"The concentration of the aliquot (ng/ul)\",\"default\":null},{\"name\":\"insertSize\",\"type\":[\"null\",\"int\"],\"doc\":\"The insert size for the aliquot\",\"default\":null},{\"name\":\"recordedAt\",\"type\":{\"type\":\"long\",\"logicalType\":\"timestamp-millis\"},\"doc\":\"The date and time that the aliquot was recorded\"}]}"
}
45 changes: 45 additions & 0 deletions spec/lib/tasks/update_aliquots_with_uuids.rake_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

require 'rails_helper'
require 'rake'

# only load Rake tasks if they haven't been loaded already
Rails.application.load_tasks if Rake::Task.tasks.empty?

RSpec.describe 'RakeTasks' do
# Create a default SMRTLink version to be used by factories
before do
Pacbio::SmrtLinkVersion.find_by(name: 'v13_sequel_iie') || create(:pacbio_smrt_link_version, name: 'v13_sequel_iie', default: true)
end

describe 'aliquots:add_missing_uuids' do
define_negated_matcher :not_change, :change

it 'assigns a random UUID to all aliquots that currently lack one.' do
aliquots_with_empty_uuid = create_list(:aliquot, 5, used_by: create(:pacbio_pool), uuid: nil)
aliquots_with_uuid = create_list(:aliquot, 5, used_by: create(:pacbio_pool))

# Get uuids to check they are not affected
uuids = aliquots_with_uuid.map(&:uuid)

# We shouldnt change the amount of aliquots
expect { Rake::Task['aliquots:add_missing_uuids'].invoke }.to not_change(Aliquot, :count).and output(
<<~HEREDOC
-> Updating aliquots with UUID
HEREDOC
).to_stdout

# Aliquots without uuids should have a uuid set
aliquots_with_empty_uuid.each do |aliquot|
aliquot.reload
expect(aliquot.uuid).not_to be_nil
end

# Other aliquots should not be affected
aliquots_with_uuid.each do |aliquot|
aliquot.reload
expect(uuids).to include aliquot.uuid
end
end
end
end
6 changes: 6 additions & 0 deletions spec/models/aliquot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
require 'rails_helper'

RSpec.describe Aliquot do
context 'uuidable aliquot' do
let(:uuidable_model) { :aliquot }

it_behaves_like 'uuidable'
end

it 'is invalid without volume' do
expect(build(:aliquot, volume: nil)).not_to be_valid
end
Expand Down

0 comments on commit 290447a

Please sign in to comment.