From: martinahansen Date: Wed, 18 Sep 2013 10:30:49 +0000 (+0000) Subject: added amiguity.rb X-Git-Url: https://git.donarmstrong.com/?p=biopieces.git;a=commitdiff_plain;h=8a9407d8ffde7030f87f800b99f1d5b18eb736db added amiguity.rb git-svn-id: http://biopieces.googlecode.com/svn/trunk@2200 74ccb610-7750-0410-82ae-013aeee3265d --- diff --git a/code_ruby/lib/maasha/seq/ambiguity.rb b/code_ruby/lib/maasha/seq/ambiguity.rb new file mode 100644 index 0000000..896edad --- /dev/null +++ b/code_ruby/lib/maasha/seq/ambiguity.rb @@ -0,0 +1,71 @@ +# Copyright (C) 2013 Martin A. Hansen. + +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +# http://www.gnu.org/copyleft/gpl.html + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +# This software is part of the Biopieces framework (www.biopieces.org). + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +require 'inline' +#autoload :NArray, 'narray' + +module Ambiguity + # IUPAC alphabet and binary encoding of the same + # http://en.wikipedia.org/wiki/Nucleic_acid_notation + + AMBIGUITY_STR = "ACGTUWSMKRYBDHVNacgtuwsmkrybdhvn" + AMBIGUITY_BIN = "\x08\x04\x02\x01\x01\x09\x06\x0c\x03\x0a\x05\x07\x0b\x0d\x0e\x0f\x08\x04\x02\x01\x01\x09\x06\x0c\x03\x0a\x05\x07\x0b\x0d\x0e\x0f" + + def self.hamming_distance(seq1, seq2) + (NArray.to_na(seq1.tr(AMBIGUITY_STR, AMBIGUITY_BIN), 'byte') & + NArray.to_na(seq2.tr(AMBIGUITY_STR, AMBIGUITY_BIN), 'byte')).eq(0).sum + end + + def add_ambiguity_macro inline_builder + # Macro for matching nucleotides including ambiguity codes. + inline_builder.prefix %{ + #define MATCH(A,B) ((bitmap[A] & bitmap[B]) != 0) + } + + # Bitmap for matching nucleotides including ambiguity codes. + # For each value bits are set from the left: bit pos 1 for A, + # bit pos 2 for T, bit pos 3 for C, and bit pos 4 for G. + inline_builder.prefix %{ + char bitmap[256] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 1,14, 4,11, 0, 0, 8, 7, 0, 0,10, 0, 5,15, 0, + 0, 0, 9,12, 2, 2,13, 3, 0, 6, 0, 0, 0, 0, 0, 0, + 0, 1,14, 4,11, 0, 0, 8, 7, 0, 0,10, 0, 5,15, 0, + 0, 0, 9,12, 2, 2,13, 3, 0, 6, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }; + } + end +end +