|
| 1 | +# # This rule applies the barcodes from a rack into a plate |
| 2 | +# { |
| 3 | +# ?plate se:a """Plate""" . |
| 4 | +# ?rack se:a """TubeRack""" . |
| 5 | +# ?rack se:mergeInto ?plate . |
| 6 | +# ?rack se:contains ?tube . |
| 7 | +# ?plate se:contains ?well . |
| 8 | +# ?tube se:location ?location . |
| 9 | +# ?tube se:barcode ?barcode . |
| 10 | +# ?well se:location ?location . |
| 11 | +# } => { |
| 12 | +# :step :addFacts { ?well se:barcode ?barcode . } . |
| 13 | +# :step :removeFacts { ?tube se:barcode ?barcode . } . |
| 14 | +# :step :addFacts { ?tube se:barcode """""" . } . |
| 15 | +# :step :addFacts { ?tube se:previousBarcode ?barcode . } . |
| 16 | +# :step :addFacts { ?tube se:appliedBarcodeTo ?well . } . |
| 17 | +# :step :addFacts { ?rack se:mergedInto ?plate .}. |
| 18 | +# :step :addFacts { ?plate se:mergedFrom ?rack .}. |
| 19 | +# }. |
| 20 | + |
| 21 | +class MoveBarcodesFromTubeRackToPlate |
| 22 | + attr_reader :asset_group |
| 23 | + |
| 24 | + |
| 25 | + def initialize(params) |
| 26 | + @asset_group = params[:asset_group] |
| 27 | + end |
| 28 | + |
| 29 | + def assets_compatible_with_step_type |
| 30 | + [plate, tube_rack].flatten.compact.count > 0 |
| 31 | + end |
| 32 | + |
| 33 | + |
| 34 | + def plate |
| 35 | + asset_group.assets.select{|a| a.facts.where(predicate: 'a', object: 'Plate').count > 0 }.first |
| 36 | + end |
| 37 | + |
| 38 | + def tube_rack |
| 39 | + asset_group.assets.select{|a| a.facts.where(predicate: 'a', object: 'TubeRack').count > 0}.first |
| 40 | + end |
| 41 | + |
| 42 | + def wells_for(asset) |
| 43 | + asset.facts.where(predicate: 'contains').map(&:object_asset) |
| 44 | + end |
| 45 | + |
| 46 | + def well_at_location(asset, location) |
| 47 | + wells_for(asset).select{|w| w.facts.where(predicate: 'location', object: location).count > 0}.first |
| 48 | + end |
| 49 | + |
| 50 | + def traverse_wells(asset, &block) |
| 51 | + wells_for(asset).each do |w| |
| 52 | + location = w.facts.where(predicate: 'location').first.object |
| 53 | + yield w, location |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + def process |
| 58 | + FactChanges.new.tap do |updates| |
| 59 | + if assets_compatible_with_step_type |
| 60 | + traverse_wells(tube_rack) do |well_from_tube_rack, location| |
| 61 | + well_from_plate = well_at_location(plate, location) |
| 62 | + updates.remove_where(well_from_tube_rack, 'barcode', well_from_tube_rack.barcode) |
| 63 | + updates.add(well_from_tube_rack, 'previousBarcode', well_from_tube_rack.barcode) |
| 64 | + updates.add(well_from_tube_rack, 'appliedBarcodeTo', well_from_plate) |
| 65 | + updates.add(well_from_plate, 'barcode', well_from_tube_rack.barcode) |
| 66 | + end |
| 67 | + |
| 68 | + updates.add(tube_rack, 'mergedInto', plate) |
| 69 | + updates.add(plate, 'mergedFrom', tube_rack) |
| 70 | + end |
| 71 | + end |
| 72 | + end |
| 73 | +end |
| 74 | + |
| 75 | +return unless ARGV.any?{|s| s.match(".json")} |
| 76 | + |
| 77 | +args = ARGV[0] |
| 78 | +asset_group_id = args.match(/(\d*)\.json/)[1] |
| 79 | +asset_group = AssetGroup.find(asset_group_id) |
| 80 | + |
| 81 | +begin |
| 82 | + updates = MoveBarcodesFromTubeRackToPlate.new(asset_group: asset_group).process |
| 83 | + json = updates.to_json |
| 84 | + JSON.parse(json) |
| 85 | + puts json |
| 86 | +rescue InvalidDataParams => e |
| 87 | + puts ({ set_errors: e.errors }.to_json) |
| 88 | +rescue StandardError => e |
| 89 | + puts ({ set_errors: ['Unknown error while parsing file'+e.backtrace.to_s]}.to_json) |
| 90 | +end |
| 91 | + |
| 92 | + |
0 commit comments