Skip to content

Instantly share code, notes, and snippets.

@hirokai
Last active August 29, 2015 14:15
Show Gist options
  • Save hirokai/f414de2b3ea043f14472 to your computer and use it in GitHub Desktop.
Save hirokai/f414de2b3ea043f14472 to your computer and use it in GitHub Desktop.
Make a json of list of images of MicroManager
#!/usr/bin/env ruby
#
require 'rubygems'
require 'json'
require 'find'
require 'csv'
def parent(f)
f.split(File::SEPARATOR)[0...-1].join(File::SEPARATOR)
end
# A folder with display_and_comments.txt is a unit of acquisition.
# It may have multiple metadata.txt
def search(f)
num_pos = 0
chs = []
frames = []
slices = []
Find.find(f){|mf|
next if not mf =~ /metadata\.txt$/
num_pos += 1
obj = JSON.parse(IO.read(mf))
s = obj["Summary"]
if s["ChNames"] == ["Default"]
chs << [obj[obj.keys[1]]["Channel"]]
else
chs << s["ChNames"]
end
slices << s["Slices"]
frames << Dir.glob(parent(mf)+'/*.tif').length
}
chs = chs.flatten.uniq
{:folder => f.split(File::SEPARATOR).last, :ch => chs, :pos => num_pos, :slice => slices.max, :frames => frames.max}
end
top_folders = Dir.glob('../../*/')
rr = []
top_folders.map{|folder|
r = []
Find.find(folder){|f|
next if not f =~ /display_and_comments\.txt$/
puts parent(f)
r << search(parent(f))
}
rr << {:folder => folder.split(File::SEPARATOR).last, :sets =>r}
}
def write_csv(obj)
CSV.open("list.csv", "wb") do |csv|
csv << ["Flowcell", "Acquisition", "Positions", "Channels", "Frames", "Slices"]
for fc in obj
csv << [fc[:folder]]
for s in fc[:sets]
csv << ["",s[:folder],s[:pos],s[:ch].join(", "), s[:frames], s[:slice]]
end
end
end
end
write_csv(rr)
IO.write("list.json",JSON.pretty_generate(rr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment