Guest User

Untitled

a guest
Aug 14th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. # IT Universitet i København - Algorithm Design Course (2010)
  2. # ex5: Sequence Alignment
  3. # Passes al the tests
  4. # author: Alex Usbergo (aleu@itu.dk)
  5.  
  6. def fetch_data(matrix_filename, input_filename)
  7. f = File.open(matrix_filename)
  8.  
  9. #reading the BLOSUM matrix
  10. blosum = Hash.new
  11. columns = f.readline.split[0..-1].map{|c| c.to_sym}
  12.  
  13. while (!f.eof?) do
  14. values = f.readline.split[0..-1]
  15. row = values.shift.to_sym
  16. columns.each{|c| blosum[c] = {} if blosum[c].nil?; blosum[c][row] = values.shift.to_i}
  17. end
  18.  
  19. f = File.open(input_filename)
  20.  
  21. #reading the input file
  22. input = []
  23. while (!f.eof?) do
  24. input << {:name => f.readline.split[0][1..-1].to_sym,
  25. :data => ("*" + f.readline).split(//)[0..-2].map{|c| c.to_sym}}
  26. end
  27.  
  28. return [blosum, input]
  29. end
  30.  
  31. def seqalign(x, y, m, blosum)
  32. x.size.times{|i| m[i] = [-4*(i)]}
  33. y.size.times{|j| m[0][j] = -4*(j)}
  34.  
  35. for i in 1..x.size-1 do
  36. for j in 1..y.size-1 do
  37. m[i][j] = [blosum[x[i]][y[j]] + m[i-1][j-1], -4 + m[i][j-1], -4 + m[i-1][j]].max
  38. end
  39. end
  40. m[x.size-1][y.size-1]
  41. end
  42.  
  43. def seqprint(x, y, matrix, blosum)
  44. aligned_x = []
  45. aligned_y = []
  46.  
  47. i, j = x.size-1, y.size-1
  48.  
  49. while !i.zero? || !j.zero?
  50.  
  51. #match
  52. if matrix[i][j] == matrix[i-1][j-1] + blosum[x[i]][y[j]]
  53. aligned_x << x[i] # =>
  54. aligned_y << y[j]
  55. j -= 1
  56. i -= 1
  57.  
  58. #gaps
  59. elsif matrix[i][j] == -4 + matrix[i][j-1]
  60. aligned_x << :- # =>
  61. aligned_y << y[j]
  62. j -= 1
  63.  
  64. elsif matrix[i][j] == -4 + matrix[i-1][j]
  65. aligned_x << x[i] # =>
  66. aligned_y << :-
  67. i -= 1
  68. end
  69. end
  70.  
  71. x_s = "" ; y_s = ""
  72. aligned_x.reverse_each{|c| x_s += c.to_s}
  73. aligned_y.reverse_each{|c| y_s += c.to_s}
  74.  
  75. puts "#{x_s}\n#{y_s}\n\n"
  76. end
  77.  
  78. #blosum, input = fetch_data("data/BLOSUM62.txt", "data/Toy_FASTAs.in")
  79. blosum, input = fetch_data("data/BLOSUM62.txt","data/HbB_FASTAs.in")
  80.  
  81.  
  82. for i in 0..input.size-1 do
  83. for j in i+1..input.size-1 do
  84. matrix = []
  85. value = seqalign(input[i][:data], input[j][:data], matrix, blosum)
  86. puts "#{input[i][:name]}--#{input[j][:name]}: #{value}"
  87. seqprint(input[i][:data], input[j][:data], matrix, blosum)
  88. end
  89. end
Add Comment
Please, Sign In to add comment