1 # Copyright (C) 2013 Martin A. Hansen.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # http://www.gnu.org/copyleft/gpl.html
19 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
21 # This software is part of the Biopieces framework (www.biopieces.org).
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26 require 'maasha/seq/ambiguity'
28 # Class to calculate the Hamming distance between two
30 # http://en.wikipedia.org/wiki/Hamming_distance
34 # Class method for calculating the Hamming distance between
35 # two given strings allowing for IUPAC ambiguity codes.
36 def self.distance(str1, str2)
37 raise "string length mismatch: #{str1.length} != #{str2.length}" if str1.length != str2.length
40 hd.hamming_distance_C(str1, str2, str1.length)
43 # >>>>>>>>>>>>>>> RubyInline C code <<<<<<<<<<<<<<<
46 add_ambiguity_macro(builder)
48 # C method for calculating Hamming Distance.
50 VALUE hamming_distance_C(
51 VALUE _str1, // String 1
52 VALUE _str2, // String 2
53 VALUE _len // String length
56 char *str1 = StringValuePtr(_str1);
57 char *str2 = StringValuePtr(_str2);
58 unsigned int len = FIX2UINT(_len);
60 unsigned int hamming_dist = 0;
63 for (i = 0; i < len; i++)
65 if (! MATCH(str1[i], str2[i]))
71 return UINT2NUM(hamming_dist);
77 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<