[BioRuby-cvs] bioruby/lib/bio/sequence format.rb,1.4.2.5,1.4.2.6

Naohisa Goto ngoto at dev.open-bio.org
Fri Feb 22 14:30:46 UTC 2008


Update of /home/repository/bioruby/bioruby/lib/bio/sequence
In directory dev.open-bio.org:/tmp/cvs-serv30611

Modified Files:
      Tag: BRANCH-biohackathon2008
	format.rb 
Log Message:
 * fold() and wrap() are reverted
 * Bug fix in format_features() and format_qualifiers()
 * The content of 'translate' qualifier is now wrapped by double quote


Index: format.rb
===================================================================
RCS file: /home/repository/bioruby/bioruby/lib/bio/sequence/format.rb,v
retrieving revision 1.4.2.5
retrieving revision 1.4.2.6
diff -C2 -d -r1.4.2.5 -r1.4.2.6
*** format.rb	20 Feb 2008 13:54:19 -0000	1.4.2.5
--- format.rb	22 Feb 2008 14:30:44 -0000	1.4.2.6
***************
*** 31,34 ****
--- 31,109 ----
  #   puts s.output(:embl)
  module Format
+ 
+   # INTERNAL USE ONLY, YOU SHOULD NOT CALL THIS METHOD. (And in any
+   # case, it would be difficult to successfully call this method outside
+   # its expected context).
+   #
+   # Output the FASTA format string of the sequence.  
+   #
+   # UNFORTUNATLY, the current implementation of Bio::Sequence is incapable of 
+   # using either the header or width arguments.  So something needs to be
+   # changed...
+   #
+   # Currently, this method is used in Bio::Sequence#output like so,
+   #
+   #   s = Bio::Sequence.new('atgc')
+   #   puts s.output(:fasta)                   #=> "> \natgc\n"
+   # ---
+   # *Arguments*:
+   # * (optional) _header_: String (default nil)
+   # * (optional) _width_: Fixnum (default nil)
+   # *Returns*:: String object
+   def format_fasta(header = nil, width = nil)
+     header ||= "#{@entry_id} #{@definition}"
+ 
+     ">#{header}\n" +
+     if width
+       @seq.to_s.gsub(Regexp.new(".{1,#{width}}"), "\\0\n")
+     else
+       @seq.to_s + "\n"
+     end
+   end
+ 
+   #---
+ 
+   # Not yet implemented :)
+   # Remove the nodoc command after implementation!
+   # ---
+   # *Returns*:: String object
+   #def format_gff #:nodoc:
+   #  raise NotImplementedError
+   #end
+ 
+   # INTERNAL USE ONLY, YOU SHOULD NOT CALL THIS METHOD. (And in any
+   # case, it would be difficult to successfully call this method outside
+   # its expected context).
+   #
+   # Output the Genbank format string of the sequence.  
+   # Used in Bio::Sequence#output.
+   # ---
+   # *Returns*:: String object
+   #def format_genbank
+   #  prefix = ' ' * 5
+   #  indent = prefix + ' ' * 16
+   #  fwidth = 79 - indent.length
+   #
+   #  format_features(prefix, indent, fwidth)
+   #end
+ 
+   # INTERNAL USE ONLY, YOU SHOULD NOT CALL THIS METHOD. (And in any
+   # case, it would be difficult to successfully call this method outside
+   # its expected context).
+   #
+   # Output the EMBL format string of the sequence.  
+   # Used in Bio::Sequence#output.
+   # ---
+   # *Returns*:: String object
+   #def format_embl
+   #  prefix = 'FT   '
+   #  indent = prefix + ' ' * 16
+   #  fwidth = 80 - indent.length
+   #
+   #  format_features(prefix, indent, fwidth)
+   #end
+ 
+   #+++
+ 
    private
  
***************
*** 42,53 ****
  
        head = ''
!       (position).wrap(width).each_line do |line|
          result << head << line
          head = indent
        end
  
-       result << "\n"
        result << format_qualifiers(feature.qualifiers, indent, width)
-       result << "\n"
      end
      return result
--- 117,126 ----
  
        head = ''
!       wrap(position, width).each_line do |line|
          result << head << line
          head = indent
        end
  
        result << format_qualifiers(feature.qualifiers, indent, width)
      end
      return result
***************
*** 60,80 ****
  
        if v == true
!         lines =('/' + q).wrap(width)
        elsif q == 'translation'
!         lines = ('/' + q + '="' + v + '"').fold(width)
        else
!         if ( v[/\D/] or q == 'chromosome' )
            #v.delete!("\x00-\x1f\x7f-\xff")
            v.gsub!(/"/, '""')
            v = '"' + v + '"'
          end
!         lines = ('/' + q + '=' + v).wrap(width)
        end
  
        lines.gsub!(/^/, indent)
        lines
!     end.join("\n")
    end
  
  
  end # Format
--- 133,180 ----
  
        if v == true
!         lines = wrap('/' + q, width)
        elsif q == 'translation'
!         lines = fold("/#{q}=\"#{v}\"", width)
        else
!         if v[/\D/] or q == 'chromosome'
            #v.delete!("\x00-\x1f\x7f-\xff")
            v.gsub!(/"/, '""')
            v = '"' + v + '"'
          end
!         lines = wrap('/' + q + '=' + v, width)
        end
  
        lines.gsub!(/^/, indent)
        lines
!     end.join
!   end
! 
!   def fold(str, width)
!     str.gsub(Regexp.new("(.{1,#{width}})"), "\\1\n")
    end
  
+   def wrap(str, width)
+     result = []
+     left = str.dup
+     while left and left.length > width
+       line = nil
+       width.downto(1) do |i|
+         if left[i..i] == ' ' or /[\,\;]/ =~ left[(i-1)..(i-1)]  then
+           line = left[0..(i-1)].sub(/ +\z/, '')
+           left = left[i..-1].sub(/\A +/, '')
+           break
+         end
+       end
+       if line.nil? then
+         line = left[0..(width-1)]
+         left = left[width..-1]
+       end
+       result << line
+     end
+     result << left if left
+     result_string = result.join("\n")
+     result_string << "\n" unless result_string.empty?
+     return result_string
+   end
  
  end # Format




More information about the bioruby-cvs mailing list