]> git.donarmstrong.com Git - biopieces.git/commitdiff
revamped find_homopolymers
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Tue, 4 Jun 2013 14:00:16 +0000 (14:00 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Tue, 4 Jun 2013 14:00:16 +0000 (14:00 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@2174 74ccb610-7750-0410-82ae-013aeee3265d

bp_bin/find_homopolymers
bp_test/out/find_homopolymers.out.1
bp_test/out/find_homopolymers.out.2
bp_test/test/test_find_homopolymers
code_ruby/lib/maasha/seq.rb
code_ruby/test/maasha/test_seq.rb

index 89b8e8c1d03c3360fe1463855c437b31ee9318c8..c0885e3d3bb010a1363af0ff57dd24e8acf819cc 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env ruby
 
-# Copyright (C) 2007-2010 Martin A. Hansen.
+# Copyright (C) 2007-2013 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
 
 require 'maasha/biopieces'
 require 'maasha/seq'
+require 'pp'
+
+class Seq; include Homopolymer; end
 
 casts = []
-casts << {:long=>'min', :short=>'m', :type=>'uint', :mandatory=>false, :default=>1, :allowed=>nil, :disallowed=>"0"}
+casts << {:long=>'min',     :short=>'m', :type=>'uint', :mandatory=>false, :default=>1,   :allowed=>nil, :disallowed=>"0"}
+casts << {:long=>'limit',   :short=>'l', :type=>'uint', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>"0"}
+casts << {:long=>'longest', :short=>'L', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
 
 options = Biopieces.options_parse(ARGV, casts)
 
@@ -42,10 +47,42 @@ Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
     if record[:SEQ]
       seq = Seq.new(nil, record[:SEQ])
 
-      record[:HOMOPOL_MAX] = seq.homopol_max(options[:min])
+      longest = Seq::Homopolymer.new("", 0, 0)
+      got_one = false
+      count   = 0
+
+      seq.each_homopolymer(options[:min]) do |h|
+        got_one = true
+
+        record[:HOMOPOL_PAT] = h.pattern
+        record[:HOMOPOL_LEN] = h.length
+        record[:HOMOPOL_POS] = h.pos
+
+        if options[:longest]
+          longest = h.length > longest.length ? h : longest
+        else
+          output.puts record
+
+          count += 1
+
+          break if options[:limit] and options[:limit] == count
+        end
+      end
+
+      if options[:longest]
+        if longest.length > options[:min]
+          record[:HOMOPOL_PAT] = longest.pattern
+          record[:HOMOPOL_LEN] = longest.length
+          record[:HOMOPOL_POS] = longest.pos
+        end
+
+        output.puts record
+      elsif not got_one
+        output.puts record
+      end
+    else
+      output.puts record
     end
-
-    output.puts record
   end
 end
 
index fcd0ce96c387b5c32e8cceed9a3cef9114021492..105c6a7941b4ead72555f16337c555bb71d41c02 100644 (file)
@@ -1,20 +1,84 @@
 SEQ_NAME: test1
 SEQ: attcccggggnnnnn
 SEQ_LEN: 15
-HOMOPOL_MAX: 5
+HOMOPOL_PAT: A
+HOMOPOL_LEN: 1
+HOMOPOL_POS: 0
+---
+SEQ_NAME: test1
+SEQ: attcccggggnnnnn
+SEQ_LEN: 15
+HOMOPOL_PAT: TT
+HOMOPOL_LEN: 2
+HOMOPOL_POS: 1
+---
+SEQ_NAME: test1
+SEQ: attcccggggnnnnn
+SEQ_LEN: 15
+HOMOPOL_PAT: CCC
+HOMOPOL_LEN: 3
+HOMOPOL_POS: 3
+---
+SEQ_NAME: test1
+SEQ: attcccggggnnnnn
+SEQ_LEN: 15
+HOMOPOL_PAT: GGGG
+HOMOPOL_LEN: 4
+HOMOPOL_POS: 6
+---
+SEQ_NAME: test1
+SEQ: attcccggggnnnnn
+SEQ_LEN: 15
+HOMOPOL_PAT: NNNNN
+HOMOPOL_LEN: 5
+HOMOPOL_POS: 10
+---
+SEQ_NAME: test2
+SEQ: nnnnnggggccctta
+SEQ_LEN: 15
+HOMOPOL_PAT: NNNNN
+HOMOPOL_LEN: 5
+HOMOPOL_POS: 0
+---
+SEQ_NAME: test2
+SEQ: nnnnnggggccctta
+SEQ_LEN: 15
+HOMOPOL_PAT: GGGG
+HOMOPOL_LEN: 4
+HOMOPOL_POS: 5
+---
+SEQ_NAME: test2
+SEQ: nnnnnggggccctta
+SEQ_LEN: 15
+HOMOPOL_PAT: CCC
+HOMOPOL_LEN: 3
+HOMOPOL_POS: 9
+---
+SEQ_NAME: test2
+SEQ: nnnnnggggccctta
+SEQ_LEN: 15
+HOMOPOL_PAT: TT
+HOMOPOL_LEN: 2
+HOMOPOL_POS: 12
 ---
 SEQ_NAME: test2
 SEQ: nnnnnggggccctta
 SEQ_LEN: 15
-HOMOPOL_MAX: 5
+HOMOPOL_PAT: A
+HOMOPOL_LEN: 1
+HOMOPOL_POS: 14
 ---
 SEQ_NAME: test3
 SEQ: a
 SEQ_LEN: 1
-HOMOPOL_MAX: 1
+HOMOPOL_PAT: A
+HOMOPOL_LEN: 1
+HOMOPOL_POS: 0
 ---
 SEQ_NAME: test4
 SEQ: aA
 SEQ_LEN: 2
-HOMOPOL_MAX: 2
+HOMOPOL_PAT: AA
+HOMOPOL_LEN: 2
+HOMOPOL_POS: 0
 ---
index 921394166b29317b0cbcca868090e4d27e0eada1..5eeb22cd85c84b26fcaac87377aebf9c316ad897 100644 (file)
@@ -1,20 +1,50 @@
 SEQ_NAME: test1
 SEQ: attcccggggnnnnn
 SEQ_LEN: 15
-HOMOPOL_MAX: 5
+HOMOPOL_PAT: CCC
+HOMOPOL_LEN: 3
+HOMOPOL_POS: 3
+---
+SEQ_NAME: test1
+SEQ: attcccggggnnnnn
+SEQ_LEN: 15
+HOMOPOL_PAT: GGGG
+HOMOPOL_LEN: 4
+HOMOPOL_POS: 6
+---
+SEQ_NAME: test1
+SEQ: attcccggggnnnnn
+SEQ_LEN: 15
+HOMOPOL_PAT: NNNNN
+HOMOPOL_LEN: 5
+HOMOPOL_POS: 10
+---
+SEQ_NAME: test2
+SEQ: nnnnnggggccctta
+SEQ_LEN: 15
+HOMOPOL_PAT: NNNNN
+HOMOPOL_LEN: 5
+HOMOPOL_POS: 0
+---
+SEQ_NAME: test2
+SEQ: nnnnnggggccctta
+SEQ_LEN: 15
+HOMOPOL_PAT: GGGG
+HOMOPOL_LEN: 4
+HOMOPOL_POS: 5
 ---
 SEQ_NAME: test2
 SEQ: nnnnnggggccctta
 SEQ_LEN: 15
-HOMOPOL_MAX: 5
+HOMOPOL_PAT: CCC
+HOMOPOL_LEN: 3
+HOMOPOL_POS: 9
 ---
 SEQ_NAME: test3
 SEQ: a
 SEQ_LEN: 1
-HOMOPOL_MAX: 0
 ---
 SEQ_NAME: test4
 SEQ: aA
 SEQ_LEN: 2
-HOMOPOL_MAX: 0
 ---
index 939c6f4b961d02b94e82a9371ce4ceed6fb0f85a..591a6d2011def599d20ddd730e07cf2d5aaae00f 100755 (executable)
@@ -9,3 +9,11 @@ clean
 run "$bp -I $in -m 3 -O $tmp"
 assert_no_diff $tmp $out.2
 clean
+
+run "$bp -I $in -m 3 -l 1 -O $tmp"
+assert_no_diff $tmp $out.3
+clean
+
+run "$bp -I $in -m 3 -L -O $tmp"
+assert_no_diff $tmp $out.4
+clean
index d8a73d31ba8d660fa08bb6aac9c0c24f91137151..2788e5dcc02d78cdb9e3fd38ef83749bad158b79 100644 (file)
@@ -27,8 +27,9 @@ require 'maasha/seq/digest'
 require 'maasha/seq/trim'
 require 'narray'
 
-autoload :BackTrack, 'maasha/seq/backtrack.rb'
-autoload :Dynamic,   'maasha/seq/dynamic.rb'
+autoload :BackTrack,   'maasha/seq/backtrack.rb'
+autoload :Dynamic,     'maasha/seq/dynamic.rb'
+autoload :Homopolymer, 'maasha/seq/homopolymer.rb'
 
 # Residue alphabets
 DNA     = %w[a t c g]
@@ -515,23 +516,6 @@ class Seq
     comp
   end
 
-  # Method that returns the length of the longest homopolymeric stretch
-  # found in a sequence.
-  def homopol_max(min = 1)
-    return 0 if self.seq.nil? or self.seq.empty?
-
-    found = false
-
-    self.seq.upcase.scan(/A{#{min},}|T{#{min},}|G{#{min},}|C{#{min},}|N{#{min},}/) do |match|
-      found = true
-      min   = match.size > min ? match.size : min
-    end
-
-    return 0 unless found
-    min
-  end
-
   # Method that returns the percentage of hard masked residues
   # or N's in a sequence.
   def hard_mask
index 0c847205cb6bbd06ec7df4cbb6602a654cd22cf0..8eed8d5733077915f51c1405537c04170bf028b7 100755 (executable)
@@ -546,26 +546,6 @@ class TestSeq < Test::Unit::TestCase
     assert_equal(0, @entry.composition["X"])
   end
 
-  test "#homopol_max returns 0 with empty sequence" do
-    @entry.seq = ""
-    assert_equal(0, @entry.homopol_max)
-  end
-
-  test "#homopol_max returns 0 with nil sequence" do
-    @entry.seq = nil
-    assert_equal(0, @entry.homopol_max)
-  end
-
-  test "#homopol_max returns 0 when not found" do
-    @entry.seq = "AtTcCcGggGnnNnn"
-    assert_equal(0, @entry.homopol_max(6))
-  end
-
-  test "#homopol_max returns correctly" do
-    @entry.seq = "AtTcCcGggGnnNnn"
-    assert_equal(5, @entry.homopol_max(3))
-  end
-
   test "#hard_mask returns correctly" do
     @entry.seq = "--AAAANn"
     assert_equal(33.33, @entry.hard_mask)