]> git.donarmstrong.com Git - biopieces.git/commitdiff
changed align.to_s
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Mon, 5 Mar 2012 18:37:08 +0000 (18:37 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Mon, 5 Mar 2012 18:37:08 +0000 (18:37 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@1762 74ccb610-7750-0410-82ae-013aeee3265d

code_ruby/lib/maasha/align.rb

index 3068bb9f9117eaf2e33375bdee330e63289d8c21..3405c8b97a09da87dcba0cdfbcfd5019e7ce3c82 100755 (executable)
@@ -153,18 +153,18 @@ class Align
   # Method to pretty print an alignment from an Align object.
   def to_s
     cons = Consensus.new(@entries, @options)
+    cons.mask_entries!
 
-    entries    = cons.each
-    cons_entry = cons.consensus 
-
-    max_name = entries.group_by { |entry| entry.seq_name.length }.max.first
+    max_name = @entries.group_by { |entry| entry.seq_name.length }.max.first
 
     output = ""
 
-    entries.each do |entry|
+    @entries.each do |entry|
       output << entry.seq_name + (" " * (max_name + 3 - entry.seq_name.length )) + entry.seq + $/
     end
 
+    cons_entry = cons.consensus 
+
     output << " " * (max_name + 3) + cons_entry.seq + $/
     output << " " * (max_name + 3) + cons_entry.qual.tr("[@-h]", "           ..........ooooooooooOOOOOOOOOO") unless cons_entry.qual.nil?
     output
@@ -183,11 +183,53 @@ class Align
 
       @has_qual = entries.first.qual.nil? ? false : true
 
-      @na_seq  = NArray.byte(entries.first.length, entries.size)
-      @na_qual = NArray.byte(entries.first.length, entries.size) if @has_qual
+      @na_seq  = NArray.byte(@cols, @rows)
+      @na_qual = NArray.byte(@cols, @rows) if @has_qual
 
       na_add_entries
+      consensus_calc
+    end
+
+    # Method that lowercase residues that have been removed during
+    # the determination of the consensus sequence.
+    def mask_entries!
+      na_seq = NArray.byte(@cols, @rows)
+
+      @entries.each_with_index do |entry, i|
+        na_seq[true, i]  = NArray.to_na(entry.seq.upcase, "byte")
+      end
+
+      na_seq += ((na_seq.ne('-'.ord) - @na_seq.ne(0)) * ' '.ord)
+
+      @entries.each_with_index do |entry, i|
+        entry.seq = na_seq[true, i].to_s
+      end
+    end
+
+    # Method that returns a Sequence object with a consensus sequence
+    # for the entries in an Align object.
+    def consensus
+      new_seq      = Seq.new
+      new_seq.seq  = consensus_seq
+      new_seq.qual = consensus_qual if @has_qual
+      new_seq.type = "dna"
 
+      new_seq
+    end
+
+    private
+
+    # Method to add a Seq entry object to the two NArrays; @na_seq and @na_qual
+    def na_add_entries
+      @entries.each_with_index do |entry, i|
+        @na_seq[true, i]  = NArray.to_na(entry.seq.downcase.tr(TR_NUC, TR_HEX), "byte")
+        @na_qual[true, i] = NArray.to_na(entry.qual, "byte") - SCORE_ILLUMINA if @has_qual
+      end
+    end
+
+    # Method to calculate a consensus sequence from a list of sequenced stored in two
+    # NArrays.
+    def consensus_calc
       if @has_qual
         if @options.has_key? :quality_min
           mask = mask_quality_min
@@ -226,48 +268,6 @@ class Align
       end
     end
 
-    # Method for iterating over the rows of an Align object and 
-    # return Seq objects for each sequence.
-    def each
-      entries = []
-
-      (0 ... @rows).each do |i|
-        entry = Seq.new
-        entry.seq_name = @entries[i].seq_name
-        entry.seq      = @na_seq[true, i].to_s.tr!(TR_HEX, TR_NUC).upcase
-        entry.qual     = (@na_qual[true, i].to_type("byte") + SCORE_ILLUMINA).to_s if @has_qual
-        entry.type     = @entries[i].type
-
-        if block_given?
-          yield entry
-        else
-          entries << entry
-        end
-      end
-
-      return entries unless block_given?
-    end
-
-    # Method that returns a Sequence object with a consensus sequence
-    # for the entries in an Align object.
-    def consensus
-      new_seq      = Seq.new
-      new_seq.seq  = consensus_seq
-      new_seq.qual = consensus_qual if @has_qual
-      new_seq.type = "dna"
-
-      new_seq
-    end
-
-    private
-
-    def na_add_entries
-      @entries.each_with_index do |entry, i|
-        @na_seq[true, i]  = NArray.to_na(entry.seq.downcase.tr(TR_NUC, TR_HEX), "byte")
-        @na_qual[true, i] = NArray.to_na(entry.qual, "byte") - SCORE_ILLUMINA if @has_qual
-      end
-    end
-
     # Mask that indicates which columns have more than sequence_min sequences.
     # Columns with less than sequence_min are 0'ed, otherwise set to 1.
     def mask_sequence_min