#!/usr/bin/env ruby # 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 program is part of the Biopieces framework (www.biopieces.org). # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< # Shred sequences in the stream into subsequences. # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< require 'maasha/biopieces' require 'maasha/seq' require 'pp' class Seq # Method that shreds the sequence of a sequence object into # random subsequences of a specified size. Subsequences are # alternatly picked from the + or - strand (i.e. reverse complemented) # until the specified coverage is reached. # # seq.shred { |subseq| } -> Seq # seq.shred -> [] def shred(size, max_cov) raise SeqError, "Size: #{size} < 1" if size < 1 raise SeqError, "Max coverage: #{max_cov} < 1" if max_cov < 1 entries = [] sum = 0 strand = '+' while coverage(sum) < max_cov entry = self.subseq_rand(size) entry.reverse!.complement! if strand == '-' if block_given? yield entry else entries << entry end strand = (strand == '+') ? '-' : '+' sum += size end entries end private # Method that returns the coverage of subsequences. def coverage(sum) sum / self.seq.length end end casts = [] casts << {:long=>'size', :short=>'s', :type=>'uint', :mandatory=>true, :default=>500, :allowed=>nil, :disallowed=>'0'} casts << {:long=>'coverage', :short=>'c', :type=>'uint', :mandatory=>true, :default=>100, :allowed=>nil, :disallowed=>'0'} options = Biopieces.options_parse(ARGV, casts) Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each_record do |record| if record[:SEQ] and record[:SEQ].length >= options[:size] entry = Seq.new_bp(record) entry.type = :dna entry.shred(options[:size], options[:coverage]) do |subentry| output.puts subentry.to_bp end end end end # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< __END__