=begin editStringAdder.rb * contains a working edit string adder implementation (product operator) * is ruby, hence can be treated as pseudocode * eventual deployment may be javascript, unsure * is a piece of a cheap as free HTML visualizer none the less * induced from examples in Robert Edgar, MUSCLE, BMC Bioinformatics =end =begin Example inputs... U = [6, -1, 4] V = [7, -1, 3, -2, 1] P = U Q = [5, -1, 5, -2, 1] X = [-1, 5, -3, 4] Y = [-6, 2, -2, 6, -5, 5] D = [10] E = [1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, 1, 1] =end =begin w edit_product u, v returns the edit string product for u * v where u, v are lists of integers this operation is equal to the application of edit string u followed by v example... edit_product [6, -1, 4], [7, -1, 3, -2, 1] -> [6, -1, -1, 3, -2, 1] requires edit_cleanup u =end def estring_product(u, v) w = [] uu_replacement = nil ui = 0 vi = 0 v.each do |vv| if vv < 0 w << vv elsif vv > 0 vv_left = vv uu = uu_replacement != nil ? uu_replacement : u[ui] uu_replacement = nil until vv_left == 0 if vv_left >= uu.abs w << uu vv_left -= uu.abs ui += 1 uu = u[ui] else if uu > 0 w << vv_left uu -= vv_left elsif uu < 0 w << -vv_left uu += vv_left end vv_left = 0 uu_replacement = uu end end end vi += 1 end return estring_collapse(w) end =begin v edit_cleanup u returns a cleaned up edit string for u where u is a list of integers-- consecutive positive or negative numbers are summed into one element example... [6, -1, -1, 3, -2, 1] -> [6, -2, 3, -2, 1] required by edit_product u, v =end def estring_collapse(u) v = [] u.each do |uu| if v[-1] == nil v << uu elsif (v[-1] <=> 0) == (uu <=> 0) v[-1] += uu else v << uu end end return v end =begin Example inputs again... puts (edit_product U, V).inspect puts (edit_product P, Q).inspect puts (edit_product X, Y).inspect puts (edit_product D, E).inspect =end