From: martinahansen Date: Fri, 18 Mar 2011 17:00:13 +0000 (+0000) Subject: added filesys X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=d17d198a04eb9fafcc8f8504e60b81fc9871af6e;p=biopieces.git added filesys git-svn-id: http://biopieces.googlecode.com/svn/trunk@1294 74ccb610-7750-0410-82ae-013aeee3265d --- diff --git a/code_ruby/Maasha/lib/fastq.rb b/code_ruby/Maasha/lib/fastq.rb index 44a9013..82c242e 100644 --- a/code_ruby/Maasha/lib/fastq.rb +++ b/code_ruby/Maasha/lib/fastq.rb @@ -23,54 +23,12 @@ # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< require 'seq' -require 'zlib' +require 'filesys' # 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) - tyt = *args - if tyt.first == "-" - ios = self.new(STDIN) - elsif File.pipe? *args.first - ios = self.new(File.open(*args)) - else - ios = self.zopen(*args) - end - - 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 - +class Fastq < Filesys # 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 @@ -99,22 +57,6 @@ class Fastq @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 diff --git a/code_ruby/Maasha/lib/filesys.rb b/code_ruby/Maasha/lib/filesys.rb new file mode 100644 index 0000000..f7d2b9b --- /dev/null +++ b/code_ruby/Maasha/lib/filesys.rb @@ -0,0 +1,92 @@ +# Copyright (C) 2007-2011 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 'zlib' + +# Error class for all exceptions to do with Filesys. +class FilesysError < StandardError; end + +class Filesys + include Enumerable + + # Class method allowing open to be used on (zipped) files. + # See File.open. + def self.open(*args) + pp *args + + tyt = *args + if tyt.first == "-" + ios = self.new(STDIN) + elsif File.pipe? *args.first + ios = self.new(File.open(*args)) + else + ios = self.zopen(*args) + end + + if block_given? + puts "BLOCK" + begin + puts "YIELD" + 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 + + 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