Module: Iroki::CoreExt::File

Defined in:
lib/iroki/core_ext/file/file.rb

Instance Method Summary collapse

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(fname, exact_matching: true, auto_color: false) ⇒ Object

TODO this is now pointless



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
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
# File 'lib/iroki/core_ext/file/file.rb', line 64

def parse_color_map fname,
                    exact_matching: true,
                    auto_color: false

  check_file fname, :color_map


  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"

      # color = "black" if color.nil? || color.empty?

      assert pattern, "found no pattern"

      if exact_matching # TODO should this really be everytime?
        pattern = pattern#.clean_name
      else
        # TODO flag bad regexp
        pattern = Regexp.new pattern
      end

      if color_given?(label_color) && color_given?(branch_color)
        abort_if(has_label_tag?(label_color) &&
                 has_label_tag?(branch_color),
                 "Label tag specified twice for '#{line}'")

        abort_if(has_branch_tag?(label_color) &&
                 has_branch_tag?(branch_color),
                 "Branch tag specified twice for '#{line}'")
      end

      if color_given?(label_color) && !color_given?(branch_color)
        if (color = has_label_tag? label_color)
          label_tag = Iroki::Color.get_tag color, auto_color
        elsif (color = has_branch_tag? label_color)
          branch_tag = Iroki::Color.get_tag 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 = has_label_tag? label_color)
            label_tag = Iroki::Color.get_tag color, auto_color
          elsif (color = has_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 = has_branch_tag? branch_color)
            branch_tag = Iroki::Color.get_tag color, auto_color
          elsif (color = has_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

      # if auto_color
      #   patterns[pattern] = "[&!color=\"#{auto_colors[color]}\"]"
      # else
      #   patterns[pattern] = Iroki::Color.get_tag color, auto_color
      # end

      patterns[pattern] = { label: label_tag, branch: branch_tag }
    end
  end

  patterns
end

#parse_color_map_iroki(fname, iroki_to_name, exact_matching: true, auto_color: false) ⇒ Object



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
175
176
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/iroki/core_ext/file/file.rb', line 144

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?
      # pattern = pattern.clean_name
        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)
        abort_if(has_label_tag?(label_color) &&
                 has_label_tag?(branch_color),
                 "Label tag specified twice for '#{line}'")

        abort_if(has_branch_tag?(label_color) &&
                 has_branch_tag?(branch_color),
                 "Branch tag specified twice for '#{line}'")
      end

      if color_given?(label_color) && !color_given?(branch_color)
        if (color = has_label_tag? label_color)
          label_tag = Iroki::Color.get_tag color, auto_color
        elsif (color = has_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 = has_label_tag? label_color)
            label_tag = Iroki::Color.get_tag color, auto_color
          elsif (color = has_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 = has_branch_tag? branch_color)
            branch_tag = Iroki::Color.get_tag color, auto_color
          elsif (color = has_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

      # if auto_color
      #   patterns[pattern] = "[&!color=\"#{auto_colors[color]}\"]"
      # else
      #   patterns[pattern] = Iroki::Color.get_tag color, auto_color
      # end

      patterns[pattern] = { label: label_tag, branch: branch_tag }
    end
  end

  patterns
end

#parse_name_map(fname) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/iroki/core_ext/file/file.rb', line 241

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}"

      # oldname = oldname.clean_name
      # newname = newname.clean_name

      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 ‘;’

Returns:

  • (Boolean)


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