scinote-web/app/utilities/first_time_data_generator.rb

1134 lines
32 KiB
Ruby
Raw Normal View History

2016-02-12 23:52:43 +08:00
module FirstTimeDataGenerator
2018-01-09 22:57:06 +08:00
# Create data for demo for new users
2018-04-12 20:38:22 +08:00
def seed_demo_data(user, team)
2016-02-12 23:52:43 +08:00
@user = user
2017-01-25 17:24:50 +08:00
# If private private team does not exist,
2016-02-12 23:52:43 +08:00
# there was something wrong with user creation.
# Do nothing
2017-01-25 17:24:50 +08:00
return unless team
2016-02-12 23:52:43 +08:00
# create custom repository samples
repository = Repository.create(
name: 'Samples',
team: team,
created_by: user
)
# create list value column for sample types
repository_column_sample_types = RepositoryColumn.create(
repository: repository,
created_by: user,
data_type: :RepositoryListValue,
name: 'Sample Types'
)
# create list value column for sample groups
repository_column_sample_groups = RepositoryColumn.create(
repository: repository,
created_by: user,
data_type: :RepositoryListValue,
name: 'Sample Groups'
)
# create few list items for sample types
repository_items_sample_types = []
['Potato leaves', 'Tea leaves', 'Potato bug'].each do |name|
item = RepositoryListItem.create(
data: name,
created_by: user,
last_modified_by: user,
repository_column: repository_column_sample_types,
repository: repository
)
repository_items_sample_types << item
end
# create few list items for sample groups
repository_items_sample_groups = []
%i(Fodder Nutrient Seed).each do |name|
item = RepositoryListItem.create(
data: name,
created_by: user,
last_modified_by: user,
repository_column: repository_column_sample_groups,
repository: repository
)
repository_items_sample_groups << item
end
2018-02-27 17:40:52 +08:00
repository_rows_to_assign = []
# Generate random custom respository sample names and assign sample types
# and groups
2018-04-12 20:38:22 +08:00
2018-02-27 17:40:52 +08:00
repository_sample_name = (0...3).map { 65.+(rand(26)).chr }.join << '/'
2018-02-27 17:43:57 +08:00
(1..5).each do |index|
repository_row = RepositoryRow.create(
repository: repository,
created_by: user,
last_modified_by: user,
2018-02-27 17:40:52 +08:00
name: repository_sample_name + index.to_s
)
RepositoryListValue.create(
created_by: user,
last_modified_by: user,
repository_list_item: repository_items_sample_types[
rand(0..(repository_items_sample_types.length - 1))
],
repository_cell_attributes: {
repository_row: repository_row,
repository_column: repository_column_sample_types
}
)
RepositoryListValue.create(
created_by: user,
last_modified_by: user,
repository_list_item: repository_items_sample_groups[
rand(0..(repository_items_sample_groups.length - 1))
],
repository_cell_attributes: {
repository_row: repository_row,
repository_column: repository_column_sample_groups
}
)
2018-02-27 17:40:52 +08:00
repository_rows_to_assign << repository_row
end
2016-02-12 23:52:43 +08:00
# Create sample types
SampleType.create(
2017-01-25 17:24:50 +08:00
name: 'Potato leaves',
team: team
2016-02-12 23:52:43 +08:00
)
SampleType.create(
2017-01-25 17:24:50 +08:00
name: 'Tea leaves',
team: team
2016-02-12 23:52:43 +08:00
)
SampleType.create(
2017-01-25 17:24:50 +08:00
name: 'Potato bug',
team: team
2016-02-12 23:52:43 +08:00
)
SampleGroup.create(
2017-01-25 17:24:50 +08:00
name: 'Fodder',
team: team,
color: Constants::TAG_COLORS[1]
2016-02-12 23:52:43 +08:00
)
SampleGroup.create(
2017-01-25 17:24:50 +08:00
name: 'Nutrient',
team: team,
color: Constants::TAG_COLORS[0]
2016-02-12 23:52:43 +08:00
)
SampleGroup.create(
2017-01-25 17:24:50 +08:00
name: 'Seed',
team: team,
color: Constants::TAG_COLORS[2]
2016-02-12 23:52:43 +08:00
)
samples = []
# Generate random sample names start
# and put it on the beginning of 5 samples
sample_name = (0...3).map{65.+(rand(26)).chr}.join << '/'
for i in 1..5
samples << Sample.create(
name: sample_name + i.to_s,
2017-01-25 17:24:50 +08:00
team: team,
2016-02-12 23:52:43 +08:00
user: user,
2017-01-25 17:24:50 +08:00
sample_type: rand < 0.8 ? pluck_random(team.sample_types) : nil,
sample_group: rand < 0.8 ? pluck_random(team.sample_groups) : nil
2016-02-12 23:52:43 +08:00
)
end
sample_name = (0...3).map{65.+(rand(26)).chr}.join << '/'
for i in 1..5
samples << Sample.create(
name: sample_name + i.to_s,
2017-01-25 17:24:50 +08:00
team: team,
2016-02-12 23:52:43 +08:00
user: user,
2017-01-25 17:24:50 +08:00
sample_type: rand < 0.8 ? pluck_random(team.sample_types) : nil,
sample_group: rand < 0.8 ? pluck_random(team.sample_groups) : nil
2016-02-12 23:52:43 +08:00
)
end
2017-01-25 17:24:50 +08:00
name = 'Demo project - qPCR'
exp_name = 'My first experiment'
2016-07-21 19:11:15 +08:00
# If there is an existing demo project, archive and rename it
2017-01-25 17:24:50 +08:00
if team.projects.where(name: name).present?
# TODO: check if we still need this code
# old = team.projects.where(name: 'Demo project - qPCR')[0]
2016-07-21 19:11:15 +08:00
# old.archive! user
i = 1
2017-01-25 22:00:14 +08:00
while team.projects.where(
name: name = "Demo project - qPCR (#{i})"
).present?
2016-07-21 19:11:15 +08:00
i += 1
end
end
2016-02-12 23:52:43 +08:00
project = Project.create(
2016-07-21 19:11:15 +08:00
visibility: 0,
name: name,
2016-02-12 23:52:43 +08:00
due_date: nil,
2017-01-25 17:24:50 +08:00
team: team,
2016-02-12 23:52:43 +08:00
created_by: user,
2016-07-21 19:11:15 +08:00
created_at: generate_random_time(1.week.ago),
2016-02-12 23:52:43 +08:00
last_modified_by: user,
archived: false
)
experiment = Experiment.create(
name: exp_name,
2017-01-25 17:24:50 +08:00
description: 'This is my very first experiment',
project: project,
created_by: user,
created_at: project.created_at + 5.minutes,
last_modified_by: user
2016-02-12 23:52:43 +08:00
)
# Automatically assign project author onto project
UserProject.create(
user: user,
project: project,
role: 0,
2016-07-21 19:11:15 +08:00
created_at: generate_random_time(1.week.ago)
2016-02-12 23:52:43 +08:00
)
# Activity for creating project
Activity.new(
2016-02-12 23:52:43 +08:00
type_of: :create_project,
user: user,
project: project,
message: I18n.t(
2017-01-25 17:24:50 +08:00
'activities.create_project',
2016-02-12 23:52:43 +08:00
user: user.full_name,
project: project.name
),
created_at: project.created_at,
updated_at: project.created_at
).sneaky_save
2016-02-12 23:52:43 +08:00
# Add a comment
generate_project_comment(
project,
user,
2017-01-25 17:24:50 +08:00
'I\'ve created a demo project'
2016-02-12 23:52:43 +08:00
)
# Create a module group
my_module_group = MyModuleGroup.create(
experiment: experiment
2016-02-12 23:52:43 +08:00
)
# Create project modules
my_modules = []
my_module_names = [
2017-01-25 17:24:50 +08:00
'Experiment design',
'Sampling biological material',
'RNA isolation',
'RNA quality & quantity - BIOANALYSER',
'Reverse transcription',
'qPCR',
'Data quality control',
'Data analysis - ddCq'
2016-02-12 23:52:43 +08:00
]
2017-01-25 17:24:50 +08:00
qpcr_module_description = 'PCR is a method where an enzyme
2016-02-12 23:52:43 +08:00
(thermostable DNA polymerase, originally isolated in 1960s
from bacterium Thermus aquaticus, growing in hot lakes of
Yellowstone park, USA) amplifies a short specific part of
the template DNA (amplicon) in cycles. In every cycle the
number of short specific sections of DNA is doubled, leading
to an exponential amplification of targets. More on how
2017-01-25 17:24:50 +08:00
conventional PCR works can be found here.'
2016-02-12 23:52:43 +08:00
my_module_names.each_with_index do |name, i|
my_module = MyModule.create(
name: name,
created_by: user,
2016-07-21 19:11:15 +08:00
created_at: generate_random_time(6.days.ago),
2016-02-12 23:52:43 +08:00
due_date: Time.now + (2 * i + 1).weeks,
description: i == 5 ? qpcr_module_description : nil,
2016-08-11 22:24:44 +08:00
x: (i < 4 ? i % 4 : 7 - i) * 32,
y: (i / 4) * 16,
2016-08-18 14:33:08 +08:00
experiment: experiment,
2016-02-12 23:52:43 +08:00
workflow_order: i,
my_module_group: my_module_group
)
my_modules << my_module
# Add connections between current and previous module
if i > 0
Connection.create(
input_id: my_module.id,
2017-01-25 17:24:50 +08:00
output_id: my_modules[i - 1].id
2016-02-12 23:52:43 +08:00
)
end
# Create module activity
Activity.new(
2016-02-12 23:52:43 +08:00
type_of: :create_module,
user: user,
project: project,
my_module: my_module,
message: I18n.t(
2017-01-25 17:24:50 +08:00
'activities.create_module',
2016-02-12 23:52:43 +08:00
user: user.full_name,
module: my_module.name
),
created_at: my_module.created_at,
updated_at: my_module.created_at
).sneaky_save
2016-02-12 23:52:43 +08:00
UserMyModule.create(
user: user,
my_module: my_module,
assigned_by: user,
2016-07-21 19:11:15 +08:00
created_at: generate_random_time(my_module.created_at, 2.minutes)
2016-02-12 23:52:43 +08:00
)
Activity.new(
2016-02-12 23:52:43 +08:00
type_of: :assign_user_to_module,
user: user,
project: project,
my_module: my_module,
message: I18n.t(
2017-01-25 17:24:50 +08:00
'activities.assign_user_to_module',
2016-02-12 23:52:43 +08:00
assigned_user: user.full_name,
module: my_module.name,
assigned_by_user: user.full_name
),
created_at: generate_random_time(my_module.created_at, 2.minutes),
updated_at: generate_random_time(my_module.created_at, 2.minutes)
).sneaky_save
2016-02-12 23:52:43 +08:00
end
# Create an archived module
archived_module = MyModule.create(
2017-01-25 17:24:50 +08:00
name: 'Data analysis - Pfaffl method',
2016-02-12 23:52:43 +08:00
created_by: user,
2016-07-21 19:11:15 +08:00
created_at: generate_random_time(6.days.ago),
2016-02-12 23:52:43 +08:00
due_date: Time.now + 1.week,
description: nil,
x: -1,
y: -1,
experiment: experiment,
2016-02-12 23:52:43 +08:00
workflow_order: -1,
my_module_group: nil,
archived: true,
2016-07-21 19:11:15 +08:00
archived_on: generate_random_time(3.days.ago),
2016-02-12 23:52:43 +08:00
archived_by: user
)
# Activity for creating archived module
Activity.new(
2016-02-12 23:52:43 +08:00
type_of: :create_module,
user: user,
project: project,
my_module: archived_module,
message: I18n.t(
2017-01-25 17:24:50 +08:00
'activities.create_module',
2016-02-12 23:52:43 +08:00
user: user.full_name,
module: archived_module.name
),
created_at: archived_module.created_at,
updated_at: archived_module.created_at
).sneaky_save
2016-02-12 23:52:43 +08:00
# Activity for archiving archived module
Activity.new(
2016-02-12 23:52:43 +08:00
type_of: :archive_module,
user: user,
project: project,
my_module: archived_module,
message: I18n.t(
2017-01-25 17:24:50 +08:00
'activities.archive_module',
2016-02-12 23:52:43 +08:00
user: user.full_name,
module: archived_module.name
),
created_at: archived_module.archived_on,
updated_at: archived_module.archived_on
).sneaky_save
2016-02-12 23:52:43 +08:00
# Assign new user to archived module
UserMyModule.create(
user: user,
my_module: archived_module,
assigned_by: user,
2016-07-21 19:11:15 +08:00
created_at: generate_random_time(archived_module.created_at, 2.minutes)
2016-02-12 23:52:43 +08:00
)
Activity.new(
2016-02-12 23:52:43 +08:00
type_of: :assign_user_to_module,
user: user,
project: project,
my_module: archived_module,
message: I18n.t(
2017-01-25 17:24:50 +08:00
'activities.assign_user_to_module',
2016-02-12 23:52:43 +08:00
assigned_user: user.full_name,
module: archived_module.name,
assigned_by_user: user.full_name
),
created_at: generate_random_time(archived_module.created_at, 2.minutes),
updated_at: generate_random_time(archived_module.created_at, 2.minutes)
).sneaky_save
2016-02-12 23:52:43 +08:00
# Assign 4 samples to modules
samples_to_assign = []
taken_sample_ids = []
for _ in 1..4
begin
sample = samples.sample
end while sample.id.in? taken_sample_ids
taken_sample_ids << sample.id
samples_to_assign << sample
end
my_modules[1].downstream_modules.each do |mm|
2016-02-12 23:52:43 +08:00
samples_to_assign.each do |s|
SampleMyModule.create(
sample: s,
my_module: mm
)
end
2018-02-27 17:40:52 +08:00
repository_rows_to_assign.each do |repository_row|
MyModuleRepositoryRow.create!(
repository_row: repository_row,
my_module: mm,
assigned_by: user
)
end
2016-02-12 23:52:43 +08:00
end
# Add comments to modules
generate_module_comment(
my_modules[0],
user,
'We should have a meeting to discuss sampling parametrs soon.'
2016-02-12 23:52:43 +08:00
)
generate_module_comment(
my_modules[0],
user,
'I agree.'
2016-02-12 23:52:43 +08:00
)
generate_module_comment(
my_modules[1],
user,
'The samples have arrived.'
2016-02-12 23:52:43 +08:00
)
generate_module_comment(
my_modules[2],
user,
'Due date has been postponed for a day.'
2016-02-12 23:52:43 +08:00
)
generate_module_comment(
my_modules[4],
user,
'Please show Steve the RT procedure.'
2016-02-12 23:52:43 +08:00
)
generate_module_comment(
my_modules[5],
user,
'The results must be very definitive.'
2016-02-12 23:52:43 +08:00
)
generate_module_comment(
my_modules[7],
user,
'The due date here is flexible.'
2016-02-12 23:52:43 +08:00
)
2016-07-21 19:11:15 +08:00
# Create tags and add them to module
drylab_tag = Tag.create(
2017-01-25 17:24:50 +08:00
name: 'Drylab',
color: Constants::TAG_COLORS[8],
2016-07-21 19:11:15 +08:00
project: project,
created_by: user,
last_modified_by: user
)
wetlab_tag = Tag.create(
2017-01-25 17:24:50 +08:00
name: 'Wetlab',
color: Constants::TAG_COLORS[14],
2016-07-21 19:11:15 +08:00
project: project,
created_by: user,
last_modified_by: user
)
decide_tag = Tag.create(
2017-01-25 17:24:50 +08:00
name: 'Decide',
color: Constants::TAG_COLORS[5],
2016-07-21 19:11:15 +08:00
project: project,
created_by: user,
last_modified_by: user
)
2016-02-12 23:52:43 +08:00
2016-07-21 19:11:15 +08:00
# Add tags to module
my_modules[0].tags << drylab_tag
my_modules[1].tags << wetlab_tag
my_modules[2].tags << wetlab_tag
my_modules[3].tags << wetlab_tag
my_modules[4].tags << wetlab_tag
my_modules[5].tags << drylab_tag
my_modules[6].tags << drylab_tag
my_modules[7].tags << drylab_tag
my_modules[7].tags << decide_tag
my_modules[7].save
# Load table contents yaml file
2017-01-25 17:24:50 +08:00
tab_content = YAML.load_file(
2018-01-09 22:57:06 +08:00
"#{Rails.root}/app/assets/demo_files/tables_content.yaml"
2017-01-25 17:24:50 +08:00
)
2016-07-21 19:11:15 +08:00
# Create module content
# ----------------- Module 1 ------------------
2016-02-12 23:52:43 +08:00
module_step_names = [
2017-01-25 17:24:50 +08:00
'Gene expression'
2016-02-12 23:52:43 +08:00
]
module_step_descriptions = [
2017-01-25 17:24:50 +08:00
'Compare response of PVYNTN, cab4 and PR1 genes in mock/virus ' \
'inoculated potatoes & in time'
2016-02-12 23:52:43 +08:00
]
2017-01-25 17:24:50 +08:00
generate_module_steps(my_modules[0],
module_step_names,
module_step_descriptions)
2016-02-12 23:52:43 +08:00
2016-07-21 19:11:15 +08:00
# Results
2018-01-09 22:57:06 +08:00
DelayedUploaderDemo.delay(queue: :demo).generate_result_asset(
2016-07-21 19:11:15 +08:00
my_module: my_modules[0],
current_user: user,
current_team: team,
2017-01-25 17:24:50 +08:00
result_name: 'sF',
2016-07-21 19:11:15 +08:00
created_at: generate_random_time(my_modules[0].created_at, 2.days),
2017-01-25 17:24:50 +08:00
file_name: 'samples.txt'
2016-07-21 19:11:15 +08:00
)
temp_result = Result.new(
2017-01-25 17:24:50 +08:00
name: 'Experimental design',
2016-07-21 19:11:15 +08:00
my_module: my_modules[0],
created_at: generate_random_time(my_modules[0].created_at, 1.days),
user: user
)
generate_result_comment(
temp_result,
user,
'The table shows proposed number of biological replicates.'
2016-07-21 19:11:15 +08:00
)
temp_result.table = Table.new(
created_by: user,
team: team,
2017-01-25 17:24:50 +08:00
contents: tab_content['module1']['experimental_design']
2016-07-21 19:11:15 +08:00
)
temp_result.save
# ----------------- Module 2 ------------------
2016-02-12 23:52:43 +08:00
module_step_names = [
2017-01-25 17:24:50 +08:00
'Inoculation of potatoes',
'Collection of potatoes',
'Store samples'
2016-02-12 23:52:43 +08:00
]
module_step_descriptions = [
2017-01-25 17:24:50 +08:00
'50% of samples should be mock inoculated while other 50% with ' \
'PVY NTN virus.',
'50% of PVYNTN inoculated potatos and 50% of Mock inoculated potatos ' \
'collect 1 day post inocullation while other halph of samples collect ' \
'6 days post inoculation.',
'Collect samples in 2ml tubes and put them in liquid nitrogen '\
'and store at 80°C.'
2016-02-12 23:52:43 +08:00
]
2017-01-25 17:24:50 +08:00
generate_module_steps(my_modules[1],
module_step_names,
module_step_descriptions)
2016-02-12 23:52:43 +08:00
2016-07-21 19:11:15 +08:00
# Add file to existig step
2018-01-09 22:57:06 +08:00
DelayedUploaderDemo.delay(queue: :demo).add_step_asset(
2017-01-25 17:24:50 +08:00
step: my_modules[1].protocol.steps.where('position = 0').take,
2016-07-21 19:11:15 +08:00
current_user: user,
current_team: team,
2017-01-25 17:24:50 +08:00
file_name: 'sample-potatoe.txt'
2016-07-21 19:11:15 +08:00
)
# Results
2018-01-09 22:57:06 +08:00
DelayedUploaderDemo.delay(queue: :demo).generate_result_asset(
2016-07-21 19:11:15 +08:00
my_module: my_modules[1],
current_user: user,
current_team: team,
2017-01-25 17:24:50 +08:00
result_name: 'PVY-inoculated plant, symptoms',
2016-07-21 19:11:15 +08:00
created_at: generate_random_time(my_modules[1].created_at, 1.days),
2017-01-25 17:24:50 +08:00
file_name: 'DSCN0660.JPG'
2016-07-21 19:11:15 +08:00
)
2018-01-09 22:57:06 +08:00
DelayedUploaderDemo.delay(queue: :demo).generate_result_asset(
2016-07-21 19:11:15 +08:00
my_module: my_modules[1],
current_user: user,
current_team: team,
2017-01-25 17:24:50 +08:00
result_name: 'mock-inoculated plant',
2016-07-21 19:11:15 +08:00
created_at: generate_random_time(my_modules[1].created_at, 2.days),
2017-01-25 17:24:50 +08:00
file_name: 'DSCN0354.JPG'
2016-07-21 19:11:15 +08:00
)
2018-01-09 22:57:06 +08:00
DelayedUploaderDemo.delay(queue: :demo).generate_result_asset(
2016-07-21 19:11:15 +08:00
my_module: my_modules[1],
current_user: user,
current_team: team,
2017-01-25 17:24:50 +08:00
result_name: 'Height of plants at 6dpi',
2016-07-21 19:11:15 +08:00
created_at: generate_random_time(my_modules[1].created_at, 3.days),
2017-01-25 17:24:50 +08:00
file_name: '6dpi_height.JPG'
2016-07-21 19:11:15 +08:00
)
# Add a text result
temp_result = Result.new(
2017-01-25 17:24:50 +08:00
name: 'Number of samples',
2016-07-21 19:11:15 +08:00
my_module: my_modules[1],
user: user,
created_at: generate_random_time(my_modules[1].created_at, 4.days)
)
2017-01-25 17:24:50 +08:00
temp_text = "There are many biological replicates we harvested " \
"for each type of sample (code-names):\n\n"
2016-07-21 19:11:15 +08:00
samples_to_assign.each do |s|
temp_text << "* #{s.name}\n\n"
end
temp_result.result_text = ResultText.new(
text: temp_text
)
temp_result.save
# Create result activity
Activity.new(
2016-07-21 19:11:15 +08:00
type_of: :add_result,
project: project,
my_module: my_modules[1],
user: user,
created_at: temp_result.created_at,
updated_at: temp_result.created_at,
2016-07-21 19:11:15 +08:00
message: I18n.t(
2017-01-25 17:24:50 +08:00
'activities.add_text_result',
2016-07-21 19:11:15 +08:00
user: user.full_name,
result: temp_result.name
)
).sneaky_save
2016-07-21 19:11:15 +08:00
# ----------------- Module 3 ------------------
2016-02-12 23:52:43 +08:00
module_step_names = [
2017-01-25 17:24:50 +08:00
'Homogenization of the material',
'Isolation of RNA with RNeasy plant mini kit'
2016-02-12 23:52:43 +08:00
]
module_step_descriptions = [
2017-01-25 17:24:50 +08:00
' Use tissue lyser: 1 min on step 3.',
2016-02-12 23:52:43 +08:00
nil
]
generate_module_steps(my_modules[2], module_step_names, module_step_descriptions)
2016-07-21 19:11:15 +08:00
# Add file to existig step
2018-01-09 22:57:06 +08:00
DelayedUploaderDemo.delay(queue: :demo).add_step_asset(
2017-01-25 17:24:50 +08:00
step: my_modules[2].protocol.steps.where('position = 1').take,
2016-07-21 19:11:15 +08:00
current_user: user,
current_team: team,
2017-01-25 17:24:50 +08:00
file_name: 'RNeasy-Plant-Mini-Kit-EN.pdf'
2016-07-21 19:11:15 +08:00
)
# Results
temp_result = Result.new(
2017-01-25 17:24:50 +08:00
name: 'Nanodrop results',
2016-07-21 19:11:15 +08:00
my_module: my_modules[2],
created_at: generate_random_time(my_modules[2].created_at, 1.days),
user: user
)
generate_result_comment(
temp_result,
user,
2016-08-30 19:06:49 +08:00
'PVY NTN 6dpi isolation seems to have failed, ' \
'please repeat nanodrop measurement.'
2016-07-21 19:11:15 +08:00
)
temp_result.table = Table.new(
created_by: user,
team: team,
2017-01-25 17:24:50 +08:00
contents: tab_content['module3']['nanodrop']
2016-07-21 19:11:15 +08:00
)
temp_result.save
# Create result activity
Activity.new(
2016-07-21 19:11:15 +08:00
type_of: :add_result,
project: project,
my_module: my_modules[2],
user: user,
created_at: temp_result.created_at,
updated_at: temp_result.created_at,
2016-07-21 19:11:15 +08:00
message: I18n.t(
2017-01-25 17:24:50 +08:00
'activities.add_text_result',
2016-07-21 19:11:15 +08:00
user: user.full_name,
result: temp_result.name
)
).sneaky_save
2016-07-21 19:11:15 +08:00
# Second result
2018-01-09 22:57:06 +08:00
DelayedUploaderDemo.delay(queue: :demo).generate_result_asset(
2016-07-21 19:11:15 +08:00
my_module: my_modules[2],
current_user: user,
current_team: team,
2017-01-25 17:24:50 +08:00
result_name: 'Agarose gel electrophoresis of totRNA samples',
2016-07-21 19:11:15 +08:00
created_at: generate_random_time(my_modules[2].created_at, 3.days),
2017-01-25 17:24:50 +08:00
file_name: 'totRNA_gel.jpg'
2016-07-21 19:11:15 +08:00
)
# ----------------- Module 4 ------------------
2016-02-12 23:52:43 +08:00
module_step_names = [
2017-01-25 17:24:50 +08:00
'Use Nano chip for testing RNA integrity'
2016-02-12 23:52:43 +08:00
]
module_step_descriptions = [
nil
]
generate_module_steps(my_modules[3], module_step_names, module_step_descriptions)
2016-07-21 19:11:15 +08:00
# Add file to existig step
2018-01-09 22:57:06 +08:00
DelayedUploaderDemo.delay(queue: :demo).add_step_asset(
2017-01-25 17:24:50 +08:00
step: my_modules[3].protocol.steps.where('position = 0').take,
2016-07-21 19:11:15 +08:00
current_user: user,
current_team: team,
2017-01-25 17:24:50 +08:00
file_name: 'G2938-90034_KitRNA6000Nano_ebook.pdf'
2016-07-21 19:11:15 +08:00
)
# Results
2018-01-09 22:57:06 +08:00
DelayedUploaderDemo.delay(queue: :demo).generate_result_asset(
2016-07-21 19:11:15 +08:00
my_module: my_modules[3],
current_user: user,
current_team: team,
2017-01-25 17:24:50 +08:00
result_name: 'Result of RNA integrity',
2016-07-21 19:11:15 +08:00
created_at: generate_random_time(my_modules[3].created_at, 2.days),
2017-01-25 17:24:50 +08:00
file_name: 'Bioanalyser_result.JPG'
2016-07-21 19:11:15 +08:00
)
# ----------------- Module 5 ------------------
2016-02-12 23:52:43 +08:00
module_step_names = [
2017-01-25 17:24:50 +08:00
'RNA denaturation',
'Prepare mastermix for RT',
'RT reaction'
2016-02-12 23:52:43 +08:00
]
module_step_descriptions = [
2017-01-25 17:24:50 +08:00
'1 ug of RNA denature at 80°C for 5 min --> ice',
'High Capacity cDNA Reverse Transcription Kit (Applied Biosystems)',
'25°C for 10 min 37°C for 2 h'
2016-02-12 23:52:43 +08:00
]
2017-01-25 17:24:50 +08:00
generate_module_steps(my_modules[4],
module_step_names,
module_step_descriptions)
2016-02-12 23:52:43 +08:00
2016-07-21 19:11:15 +08:00
module_checklist_items = [
2017-01-25 17:24:50 +08:00
'RT buffer',
'dNTP mix',
'Random Primers',
'RNase inhibitor',
'Reverse transcriptase',
'Optional: Luciferase mRNA (denatured)',
'H2O to 12.5 ul'
2016-07-21 19:11:15 +08:00
]
# Add checklist to step
2017-01-25 17:24:50 +08:00
step = my_modules[4].protocol.steps.where('position = 1').take
2016-07-21 19:11:15 +08:00
checklist = Checklist.new(
2017-01-25 17:24:50 +08:00
name: 'Mastermix',
2016-07-21 19:11:15 +08:00
step: step
)
module_checklist_items.each do |item|
checklist.checklist_items << ChecklistItem.new(
text: item
)
end
checklist.save
# ----------------- Module 6 ------------------
2016-02-12 23:52:43 +08:00
module_step_names = [
2017-01-25 17:24:50 +08:00
'Sample preparation',
'Reaction setup',
'Use Applied Biosystem 7300 instrument for qPCR',
'Setup of the 96 plate'
2016-02-12 23:52:43 +08:00
]
module_step_descriptions = [
nil,
nil,
2017-01-25 17:24:50 +08:00
'Use following cycling condtions:',
'Template of the 96-well plate'
2016-02-12 23:52:43 +08:00
]
2017-01-25 17:24:50 +08:00
generate_module_steps(my_modules[5],
module_step_names,
module_step_descriptions)
2016-02-12 23:52:43 +08:00
2016-07-21 19:11:15 +08:00
# Add file to existig steps
2018-01-09 22:57:06 +08:00
DelayedUploaderDemo.delay(queue: :demo).add_step_asset(
2017-01-25 17:24:50 +08:00
step: my_modules[5].protocol.steps.where('position = 0').take,
2016-07-21 19:11:15 +08:00
current_user: user,
current_team: team,
2017-01-25 17:24:50 +08:00
file_name: 'sample_preparation.JPG'
2016-07-21 19:11:15 +08:00
)
2018-01-09 22:57:06 +08:00
DelayedUploaderDemo.delay(queue: :demo).add_step_asset(
2017-01-25 17:24:50 +08:00
step: my_modules[5].protocol.steps.where('position = 1').take,
2016-07-21 19:11:15 +08:00
current_user: user,
current_team: team,
2017-01-25 17:24:50 +08:00
file_name: 'reaction_setup.JPG'
2016-07-21 19:11:15 +08:00
)
2018-01-09 22:57:06 +08:00
DelayedUploaderDemo.delay(queue: :demo).add_step_asset(
2017-01-25 17:24:50 +08:00
step: my_modules[5].protocol.steps.where('position = 2').take,
2016-07-21 19:11:15 +08:00
current_user: user,
current_team: team,
2017-01-25 17:24:50 +08:00
file_name: 'cycling_conditions.JPG'
2016-07-21 19:11:15 +08:00
)
2018-01-09 22:57:06 +08:00
DelayedUploaderDemo.delay(queue: :demo).add_step_asset(
2017-01-25 17:24:50 +08:00
step: my_modules[5].protocol.steps.where('position = 3').take,
2016-07-21 19:11:15 +08:00
current_user: user,
current_team: team,
2017-01-25 17:24:50 +08:00
file_name: '96plate.doc'
2016-07-21 19:11:15 +08:00
)
# Results
# Add a hard-coded table result
temp_result = Result.new(
2017-01-25 17:24:50 +08:00
name: 'Sample distribution on the plate',
2016-07-21 19:11:15 +08:00
my_module: my_modules[5],
user: user,
created_at: generate_random_time(my_modules[5].created_at, 1.days)
)
temp_result.table = Table.new(
created_by: user,
team: team,
2017-01-25 17:24:50 +08:00
contents: tab_content['module6']['distribution'] % {
sample0: samples_to_assign[0].name,
sample1: samples_to_assign[1].name,
sample2: samples_to_assign[2].name,
sample3: samples_to_assign[3].name
}
2016-07-21 19:11:15 +08:00
)
temp_result.save
# Create result activity
Activity.new(
2016-07-21 19:11:15 +08:00
type_of: :add_result,
project: project,
my_module: my_modules[5],
user: user,
created_at: temp_result.created_at,
updated_at: temp_result.created_at,
2016-07-21 19:11:15 +08:00
message: I18n.t(
2017-01-25 17:24:50 +08:00
'activities.add_table_result',
2016-07-21 19:11:15 +08:00
user: user.full_name,
result: temp_result.name
)
).sneaky_save
2016-07-21 19:11:15 +08:00
# Results
2018-01-09 22:57:06 +08:00
DelayedUploaderDemo.delay(queue: :demo).generate_result_asset(
2016-07-21 19:11:15 +08:00
my_module: my_modules[5],
current_user: user,
current_team: team,
2017-01-25 17:24:50 +08:00
result_name: 'Mixtures and plate setup',
2016-07-21 19:11:15 +08:00
created_at: generate_random_time(my_modules[5].created_at, 2.days),
2017-01-25 17:24:50 +08:00
file_name: 'Mixes_Templats.xls'
2016-07-21 19:11:15 +08:00
)
2018-01-09 22:57:06 +08:00
DelayedUploaderDemo.delay(queue: :demo).generate_result_asset(
2016-07-21 19:11:15 +08:00
my_module: my_modules[5],
current_user: user,
current_team: team,
2017-01-25 17:24:50 +08:00
result_name: 'Raw data from ABI 7300',
2016-07-21 19:11:15 +08:00
created_at: generate_random_time(my_modules[5].created_at, 3.days),
2017-01-25 17:24:50 +08:00
file_name: 'BootCamp-Experiment-results-20122.sds'
2016-07-21 19:11:15 +08:00
)
2018-01-09 22:57:06 +08:00
DelayedUploaderDemo.delay(queue: :demo).generate_result_asset(
2016-07-21 19:11:15 +08:00
my_module: my_modules[5],
current_user: user,
current_team: team,
2017-01-25 17:24:50 +08:00
result_name: 'All results - curves',
2016-07-21 19:11:15 +08:00
created_at: generate_random_time(my_modules[5].created_at, 4.days),
2017-01-25 17:24:50 +08:00
file_name: 'curves.JPG'
2016-07-21 19:11:15 +08:00
)
# ----------------- Module 7 ------------------
2016-02-12 23:52:43 +08:00
module_step_names = [
2017-01-25 17:24:50 +08:00
'Check negative controls NTC',
'Eliminate results that have positive NTCs'
2016-02-12 23:52:43 +08:00
]
module_step_descriptions = [
2017-01-25 17:24:50 +08:00
'They have to be negative when using TaqMan assays. If they are ' \
'positive when using SYBR assays check also melitng curve where signal ' \
'comes from. - if it is primer dimer result is negative - If it is ' \
'specific signal it is positive',
'And repeat procedure'
2016-02-12 23:52:43 +08:00
]
2017-01-25 17:24:50 +08:00
generate_module_steps(my_modules[6],
module_step_names,
module_step_descriptions)
2016-02-12 23:52:43 +08:00
2016-07-21 19:11:15 +08:00
# Add comment to step
2017-01-25 17:24:50 +08:00
step = my_modules[6].protocol.steps.where('position = 1').take
2016-07-21 19:11:15 +08:00
step.save
generate_step_comment(
step,
user,
'What is the Cq that should be considered as positive result?'
)
2016-07-21 19:11:15 +08:00
# ----------------- Module 8 ------------------
2016-02-12 23:52:43 +08:00
module_step_names = [
2017-01-25 17:24:50 +08:00
'Template for ddCq analysis'
2016-02-12 23:52:43 +08:00
]
module_step_descriptions = [
nil
]
2017-01-25 17:24:50 +08:00
generate_module_steps(my_modules[7],
module_step_names,
module_step_descriptions)
2016-02-12 23:52:43 +08:00
2016-07-21 19:11:15 +08:00
# Add file to existig step
2018-01-09 22:57:06 +08:00
DelayedUploaderDemo.delay(queue: :demo).add_step_asset(
2017-01-25 17:24:50 +08:00
step: my_modules[7].protocol.steps.where('position = 0').take,
2016-07-21 19:11:15 +08:00
current_user: user,
current_team: team,
2017-01-25 17:24:50 +08:00
file_name: 'ddCq-quantification_diagnostics-template.xls'
2016-02-12 23:52:43 +08:00
)
2016-07-21 19:11:15 +08:00
# Add result
2018-01-09 22:57:06 +08:00
DelayedUploaderDemo.delay(queue: :demo).generate_result_asset(
2016-07-21 19:11:15 +08:00
my_module: my_modules[7],
current_user: user,
current_team: team,
2017-01-25 17:24:50 +08:00
result_name: 'Results of ddCq method',
2016-07-21 19:11:15 +08:00
created_at: generate_random_time(my_modules[7].created_at, 1.days),
2017-01-25 17:24:50 +08:00
file_name: 'ddCq-quantification_diagnostics-results.xls'
2016-02-12 23:52:43 +08:00
)
2018-01-09 22:57:06 +08:00
DelayedUploaderDemo.delay(queue: :demo).generate_result_asset(
2016-07-21 19:11:15 +08:00
my_module: my_modules[7],
current_user: user,
current_team: team,
2017-01-25 17:24:50 +08:00
result_name: 'Dilution curve and efficiency',
2016-07-21 19:11:15 +08:00
created_at: generate_random_time(my_modules[7].created_at, 2.days),
2017-01-25 17:24:50 +08:00
file_name: 'dilution_curve-efficiency.JPG'
2016-07-21 19:11:15 +08:00
)
2016-02-12 23:52:43 +08:00
2018-01-09 22:57:06 +08:00
DelayedUploaderDemo.delay(queue: :demo).generate_result_asset(
2016-07-21 19:11:15 +08:00
my_module: my_modules[7],
current_user: user,
current_team: team,
2017-01-25 17:24:50 +08:00
result_name: 'Relative quantification results',
2016-07-21 19:11:15 +08:00
created_at: generate_random_time(my_modules[7].created_at, 3.days),
2017-01-25 17:24:50 +08:00
file_name: 'result-ddCq.JPG'
2016-02-12 23:52:43 +08:00
)
2016-07-21 19:11:15 +08:00
# Add a text result
2016-02-12 23:52:43 +08:00
temp_result = Result.new(
2017-01-25 17:24:50 +08:00
name: 'Markdown remarks',
2016-07-21 19:11:15 +08:00
my_module: my_modules[7],
2016-02-12 23:52:43 +08:00
user: user,
2016-07-21 19:11:15 +08:00
created_at: generate_random_time(my_modules[7].created_at, 4.days)
2016-02-12 23:52:43 +08:00
)
2017-01-25 17:24:50 +08:00
temp_text = "__Bolded text__\n\ndouble Enter " \
"to go to new line\n\n- bulletpoint"
2016-07-21 19:11:15 +08:00
temp_result.result_text = ResultText.new(
text: temp_text
2016-02-12 23:52:43 +08:00
)
temp_result.save
# Create result activity
Activity.new(
2016-02-12 23:52:43 +08:00
type_of: :add_result,
project: project,
2016-07-21 19:11:15 +08:00
my_module: my_modules[7],
2016-02-12 23:52:43 +08:00
user: user,
created_at: temp_result.created_at,
updated_at: temp_result.created_at,
2016-02-12 23:52:43 +08:00
message: I18n.t(
2017-01-25 17:24:50 +08:00
'activities.add_text_result',
2016-02-12 23:52:43 +08:00
user: user.full_name,
result: temp_result.name
)
).sneaky_save
2016-02-12 23:52:43 +08:00
# create thumbnail
experiment.delay.generate_workflow_img
2016-02-12 23:52:43 +08:00
end
# WARNING: This only works on PostgreSQL
def pluck_random(scope)
2017-01-25 17:24:50 +08:00
scope.order('RANDOM()').first
2016-02-12 23:52:43 +08:00
end
2016-07-21 19:11:15 +08:00
def generate_random_time(*args)
early = args[0]
if args.size == 1
rand(early..Time.now)
else
late = early + args[1]
late = Time.now if late > Time.now
rand(early..late)
end
end
2016-02-12 23:52:43 +08:00
# Create steps for given module
def generate_module_steps(my_module, step_names, step_descriptions)
step_names.each_with_index do |name, i|
2016-07-21 19:11:15 +08:00
created_at = generate_random_time(my_module.created_at, 5.hours)
2016-02-12 23:52:43 +08:00
completed = rand <= 0.3
2017-01-25 17:24:50 +08:00
completed_on = nil
completed_on = generate_random_time(created_at, 10.hours) if completed
2016-02-12 23:52:43 +08:00
step = Step.create(
created_at: created_at,
name: name,
description: step_descriptions[i],
position: i,
completed: completed,
user: @user,
2016-07-21 19:11:15 +08:00
protocol: my_module.protocol,
2016-02-12 23:52:43 +08:00
completed_on: completed_on
)
# Create activity
Activity.new(
2016-02-12 23:52:43 +08:00
type_of: :create_step,
2016-07-28 22:40:51 +08:00
project: my_module.experiment.project,
2016-02-12 23:52:43 +08:00
my_module: my_module,
user: step.user,
created_at: created_at,
updated_at: created_at,
2016-02-12 23:52:43 +08:00
message: I18n.t(
2017-01-25 17:24:50 +08:00
'activities.create_step',
2016-02-12 23:52:43 +08:00
user: step.user.full_name,
step: i,
step_name: step.name
)
).sneaky_save
2017-01-25 17:24:50 +08:00
if completed
Activity.new(
2016-02-12 23:52:43 +08:00
type_of: :complete_step,
2016-07-28 22:40:51 +08:00
project: my_module.experiment.project,
2016-02-12 23:52:43 +08:00
my_module: my_module,
user: step.user,
created_at: completed_on,
updated_at: completed_on,
2016-02-12 23:52:43 +08:00
message: I18n.t(
2017-01-25 17:24:50 +08:00
'activities.complete_step',
2016-02-12 23:52:43 +08:00
user: step.user.full_name,
2017-01-25 17:24:50 +08:00
step: i + 1,
2016-02-12 23:52:43 +08:00
step_name: step.name,
2016-07-21 19:11:15 +08:00
completed: my_module.protocol.completed_steps.count,
2017-01-25 17:24:50 +08:00
all: i + 1
2016-02-12 23:52:43 +08:00
)
).sneaky_save
2016-02-12 23:52:43 +08:00
# Also add random comments to completed steps
if rand < 0.3
2017-01-25 17:24:50 +08:00
polite_comment = 'This looks well.'
2016-02-12 23:52:43 +08:00
elsif rand < 0.4
2017-01-25 17:24:50 +08:00
polite_comment = 'Seems satisfactory.'
2016-02-12 23:52:43 +08:00
elsif rand < 0.4
2017-01-25 17:24:50 +08:00
polite_comment = 'Try a bit harder next time.'
2016-02-12 23:52:43 +08:00
end
if polite_comment
2016-07-21 19:11:15 +08:00
commented_on = generate_random_time(completed_on)
generate_step_comment(
step,
@user,
polite_comment,
commented_on
2016-02-12 23:52:43 +08:00
)
end
end
end
end
def generate_project_comment(project, user, message, created_at = nil)
created_at ||= generate_random_time(project.created_at, 1.week)
ProjectComment.create(
user: user,
message: message,
created_at: created_at,
project: project
)
Activity.new(
type_of: :add_comment_to_project,
user: user,
project: project,
created_at: created_at,
updated_at: created_at,
message: t('activities.add_comment_to_project',
user: user.full_name,
project: project.name)
).sneaky_save
end
def generate_module_comment(my_module, user, message, created_at = nil)
created_at ||= generate_random_time(my_module.created_at, 1.day)
TaskComment.create(
user: user,
message: message,
created_at: created_at,
my_module: my_module
)
Activity.new(
type_of: :add_comment_to_module,
user: user,
project: my_module.experiment.project,
my_module: my_module,
created_at: created_at,
updated_at: created_at,
message: t('activities.add_comment_to_module',
user: user.full_name,
module: my_module.name)
).sneaky_save
end
def generate_result_comment(result, user, message, created_at = nil)
created_at ||= generate_random_time(result.created_at, 1.days)
ResultComment.new(
user: user,
message: message,
created_at: created_at,
result: result
)
Activity.new(
type_of: :add_comment_to_result,
user: user,
project: result.my_module.experiment.project,
my_module: result.my_module,
created_at: created_at,
updated_at: created_at,
message: t('activities.add_comment_to_result',
user: user.full_name,
result: result.name)
).sneaky_save
end
def generate_step_comment(step, user, message, created_at = nil)
created_at ||= generate_random_time(step.created_at, 2.hours)
StepComment.new(
user: user,
message: message,
created_at: created_at,
step: step
)
Activity.new(
type_of: :add_comment_to_step,
user: user,
project: step.protocol.my_module.experiment.project,
my_module: step.protocol.my_module,
created_at: created_at,
updated_at: created_at,
message: t('activities.add_comment_to_step',
user: user.full_name,
step: step.position + 1,
step_name: step.name)
).sneaky_save
end
2016-07-21 19:11:15 +08:00
end