-
Notifications
You must be signed in to change notification settings - Fork 0
/
klassextant_export.rb
99 lines (89 loc) · 2.5 KB
/
klassextant_export.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
module KlassextantExport
require 'csv'
require 'json'
def export_class_parts( format='csv' )
case format
when 'csv'
CSV.open('class_parts.csv', 'wb') do |csv|
fb.analytes.each do |an|
csv << [ [an[:prefix], *an[:class_parts], 'end'][0..5].join('-') ]
end
end
else
raise ArgumentError, 'Unsupported export type', format
end
end
def export_stem_counts( format='csv' )
case format
when 'csv'
CSV.open("stem_counts.csv", 'wb') do |csv|
csv << ['stem', 'raw_positions', 'clusters', 'total_use', 'avg_pos', 'median']
@stem_counts.each do |k,v|
csv << [k, v[:raw], v[:clusters], v[:total_use], v[:avg_pos], v[:median]]
end
end
when 'json'
File.open("stem_counts.json", "w") do |f|
f.write( JSON.generate(@stem_counts) )
end
else
raise ArgumentError, 'Unsupported export type', format
end
end
def export_stem_trees( format='json', kind=:rich )
flare_tree = nest_by_alpha(
(kind == :rich) ? @stem_tree.reject{|k,v| v.size == 1 } : @stem_tree
)
file_name = ['stem_tree',kind.to_s].join('_')
case format
when 'json'
File.open("#{file_name}.ison", "w") do |f|
f.write( JSON.generate(flare_tree) )
end
else
raise ArgumentError, 'Unsupported export type', format
end
end
def export_stem_flats( format='json' )
@tree = {'name' => 'flare','children' => []}
st.
select {|k,v| v.size == 1 }.
group_by {|k,v| k[0] }.
reduce({}) {|a,k| a[k.first] = k.last.map(&:first); a }.
reduce(@tree) {|tree,group|
tree['children'] << {
'name' => group.first,
'children' => group.last.map{|stem|
{'name'=>stem, 'value'=>1}
}
}
}
case format
when 'json'
File.open("stem_flats.ison", "w") do |f|
f.write( JSON.generate(@tree) )
end
else
raise ArgumentError, 'Unsupported export type', format
end
end
# only nests 3 levels deep, as per the data
# group_by param is the 'root' level
def nest_by_alpha( tree )
flare_tree = {'name':'flare','children':[]}
tree.
group_by{|k,v| k[0] }.
each do |k,v| flare_tree['children'] << {
'name' => k,
'children' => v.map {|k2,v2| {
'name' => k2,
'children' => v2.map {|k3,v3| {
'name' => k3,
'value' => '1'}
}
}
}
}
end
end
end