Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.44 KB | None | 0 0
  1. # Computing minimum alignments and minimum scores using a brute-force iterative approach.
  2.  
  3. # min_alignment_brute_force(seq0,seq1)
  4. #
  5. # param seq0, seq1: sequences.
  6. # result: a minimum alignment.
  7. # effects: none.
  8. #
  9. # Computes the minimum alignment by comparing all extension sequences of length seq0.length + seq1.length
  10. # with each other.
  11. #
  12. def min_alignment_brute_force(seq0, seq1)
  13.   # Initialize.
  14.   min_score = seq0.length + seq1.length + 1  # min score
  15.   eseq0 = Ext_Sequence.new(seq1.length, seq0)  # first extension sequence to test
  16.  
  17.   while eseq0.length == seq0.length + seq1.length
  18.     # Initialize.
  19.     eseq1 = Ext_Sequence.new(seq0.length, seq1)  # rist extension sequence to compare with
  20.  
  21.     while eseq1.length == seq0.length + seq1.length
  22.       # Check if new alignment is better and if so update min score and min alignment.
  23.       if eseq0.score(eseq1) < min_score
  24.         min_score = eseq0.score(eseq1)
  25.         min_align = [eseq0, eseq1]
  26.       end
  27.  
  28.       eseq1 = eseq1.next()
  29.     end
  30.  
  31.     eseq0 = eseq0.next()
  32.   end
  33.   min_align
  34. end
  35.  
  36. class Ext_Sequence < Array
  37.   # Construct an extension sequence from seq with n leading nil.
  38.   #
  39.   # param n: non-negative integer
  40.   # param seq: a sequence
  41.   #
  42.   def initialize(n, seq)
  43.     super(n)
  44.     self.concat(seq)
  45.   end
  46.    
  47.   # score(eseq)
  48.   #
  49.   # param eseq: extension sequence of the same length.
  50.   # result: score of alignment.
  51.   # effects: none.
  52.   #
  53.   def score(eseq)
  54.     score = 0
  55.     for i in 0 ... self.length
  56.       score += if self[i] == eseq[i] then 0 else 1 end
  57.     end
  58.     score
  59.   end
  60.  
  61.   # next()
  62.   #
  63.   # result: the next extension sequence extending the same sequence.
  64.   # effects: constructs the extension sequence.
  65.   #    
  66.   def next()
  67.     i = self.length-1
  68.     nils = 0
  69.     # Determine position of last non-nil and number of nils.
  70.     while i >= 0 and self[i] == nil
  71.       nils += 1
  72.       i -= 1
  73.     end
  74.     non_nil = i
  75.     if i == -1
  76.       Ext_Sequence.new(0, self + [nil])
  77.     else
  78.       # Determine where nil and its right neighbor switch positions.
  79.       while i >= 0 and self[i] != nil
  80.         i -= 1
  81.       end
  82.       if i < 0
  83.         # No position found: extension sequences is extended by one.
  84.         Ext_Sequence.new(nils + 1, self[i+1 .. non_nil])
  85.         # Switch and move nils to the left.
  86.       else
  87.         Ext_Sequence.new(0, self[0, i] + [self[i+1]] + [nil] + ([nil] *  nils) + self[i+2 .. non_nil])
  88.       end
  89.     end
  90.   end
  91. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement