From: martinahansen Date: Tue, 12 Oct 2010 15:18:53 +0000 (+0000) Subject: added align ruby lib X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;ds=sidebyside;h=f53a0eaa73080df63641b4d447a13162ef084a00;p=biopieces.git added align ruby lib git-svn-id: http://biopieces.googlecode.com/svn/trunk@1120 74ccb610-7750-0410-82ae-013aeee3265d --- diff --git a/code_ruby/Maasha/lib/align.rb b/code_ruby/Maasha/lib/align.rb new file mode 100644 index 0000000..5c0b9f5 --- /dev/null +++ b/code_ruby/Maasha/lib/align.rb @@ -0,0 +1,104 @@ +raise "Ruby 1.9 or later required" if RUBY_VERSION < "1.9" + +# Copyright (C) 2007-2010 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). + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +module Align + # Error class for all exceptions to do with matrix class. + class MatrixError < StandardError; end + + class Matrix + attr_accessor :rows, :cols + + def initialize(rows, cols) + @rows = rows + @cols = cols + + raise MatrixError, "Bad rows value: #{rows}" if rows <= 0 + raise MatrixError, "Bad columns value: #{cols}" if cols <= 0 + + @matrix = matrix + end + + def set(row, col) + raise MatrixError, "Bad row value: #{row}" unless (0 ... @rows).include? row + raise MatrixError, "Bad column value: #{col}" unless (0 ... @cols).include? col + + @matrix[@cols * row + col] = 1 + @matrix + end + + def to_s + rows = "" + (0 ... @rows).each do |row| + rows << @matrix[@cols * row, @cols].join(" ") + "\n" + end + + rows + end + + def slice(row_beg, row_end, col_beg, col_end) + raise MatrixError, "Bad row_beg value: #{row_beg}" unless (0 ... @rows).include? row_beg + raise MatrixError, "Bad col_beg value: #{col_beg}" unless (0 ... @cols).include? col_beg + raise MatrixError, "Bad row_end value: #{row_end}" unless (row_beg ... @rows).include? row_end + raise MatrixError, "Bad col_end value: #{col_end}" unless (col_beg ... @rows).include? col_end + + new_rows = row_end - row_beg + 1 + new_cols = col_end - col_beg + 1 + submatrix = Matrix.new(new_rows, new_cols) + + (0 ... @rows).each do |row| + (0 ... @cols).each do |col| + if set?(row, col) + if (0 ... new_rows).include? row - row_beg and (0 ... new_cols).include? col - col_beg + submatrix.set(row - row_beg, col - col_beg) + end + end + end + end + + submatrix + end + + private + + def matrix + [0] * (@rows * @cols) + end + + def set?(row, col) + if @matrix[@cols * row + col] == 0 + return false + else + return true + end + end + end + + # Error class for all exceptions to do with matrix class. + class AlignError < StandardError; end + + class Align + end +end