Method: String#compare_with

Defined in:
lib/viral_seq/string.rb

#compare_with(seq2) ⇒ Integer

compare two sequences as String objects, two sequence strings need to aligned first

Examples:

compare two sequence strings, without alignment and with alignment

seq1 = 'AAGGCGTAGGAC'
seq2 = 'AAGCTTAGGACG'
seq1.compare_with(seq2) # no alignment
=> 8
aligned_seqs = ViralSeq::Muscle.align(seq1,seq2) # align using MUSCLE
aligned_seqs[0].compare_with(aligned_seqs[1])
=> 4

Parameters:

  • seq2 (String)

    the sequence string to compare with

Returns:

  • (Integer)

    the total number of differences as integer



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/viral_seq/string.rb', line 108

def compare_with(seq2)
  seq1 = self
  length = seq1.size
  diff = 0
  (0..(length-1)).each do |position|
    nt1 = seq1[position]
    nt2 = seq2[position]
    diff += 1 unless nt1 == nt2
  end
  return diff
end