]> git.donarmstrong.com Git - biopieces.git/commitdiff
added fastq ruby lib
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Tue, 12 Oct 2010 15:17:34 +0000 (15:17 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Tue, 12 Oct 2010 15:17:34 +0000 (15:17 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@1119 74ccb610-7750-0410-82ae-013aeee3265d

code_ruby/Maasha/lib/fastq.rb [new file with mode: 0644]
code_ruby/Maasha/test/test_align.rb [new file with mode: 0755]
code_ruby/Maasha/test/test_fastq.rb [new file with mode: 0755]

diff --git a/code_ruby/Maasha/lib/fastq.rb b/code_ruby/Maasha/lib/fastq.rb
new file mode 100644 (file)
index 0000000..b8ce35e
--- /dev/null
@@ -0,0 +1,112 @@
+# 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 'seq'
+require 'zlib'
+
+# Error class for all exceptions to do with FASTQ.
+class FastqError < StandardError; end
+
+class Fastq
+  include Enumerable
+
+  # Class method allowing open to be used on (zipped) files.
+  # See File.open.
+  def self.open(*args)
+    ios = self.zopen(*args)
+
+    if block_given?
+      begin
+        yield ios
+      ensure
+        ios.close
+      end
+    else
+      return ios
+    end
+  end
+
+  def initialize(io, type=nil)
+    @io   = io
+    @type = type
+  end
+
+  # Method to close ios.
+  def close
+    @io.close
+  end
+
+  # Iterator method for parsing FASTQ enries.
+  def each
+    while entry = get_entry do
+      yield entry
+    end
+  end
+
+  # Method to get the next FASTQ entry form an ios and return this
+  # as a Seq object. If no entry is found or eof then nil is returned.
+  def get_entry
+    begin
+      seq_name  = @io.gets.chomp!
+      seq       = @io.gets.chomp!
+      qual_name = @io.gets.chomp!
+      qual      = @io.gets.chomp!
+
+      entry          = Seq.new
+      entry.type     = @type.nil? ? nil : @type.downcase
+      entry.seq      = seq
+      entry.seq_name = seq_name[1 .. seq_name.length]
+      entry.qual     = qual
+
+      entry
+    rescue
+      nil
+    end
+  end
+
+  # TODO - this should be some custom to_s method instead.
+  def puts(record)
+    if record.has_key? :SEQ_NAME and record.has_key? :SEQ
+      @io.print ">#{record[:SEQ_NAME]}\n"
+      @io.print "#{record[:SEQ]}\n"
+    end
+  end
+
+  private
+
+  # Helper method to return an ios to a file that may be zipped in which case
+  # the ios is unzipped on the fly. See File.open.
+  def self.zopen(*args)
+    ios = File.open(*args)
+
+    begin
+      ios = Zlib::GzipReader.new(ios)
+    rescue
+      ios.rewind
+    end
+
+    self.new(ios)
+  end
+end
+
+
+__END__
diff --git a/code_ruby/Maasha/test/test_align.rb b/code_ruby/Maasha/test/test_align.rb
new file mode 100755 (executable)
index 0000000..b1abb1b
--- /dev/null
@@ -0,0 +1,130 @@
+#!/usr/bin/env ruby
+$:.unshift File.join(File.dirname(__FILE__),'..','lib')
+
+# 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).
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+require 'test/unit'
+require 'align'
+require 'pp'
+
+class MatrixTest < Test::Unit::TestCase
+  def test_Matrix_initialize_raise_on_bad_rows_value
+    [-2, 0].each do |val|
+      assert_raise(Align::MatrixError) { Align::Matrix.new(val, 2) }
+    end
+  end
+
+  def test_Matrix_initialize_raise_on_bad_cols_value
+    [-2, 0].each do |val|
+      assert_raise(Align::MatrixError) { Align::Matrix.new(2, val) }
+    end
+  end
+
+  def test_Matrix_initialize_dont_raise_on_ok_values
+    assert_nothing_raised { Align::Matrix.new(2, 2) }
+  end
+
+  def test_Matrix_set_raise_on_bad_row_value
+    matrix = Align::Matrix.new(2,3)
+    [-1, 2, 3].each do |row|
+      assert_raise(Align::MatrixError) { matrix.set(row, 2) }
+    end
+  end
+
+  def test_Matrix_set_raise_on_bad_col_value
+    matrix = Align::Matrix.new(2,3)
+    [-1, 3, 4].each do |col|
+      assert_raise(Align::MatrixError) { matrix.set(1, col) }
+    end
+  end
+  
+  def test_Matrix_set_dont_raise_on_ok_values
+    matrix = Align::Matrix.new(2,3)
+    (0 ... 2).each do |row|
+      (0 ... 3).each do |col|
+        matrix.set(row, col)
+      end
+    end
+  end
+
+  def test_Matrix_set_returns_corretly
+    matrix = Align::Matrix.new(2,3)
+    assert_equal([0, 0, 1, 0, 0, 0], matrix.set(0, 2))
+  end
+
+  def test_Matrix_to_s_returns_correctly
+    matrix = Align::Matrix.new(2,3)
+    matrix.set(0,0)
+    matrix.set(0,2)
+    matrix.set(1,0)
+    matrix.set(1,2)
+    assert_equal("1 0 1\n1 0 1\n", matrix.to_s)
+  end
+
+  def test_Matrix_slice_raise_on_bad_row_beg_values
+    matrix = Align::Matrix.new(4,4)
+    [-1, 4].each do |val|
+      assert_raise(Align::MatrixError) { matrix.slice(val, 1, 1, 1) }
+    end
+  end
+
+  def test_Matrix_slice_raise_on_bad_row_end_values
+    matrix = Align::Matrix.new(4,4)
+    [-1, 2, 4].each do |val|
+      assert_raise(Align::MatrixError) { matrix.slice(3, val, 1, 1) }
+    end
+  end
+
+  def test_Matrix_slice_raise_on_bad_col_beg_values
+    matrix = Align::Matrix.new(4,4)
+    [-1, 4].each do |val|
+      assert_raise(Align::MatrixError) { matrix.slice(1, 1, val, 1) }
+    end
+  end
+
+  def test_Matrix_slice_raise_on_bad_col_end_values
+    matrix = Align::Matrix.new(4,4)
+    [-1, 2, 4].each do |val|
+      assert_raise(Align::MatrixError) { matrix.slice(1, 1, 3, val) }
+    end
+  end
+
+  def test_Matrix_slice_dont_raise_on_ok_values
+    matrix = Align::Matrix.new(4,4)
+    (0 ... 4).each do |val|
+      assert_nothing_raised {matrix.slice(val, val, val, val) }
+    end
+  end
+
+  def test_Matrix_slice_returns_correctly
+    matrix = Align::Matrix.new(4,4)
+    matrix.set(1,1)
+    matrix.set(2,2)
+    assert_equal( "0 0\n0 1\n", matrix.slice(0, 1, 0, 1).to_s )
+    assert_equal( "1 0\n0 1\n", matrix.slice(1, 2, 1, 2).to_s )
+    assert_equal( "1 0\n0 0\n", matrix.slice(2, 3, 2, 3).to_s )
+  end
+end
+
diff --git a/code_ruby/Maasha/test/test_fastq.rb b/code_ruby/Maasha/test/test_fastq.rb
new file mode 100755 (executable)
index 0000000..c06a07e
--- /dev/null
@@ -0,0 +1,57 @@
+#!/usr/bin/env ruby
+$:.unshift File.join(File.dirname(__FILE__),'..','lib')
+
+# 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).
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+require 'test/unit'
+require 'fastq'
+require 'stringio'
+require 'pp'
+
+class FastqTest < Test::Unit::TestCase
+  def setup
+    @io    = StringIO.new("@test1\nATCG\n+\nABCD\n@test2\natcg\n+test2\n@ABG\n")
+    @fastq = Fastq.new(@io)
+  end
+
+  def test_Fastq_get_entry_obtains_the_correct_seq_name
+    assert_equal("test1", @fastq.get_entry.seq_name)
+  end
+
+  def test_Fasta_get_entry_obtains_the_correct_type
+    fastq = Fastq.new(@io, 'DNA')
+    assert_equal("dna", fastq.get_entry.type)
+  end
+
+  def test_Fastq_get_entry_with_two_entries_obtain_correct
+    assert_equal("ATCG", @fastq.get_entry.seq)
+    assert_equal("atcg", @fastq.get_entry.seq)
+  end
+
+  def test_Fastq_each_loop_ends_correctly
+    @fastq.each do |entry|
+    end
+  end
+end