From 381ac0da9f2a23e39e5fe3708b120cea3530b8d6 Mon Sep 17 00:00:00 2001 From: martinahansen Date: Tue, 4 Jun 2013 14:05:26 +0000 Subject: [PATCH] added missing files git-svn-id: http://biopieces.googlecode.com/svn/trunk@2176 74ccb610-7750-0410-82ae-013aeee3265d --- bp_test/out/find_homopolymers.out.3 | 22 ++++++ bp_test/out/find_homopolymers.out.4 | 22 ++++++ code_ruby/lib/maasha/seq/homopolymer.rb | 55 +++++++++++++++ code_ruby/test/maasha/seq/test_homopolymer.rb | 68 +++++++++++++++++++ 4 files changed, 167 insertions(+) create mode 100644 bp_test/out/find_homopolymers.out.3 create mode 100644 bp_test/out/find_homopolymers.out.4 create mode 100644 code_ruby/lib/maasha/seq/homopolymer.rb create mode 100755 code_ruby/test/maasha/seq/test_homopolymer.rb diff --git a/bp_test/out/find_homopolymers.out.3 b/bp_test/out/find_homopolymers.out.3 new file mode 100644 index 0000000..740c170 --- /dev/null +++ b/bp_test/out/find_homopolymers.out.3 @@ -0,0 +1,22 @@ +SEQ_NAME: test1 +SEQ: attcccggggnnnnn +SEQ_LEN: 15 +HOMOPOL_PAT: CCC +HOMOPOL_LEN: 3 +HOMOPOL_POS: 3 +--- +SEQ_NAME: test2 +SEQ: nnnnnggggccctta +SEQ_LEN: 15 +HOMOPOL_PAT: NNNNN +HOMOPOL_LEN: 5 +HOMOPOL_POS: 0 +--- +SEQ_NAME: test3 +SEQ: a +SEQ_LEN: 1 +--- +SEQ_NAME: test4 +SEQ: aA +SEQ_LEN: 2 +--- diff --git a/bp_test/out/find_homopolymers.out.4 b/bp_test/out/find_homopolymers.out.4 new file mode 100644 index 0000000..27f9400 --- /dev/null +++ b/bp_test/out/find_homopolymers.out.4 @@ -0,0 +1,22 @@ +SEQ_NAME: test1 +SEQ: attcccggggnnnnn +SEQ_LEN: 15 +HOMOPOL_PAT: NNNNN +HOMOPOL_LEN: 5 +HOMOPOL_POS: 10 +--- +SEQ_NAME: test2 +SEQ: nnnnnggggccctta +SEQ_LEN: 15 +HOMOPOL_PAT: NNNNN +HOMOPOL_LEN: 5 +HOMOPOL_POS: 0 +--- +SEQ_NAME: test3 +SEQ: a +SEQ_LEN: 1 +--- +SEQ_NAME: test4 +SEQ: aA +SEQ_LEN: 2 +--- diff --git a/code_ruby/lib/maasha/seq/homopolymer.rb b/code_ruby/lib/maasha/seq/homopolymer.rb new file mode 100644 index 0000000..9aa46eb --- /dev/null +++ b/code_ruby/lib/maasha/seq/homopolymer.rb @@ -0,0 +1,55 @@ +# Copyright (C) 2007-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). + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +module Homopolymer + # Error class for all exceptions to do with Homopolymer. + class HomopolymerError < StandardError; end + + def each_homopolymer(min = 1) + list = [] + + self.seq.upcase.scan(/A{#{min},}|T{#{min},}|G{#{min},}|C{#{min},}|N{#{min},}/) do |match| + hp = Homopolymer.new(match, match.length, $`.length) + + if block_given? + yield hp + else + list << hp + end + end + + block_given? ? self : list + end + + class Homopolymer + attr_reader :pattern, :length, :pos + + def initialize(pattern, length, pos) + @pattern = pattern + @length = length + @pos = pos + end + end +end diff --git a/code_ruby/test/maasha/seq/test_homopolymer.rb b/code_ruby/test/maasha/seq/test_homopolymer.rb new file mode 100755 index 0000000..a574fd8 --- /dev/null +++ b/code_ruby/test/maasha/seq/test_homopolymer.rb @@ -0,0 +1,68 @@ +#!/usr/bin/env ruby +$:.unshift File.join(File.dirname(__FILE__), '..', '..', '..') + +# Copyright (C) 2007-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 'test/unit' +require 'test/helper' +require 'maasha/seq' + +class HomopolymerTest < Test::Unit::TestCase + def setup + # 0 1 + # 01234567890123456789 + @entry = Seq.new("test", "tacgatgctagcatgcacgg") + @entry.extend(Homopolymer) + end + + test "#each_homopolymer returns empty with empty sequence" do + @entry.seq = "" + assert_equal([], @entry.each_homopolymer) + end + + test "#each_homopolymer returns empty when none found" do + @entry.seq = "AtTcCcGggGnnNnn" + assert_equal([], @entry.each_homopolymer(6)) + end + + test "#each_homopolymer returns correctly" do + @entry.seq = "AtTcCcGggGnnNnn" + assert_equal('CCC', @entry.each_homopolymer(3).first.pattern) + assert_equal(3, @entry.each_homopolymer(3).first.pos) + assert_equal(3, @entry.each_homopolymer(3).first.length) + end + + test "#each_homopolymer in block context returns correctly" do + @entry.seq = "AtTcCcGggGnnNnn" + + @entry.each_homopolymer(3) do |hp| + assert_equal('CCC', hp.pattern) + assert_equal(3, hp.pos) + assert_equal(3, hp.length) + + break + end + end +end -- 2.39.2