Class: BioDSL::Fasta
- Inherits:
-
Object
- Object
- BioDSL::Fasta
- Defined in:
- lib/BioDSL/fasta.rb
Overview
Class for reading and writing FASTA files.
Defined Under Namespace
Classes: IO
Instance Attribute Summary collapse
-
#seq ⇒ Object
Returns the value of attribute seq.
-
#seq_name ⇒ Object
Returns the value of attribute seq_name.
Class Method Summary collapse
Instance Method Summary collapse
- #each ⇒ Object
-
#initialize(io) ⇒ Fasta
constructor
A new instance of Fasta.
-
#next_entry ⇒ Object
Method to get the next FASTA entry form an ios and return this as a Seq object.
- #puts(*args) ⇒ Object
Constructor Details
#initialize(io) ⇒ Fasta
Returns a new instance of Fasta.
63 64 65 66 67 68 69 |
# File 'lib/BioDSL/fasta.rb', line 63 def initialize(io) @io = io @seq_name = nil @seq = '' @got_first = nil @got_last = nil end |
Instance Attribute Details
#seq ⇒ Object
Returns the value of attribute seq.
61 62 63 |
# File 'lib/BioDSL/fasta.rb', line 61 def seq @seq end |
#seq_name ⇒ Object
Returns the value of attribute seq_name.
61 62 63 |
# File 'lib/BioDSL/fasta.rb', line 61 def seq_name @seq_name end |
Class Method Details
Instance Method Details
#each ⇒ Object
71 72 73 74 75 |
# File 'lib/BioDSL/fasta.rb', line 71 def each while (entry = next_entry) yield entry end end |
#next_entry ⇒ Object
Method to get the next FASTA entry form an ios and return this as a Seq object. If no entry is found or eof then nil is returned.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/BioDSL/fasta.rb', line 83 def next_entry @io.each do |line| line.chomp! next if line.empty? if line[0] == '>' if !@got_first && !@seq.empty? unless @seq.empty? fail FastaError, 'Bad FASTA format -> content before Fasta ' \ "header: #{@seq}" end end @got_first = true if @seq_name entry = Seq.new(seq_name: @seq_name, seq: @seq) @seq_name = line[1..-1] @seq = '' if @seq_name.empty? fail FastaError, 'Bad FASTA format -> truncated Fasta header: ' \ 'no content after \'>\'' end return entry else @seq_name = line[1..-1] if @seq_name.empty? fail FastaError, 'Bad FASTA format -> truncated Fasta header: ' \ ' no content after \'>\'' end end else @seq << line end end if @seq_name @got_last = true entry = Seq.new(seq_name: @seq_name, seq: @seq) @seq_name = nil return entry end if !@got_last && !@seq.empty? fail FastaError, 'Bad FASTA format -> content witout Fasta header: ' + @seq end nil end |
#puts(*args) ⇒ Object
77 78 79 |
# File 'lib/BioDSL/fasta.rb', line 77 def puts(*args) @io.puts(*args) end |