module BioTCM::Table::Extensions::String
{Table}'s extention to String
Public Instance Methods
to_table(seperator: "\t")
click to toggle source
Create a {BioTCM::Table} based on a String
or fill the given table @param seperator [String]
# File lib/biotcm/table.rb, line 449 def to_table(seperator: "\t") stuff = split(/\r\n|\n/) # Comments comments = [] while stuff[0] =~ /\# / # Some tables' head lines start with a '#', such as *mim2gene.txt* in OMIM break if stuff[0] =~ /\# [\w ]+\t/ comments << stuff.shift.gsub(/^\# /, '') end # Headline col_keys = stuff.shift.split(seperator) raise ArgumentError, 'Duplicated column names' unless col_keys.uniq!.nil? primary_key = stuff.first && stuff.first.split(seperator, -1).size == col_keys.size + 1 ? nil : col_keys.shift col_keys = col_keys.map.with_index { |n, i| [n, i] }.to_h # Table content row_keys = {} content = [] stuff.each_with_index do |line, line_index| col = line.split(seperator, -1) if col.size != col_keys.size + 1 raise ArgumentError, "Row size inconsistent in line #{line_index + 2}" elsif row_keys[col[0]] raise ArgumentError, "Duplicated primary key: #{col[0]}" end row_keys[col.shift] = row_keys.size content << col end # Build a table to return BioTCM::Table.build( primary_key: primary_key, row_keys: row_keys, col_keys: col_keys, content: content, comments: comments ) end