Top Level Namespace
Defined Under Namespace
Modules: Cmdtabs
Instance Method Summary collapse
-
#aggregate_column(input_table, col_index, col_agg, sep) ⇒ Object
aggregate_column_data.rb.
- #build_pattern(col_filter, keywords) ⇒ Object
- #create_table(indexed_metrics, samples_tag, attributes, metric_names) ⇒ Object
-
#desaggregate_column(input_table, col_index, sep) ⇒ Object
desaggregate_column_data.rb.
- #expanded_match(string, pattern, match_mode) ⇒ Object
- #extract_columns(table, columns2extract) ⇒ Object
- #filter(line, all_patterns, search_mode, match_mode, reverse = false) ⇒ Object
- #filter_columns(input_table, options) ⇒ Object
- #index_array(array, col_from = 0, col_to = 1) ⇒ Object
-
#index_metrics(input_data, attributes) ⇒ Object
create_metric_table.rb.
-
#link_table(indexed_linker, tabular_file, drop_line, sep) ⇒ Object
table_linker.rb.
-
#load_and_parse_tags(tags, sep) ⇒ Object
tag_table.rb.
-
#load_files(files_path) ⇒ Object
merge_tabular.rb.
- #load_input_data(input_path, sep = "\t", limit = 0) ⇒ Object
-
#load_records(input_file, cols, full) ⇒ Object
intersect_columns.rb.
- #load_several_files(all_files, sep = "\t", limit = 0) ⇒ Object
-
#merge_and_filter_tables(input_files, options) ⇒ Object
column_filter.
- #merge_files(files) ⇒ Object
-
#name_replaces(tabular_input, sep, cols_to_replace, translation_index, remove_untranslated = false) ⇒ Object
standard_name_replacer.rb.
- #parse_column_indices(sep, col_string) ⇒ Object
- #shift_by_array_indexes(arr_sub, indexes) ⇒ Object
- #tag_file(input_file, tags, header) ⇒ Object
- #write_output_data(output_data, output_path = nil, sep = "\t") ⇒ Object
Instance Method Details
#aggregate_column(input_table, col_index, col_agg, sep) ⇒ Object
aggregate_column_data.rb
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/cmdtabs/cmdtabs_lib.rb', line 49 def aggregate_column(input_table, col_index, col_agg, sep) aggregated_data = {} aggregated_data_arr = [] input_table.each do |fields| key = fields[col_index] val = fields[col_agg] query = aggregated_data[key] if query.nil? aggregated_data[key] = [val] else query << val end end aggregated_data.each do |k, value| aggregated_data_arr << [k, value.join(sep)] end return aggregated_data_arr end |
#build_pattern(col_filter, keywords) ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/cmdtabs/cmdtabs_lib.rb', line 247 def build_pattern(col_filter, keywords) pattern = {} if !col_filter.nil? && !keywords.nil? keys_per_col = keywords.split('%') abort('Number of keywords not equal to number of filtering columns') if keys_per_col.length != col_filter.length col_filter.each_with_index do |col, i| pattern[col] = keys_per_col[i].split('&') end end return pattern end |
#create_table(indexed_metrics, samples_tag, attributes, metric_names) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/cmdtabs/cmdtabs_lib.rb', line 109 def create_table(indexed_metrics, samples_tag, attributes, metric_names) allTags = attributes + metric_names table_output = [] corrupted_records = [] indexed_metrics.each do |sample_name, sample_data| record = [sample_name] allTags.each do |tag| record << sample_data[tag] end if record.count(nil) > 0 warn("Record #{sample_name} is corrupted:\n#{record.inspect}") corrupted_records << record else table_output << record end end allTags.unshift(samples_tag) table_output.unshift(allTags) # Add header corrupted_records.unshift(allTags) # Add header return table_output, corrupted_records end |
#desaggregate_column(input_table, col_index, sep) ⇒ Object
desaggregate_column_data.rb
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/cmdtabs/cmdtabs_lib.rb', line 70 def desaggregate_column(input_table, col_index, sep) desaggregated_data = [] input_table.each do |fields| aggregated_fields = fields[col_index] aggregated_fields.split(sep).each do |field| record = fields.clone record[col_index] = field desaggregated_data << record end end return desaggregated_data end |
#expanded_match(string, pattern, match_mode) ⇒ Object
259 260 261 262 263 264 |
# File 'lib/cmdtabs/cmdtabs_lib.rb', line 259 def (string, pattern, match_mode) is_match = false is_match = true if string.include?(pattern) && match_mode == 'i' is_match = true if string == pattern && match_mode == 'c' return is_match end |
#extract_columns(table, columns2extract) ⇒ Object
345 346 347 348 349 350 351 |
# File 'lib/cmdtabs/cmdtabs_lib.rb', line 345 def extract_columns(table, columns2extract) storage = [] table.each do |row| storage << shift_by_array_indexes(row, columns2extract) end return storage end |
#filter(line, all_patterns, search_mode, match_mode, reverse = false) ⇒ Object
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/cmdtabs/cmdtabs_lib.rb', line 266 def filter(line, all_patterns, search_mode, match_mode, reverse = false) filter = false all_patterns.each do |col, patterns| is_match = false patterns.each do |pattern| is_match = (line[col], pattern, match_mode) break if is_match end if is_match && search_mode == 's' filter = false break elsif !is_match && search_mode == 'c' filter = true break elsif !is_match filter = true end end if reverse filter = !filter end return filter end |
#filter_columns(input_table, options) ⇒ Object
290 291 292 293 294 295 296 297 298 299 |
# File 'lib/cmdtabs/cmdtabs_lib.rb', line 290 def filter_columns(input_table, ) pattern = build_pattern([:col_filter], [:keywords]) filtered_table = [] input_table.each do |line| if pattern.nil? || !filter(line, pattern, [:search_mode], [:match_mode], [:reverse]) filtered_table << shift_by_array_indexes(line, [:cols_to_show]) end end return filtered_table end |
#index_array(array, col_from = 0, col_to = 1) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/cmdtabs/cmdtabs_lib.rb', line 26 def index_array(array, col_from=0, col_to=1) indexed_array = {} array.each do |elements| indexed_array[elements[col_from]] = elements[col_to] end return indexed_array end |
#index_metrics(input_data, attributes) ⇒ Object
create_metric_table.rb
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/cmdtabs/cmdtabs_lib.rb', line 87 def index_metrics(input_data, attributes) n_attrib = attributes.length indexed_metrics = {} metric_names = [] input_data.each do |entry| sample_id = entry.shift sample_attributes = entry[0..n_attrib - 1] metric_name, metric = entry[n_attrib..n_attrib + 1] metric_names << metric_name if !metric_names.include?(metric_name) query = indexed_metrics[sample_id] if query.nil? indexed_metrics[sample_id] = {metric_name => metric} attributes.each_with_index do |attrib, i| indexed_metrics[sample_id][attrib] = sample_attributes[i] end else query[metric_name] = metric end end return metric_names, indexed_metrics end |
#link_table(indexed_linker, tabular_file, drop_line, sep) ⇒ Object
table_linker.rb
307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/cmdtabs/cmdtabs_lib.rb', line 307 def link_table(indexed_linker, tabular_file, drop_line, sep) linked_table = [] tabular_file.each do |fields| id = fields.first info_id = indexed_linker[id] if !info_id.nil? linked_table << fields.push(info_id) else linked_table << fields if !drop_line end end return linked_table end |
#load_and_parse_tags(tags, sep) ⇒ Object
tag_table.rb
322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/cmdtabs/cmdtabs_lib.rb', line 322 def (, sep) = [] .each do |tag| if File.exist?(tag) << load_input_data(tag, sep) break else << tag.split(sep) end end return .flatten end |
#load_files(files_path) ⇒ Object
merge_tabular.rb
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/cmdtabs/cmdtabs_lib.rb', line 146 def load_files(files_path) files = {} files_path.each do |file_name| file = [] input_table = load_input_data(file_name) input_table.each do |fields| fields.map{|field| if field == "" '-' else field end } next if fields.count('-') == fields.length #skip blank records file << fields end files[file_name] = file end return files end |
#load_input_data(input_path, sep = "\t", limit = 0) ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'lib/cmdtabs/cmdtabs_lib.rb', line 4 def load_input_data(input_path, sep="\t", limit=0) if input_path == '-' input_data = STDIN else input_data = File.readlines(input_path) end input_data_arr = input_data.map {|line| line = line.chomp.split(sep, limit)} return input_data_arr end |
#load_records(input_file, cols, full) ⇒ Object
intersect_columns.rb
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/cmdtabs/cmdtabs_lib.rb', line 133 def load_records(input_file, cols, full) records = {} full_row_of_records = {} input_file.each do |fields| field = cols.map { |c| fields[c]} records[field] = true full_row_of_records[field] = fields if full end return records.keys, full_row_of_records end |
#load_several_files(all_files, sep = "\t", limit = 0) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/cmdtabs/cmdtabs_lib.rb', line 14 def load_several_files(all_files, sep = "\t", limit=0) loaded_files = {} all_files.each do |file| if FileTest.directory?(file) STDERR.puts "#{file} is not a valid file" next end loaded_files[file] = load_input_data(file, sep, limit) end return loaded_files end |
#merge_and_filter_tables(input_files, options) ⇒ Object
column_filter
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/cmdtabs/cmdtabs_lib.rb', line 220 def merge_and_filter_tables(input_files, ) header = [] filtered_table = [] [:cols_to_show] = (0..input_files.first[0].length - 1).to_a if [:cols_to_show].nil? input_files.each do |filename, file| if [:header] if header.empty? header = file.shift else file.shift end end filtered_table.concat(filter_columns(file, )) end filtered_table.uniq! if [:uniq] if !header.empty? header = shift_by_array_indexes(header, [:cols_to_show]) filtered_table.unshift(header) end return filtered_table end |
#merge_files(files) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/cmdtabs/cmdtabs_lib.rb', line 168 def merge_files(files) parent_table = {} table_length = 0 files.each do |file_names, file| local_length = 0 file.each do |fields| id = fields.shift local_length = fields.length if !parent_table.has_key?(id) parent_table[id] = Array.new(table_length,'-') elsif parent_table[id].length < table_length parent_table[id].concat(Array.new(table_length-parent_table[id].length,'-')) end parent_table[id].concat(fields) end table_length += local_length end parent_table_arr = [] parent_table.each do |id, fields| diference = table_length - fields.length fields.concat(Array.new(diference,'-')) if diference > 0 fields.unshift(id) parent_table_arr << fields end return parent_table_arr end |
#name_replaces(tabular_input, sep, cols_to_replace, translation_index, remove_untranslated = false) ⇒ Object
standard_name_replacer.rb
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/cmdtabs/cmdtabs_lib.rb', line 199 def name_replaces(tabular_input, sep, cols_to_replace, translation_index, remove_untranslated=false) translated_fields = [] untranslated_fields = [] tabular_input.each do |fields| cols_to_replace.each do |col| replaced_field = translation_index[fields[col]] if !replaced_field.nil? fields[col] = replaced_field translated_fields << fields elsif !remove_untranslated translated_fields << fields else untranslated_fields << fields end end end return translated_fields, untranslated_fields end |
#parse_column_indices(sep, col_string) ⇒ Object
242 243 244 245 |
# File 'lib/cmdtabs/cmdtabs_lib.rb', line 242 def parse_column_indices(sep, col_string) cols = col_string.split(sep).map{|col| col.to_i- 1} return cols end |
#shift_by_array_indexes(arr_sub, indexes) ⇒ Object
301 302 303 304 |
# File 'lib/cmdtabs/cmdtabs_lib.rb', line 301 def shift_by_array_indexes(arr_sub, indexes) subsetted_arr = indexes.map{ |idx| arr_sub[idx]} return subsetted_arr end |
#tag_file(input_file, tags, header) ⇒ Object
335 336 337 338 339 340 341 342 |
# File 'lib/cmdtabs/cmdtabs_lib.rb', line 335 def tag_file(input_file, , header) taged_file = [] input_file.each_with_index do |fields, n_row| next if n_row == 0 && header taged_file << .dup.concat(fields) end return taged_file end |
#write_output_data(output_data, output_path = nil, sep = "\t") ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/cmdtabs/cmdtabs_lib.rb', line 34 def write_output_data(output_data, output_path=nil, sep="\t") if !output_path.nil? File.open(output_path, 'w') do |out_file| output_data.each do |line| out_file.puts(line.join(sep)) end end else output_data.each do |line| STDOUT.puts line.join(sep) end end end |