]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/shred_seq
polished shred_seq
[biopieces.git] / bp_bin / shred_seq
1 #!/usr/bin/env ruby
2
3 # Copyright (C) 2007-2011 Martin A. Hansen.
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
22
23 # This program is part of the Biopieces framework (www.biopieces.org).
24
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26
27 # Shred sequences in the stream into subsequences.
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31
32 require 'biopieces'
33 require 'seq'
34 require 'pp'
35
36 class Seq
37   # Method that shreds the sequence of a sequence object into
38   # random subsequences of a specified size. Subsequences are
39   # alternatly picked from the + or - strand (i.e. reverse complemented)
40   # until the specified coverage is reached.
41   #
42   # seq.shred { |subseq| } -> Seq
43   # seq.shred              -> []
44   def shred(size, max_cov)
45     entries = []
46     sum     = 0
47     strand  = '+'
48
49     while (sum / self.seq.length) < max_cov
50       if self.seq.size - size == 0
51         start = 0
52       else
53         start = rand(self.seq.size - size)
54       end
55
56       stop     = start + size - 1
57       seq_name = self.seq_name + "[#{start + 1}-#{stop + 1}:#{strand}]"
58       seq      = self.seq[start .. stop]
59       entry    = Seq.new(seq_name, seq, 'dna')
60
61       entry.revcomp if strand == '-'
62
63       if block_given?
64         yield entry
65       else
66         entries << entry
67       end
68       
69       strand = (strand == '+') ? '-' : '+'
70
71       sum += size
72     end
73
74     entries
75   end
76 end
77
78 casts = []
79 casts << {:long=>'size',     :short=>'s', :type=>'uint', :mandatory=>true, :default=>500, :allowed=>nil, :disallowed=>'0'}
80 casts << {:long=>'coverage', :short=>'c', :type=>'uint', :mandatory=>true, :default=>100, :allowed=>nil, :disallowed=>'0'}
81
82 bp = Biopieces.new
83
84 options = bp.parse(ARGV, casts)
85
86 bp.each_record do |record|
87   if record.has_key? :SEQ and record[:SEQ].length >= options[:size]
88     entry = Seq.new(record[:SEQ_NAME], record[:SEQ], record[:SCORES])
89
90     entry.shred(options[:size], options[:coverage]) do |subentry|
91       bp.puts subentry.to_bp
92     end
93   end
94 end
95
96 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
97
98
99 __END__