]> git.donarmstrong.com Git - biopieces.git/blobdiff - bp_bin/find_SNPs
added find_SNPs biopiece
[biopieces.git] / bp_bin / find_SNPs
diff --git a/bp_bin/find_SNPs b/bp_bin/find_SNPs
new file mode 100755 (executable)
index 0000000..6671144
--- /dev/null
@@ -0,0 +1,91 @@
+#!/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 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+# Find SNPs in records of SAM type in the stream.
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
+require 'maasha/biopieces'
+require 'pp'
+
+options = Biopieces.options_parse(ARGV)
+
+snp_hash = Hash.new { |h,k| h[k] = Hash.new { |h,k| h[k] = Hash.new(0) } }
+
+Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
+  input.each_record do |record|
+    output.puts record
+
+    if record.has_key? :ALIGN  and
+       record.has_key? :S_ID   and
+       record.has_key? :S_BEG  and
+       record[:ALIGN] != '.'
+
+       record[:ALIGN].split(',').each do |snp|
+         pos, event = snp.split(':')
+
+         pos += record[:S_BEG]
+
+         snp_hash[record[:S_ID].to_sym][pos.to_sym][event.to_sym] += 1
+       end
+    end
+  end
+
+  snp_hash.each_pair do |s_id, pos_hash|
+    new_record = {}
+    new_record[:REC_TYPE] = 'SNP'
+    new_record[:S_ID]     = s_id
+
+    pos_hash.each_pair do |pos, event_hash|
+      new_record[:POS] = pos
+
+      event_hash.each_pair do |event, count|
+        new_record[:EVENT]     = event
+        new_record[:SNP_COUNT] = count
+
+        before, after = event.to_s.split('>')
+
+        if before == '-'
+          type = 'INSERTION'
+        elsif after == '-'
+          type = 'DELETION'
+        else
+          type = 'MISMATCH'
+        end
+         new_record[:TYPE] = type
+      end
+
+      output.puts new_record
+    end
+  end
+end
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
+__END__