From 82390df92c208bf28474845a4959a34fd1488635 Mon Sep 17 00:00:00 2001 From: Benjamin Kiah Stroud <32469930+bkiahstroud@users.noreply.github.com> Date: Fri, 4 Oct 2024 10:42:08 -0700 Subject: [PATCH] script to map IAIDs by CSV Ref: - https://github.com/scientist-softserv/nnp/issues/320 --- bin/map_iaids_by_csv.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100755 bin/map_iaids_by_csv.rb diff --git a/bin/map_iaids_by_csv.rb b/bin/map_iaids_by_csv.rb new file mode 100755 index 0000000..50c8543 --- /dev/null +++ b/bin/map_iaids_by_csv.rb @@ -0,0 +1,33 @@ +#!/usr/bin/env ruby + +require 'csv' +require 'json' +require 'fileutils' +require 'pathname' + +input_csv_path = Pathname.new(ARGV[0]) +raise "No file found at #{input_csv_path}" unless input_csv_path.exist? + +csv_raw = CSV.read(input_csv_path) +iaid_index = csv_raw[0].index('iaid') +iaids = csv_raw.map { |row| row[iaid_index].split('|') } +iaids.flatten! +iaids.uniq! +iaids -= ['iaid', ' '] # remove column header and blank values +iaids.sort! + +puts "Found #{iaids.size} unique IAIDs" + +h = {} +iaids.each { |iaid| h[iaid] = {} } + +filename, _ext = input_csv_path.basename.to_s.split('.') +json_filename = "#{filename}IAIDs.json" +json_path = Pathname.new("tmp/#{json_filename}") +FileUtils.mkdir_p(json_path.dirname) + +File.open(json_path, 'w') do |file| + file.puts JSON.pretty_generate(JSON.parse(h.to_json)) +end + +puts "Output: #{json_path}"