Class: PubliSci::Readers::RMatrix

Inherits:
Object
  • Object
show all
Includes:
Dataset::DataCube
Defined in:
lib/publisci/readers/r_matrix.rb

Instance Method Summary collapse

Methods included from Dataset::DataCube

#abbreviate_known, #code_lists, #component_gen, #component_specifications, #concept_codes, #data_structure_definition, #dataset, #defaults, #dimension_properties, #encode_data, #generate, #generate_resources, #measure_properties, #observations, #prefixes, #vocabulary

Methods included from PubliSci::RDFParser

#add_node, #bnode_value, #encode_value, #get_ary, #get_hashes, #is_complex?, #is_uri?, #load_string, #observation_hash, #sanitize, #sanitize_hash, #strip_prefixes, #strip_uri, #to_literal, #to_resource, #turtle_indent

Instance Method Details

#codes(client, var, options = {}) ⇒ Object



100
101
102
# File 'lib/publisci/readers/r_matrix.rb', line 100

def codes(client, var, options={})
  []
end

#dimensions(client, var, options = {}) ⇒ Object



95
96
97
98
# File 'lib/publisci/readers/r_matrix.rb', line 95

def dimensions(client, var, options={})
  # dimension_properties([""],var)
  []
end

#generate_n3(client, var, outfile_base, options = {}) ⇒ Object



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
# File 'lib/publisci/readers/r_matrix.rb', line 12

def generate_n3(client, var, outfile_base, options={})
  meas = measures(client,var,options)
  dim = dimensions(client,var,options)
  codes = codes(client,var,options)

  outvar = sanitize([var]).first

  probes_per_file = options[:probes_per_file] || 100
  col_select = "colnames"
  col_select = "names" if options[:type] == :dataframe

  #write structure
  open(outfile_base+'_structure.ttl','w'){|f| f.write structure(client,var,outvar,options)}

  probes=client.eval("#{col_select}(#{var})").to_ruby
  if probes == nil
    client.eval("colnames(#{var})=1:ncol(#{var})")
    probes=client.eval("#{col_select}(#{var})").to_ruby
  end
  markers = rows(client,var,options)

  if options[:print]
    puts prefixes(var,options)
  end

  if options[:output] == :string
    str = prefixes(var,options)
  end

  probes.each_with_index{|probe,i|
    #write prefixes and erase old file on first run
    unless options[:print] || options[:output] == :string
      open(outfile_base+"_#{i/probes_per_file}.ttl",'w'){|f| f.write prefixes(var,options)} if i % probes_per_file == 0
    end
    i+=1
    obs_data = observation_data(client,var,i,markers,options)
    labels = labels_for(client,var,probe)

    # labels = sanitize(labels)
    # return obs_data
    if options[:print]
      observations(meas,dim,codes,obs_data,labels,outvar,options).each{|obs| puts obs}
    end

    if options[:output] == :string
      observations(meas,dim,codes,obs_data,labels,outvar,options).each{|obs| str << obs}
    end

    unless options[:print] || options[:output] == :string
      open(outfile_base+"_#{i/probes_per_file}.ttl",'a'){|f| observations(meas,dim,codes,obs_data,labels,outvar,options).map{|obs| f.write obs}}
      puts "#{i}/#{probes.size}" unless options[:quiet]
    end
  }

  if options[:output] == :string
    str
  end
end

#labels_for(connection, var, probe_id, options = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/publisci/readers/r_matrix.rb', line 104

def labels_for(connection,var,probe_id,options={})
  row_names = connection.eval("row.names(#{var})")
  # row_names = ([email protected]_ruby.size).to_a unless row_names.first
  if row_names == connection.eval('NULL')
    row_names = (1..connection.eval("nrow(#{var})").payload.first).to_a
  else
    row_names = row_names.payload
  end

  labels = (1..(row_names.size)).to_a.map(&:to_s)
  labels = labels.map{|l|
    l.insert(0,probe_id.to_s + "_")
  }

  labels
end

#measures(client, var, options = {}) ⇒ Object

for now just make everything a measure



86
87
88
89
90
91
92
93
# File 'lib/publisci/readers/r_matrix.rb', line 86

def measures(client, var, options={})
  if options[:measures]
    options[:measures]
  else
    ["probe","marker","value"]
  end
  # measure_properties(measures,var,options)
end

#observation_data(client, var, probe_number, row_names, options = {}) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/publisci/readers/r_matrix.rb', line 132

def observation_data(client, var, probe_number, row_names, options={})

  data = {}
  # geno_chr = client.eval("#{var}$geno$'#{chr}'")
  # n_individuals = client.eval("#{var}$pheno[[1]]").to_ruby.size
  # entries_per_individual = @rexp.payload["geno"].payload[row_individ].payload["map"].payload.size * @rexp.payload["geno"].payload.names.size
  col_label = "probe"
  row_label = "marker"
  val_label = "value"

  if options[:measures]
    col_label = options[:measures][0] || "probe"
    row_label = options[:measures][1] || "marker"
    val_label = options[:measures][2] || "value"
  end

  data["#{col_label}"] = []
  data["#{row_label}"] = []
  data["#{val_label}"] = []

  # n_individuals.times{|row_individ|
  # puts "#{row_individ}/#{n_individuals}"

  col_select = "colnames"
  col_select = "names" if options[:type] == :dataframe

  if options[:type] == :dataframe
    probe_obj = client.eval("#{var}[[#{probe_number}]]").to_ruby
  else
    probe_obj = client.eval("#{var}[,#{probe_number}]").to_ruby
  end
  # puts probe_obj
  probe_id = client.eval("#{col_select}(#{var})[[#{probe_number}]]").to_ruby
  data["#{col_label}"] = (1..(probe_obj.size)).to_a.fill(probe_id)
  probe_obj.each_with_index{|lod,i|
    data["#{row_label}"] << row_names[i]
    data["#{val_label}"] << lod
  }

  data.map{|k,v| v.flatten!}
  data
end

#rows(connection, var, options = {}) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/publisci/readers/r_matrix.rb', line 121

def rows(connection,var,options={})
  row_names = connection.eval("row.names(#{var})")
  #hacky solution because rserve client's .to_ruby method doesn't fully work
  if row_names == connection.eval('NULL')
    row_names = (1..connection.eval("nrow(#{var})").payload.first).to_a
  else
    row_names = row_names.payload
  end
  row_names
end

#structure(client, var, outvar, options = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/publisci/readers/r_matrix.rb', line 71

def structure(client,var,outvar,options={})
  meas = measures(client,var,options)
  dim = dimensions(client,var,options)
  codes = codes(client,var,options)

  str = prefixes(var, options)
  str << data_structure_definition(meas,[],codes,outvar,options)
  str << dataset(outvar,options)
  component_specifications(meas, dim, codes, var, options).map{ |c| str << c }
  measure_properties(meas,var,options).map{|m| str << m}

  str
end