Skip to content

Commit

Permalink
Added column replicates in CSV box creation (#1983)
Browse files Browse the repository at this point in the history
* Added column replicates in CSV box creation

* Specs fixed

* Update app/models/box_form.rb

Co-authored-by: Julien Portalier <[email protected]>

---------

Co-authored-by: Julien Portalier <[email protected]>
  • Loading branch information
leandroradusky and ysbaddaden authored Aug 4, 2023
1 parent 171f6dd commit 792b728
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 43 deletions.
18 changes: 9 additions & 9 deletions app/assets/javascripts/components/upload_csv_box.js.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,17 @@ var UploadCsvBox = React.createClass({
const tooltipText = notFoundUuids.slice(0, 5).map((batch_number) => <div>{batch_number}</div>);

const rowContent = errorMessage ?
<div className="uploaded-samples-count">
{uploadedSamplesCount} {samplesText}
{batchesNotFound > 0 && (
<span className="dashed-underline">
{" ("}{batchesNotFound} {batchesText} not found{")"}
</span>
)}
</div> :
<div className="uploaded-samples-count">
{errorMessage}
</div>;
</div>:
<div className="uploaded-samples-count">
{uploadedSamplesCount} {samplesText}
{batchesNotFound > 0 && (
<span className="dashed-underline">
{" ("}{batchesNotFound} {batchesText} not found{")"}
</span>
)}
</div>;

return (
<div className="items-row" key={filename}>
Expand Down
22 changes: 13 additions & 9 deletions app/controllers/boxes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,23 @@ def validate

# TODO: should be handled by BoxForm (& duplicates BoxForm#parse_csv)
CSV.open(params[:csv_box].path, headers: true) do |csv|
while csv.headers == true
csv.readline
end
unique_rows = Set.new
csv.each do |row|

unless csv.headers == ["Batch", "Concentration", "Distractor", "Instructions"]
@error_message = "Invalid columns"
return # rubocop:disable Lint/NonLocalExitFromIterator
end
unless csv.headers == ["Batch", "Concentration", "Distractor", "Instructions", "Replicates"]
@error_message = "Invalid columns"
return # rubocop:disable Lint/NonLocalExitFromIterator
end

if unique_rows.include?([row["Batch"], row["Concentration"], row["Distractor"], row["Instructions"]])
@error_message = "Batch, Concentration, Distractor, Instructions must be unique"
return # rubocop:disable Lint/NonLocalExitFromIterator
end
unique_rows << [row["Batch"], row["Concentration"], row["Distractor"], row["Instructions"]]

csv.each do |row|
if batch_number = row["Batch"].presence&.strip
batch_numbers << batch_number
@samples_count += 1
@samples_count += row["Replicates"].to_i
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/box_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def parse_csv(path)
instruction: row["Instructions"],
concentrations: {
"0" => {
replicate: 1,
replicate: row["Replicates"].to_i,
concentration: Integer(Float(row["Concentration"]&.strip)),
},
},
Expand Down
14 changes: 5 additions & 9 deletions public/templates/upload_box.csv
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
Batch,Concentration,Distractor,Instructions
batch1,100,no,Add 175 ml of media
batch1,100,no,Add 175 ml of media
batch1,1000,no,Add 175 ml of media
batch1,1000,no,Add 175 ml of media
batch2,2,yes,Add 250 ml of media
batch2,2,yes,Add 250 ml of media
batch2,200,yes,Add 100 ml of media
batch2,200,yes,Add 100 ml of media
Batch,Concentration,Distractor,Instructions,Replicates
borrar,100,no,Add 175 ml of media,2
borrar,1000,no,Add 175 ml of media,2
Blank,2,yes,Add 250 ml of media,4
Blank,200,yes,Add 100 ml of media,4
6 changes: 3 additions & 3 deletions spec/controllers/boxes_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@
end

it "validates CSV headers" do
csv_file = fixture_file_upload(Rails.root.join("spec/fixtures/csvs/samples_results_1.csv"), "text/csv")
csv_file = fixture_file_upload(Rails.root.join("spec/fixtures/csvs/csv_box_no_headers.csv"), "text/csv")

expect do
post :validate, params: { csv_box: csv_file }, format: "json"
Expand All @@ -292,7 +292,7 @@
expect(JSON.parse(response.body)).to eq({
"found_batches" => ["DISTRACTOR"],
"not_found_batches" => [],
"samples_count" => 3,
"samples_count" => 4,
})
end

Expand All @@ -307,7 +307,7 @@
expect(JSON.parse(response.body)).to eq({
"found_batches" => ["DISTRACTOR"],
"not_found_batches" => ["VIRUS"],
"samples_count" => 5,
"samples_count" => 6,
})
end
end
Expand Down
8 changes: 3 additions & 5 deletions spec/fixtures/csvs/csv_box_1.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Batch,Concentration,Distractor,Instructions
DISTRACTOR,10,no,Add 10ml
DISTRACTOR,10,no,Add 10ml
DISTRACTOR,20,no,Add 10ml
DISTRACTOR,20,no,Add 10ml
Batch,Concentration,Distractor,Instructions,Replicates
DISTRACTOR,10,no,Add 10ml,2
DISTRACTOR,20,no,Add 10ml,2
11 changes: 4 additions & 7 deletions spec/fixtures/csvs/csv_box_2.csv
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
Batch,Concentration,Distractor,Instructions
VIRUS,10,no,Add 10ml
VIRUS,10,no,Add 10ml
DISTRACTOR,10,yes,Add 10ml
DISTRACTOR,10,yes,Add 10ml
DISTRACTOR,20,yes,Add 10ml
DISTRACTOR,20,yes,Add 10ml
Batch,Concentration,Distractor,Instructions,Replicates
VIRUS,10,no,Add 10ml,2
DISTRACTOR,10,yes,Add 10ml,2
DISTRACTOR,20,yes,Add 10ml,2
3 changes: 3 additions & 0 deletions spec/fixtures/csvs/csv_box_no_headers.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
VIRUS,10,no,Add 10ml,2
DISTRACTOR,10,yes,Add 10ml,2
DISTRACTOR,20,yes,Add 10ml,2

0 comments on commit 792b728

Please sign in to comment.