]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/shred_seq
94d5835e53837f84b0e81aa8bffa9200feb557cf
[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 'maasha/biopieces'
33 require 'maasha/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     raise SeqError, "Size: #{size} < 1"            if size    < 1
46     raise SeqError, "Max coverage: #{max_cov} < 1" if max_cov < 1
47
48     entries = []
49     sum     = 0
50     strand  = '+'
51
52     while coverage(sum) < max_cov
53       entry = self.subseq_rand(size)
54       entry.reverse!.complement! if strand == '-'
55
56       if block_given?
57         yield entry
58       else
59         entries << entry
60       end
61       
62       strand = (strand == '+') ? '-' : '+'
63
64       sum += size
65     end
66
67     entries
68   end
69
70   private
71
72   # Method that returns the coverage of subsequences.
73   def coverage(sum)
74     sum / self.seq.length
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 options = Biopieces.options_parse(ARGV, casts)
83
84 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
85   input.each_record do |record|
86     if record[:SEQ] and record[:SEQ].length >= options[:size]
87       entry      = Seq.new(record[:SEQ_NAME], record[:SEQ], record[:SCORES])
88       entry.type = :dna
89
90       entry.shred(options[:size], options[:coverage]) do |subentry|
91         output.puts subentry.to_bp
92       end
93     end
94   end
95 end
96
97
98 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
99
100
101 __END__