Skip to content

Instantly share code, notes, and snippets.

@wbwakeman
Created March 11, 2022 19:36
Show Gist options
  • Save wbwakeman/c832140412edb746c769906e2d8d762a to your computer and use it in GitHub Desktop.
Save wbwakeman/c832140412edb746c769906e2d8d762a to your computer and use it in GitHub Desktop.
This is an update of Nile's 2019 datafix script that uploads Neuropixels ccf infomation from ccf_regions.csv files to the LIMS2 ecephys_channels table
class Pbs1874Datafix < AIBS::Datafix
# https://github.com/AllenInstitute/AllenSDK/issues/992
# http://stash.corp.alleninstitute.org/projects/TECH/repos/lims/browse/db/datafixes/20190911134728_pbs1874_datafix.rb?at=ac873b70756defe209124330f8723f1e99de03c3
#create_ldds_row # this mandatory statement belongs at the beginning of the script since part of the purpose of this class is to determine if your datafix script has already been run. (see http://confluence.corp.alleninstitute.org/display/IT/datafix).
structures = {}
acro_id = {}
isocortical = []
layer = Set.new
layer_re = /.*[Ll]ayer.*\d+.*/
StructureGraph.find(1).graph_as_list().each do |st|
stid = st["id"].to_i
if st["path"].include?(",315,")
isocortical << stid
if !layer_re.match(st["name"]).nil?
layer.add(stid)
end
end
structures[stid] = st
acro_id[st["acronym"]] = stid
end
unlayer_map = {}
isocortical.each do |stid|
if layer.include?(stid)
structure = structures.fetch(stid)
path = structure["path"]
.slice(1..structure["path"].length-1)
.split(",")
.reverse
.map {|x| x.to_i}
path.each do |ancestor_id|
if !layer.include?(ancestor_id)
unlayer_map[stid] = ancestor_id
break
end
end
end
end
parent_dir = "/allen/programs/braintv/workgroups/neuralcoding/corbettb/VBN_production"
dir_glob = File.join(parent_dir, "**/ccf_regions.csv")
dir_re = /#{parent_dir}\/(?<session_id>\d+)_\d+_\d+_(?<probe_name>probe[a-z|A-Z])_sorted.*/
Dir.glob(dir_glob).each do |path|
match = dir_re.match(path)
if match.nil?
raise "wat"
end
session_id = match.named_captures["session_id"].to_i
probe_name = match.named_captures["probe_name"]
probes = EcephysProbe.where(ecephys_session_id: session_id, name: probe_name)
if probes.length != 1
puts "unable to find #{probe_name} for session #{session_id}"
next
end
probe = probes.first
table = CSV.table(path)
table.each do |row|
if !row.is_a?(Hash)
row = row.to_hash
end
#local_index = row.fetch(nil).to_i
local_index = row.fetch(:channels).to_i
channel = EcephysChannel.where(local_index: local_index, ecephys_probe_id: probe.id).first
if channel.nil?
puts "no channel at local index #{local_index} for probe #{probe.id} (#{probe.name}) on session #{probe.ecephys_session_id}"
next
end
structure_acronym = row.fetch(:structure_acronym)
# if !/.*6$/.match(structure_acronym).nil?
# structure_acronym = structure_acronym + "a"
# end
if structure_acronym != 'none'
structure_id = acro_id.fetch(structure_acronym)
structure_id = unlayer_map.fetch(structure_id, structure_id)
channel.manual_structure_id = structure_id
else
channel.manual_structure_id = nil
end
ap = row.fetch(:ap).to_f * 1000
dv = row.fetch(:dv).to_f * 1000
lr = row.fetch(:ml).to_f * 1000
depth = row.fetch(:cortical_depth).to_f
channel.anterior_posterior_ccf_coordinate = ap
channel.dorsal_ventral_ccf_coordinate = dv
channel.left_right_ccf_coordinate = lr
channel.manual_cortical_depth = depth
channel.save!
end
end
#end # end ActiveRecord::Base.transaction
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment