]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/merge_pair_seq
56c296e152b8b7c2d6d39e02b5000defdb2a8f7a
[biopieces.git] / bp_bin / merge_pair_seq
1 #!/usr/bin/env ruby
2
3 # Copyright (C) 2007-2013 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 # Join sequences in the stream.
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31 require 'maasha/biopieces'
32 require 'maasha/seq'
33 require 'pp'
34
35 def names_match(entry1, entry2)
36   if entry1.seq_name =~ /^([^ ]+) \d:/
37     name1 = $1
38   elsif entry1.seq_name =~ /^(.+)\/\d$/
39     name1 = $1
40   else
41     raise "Could not match sequence name: #{entry1.seq_name}"
42   end
43
44   if entry2.seq_name =~ /^([^ ]+) \d:/
45     name2 = $1
46   elsif entry2.seq_name =~ /^(.+)\/\d$/
47     name2 = $1
48   else
49     raise "Could not match sequence name: #{entry2.seq_name}"
50   end
51
52   name1 == name2
53 end
54
55 options = Biopieces.options_parse(ARGV)
56
57 entry1 = nil
58 entry2 = nil
59
60 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
61   input.each_record do |record|
62     if record[:SEQ_NAME] and record[:SEQ]
63       if entry1.nil?
64          entry1 = Seq.new_bp(record)
65       elsif entry2.nil?
66         entry2 = Seq.new_bp(record)
67       end
68
69       if entry1 and entry2
70         if names_match(entry1, entry2)
71           seq_len_left  = entry1.length
72           seq_len_right = entry2.length
73
74           entry1 << entry2
75
76           new_record = entry1.to_bp
77           new_record[:SEQ_LEN_LEFT]  = seq_len_left
78           new_record[:SEQ_LEN_RIGHT] = seq_len_right
79           output.puts new_record
80         else
81           raise "names mismatch: #{entry1.seq_name} != #{entry2.seq_name}"
82         end
83
84         entry1 = nil
85         entry2 = nil
86       end
87     else
88       output.puts record
89     end
90   end
91 end
92
93
94 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
95
96
97 __END__