Module: Iroki::CoreExt::File
- Defined in:
- lib/iroki/core_ext/file/file.rb
Instance Method Summary collapse
- #check_file(arg, which) ⇒ Object
-
#parse_color_map_iroki(fname, iroki_to_name, exact_matching: true, auto_color: false) ⇒ Object
TODO what’s the point of the iroki_to_name? To allow wonky chars maybe?.
- #parse_name_map(fname) ⇒ Object
- #valid_newick?(fname) ⇒ Boolean
Instance Method Details
#check_file(arg, which) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/iroki/core_ext/file/file.rb', line 51 def check_file arg, which help = " Try iroki --help for help." abort_if arg.nil?, "You must provide a #{which} file.#{help}" abort_unless Object::File.exists?(arg), "The file '#{arg}' doesn't exist.#{help}" arg end |
#parse_color_map_iroki(fname, iroki_to_name, exact_matching: true, auto_color: false) ⇒ Object
TODO what’s the point of the iroki_to_name? To allow wonky chars maybe?
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 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 174 |
# File 'lib/iroki/core_ext/file/file.rb', line 65 def parse_color_map_iroki(fname, iroki_to_name, exact_matching: true, auto_color: false) check_file fname, :color_map name_to_iroki = iroki_to_name.invert patterns = {} Object::File.open(fname, "rt").each_line do |line| unless line.start_with? "#" label_tag = "" branch_tag = "" pattern, label_color, branch_color = line.chomp.split "\t" pattern.strip! if pattern label_color.strip! if label_color branch_color.strip! if branch_color # color = "black" if color.nil? || color.empty? assert pattern, "found no pattern" if exact_matching # TODO should this really be everytime? if name_to_iroki.has_key? pattern pattern = name_to_iroki[pattern] else AbortIf::logger.info "String '#{pattern}' has no " + "match in " + "name_to_iroki map" end else # TODO flag bad regexp pattern = Regexp.new pattern end if color_given?(label_color) && color_given?(branch_color) if label_tag?(label_color) && label_tag?(branch_color) abort_unless label_color == branch_color, "Label tags specified twice for " + "#{line.inspect}, but the tags don't " + "match." # ie both are label tags specifying the same color branch_color = nil elsif branch_tag?(label_color) && branch_tag?(branch_color) abort_unless label_color == branch_color, "Branch tags specified twice for " + "#{line.inspect}, but the tags don't " + "match." # ie both are branch tags specifying the same color label_color = nil end end if color_given?(label_color) && !color_given?(branch_color) if (color = label_tag? label_color) label_tag = Iroki::Color.get_tag color, auto_color elsif (color = branch_tag? label_color) branch_tag = Iroki::Color.get_tag color, auto_color elsif line.match(/\t\Z/) # empty branch color, branch # will be black label_tag = Iroki::Color.get_tag label_color, auto_color else label_tag = Iroki::Color.get_tag label_color, auto_color branch_tag = Iroki::Color.get_tag label_color, auto_color end else if color_given? label_color if (color = label_tag? label_color) label_tag = Iroki::Color.get_tag color, auto_color elsif (color = branch_tag? label_color) branch_tag = Iroki::Color.get_tag color, auto_color else label_tag = Iroki::Color.get_tag label_color, auto_color end end if color_given? branch_color if (color = branch_tag? branch_color) branch_tag = Iroki::Color.get_tag color, auto_color elsif (color = label_tag? branch_color) label_tag = Iroki::Color.get_tag color, auto_color else branch_tag = Iroki::Color.get_tag branch_color, auto_color end end end patterns[pattern] = { label: label_tag, branch: branch_tag } end end patterns end |
#parse_name_map(fname) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/iroki/core_ext/file/file.rb', line 177 def parse_name_map fname check_file fname, :name_map name_map = {} Object::File.open(fname, "rt").each_line do |line| unless line.start_with? "#" abort_if line.chomp.split("\t").count > 2, "Line (#{line.inspect}) has more than two columns" oldname, newname = line.chomp.split "\t" oldname.strip! if oldname newname.strip! if newname abort_if oldname.nil? || oldname.empty?, "Column 1 missing for line: #{line.inspect}" abort_if newname.nil? || newname.empty?, "Column 2 missing for line: #{line.inspect}" abort_if name_map.has_key?(oldname), "#{oldname} is repeated in column 1" name_map[oldname] = newname end end abort_if duplicate_values?(name_map), "Names in column 2 of name map file must be unique" name_map end |
#valid_newick?(fname) ⇒ Boolean
Note:
Only checks that first and last chars off the file (minus trailing newlines) are correct. I.e., first char is ‘(’ and last char is ‘;’
43 44 45 46 47 48 49 |
# File 'lib/iroki/core_ext/file/file.rb', line 43 def valid_newick? fname str = Object::File.read(fname).chomp first_char = str[0] last_char = str[-1] first_char == "(" && last_char == ";" end |